递归神经网络中的时期与迭代
epoch vs. iteration in recurrent neural networks
我正在使用 RNN 和 LSTM 查看 Keras 的 text generation example,但仍然对术语 epoch 和 iteration[= 之间的区别感到困惑45=].
尽管如此,here is a previous question asking the same thing, I cannot understand the answer, or this answer is different from how I understand it and also different from how the following example handles it. Based on this answer,据说
one epoch = one forward pass and one backward pass of all the training examples
number of iterations = number of passes, each pass using [batch size] number of examples.
Example: if you have 1000 training examples, and your batch size is 500, then it will take 2 iterations to complete 1 epoch.
得出结论:(#training examples/batch size) = (#iterations/#epochs)
。
但是,下面的example,据我理解,和前面的结论不一样
# train the model, output generated text after each iteration
for iteration in range(1, 60):
print()
print('-' * 50)
print('Iteration', iteration)
model.fit(X, y, batch_size=128, nb_epoch=1)
start_index = random.randint(0, len(text) - maxlen - 1)
for diversity in [0.2, 0.5, 1.0, 1.2]:
print()
print('----- diversity:', diversity)
generated = ''
sentence = text[start_index: start_index + maxlen]
generated += sentence
print('----- Generating with seed: "' + sentence + '"')
sys.stdout.write(generated)
for i in range(400):
x = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x[0, t, char_indices[char]] = 1.
preds = model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
sys.stdout.write(next_char)
sys.stdout.flush()
print()
这里,iteration是60并且epoch的数量设置为1,这让我很困惑。看起来,有 60 迭代 ,如 for iteration in range(1, 60)
所述。对于每个 迭代 ,一个 纪元 是按照每个 for 循环的规定 model.fit(X, y, batch_size=128, nb_epoch=1)
完成的。再一次,这里有一个 batch_size=128
。那么迭代到底是什么意思?
任何人都可以根据这个例子解释iteration和epoch之间的区别?
我认为在这个例子中,迭代 意味着不同的东西:你在学习过程中迭代,并且在每个时期之后你都在用部分学习的模型做一些事情。您正在迭代地执行此操作,这就是为什么使用 迭代 词的原因。
在这种情况下,迭代仅用于显示中间结果。
我们可以删除这段代码:
for diversity in [0.2, 0.5, 1.0, 1.2]:
print()
print('----- diversity:', diversity)
generated = ''
sentence = text[start_index: start_index + maxlen]
generated += sentence
print('----- Generating with seed: "' + sentence + '"')
sys.stdout.write(generated)
for i in range(400):
x = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x[0, t, char_indices[char]] = 1.
preds = model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
sys.stdout.write(next_char)
sys.stdout.flush()
print()
而是:
for iteration in range(1, 60):
model.fit(X, y, batch_size=128, nb_epoch=1)
写入:
model.fit(X, y, batch_size=128, nb_epoch=60)
我正在使用 RNN 和 LSTM 查看 Keras 的 text generation example,但仍然对术语 epoch 和 iteration[= 之间的区别感到困惑45=].
尽管如此,here is a previous question asking the same thing, I cannot understand the answer, or this answer is different from how I understand it and also different from how the following example handles it. Based on this answer,据说
one epoch = one forward pass and one backward pass of all the training examples
number of iterations = number of passes, each pass using [batch size] number of examples.
Example: if you have 1000 training examples, and your batch size is 500, then it will take 2 iterations to complete 1 epoch.
得出结论:(#training examples/batch size) = (#iterations/#epochs)
。
但是,下面的example,据我理解,和前面的结论不一样
# train the model, output generated text after each iteration
for iteration in range(1, 60):
print()
print('-' * 50)
print('Iteration', iteration)
model.fit(X, y, batch_size=128, nb_epoch=1)
start_index = random.randint(0, len(text) - maxlen - 1)
for diversity in [0.2, 0.5, 1.0, 1.2]:
print()
print('----- diversity:', diversity)
generated = ''
sentence = text[start_index: start_index + maxlen]
generated += sentence
print('----- Generating with seed: "' + sentence + '"')
sys.stdout.write(generated)
for i in range(400):
x = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x[0, t, char_indices[char]] = 1.
preds = model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
sys.stdout.write(next_char)
sys.stdout.flush()
print()
这里,iteration是60并且epoch的数量设置为1,这让我很困惑。看起来,有 60 迭代 ,如 for iteration in range(1, 60)
所述。对于每个 迭代 ,一个 纪元 是按照每个 for 循环的规定 model.fit(X, y, batch_size=128, nb_epoch=1)
完成的。再一次,这里有一个 batch_size=128
。那么迭代到底是什么意思?
任何人都可以根据这个例子解释iteration和epoch之间的区别?
我认为在这个例子中,迭代 意味着不同的东西:你在学习过程中迭代,并且在每个时期之后你都在用部分学习的模型做一些事情。您正在迭代地执行此操作,这就是为什么使用 迭代 词的原因。
在这种情况下,迭代仅用于显示中间结果。 我们可以删除这段代码:
for diversity in [0.2, 0.5, 1.0, 1.2]:
print()
print('----- diversity:', diversity)
generated = ''
sentence = text[start_index: start_index + maxlen]
generated += sentence
print('----- Generating with seed: "' + sentence + '"')
sys.stdout.write(generated)
for i in range(400):
x = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x[0, t, char_indices[char]] = 1.
preds = model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
sys.stdout.write(next_char)
sys.stdout.flush()
print()
而是:
for iteration in range(1, 60):
model.fit(X, y, batch_size=128, nb_epoch=1)
写入:
model.fit(X, y, batch_size=128, nb_epoch=60)