keras - Keras LSTM,预期为 3,但得到了形状为 [] 的数组
问题描述
我正在尝试从带注释的文本中找出与单词相关的标签。我正在使用一个bidirectional LSTM
. 我有X_train
它的形状(1676, 39)和Y_train
相同的形状(1676, 39)。
input = Input(shape=(sequence_length,))
model = Embedding(input_dim=n_words, output_dim=20,
input_length=sequence_length, mask_zero=True)(input)
model = Bidirectional(LSTM(units=50, return_sequences=True,
recurrent_dropout=0.1))(model)
out_model = TimeDistributed(Dense(50, activation="softmax"))(model)
model = Model(input, out_model)
model.compile(optimizer="rmsprop", loss= "categorical_crossentropy", metrics=["accuracy"])
model.fit(X_train, Y_train, batch_size=32, epochs= 10,
validation_split=0.1)
执行此操作时,我收到错误:
ValueError: Error when checking target: expected time_distributed_5 to have 3 dimensions, but got array with shape (1676, 39).
我无法找出如何提供 Keras LSTM 模型所需的适当尺寸。
解决方案
结果,在LSTM
你设置return_sequences=True
的层中,该层的输出是一个形状为 [ batch_size * 39 * 50 ] 的张量。然后将此张量传递给TimeDistributed层。TimeDistributed
在每个时间戳上应用密集层。该层的输出也是 [ batch_size * 39 * 50 ]。如您所见,您通过 3 维张量进行预测,而您的基本事实是 2 维 (1676, 39)。
如何解决问题?
1)return_sequences=True
从 LSTM 参数中删除。
2) 移除TimeDistributed
层并直接应用密集层。
inps = keras.layers.Input(shape=(39,))
embedding = keras.layers.Embedding(vocab_size, 16)(inps)
rnn = keras.layers.LSTM(50)(embedding)
dense = keras.layers.Dense(50, activation="softmax")(rnn)
prediction = keras.layers.Dense(39, activation='softmax')(dense)
推荐阅读
- mongodb - MongoDb 拒绝来自 docker-hub 的连接
- android - 避免地图自动缩小android
- javascript - 如果元素开始隐藏,css 过渡不起作用
- c - 无法使文件描述符 _write 函数工作 - Windows 10
- java - prometheus 占用 JDBC 连接
- ruby-on-rails - Rails 5.2 非当前用户的服务对象
- rhel - pam_open_session:系统错误和 sudo:策略插件会话初始化失败,etc/pam.d/password-auth 中没有通过过期
- r - 如何更改变量的比例?
- html - 如何将内部设置为全宽
- amazon-web-services - AWS API 部署