gpt4 book ai didi

machine-learning - 训练 Keras Stateful LSTM return_seq=true 不学习

转载 作者:行者123 更新时间:2023-11-30 09:00:16 26 4
gpt4 key购买 nike

考虑这个最小的可运行示例:

from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
import numpy as np
import matplotlib.pyplot as plt


max = 30
step = 0.5
n_steps = int(30/0.5)

x = np.arange(0,max,step)
x = np.cos(x)*(max-x)/max

y = np.roll(x,-1)
y[-1] = x[-1]

shape = (n_steps,1,1)
batch_shape = (1,1,1)

x = x.reshape(shape)
y = y.reshape(shape)

model = Sequential()
model.add(LSTM(50, return_sequences=True, stateful=True, batch_input_shape=batch_shape))
model.add(LSTM(50, return_sequences=True, stateful=True))

model.add(Dense(1))

model.compile(loss='mse', optimizer='rmsprop')

for i in range(1000):
model.reset_states()
model.fit(x,y,nb_epoch=1, batch_size=1)
p = model.predict(x, batch_size=1)
plt.clf()
plt.axis([-1,31, -1.1, 1.1])
plt.plot(x[:, 0, 0], '*')
plt.plot(y[:,0,0],'o')
plt.plot(p[:,0,0],'.')
plt.draw()
plt.pause(0.001)
<小时/>

如 keras API 中所述 https://keras.io/layers/recurrent/

the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch

因此,我使用 batch_size = 1 并尝试预测每个时间步的衰减余弦函数中的下一个值。预测,或者下图中的红点应该进入绿色圆圈,以便脚本正确预测它,但是它没有收敛......有什么想法让它学习吗?

1

最佳答案

问题在于分别为每个时期调用model.fit。在这种情况下,优化器参数会被重置,这对训练过程是有害的。另一件事是在预测之前调用 reset_states - 就好像它没有被调用一样 - 来自 fitstates 是预测的起始状态,也可能是这样是有害的。最终代码如下:

for epoch in range(1000):
model.reset_states()
tot_loss = 0
for batch in range(n_steps):
batch_loss = model.train_on_batch(x[batch:batch+1], y[batch:batch+1])
tot_loss+=batch_loss

print "Loss: " + str(tot_loss/float(n_steps))
model.reset_states()
p = model.predict(x, batch_size=1)

关于machine-learning - 训练 Keras Stateful LSTM return_seq=true 不学习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42811746/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com