如何在Keras中训练(拟合)级联模型?

时间:2018-09-06 09:09:14

标签: python keras conv-neural-network lstm

您好,这是我为进行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形状为(25000,5) y_train形状为(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的形状。 如何在此级联模型中训练和预测输出数据?谢谢

0 个答案:

没有答案
相关问题