Reproducible results with Keras I was trying to classify some images using VGG16 and I realized if I run the same code a second (or third) time I won't get the same results even though random_state in train_test_split is set to 0.
could it be because of shuffle? how do I get the same results? because it feels like I could run the same code forever hoping for better results!
x = []
y = []

folders=glob(path+"\\*")

for f in folders:
    for img_name in os.listdir(f):
        if "Pneumonia" in f:
            y.append(2)
        elif "No_findings" in f:
            y.append(1)
        else:
            y.append(0)
        img = cv2.imread(f+'\\'+img_name)
        img = cv2.resize(img, (224,224))
        x.append(img)

x=np.array(x)
y=np.array(y)

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=0, shuffle=True)
x_train, x_test= x_train/255.0, x_test/255.0
y_train = to_categorical(y_train, 3)
y_test = to_categorical(y_test, 3)

IMAGE_SIZE=[224,224]
model_1= VGG16(input_shape=IMAGE_SIZE+[3],weights='imagenet', include_top=False)

for layer in model_1.layers:
    layer.trainable = False

model_1.layers[-1].trainable = model_1.layers[-2].trainable = True
from keras import models
from keras import layers
model = models.Sequential()
model.add(model_1)
model.add(layers.Flatten())
model.add(layers.Dense(3, activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['acc'])
r=model.fit(x_train, y_train, epochs=15, validation_data=(x_test, y_test), batch_size=64)
model.summary()


This is because initialisation in Keras is not reproducible out of box. You would need to assign initial weights explicitly so it is reproducible. Use custom initialiser as explained in the documentation.
