简体   繁体   English

Keras LSTM - 为什么“相同”模型和相同权重的结果不同?

[英]Keras LSTM - why different results with “same” model & same weights?

( NOTE: Properly fixing the RNG state before each model creating as described in comment in comment practically fixed my problem, as within 3 decimals results are consistent, but they aren't exactly so , so there's somewhere a hidden source of randomness not fixed by seeding the RNG... probably some lib uses time milisecs or smth... if anyone has an idea on that, it would be cool to know, so I will wait and not close question yet :) ) 注意:在每个模型创建之前正确修复 RNG 状态,如评论中的评论中所述实际上解决了我的问题,因为小数点后 3 位内的结果是一致的,但它们并非完全如此,因此在某处存在未修复的隐藏随机源播种 RNG ......可能有些库使用时间毫秒或 smth ......如果有人对此有想法,知道会很酷,所以我会等待而不是关闭问题:) )

I create a Keras LSTM model (used to predict some time series data, not important what), and every time I try to re-create an identical model (same mode config loaded from json, same weights loaded from file, same args to compile function), I get wildly different results on same train and test data .我创建了一个Keras LSTM 模型(用于预测一些时间序列数据,不重要的是什么),并且每次我尝试重新创建一个相同的模型(从 json 加载相同的模式配置,从文件加载相同的权重,编译相同的 args函数),我在相同的训练和测试数据上得到了截然不同的结果 WHY?为什么?

Code is roughly like this:代码大致是这样的:

# fix random
import random
random.seed(42)

# make model & compile
model = Sequential([
    LSTM(50, input_shape=(None, 1), return_sequences=True),
    LSTM(100, return_sequences=False),
    Dense(1),
    Activation("linear")
])
model.compile(loss="mse", optimizer="rmsprop")

# save it and its initial random weights
model_json = model.to_json()
model.save_weights("model.h5")

# fit and predict
model.fit(x_train, y_train, epochs=3)
r = model.predict(x_test)

# create new "identical" model
model2 = model_from_json(model_json)
model2.load_weights("model.h5")
model2.compile(loss="mse", optimizer="rmsprop")

# fit and predict "identical" model
model2.fit(x_train, y_train, epochs=3)
r2 = model2.predict(x_test)

# ...different results :(

I know that the model has initial random weights, so I'm saving them up and reloading them.我知道模型具有初始随机权重,因此我将它们保存并重新加载。 I'm also paranoid enough to assume there are some "hidden" params that I may not know of, so I serialize model to json and reload instead of recreating an identical one by hand (tried that, same thing btw).我也很偏执,假设有一些我可能不知道的“隐藏”参数,所以我将模型序列化为 json 并重新加载,而不是手动重新创建一个相同的参数(尝试过,同样的事情)。 And I also fixed the random number generator.我还修复了随机数生成器。

It's my first time with Keras, and I'm also a beginners to neural networks in general.这是我第一次使用 Keras,而且我也是一般神经网络的初学者。 But this this drives me crazy... wtf can vary?!但这让我发疯...... wtf 可能会有所不同?!


On fixing random number generators: I run Keras with the TensorFlow backend, and I have these lines of code at the start to try and fix the RNGs for experimental purposes:关于修复随机数生成器:我使用 TensorFlow 后端运行 Keras,并且在开始时我有这些代码行来尝试修复 RNG 以用于实验目的:

import random
random.seed(42)
import numpy
numpy.random.seed(42)
from tensorflow import set_random_seed
set_random_seed(42)

...but they still don't fix the randomness. ...但他们仍然没有解决随机性。

