【问题标题】:keras RNN 3d tensor input and 2d tensor output: Error when checking model targetkeras RNN 3d 张量输入和 2d 张量输出:检查模型目标时出错
【发布时间】:2016-12-11 21:57:34
【问题描述】:

我正在尝试使用 LSTM 对多样本时间序列数据进行建模。我的输入数据具有形状 (100, 93, 6) - 100 个独立的时间序列(来自相同/相似的过程),93 个时间步长,每个观察的 6 个维度。输出形状为 (100, 93) - 每个独立时间序列的每个时间步长一个布尔输出。 (当然,这是一小部分真实数据)。但是,我不知道如何在 Keras 中构建这样的网络:

from keras.models import Sequential
from keras.layers import LSTM, core, Activation, Dense
import numpy as np

data = np.load('sample.npz')
X = data['x']
y = data['y']
print('X shape: ',X.shape)
print('{} samples, {} time steps, {} observations at each time step, per sample\n'.format(*X.shape))

print('y shape: ',y.shape)
print('{} samples, {} time steps, boolean outcome per observation\n'.format(*y.shape))

print(X[0][2], X[0][55])
print(y[0][2], y[0][92])

X 形状:(100, 93, 6) 100 个样本,93 个时间步长,6 个观测值 每个时间步,每个样本

y 形状:(100, 93) 100 个样本,93 个时间步长,每个布尔结果 观察

[ 1.80000000e+01 1.56000000e+05 2.00000000e+03 1.00000000e+04 3.00000000e+00 5.94000000e+04] [ 0. 0. 0. 0. 0. 0.]

1.0 0.0

model = Sequential()
model.add(LSTM(output_dim=4, input_shape=(93, 6), return_sequences=False))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(X, y, verbose=2)

异常:检查模型目标时出错:预期的 dense_2 有 形状 (None, 1) 但得到了形状 (100, 93) 的数组

我相信 Keras 假设每个时间序列有一个输出 (Y),而每个时间序列的每个时间步长有一个输出。如何让它在 Keras 中工作?

【问题讨论】:

    标签: tensorflow keras recurrent-neural-network lstm


    【解决方案1】:

    我错过了TimeDistributed Layer.. 这有效:

    model = Sequential()
    model.add(LSTM(output_dim=4, input_shape=(93, 6), return_sequences=True))
    
    model.add(TimeDistributed(Dense(1, activation='sigmoid')))
    model.compile(loss='binary_crossentropy', optimizer='adam')
    

    【讨论】:

    • 我的 Xtrian 尺寸 [800, 14, 1000] 和 Ypredict 尺寸 [800, 14, 1000] 我发现 TimeDistributed 对我有用。非常感谢!
    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多