如何在Jupyter Notebook中多次拟合/运行神经网络? [英] How to fit/run the neural network multiple times in jupyter notebook?
问题描述
我正在使用keras来构建一个简单的神经网络,如下所示:
I'm using keras to construct a simple neural network as follows:
import keras
from keras.models import Sequential
from keras.layers import Dense
classifier = Sequential()
classifier.add(Dense(10, kernel_initializer='uniform', activation= 'relu', input_dim = 2))
...
classifier.compile(optimizer= 'adam',loss='binary_crossentropy', metrics=['accuracy'])
classifier.fit(X_train,y_train,batch_size=10,epochs=100)
当我第一次在jupyter笔记本中运行该代码时,该代码完全可以正常工作并获得90%的精度.但是当我重新运行它时,它的准确性急剧下降到50%,并且在训练过程中准确性没有提高.另外,如果我在同一个笔记本页面中构造另一个这样的NN,它也会有此问题.
The code works totally fine and get 90% accuracy when I first run it in jupyter notebook. But when I rerun it, its accuracy dramatically dropped to 50%, and the accuracy didn't improve during the training process. Also, if I construct another NN like this in the same notebook page, it also has this problem.
如果我想在重新运行代码或在同一笔记本页面中运行另一个NN时获得正确的结果,该怎么办?
So what should I do if I want to get the right result when I rerun the code or run the another NN in the same notebook page?
PS:我正在使用tensorflow后端.
PS: I'm using tensorflow backend.
推荐答案
编辑:结果的不同主要是因为权重初始化和批次.但是固定种子不足以实现完全可重复性,请参阅:
Results are different mostly because of weights initialization and batches. But seed fixing is not enough for full reproducibility, see:
- https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development
- 即使设置了随机种子,为什么在Keras中仍无法获得可重复的结果?
- https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development
- Why can't I get reproducible results in Keras even though I set the random seeds?
上一个答案:
由于
- 随机权重初始化 Sli算法(例如Adam)中的
- 随机批处理拆分/排序
- random weight initialization
- random batch splitting/sorting in SGD algorithms such as Adam
例如,此代码
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten
def run():
classifier = Sequential()
classifier.add(Flatten(input_shape=(28, 28)))
classifier.add(Dense(10, kernel_initializer='uniform', activation= 'relu'))
classifier.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
X_train, y_train = keras.datasets.mnist.load_data()[0]
X_train = X_train[:100] # for example
y_train = keras.utils.to_categorical(y_train)[:100]
classifier.fit(X_train, y_train, batch_size=10, epochs=100)
在每次运行中给出不同的结果.
gives a different result on each run.
>>> run()
Epoch 1/100
100/100 [==============================] - 0s 4ms/step - loss: 10.1763 - acc: 0.1700
...
Epoch 100/100
100/100 [==============================] - 0s 2ms/step - loss: 4.5131 - acc: 0.4700
>>> run()
Epoch 1/100
100/100 [==============================] - 0s 5ms/step - loss: 7.2993 - acc: 0.2000
...
Epoch 1/100
100/100 [==============================] - 0s 2ms/step - loss: 0.8059 - acc: 0.7000
您可以在keras随机生成器(numpy)中修复种子,以实现可重复性.
You can fix seed in keras random generator (which is numpy) for reproducibility.
>>> np.random.seed(1)
>>> run()
Epoch 1/100
100/100 [==============================] - 0s 5ms/step - loss: 7.6193 - acc: 0.1500
...
Epoch 100/100
100/100 [==============================] - 0s 2ms/step - loss: 0.3224 - acc: 0.6400
>>> np.random.seed(1)
>>> run()
Epoch 1/100
100/100 [==============================] - 0s 5ms/step - loss: 7.6193 - acc: 0.1500
...
Epoch 100/100
100/100 [==============================] - 0s 2ms/step - loss: 0.3224 - acc: 0.6400
https://github.com/keras-team/keras /issues/2743#issuecomment-219777627
P.S.,如果数据/模型存在一些问题(例如在该示例中,数据太少且模型太简单),代码的结果可能会大不相同. 90%可能只是过度拟合.在另一个独立的测试数据上检查分类器.
P.S. Code may have very different results, if there are some problems with data/model (as in this mnist example with too small data and too easy model). 90% could be just overfitting. Check classifier on another independent test data.
这篇关于如何在Jupyter Notebook中多次拟合/运行神经网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!