【发布时间】:2017-09-21 01:15:46
【问题描述】:
我只是想训练一个 LSTM 来反转一个整数序列。我的方法是 this 教程的修改版本,其中他只是回显输入序列。它是这样的:
- 生成长度为 R 的随机序列 S(可能的值范围为 0 到 99)
- 在长度为 L(移动窗口)的子序列中打断上面的句子
- 每个子序列都有它的反向作为真值标签
因此,这将生成 (R - L + 1) 个子序列,这是一个形状为 (R - L + 1) x L 的输入矩阵。例如,使用:
S = 1 2 3 4 5 ... 25 (1 to 25)
R = 25
L = 5
我们最终得到 21 个句子:
s1 = 1 2 3 4 5, y1 = 5 4 3 2 1
s2 = 2 3 4 5 6, y2 = 6 5 4 3 2
...
s21 = 21 22 23 24 25, y21 = 25 24 23 22 21
这个输入矩阵然后被单热编码并馈送到 keras。然后我对另一个序列重复该过程。问题是它不收敛,准确率很低。我做错了什么?
在下面的代码中,我使用 R = 500 和 L = 5,它给出了 496 个子序列,batch_size = 16(所以我们每个“训练会话”有 31 次更新):
代码如下:
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import TimeDistributed
from keras.layers import LSTM
from random import randint
from keras.utils.np_utils import to_categorical
import numpy as np
def one_hot_encode(sequence, n_unique=100):
encoding = list()
for value in sequence:
vector = [0 for _ in range(n_unique)]
vector[value] = 1
encoding.append(vector)
return np.array(encoding)
def one_hot_decode(encoded_seq):
return [np.argmax(vector) for vector in encoded_seq]
def get_data(rows = 500, length = 5, n_unique=100):
s = [randint(0, n_unique-1) for i in range(rows)]
x = []
y = []
for i in range(0, rows-length + 1, 1):
x.append(one_hot_encode(s[i:i+length], n_unique))
y.append(one_hot_encode(list(reversed(s[i:i+length])), n_unique))
return np.array(x), np.array(y)
N = 50000
LEN = 5
#ROWS = LEN*LEN - LEN + 1
TIMESTEPS = LEN
ROWS = 10000
FEATS = 10 #randint
BATCH_SIZE = 588
# fit model
model = Sequential()
model.add(LSTM(100, batch_input_shape=(BATCH_SIZE, TIMESTEPS, FEATS), return_sequences=True, stateful=True))
model.add(TimeDistributed(Dense(FEATS, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
print(model.summary())
# train LSTM
for epoch in range(N):
# generate new random sequence
X,y = get_data(500, LEN, FEATS)
# fit model for one epoch on this sequence
model.fit(X, y, epochs=1, batch_size=BATCH_SIZE, verbose=2, shuffle=False)
model.reset_states()
# evaluate LSTM
X,y = get_data(500, LEN, FEATS)
yhat = model.predict(X, batch_size=BATCH_SIZE, verbose=0)
# decode all pairs
for i in range(len(X)):
print('Expected:', one_hot_decode(y[i]), 'Predicted', one_hot_decode(yhat[i]))
谢谢!
编辑:似乎正在提取序列的最后一个数字:
Expected: [7, 3, 7, 7, 6] Predicted [3, 9, 7, 7, 6]
Expected: [6, 7, 3, 7, 7] Predicted [4, 6, 3, 7, 7]
Expected: [6, 6, 7, 3, 7] Predicted [4, 3, 7, 3, 7]
Expected: [1, 6, 6, 7, 3] Predicted [3, 3, 6, 7, 3]
Expected: [8, 1, 6, 6, 7] Predicted [4, 3, 6, 6, 7]
Expected: [8, 8, 1, 6, 6] Predicted [3, 3, 1, 6, 6]
Expected: [9, 8, 8, 1, 6] Predicted [3, 9, 8, 1, 6]
Expected: [5, 9, 8, 8, 1] Predicted [3, 3, 8, 8, 1]
Expected: [9, 5, 9, 8, 8] Predicted [7, 7, 9, 8, 8]
Expected: [0, 9, 5, 9, 8] Predicted [7, 9, 5, 9, 8]
Expected: [7, 0, 9, 5, 9] Predicted [5, 7, 9, 5, 9]
Expected: [1, 7, 0, 9, 5] Predicted [7, 9, 0, 9, 5]
Expected: [9, 1, 7, 0, 9] Predicted [5, 9, 7, 0, 9]
Expected: [4, 9, 1, 7, 0] Predicted [6, 3, 1, 7, 0]
Expected: [4, 4, 9, 1, 7] Predicted [4, 3, 9, 1, 7]
Expected: [0, 4, 4, 9, 1] Predicted [3, 9, 4, 9, 1]
Expected: [1, 0, 4, 4, 9] Predicted [5, 5, 4, 4, 9]
Expected: [3, 1, 0, 4, 4] Predicted [3, 3, 0, 4, 4]
Expected: [0, 3, 1, 0, 4] Predicted [3, 3, 1, 0, 4]
Expected: [2, 0, 3, 1, 0] Predicted [6, 3, 3, 1, 0]
【问题讨论】:
-
我认为 LSTM 层没有这种能力。至少不是一个人。但是....为什么要为此训练 LSTM?
-
这只是一个探索 LSTM 的例子。你认为它无法学习如何反转序列吗?
-
是的....除非我理解错了,否则我认为它不能。原因:您希望结果的第一步基于最后一个数字,它只会在最后一步看到。因此,输出序列中的第一个数字完全看不到最后一个数字。最后一个可以受到第一个的影响,但我不相信第一个会受到最后一个的影响。而且由于序列是随机的,因此在数字之间找不到可能的关系。
-
另外,您使用的是
stateful=True。这与滑动窗口案例不兼容。在stateful=True中,第二批中的序列是第一批中序列的精确延续。但是你有序列的重叠部分。stateful=True仅在您不想一次通过很长的序列并且必须将它们分成更小但连续的序列时才需要。 -
我增加了样本数量并使用了`stateful=False',准确率达到了65%。很奇怪。
标签: python deep-learning keras lstm