我已经在文本数据上运行了一个LSTM模型,虽然我已经设置了随机、numpy和tensorflow的种子,但每次仍然没有产生相同的结果。我还设置了内核初始化器的种子,但也没有产生任何结果。我错过了什么吗?(我已经检查了预处理,结果都是一样的)
model = Sequential()
model.add(Embedding(input_dim=vocab_size + 1, input_length=data_max,
output_dim=3,
embeddings_initializer=keras.initializers.initializers_v2.RandomUniform(seed=seed_value)
, mask_zero=True))
model.add(Dropout(rate=0.4, seed=seed_value))
model.add((LSTM(units=4, kernel_initializer=keras.initializers.initializers_v2.GlorotUniform(seed=seed_value))))
model.add(Dropout(rate=0.5, seed=seed_value))
model.add(Dense(units=100, kernel_initializer=keras.initializers.initializers_v2.RandomNormal(0, 1, seed=seed_value)
, activation='relu'))
model.add(Dropout(rate=0.6, seed=seed_value))
model.add(Dense(units=4, kernel_initializer=keras.initializers.initializers_v2.RandomNormal(0, 1, seed=seed_value)
, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
lstm = model.fit(X_train_vector, Y_train_vector, validation_data=[X_valid, Y_valid], epochs=25, shuffle=False)
这里我展示了如何为numpy、tensorflow和random种子化
# Ensure seed is all the same ------------------
seed_value = 1
os.environ['PYTHONHASHSEED'] = str(seed_value)
random.seed(seed_value)
np.random.seed(seed_value) # np seed
tf.random.set_seed(seed_value)
session_conf = tf.compat.v1.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
# Force Tensorflow to use a single thread
sess = tf.compat.v1.Session(graph=tf.compat.v1.get_default_graph(), config=session_conf)
K.set_session(sess)
# ----------------------------------------------
1条答案
按热度按时间5cnsuln71#
您的模型或训练优化器需要学习时间,它将根据优化点进行调整。(问题是为什么相似的随机数种子不能提供相同的结果)
[样品]:
**[输出]:**您可以看到,在学习之前,值并不相等。