<p>我正在使用<code>keras</code>库在<code>python</code>中拟合一个递归神经网络。我通过改变<code>epoch</code>函数中的参数<code>nb_epoch</code>来拟合不同的<code>epoch</code>个数。目前我正在使用<code>for</code>循环,每次我改变<code>nb_epoch</code>时都会重新拟合,这是一个大量重复的工作。下面是我的代码(如果您想跳过代码详细信息的其他部分,则循环位于代码底部):</p>
<pre><code>from __future__ import division
import numpy as np
import pandas
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout
from keras.layers.recurrent import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.learning_curve import learning_curve
####################################
###
### Here I do the data processing to create trainX, testX
###
####################################
#model create:
model = Sequential()
#this is the epoch array for different nb_epoch
####################################
###
### Here I define model architecture
###
####################################
model.compile(loss="mse", optimizer="rmsprop")
#################################################
#### Defining arrays for different epoch number
#################################################
epoch_array = range(100, 2100,100)
# I create the following arrays/matrices to store the result of NN fit
# different epoch number.
train_DBN_fitted_Y = np.zeros(shape=(len(epoch_array),trainX.shape[0]))
test_DBN_fitted_Y = np.zeros(shape=(len(epoch_array),testX.shape[0]))
###############################################
###
### Following loop is the heart of the question
###
##############################################
i = 0
for epoch in epoch_array:
model.fit( trainX, trainY,
batch_size = 16, nb_epoch = epoch, validation_split = 0.05, verbose = 2)
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
trainPredict = trainPredict.reshape(trainPredict.shape[0])
testPredict = testPredict.reshape(testPredict.shape[0])
train_DBN_fitted_Y[i] = trainPredict
test_DBN_fitted_Y[i] = testPredict
i = i + 1
</code></pre>
<p>现在这个循环效率很低。因为例如,当它设置为<code>nb_epoch</code>=100时,它从<code>epoch = 1</code>开始训练,到<code>epoch = 100</code>结束,如下所示:</p>
^{pr2}$
<p>在循环的下一个迭代中,它说<code>nb_epoch = 200</code>,它再次从<code>epoch = 1</code>开始训练,并在<code>epoch = 200</code>结束。但我想做的是,在这个迭代中,从循环的最后一次迭代中的地方开始训练,即<code>epoch = 100</code>,然后<code>epoch = 101</code>等等。。。。在</p>
<p>如何修改此循环以实现此目的?在</p>