【问题标题】:How to make this 1 cell LSTM network?如何制作这个 1 单元 LSTM 网络?
【发布时间】:2018-06-28 16:27:45
【问题描述】:

我想创建一个 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

但我找不到如何使用它。

【问题讨论】:

    标签: keras lstm


    【解决方案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))
    

    警告:如果您打算将其与来自其他层的输入一起使用,则找到零的概率将非常小,以至于该层将毫无用处。

    【讨论】:

    • 这是一个学习 LSTM 的玩具网络,我想知道如何在 Keras 中设置初始状态并确保我正确设置了权重。不过感谢您的回答,我不知道我们能做到这一点。
    猜你喜欢
    • 2021-01-09
    • 1970-01-01
    • 2021-04-20
    • 2022-12-23
    • 2017-08-28
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多