【问题标题】:Neural network gives different results for each execution神经网络为每次执行提供不同的结果
【发布时间】:2017-08-30 06:18:06
【问题描述】:

这是我以 Keras 和 TensorFlow 作为后端运行的确切代码。对于相同程序的每次运行,训练结果都是不同的。有时它在第 400 次迭代中获得 100% 的准确度,有时在第 200 次迭代中获得 100% 的准确度。

training_data = np.array([[0,0],[0,1],[1,0],[1,1]], "float32")
target_data = np.array([[0],[1],[1],[0]], "float32")

model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='mean_squared_error',
              optimizer='adam',
              metrics=['binary_accuracy'])

model.fit(training_data, target_data, epochs=500, verbose=2)


Epoch 403/500
0s - loss: 0.2256 - binary_accuracy: 0.7500

那么为什么每次执行的结果都会随着训练数据的固定而变化呢?将不胜感激一些解释。

【问题讨论】:

    标签: tensorflow neural-network keras feed-forward


    【解决方案1】:

    训练集是固定的,但是我们将神经网络的初始权重设置为一个小范围内的随机值,所以每次训练网络时,你得到的结果都会略有不同。

    如果您想要可重现的结果,您可以将带有 numpy.random.seed 的 numpy 随机种子设置为固定值,因此将使用相同的权重,但请注意这可能会影响您的网络。

    【讨论】:

    • 对于 TF 2.0.0 我需要做tf.random.set_seed(1)
    猜你喜欢
    • 2018-01-05
    • 1970-01-01
    • 2020-06-21
    • 2017-01-06
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2020-02-03
    • 2017-02-21
    相关资源
    最近更新 更多