python-3.x - 如何将 LSTM 与 Dense 连接起来?
问题描述
尝试将 LSTM 与 Dense 连接时,会出现错误(尝试训练时):
input = Input(shape=(x_train.shape[1], None))
X = Embedding(num_words, max_article_len)(input)
X = LSTM(128, return_sequences=True, dropout = 0.5)(X)
X = LSTM(128)(X)
X = Dense(32, activation='softmax')(X)
model = Model(inputs=[input], outputs=[X])
...
>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)
我尝试了不同的连接选项,但错误重复:
X, h, c = LSTM(128, return_sequences=False, return_state=True, dropout = 0.5)(X)
X = Dense(32, activation='softmax')(X)
>>> ValueError: Error when checking target: expected dense to have shape (32,) but got array with shape (1,)
功能 API / Sequential 的任何解决方案选项?
数据转换代码:
train = pd.read_csv('train.csv')
articles = train['text']
y_train = train['lang']
num_words = 50000
max_article_len = 20
tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(articles)
sequences = tokenizer.texts_to_sequences(articles)
x_train = pad_sequences(sequences, maxlen=max_article_len, padding='post')
x_train.shape
>>> (18974, 100)
y_train.shape
>>> (18974,)
解决方案
最后一个参数必须设置为False
;
X = LSTM(128, return_sequences=True, dropout = 0.5)(X)
X = LSTM(128, return_sequences=False)(X)
如果您仍然有问题,那么问题一定出在您的输入形状上。
推荐阅读
- javascript - 在AngularJS中切换视图时如何让MathML正确呈现
- intellij-idea - 如何通过 IntelliJ 导出运行配置,例如 maven 配置
- indexing - CPLEX 我如何编写此类索引的方程式
- docker - Docker卷文件不可写,写为root
- python - 'bert-serving-start' 未被识别为内部或外部命令
- javascript - 使用 scaleLinear 初始位置拖动圆圈
- assembly - 为什么有多个移动而不是一个?
- android - 更改 Android 模拟器的渲染器无效
- c# - 我遇到了多次调用 Web api 服务的问题
- apache-kafka - Kafka 消费者问题(接收最新偏移量)