2

I can't reproduce the keras training result even set the seeds. I set the random seeds and use the CPU for training.

The code is provided as below;

import os

os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = ""

import tensorflow as tf
import numpy as np
import random

sd = 1 # Here sd means seed.
np.random.seed(sd)
random.seed(sd)
os.environ['PYTHONHASHSEED']=str(sd)
session_conf = tf.compat.v1.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1)
tf.random.set_seed(0)
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
tf.compat.v1.keras.backend.set_session(sess)

import pandas as pd

from tensorflow import keras
from tensorflow.keras import layers

from sklearn.model_selection import train_test_split
from nltk.tokenize import word_tokenize
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.preprocessing.sequence import pad_sequences
import pickle

import time
from tensorflow.keras.callbacks import TensorBoard


training_site = pd.read_csv('data/allTrainingSites.txt', sep='\t')
train_data, test_data = train_test_split(training_site, test_size=0.2, random_state=25)
train_data = train_data.iloc[:, 3:6].T.to_numpy()
test_data = test_data.iloc[:, 3:6].T.to_numpy()

train_data_0, train_data_1 = train_data[0], train_data[1]
test_data_0, test_data_1 = test_data[0], test_data[1]

# encode seq

def get_categ(data):
    nuc = []
    for i in data:
        for j in i: nuc.append(j)
    nuc = set(nuc)
    return nuc

nuc_len = len(get_categ(train_data[0]).union(get_categ(train_data[1])))+2
def sep_nuc(data):
    nuc = []
    for i in data: nuc.append((i))
    return nuc

embedded_seq_tr_0 = [one_hot(' '.join(sep_nuc(seq)), nuc_len) for seq in train_data_0]
embedded_seq_te_0 = [one_hot(' '.join(sep_nuc(seq)), nuc_len) for seq in test_data_0]
embedded_seq_tr_1 = [one_hot(' '.join(sep_nuc(seq)), nuc_len) for seq in train_data_1]
embedded_seq_te_1 = [one_hot(' '.join(sep_nuc(seq)), nuc_len) for seq in test_data_1]

# find the largest and padding
count_len = lambda data: len(data)
len_0 = len(max(train_data_0, key = count_len))
len_1 = len(max(train_data_1, key = count_len))

padded_seq_tr_0 = pad_sequences(embedded_seq_tr_0, len_0, padding='post')
padded_seq_te_0 = pad_sequences(embedded_seq_te_0, len_0, padding='post')
padded_seq_tr_1 = pad_sequences(embedded_seq_tr_1, len_1, padding='post')
padded_seq_te_1 = pad_sequences(embedded_seq_te_1, len_1, padding='post')

# combine array
train_01 = np.concatenate((padded_seq_tr_0,padded_seq_tr_1),axis=1)
test_01 = np.concatenate((padded_seq_te_0,padded_seq_te_1),axis=1)

train_01 = train_01.reshape(train_01.shape[0], train_01.shape[1], 1)
test_01 = test_01.reshape(test_01.shape[0], test_01.shape[1], 1)

y_train = np.asarray(train_data[2]).astype('int32')
y_test = np.asarray(test_data[2]).astype('int32')
y_train = y_train.reshape(y_train.shape[0], 1)
y_test = y_test.reshape(y_test.shape[0], 1)

# build cnn

model_cnn = keras.Sequential()
model_cnn.add(layers.Embedding(nuc_len, 20, input_length=train_01.shape[1]))
model_cnn.add(layers.Conv1D(64, 5, activation='relu', padding='same'))
model_cnn.add(layers.MaxPool1D())
model_cnn.add(layers.Dropout(0.2, seed=0))
model_cnn.add(layers.Conv1D(128, 5, activation='relu', padding='same'))
model_cnn.add(layers.MaxPool1D())
model_cnn.add(layers.Dropout(0.2, seed=0))
model_cnn.add(layers.Conv1D(256, 5, activation='relu', padding='same'))
model_cnn.add(layers.MaxPool1D())
model_cnn.add(layers.Dropout(0.2, seed=0))
model_cnn.add(layers.Conv1D(512, 5, activation='relu', padding='same'))
model_cnn.add(layers.MaxPool1D())
model_cnn.add(layers.Flatten())

model_cnn.add(layers.Dense(64, activation='relu'))
model_cnn.add(layers.Dense(16, activation='relu'))
model_cnn.add(layers.Dense(1, activation='sigmoid'))

opt_cnn = tf.keras.optimizers.Adam(learning_rate=1e-3)
# opt = tf.keras.optimizers.SGD(learning_rate=1e-3)
# model.summary()
model.summary()
model_cnn.compile(optimizer=opt_cnn, loss='BinaryCrossentropy',
              metrics=['accuracy'])



train_history_cnn = model.fit(train_01, y_train, epochs=3,
                              validation_data=(test_01, y_test),
                              batch_size=16, callbacks=[tensorboard])

I can't reproduce the keras training result even set the seeds. I set the random seeds and use the CPU for training.

i use tensorflow 2.5.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
mark Lan
  • 41
  • 1
  • `model.fit` shuffels your data as well, set `shuffle=False` – Eumel Sep 06 '21 at 07:55
  • Thank you for the reply. I added the argument but still gives different results each time training. – mark Lan Sep 06 '21 at 08:47
  • This question and the multiple answers for varying versions of Tensorflow may be of interest... https://stackoverflow.com/questions/51249811/reproducible-results-in-tensorflow-with-tf-set-random-seed – dijksterhuis Sep 07 '21 at 02:27

1 Answers1

0

Training reproducible models and results are sometimes challenging. one thing that may help you is to use the same environment(e.g. installed packages). You can copy your environments using some technologies like Docker or Docker hub. Also, you can use conda virtual environment.

Raha Moosavi
  • 527
  • 4
  • 19