我想让LSTM网络学会每次在序列中有0时就将序列的第一个值还给我,而在有另一个值时则给我0。
示例:
x = 9 8 3 1 0 3 4
y = 0 0 0 0 9 0 0
网络会存储一个值,并在收到特殊信号时将其返回。
我认为一个LSTM单元格可以做到这一点:
红色为权重,灰色区域为偏差。
这是我的模特:
model2=Sequential()
model2.add(LSTM(input_dim=1, output_dim=1, return_sequences = True))
model2.add(TimeDistributed(Dense(output_dim=1, activation='linear')))
model2.compile(loss = "mse", optimizer = "rmsprop")
以及这里如何设置单元格的权重,但是我不确定所有顺序:
# w : weights of x_t
# u : weights of h_{t-1}
# order of array: input_gate, new_input, forget_gate, output_gate
# (Tensorflow order)
w = np.array([[0, 1, 0, -100]], dtype=np.float32)
u = np.array([[1, 0, 0, 0]], dtype=np.float32)
biases = np.array([0, 0, 1, 1], dtype=np.float32)
model2.get_layer('lstm').set_weights([w, u, biases])
我对维格斯正确吗?是我在图上说的吗?
要工作,它必须具有正确的初始值。如何设置单元格的初始值c和先前输出的h?我在源代码中看到了
h_tm1 = states[0] # previous memory state
c_tm1 = states[1] # previous carry state
但是我找不到如何使用它。
答案 0 :(得分:1)
为什么不手动执行此操作?这是如此简单,而且是精确的计算。您不需要权重,对于权重这当然是不可区分的。
给出形状为(batch, steps, features)
的输入张量:
def processSequence(x):
initial = x[:,0:1]
zeros = K.cast(K.equal(x,0), K.floatx())
return initial * zeros
model.add(Lambda(processSequence))
警告:如果您打算将其与其他层的输入一起使用,则发现零的可能性将很小,以至于该层将无用。