python - 如何在 Keras 中训练(拟合)连接模型?
问题描述
您好,这是经过一些编辑后我为分类 IMDB 电影创建的模型。它由两个连接模型(LSTM 和 CNN)组成
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
np.random.seed(7)
top_words = 5000
(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)
max_review_length = 5
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
def cnn_lstm_merged():
embedding_vecor_length = 32
cnn_model = Sequential()
cnn_model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
cnn_model.add(Conv1D(filters=32, kernel_size=3, padding='same', activation='relu'))
cnn_model.add(MaxPooling1D(pool_size=2))
cnn_model.add(Flatten())
lstm_model = Sequential()
lstm_model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
lstm_model.add(LSTM(64, activation = 'relu', return_sequences=True))
lstm_model.add(Flatten())
merge = Concatenate([lstm_model, cnn_model])
hidden = Dense(1, activation = 'sigmoid')
conc_model = Sequential()
conc_model.add(merge)
conc_model.add(hidden)
conc_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = conc_model.fit([X_train, X_train], [y_train, y_train], np.reshape([y_train, y_train],(25000,2)),epochs=3, batch_size=(64,64))
return history
x_train shape is (25000, 5) y_train shape is (25000,) 我不知道如何训练这个模型,因为有问题。错误是:
File "/home/pythonist/Desktop/EnsemblingLSTM_CONV/train.py", line 79, in <module>
cnn_lstm_model = cnn_lstm_merged()
File "/home/pythonist/Desktop/EnsemblingLSTM_CONV/train.py", line 69, in cnn_lstm_merged
history = conc_model.fit([X_train, X_train], [y_train, y_train], np.reshape([y_train, y_train],(25000,2)),epochs=3, batch_size=(64,64))
TypeError: fit() got multiple values for argument 'batch_size'
即使我将 batch_size = (64, 64) 更改为 batch_size = 64 仍然给我一个错误。而且我不确定我是否正确地重塑了 y_train。如何在这个级联模型中训练和预测输出数据?谢谢
解决方案
推荐阅读
- javascript - 选择数据 cense 的数组值
- windows - Android Studio 未检测到 Pepper Android 平板电脑
- python - 试图将数组中的属性添加到 python 中的文本文件中?
- android - 在 buttonclick 上从 viewpager 获取图像和标题
- spring - 谷歌的 Spring OAuth2 Resouce Server 实现
- events - 时钟边沿事件可以在verilog中的always块内合成吗?
- xamarin.ios - Xamarin.iOS - 内存泄漏问题 UIImage/UIImageView
- javascript - 在 axios 异步调用之外返回数据
- java - 删除 Spring RepositoryRestResource 中的“_embedded”属性
- asp.net-core - Okta 使用 OpenID Connect 配置中的公钥验证 JWT 令牌