1

I've been trying to create a model that recognizes different singing techniques. I have got good results but I want to do different tests with different optimizers, layers, etc. However, I can't get reproducible results. By running twice this model training:

num_epochs = 100
batch_size = 128
history = modelo.fit(X_train_f, Y_train, validation_data=(X_test_f,Y_test), epochs=num_epochs, batch_size=batch_size, verbose=2)

I can get 25% accuracy the first run and then 34% the second. Then if I change the optimizer from "sgd" to "adam", I would get a 99%. If I come back to the previous "sgd" optimizer that got me 34% the second run, I would get 100% or something crazy like that. I don't understand why.

I've tried many things I've read in similar questions. The following lines show how I am trying to make my code to be reproducible, and these are actually the first lines of my whole code:

import numpy as np
import tensorflow as tf
import random as rn

import os

#https://stackoverflow.com/questions/57305909/tensorflow-keras-reproducibility-problem-on-google-colab

os.environ['PYTHONHASHSEED']=str(5)
np.random.seed(5)
rn.seed(12345)

session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1,
                                    inter_op_parallelism_threads=1)

tf.compat.v1.set_random_seed(1234)

sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
tf.compat.v1.keras.backend.set_session(sess)

Question is, what am I doing wrong with the code above that is not working (as I mentioned)?

Here's where I create the training sets:

from keras.datasets import mnist
from keras.utils import np_utils

from keras.models import Sequential
from keras.layers.convolutional import Conv1D, MaxPooling1D
from keras.layers.core import Dense, Flatten
from keras.layers import BatchNormalization,Activation
from keras.optimizers import SGD, Adam

from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(X,Y, test_size=0.2, random_state=2)

My model:

from tensorflow.keras import layers
from tensorflow.keras import initializers

input_dim = X_train_f.shape[1]
output_dim = Y_train.shape[1]

modelo = Sequential()
modelo.add(Conv1D(filters=6, kernel_initializer=initializers.glorot_uniform(seed=5), kernel_size=5, activation='relu', input_shape=(40, 1))) # 6
modelo.add(MaxPooling1D(pool_size=2))

modelo.add(Conv1D(filters=16, kernel_initializer=initializers.glorot_uniform(seed=5), kernel_size=5, activation='relu')) # 16
modelo.add(MaxPooling1D(pool_size=2))

modelo.add(Flatten())
modelo.add(Dense(120, kernel_initializer=initializers.glorot_uniform(seed=5), activation='relu')) # 120
modelo.add(Dense(84, kernel_initializer=initializers.glorot_uniform(seed=5), activation='relu')) # 84
modelo.add(Dense(nclases, kernel_initializer=initializers.glorot_uniform(seed=5), activation='softmax'))

sgd = SGD(lr=0.1)
#modelo.compile(loss='categorical_crossentropy', 
#               optimizer='adam', 
#               metrics=['accuracy'])
modelo.compile(loss='categorical_crossentropy', 
           optimizer=sgd, 
           metrics=['accuracy'])

modelo.summary()
modelo.input_shape
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Hi and welcome to StackOverflow. Could you please extend your question to ask for a specific help? The question right now seems too generic, and it's unclear what is being asked. – Cninroh Nov 22 '20 at 11:19
  • @Cninroh Thanks for welcoming me. I just edited the question, hope it is clearer now and can get some help :) – Andrés Osorio Nov 22 '20 at 16:57

1 Answers1

0

It is a normal situation. Adam optimizer is much more powerful comparing to SGD. Adam implicitly performs coordinate-wise gradient clipping and can hence, unlike SGD, tackle heavy-tailed noise.

Andrey
  • 5,932
  • 3
  • 17
  • 35
  • Thank you @Andrey. I understand Adam works better for my purpose than SGD. What I really want to do is to draw a comparison using around 30 different layer configurations and that's what I'm not being able to do since my results are not reproducible. – Andrés Osorio Nov 23 '20 at 23:11