简体   繁体   English

Keras TimeDistributed input_shape 不匹配

[英]Keras TimeDistributed input_shape mismatch

I'm trying to build a model with the TimeDistributed Dense layer, but I still get this error.我正在尝试使用 TimeDistributed Dense 层构建一个 model,但我仍然收到此错误。

ValueError: `TimeDistributed` Layer should be passed an `input_shape` with at least 3 dimensions, received: (None, 16)

Am I missing something?我错过了什么吗? The data has a format like the one provided in the snippet below.数据的格式类似于下面代码片段中提供的格式。 The model is simplified, but the error is the same. model被简化了,但是错误是一样的。

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, TimeDistributed, Dense
import numpy as np


data = np.random.random((100, 10, 32))
labels = np.random.randint(2, size=(100, 10, 1))

model = Sequential()

model.add(LSTM(16, input_shape=(10, 32)))
model.add(TimeDistributed(Dense(10, activation='sigmoid')))

model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(data, labels, epochs=10, batch_size=32)

Try changing the number of units in your output layer to 1 and using return_sequences=True in your LSTM layer to get an output space for each timestep:尝试将 output 层中的单元数更改为 1,并在LSTM层中使用return_sequences=True为每个时间步获取 output 空间:

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, TimeDistributed, Dense
import numpy as np


data = np.random.random((100, 10, 32))
labels = np.random.randint(2, size=(100, 10, 1))

model = Sequential()

model.add(LSTM(16, input_shape=(10, 32), return_sequences=True))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))

model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(data, labels, epochs=10, batch_size=32)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM