【问题标题】:Keras LSTM for continuous input and continuous outputKeras LSTM 用于连续输入和连续输出
【发布时间】:2018-10-25 07:36:54
【问题描述】:

例如,我有二进制数据,比如说:0、0、0、1、1、0、1、1。这可能会无限期地持续下去。对于每个输入,都有相应的输出。假设我们使用 XOR 操作。因此,输出可能如下所示:0, 0, 0, 1, 0, 1, 1, 0。

如何塑造 Keras 输入形状?如何设置时间步长?如果我声明时间步长 1 是针对每个 1 个时间步长考虑不同的情况,或者它仍然可以将先前的输入考虑为序列或学习记忆?

Keras 使用 LSTM 或 GRU 作为其隐藏层。

我已经尝试了 2 种方法来解决这个问题,但似乎都没有成功。两种方法都停留在 37.5 acc。事实上,它一直在猜测 1。

方法一:

data = [[[0], [0], [0], [1], [1], [0], [1], [1]]]
output = [[[0], [0], [0], [1], [0], [1], [1], [0]]]

model = Sequential()
model.add(GRU(10, input_shape=(8, 1), return_sequences=True))
model.add(GRU(10, return_sequences=True))
model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(np.asarray(data), np.asarray(output), epochs=3000)

方法二:

data = [[[0]], [[0]], [[0]], [[1]], [[1]], [[0]], [[1]], [[1]]]
output = [[0], [0], [0], [1], [0], [1], [1], [0]]

model = Sequential()
model.add(GRU(10, input_shape=(1, 1), return_sequences=True))
model.add(GRU(10))
model.add(Dense(1, activation='softmax'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(np.asarray(data), np.asarray(output), epochs=300)

【问题讨论】:

    标签: python machine-learning keras lstm rnn


    【解决方案1】:

    其实它一直在猜1。

    那是因为您使用softmax 作为最后一层的激活。由于最后一层只有一个单元,并且 softmax 函数对其输入进行归一化,使得元素之和等于 1,因此它始终输出 1。相反,您需要使用 sigmoid 作为最后一层的激活函数来获得输出介于 0 和 1 之间。

    【讨论】:

      【解决方案2】:

      activation='softmax' 更改为activation='sigmoid'

      【讨论】:

        猜你喜欢
        • 2018-11-13
        • 1970-01-01
        • 2019-01-04
        • 2023-03-10
        • 2016-08-21
        • 2020-05-31
        • 2018-07-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多