And I understand that the goal is to make my model to behave non-randomly despite the inherent stochastic nature of NNs.而且我知道目标是让我的模型表现出非随机性,尽管NN 具有固有的随机性。 But I need to temporarily fix this for experimental purposes (I'm even OK with it being reproducible on one machine only!).但是为了实验目的我需要暂时修复这个问题(我什至可以在一台机器上重现它!)。

Machine learning algorithms in general are non-deterministic .机器学习算法通常是非确定性的 This means that every time you run them the outcome should vary.这意味着每次运行它们时,结果都应该有所不同。 This has to do with the random initialization of the weights.这与权重的随机初始化有关。 If you want to make the results reproducible you have to eliminate the randomness from the table.如果您想让结果可重复,您必须消除表格中的随机性。 A simple way to do this is to use a random seed .一个简单的方法是使用随机种子

import numpy as np
import tensorflow as tf

np.random.seed(1234)
tf.random.set_seed(1234)

# rest of your code

If you want the randomness factor but not so high variance in your output, I would suggest either lowering your learning rate or changing your optimizer (I would suggest an SGD optimizer with a relatively low learning rate).如果您想要随机因子但输出中的方差不那么大,我建议您降低学习率或更改优化器(我建议使用学习率相对较低的 SGD 优化器)。 A cool overview of gradient descent optimization is available here !梯度下降优化清凉的概述,请点击这里


A note on TensorFlow's random generators is that besides a global seed (ie tf.random.set_seed() ), they also use an internal counter, so if you run关于 TensorFlow 随机生成器的一个说明是,除了全局种子(即tf.random.set_seed() )之外,它们还使用内部计数器,因此如果您运行

tf.random.set_seed(1234)
print(tf.random.uniform([1]).numpy())
print(tf.random.uniform([1]).numpy())

You'll get 0.5380393 and 0.3253647 , respectively.您将分别获得0.53803930.3253647 However if you re-run that same snippet, you'll get the same two numbers again.但是,如果您重新运行相同的代码段,您将再次获得相同的两个数字。

A detailed explanation of how random seeds work in TensorFlow can be found here .可以在 此处找到有关 TensorFlow 中随机种子如何工作的详细说明。


For newer TF versions take care of this too: TensorFlow 2.2 ships with a os environment variable TF_DETERMINISTIC_OPS which if set to '1' , will ensure that only deterministic GPU ops are used.对于较新的 TF 版本,也请注意这一点: TensorFlow 2.2 附带一个操作系统环境变量TF_DETERMINISTIC_OPS ,如果设置为'1' ,将确保仅使用确定性 GPU 操作。

This code is for keras using tensorflow backend此代码适用于使用 tensorflow 后端的 keras

This is because the weights are initialised using random numbers and hence you will get different results every time.这是因为权重是使用随机数初始化的,因此每次都会得到不同的结果。 This is expected behaviour.这是预期的行为。 To have reproducible result you need to set the random seed as.要获得可重复的结果,您需要将随机种子设置为。 Below example set operation-level and graph-level seeds for more information look here下面的示例设置操作级和图形级种子有关更多信息,请查看此处

import tensorflow as tf
import random as rn

os.environ['PYTHONHASHSEED'] = '0'

# Setting the seed for numpy-generated random numbers
np.random.seed(37)

# Setting the seed for python random numbers
rn.seed(1254)

# Setting the graph-level random seed.
tf.set_random_seed(89)

from keras import backend as K

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

#Force Tensorflow to use a single thread
sess = tf.Session(graph=tf.get_default_graph(), config=session_conf)

K.set_session(sess)

# Rest of the code follows from here on ...

I resolved this issue by adding os.environ['TF_DETERMINISTIC_OPS'] = '1'我通过添加os.environ['TF_DETERMINISTIC_OPS'] = '1'解决了这个问题

Here an example:这里有一个例子:

import os
os.environ['TF_DETERMINISTIC_OPS'] = '1'
#rest of the code
# tf version 2.3.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 培训和评估的准确性在keras LSTM模型中不同:为什么评估产生的结果与培训期间不同? - Training and evaluating accuracies different in keras LSTM model: why does evaluation not produce same results as during training? Keras LSTM 模型没有产生相同的结果 - Keras LSTM model not producing same results 相同的权重,实现但不同的结果 n Keras 和 Pytorch - Same weights, implementation but different results n Keras and Pytorch 训练具有相同初始权重和相同数据的模型时的结果不同 - Different results when training a model with same initial weights and same data tensorflowjs 和 keras 在相同模型和张量上的不同结果 - Different results for tensorflowjs and keras on same model and tensor Python:Keras 模型对于相同的数据和相同的模型返回不同的结果 - Python: Keras model returns different results for the same data and same model 为什么使用相同的 Keras 模型和输入进行预测会得到不同的结果? - Why am I getting different results on a prediction using the same Keras model and input? Keras model 使用相同的输入预测不同的结果 - Keras model predicts different results using the same input Keras:如何在 LSTM 模型中显示注意力权重 - Keras: How to display attention weights in LSTM model 当输入的大小发生变化时,Keras LSTM 模型对同一输入给出不同的预测? - Keras LSTM model giving different predictions on same input when size of input has changed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM