如何将此顺序模型更改为功能模型?

时间:2018-12-14 09:36:53

标签: python machine-learning keras lstm

顺序模型:

X = tokenizer.texts_to_sequences(data['text'])
X = pad_sequences(X)

embed_dim = 128
lstm_out = 300
batch_size = 32

##Buidling the LSTM network

model = Sequential()
model.add(Embedding(input_dim=2500, output_dim=embed_dim, 
input_length=X.shape[1], dropout=0.1))
model.add(LSTM(lstm_out, dropout_U=0.1, dropout_W=0.1))
model.add(Dense(2, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
model.summary()

model.summary()输出:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding_1 (Embedding)      (None, 191, 128)          320000    
_________________________________________________________________
lstm_1 (LSTM)                (None, 300)               514800    
_________________________________________________________________
dense_1 (Dense)              (None, 2)                 602       
=================================================================
Total params: 835,402
Trainable params: 835,402
Non-trainable params: 0
_________________________________________________________________

我想通过Keras中的Functional API对其进行训练,因此我要像这样更改代码:

df = pd.read_csv('test.csv', sep='^')
data = df
data['sentiment'] = ['pos' if (x>3) else 'neg' for x in data['stars']]
data['sentiment'] = ['pos' if (x > 3) else 'neg' for x in data['stars']]
data['text'] = data['text'].apply(lambda x: x.lower())
data['text'] = data['text'].apply((lambda x: re.sub('[^a-zA-z0-9\s]', '', x)))

tokenizer = Tokenizer(nb_words=2500, split=' ')
tokenizer.fit_on_texts(data['text'])
X = tokenizer.texts_to_sequences(data['text'])
X = pad_sequences(X)

embed_dim = 128
lstm_out = 300
batch_size = 32
## X.shape is (5,191)

inputs = Input(shape=(X.shape[1],1))
x = Embedding(input_dim=2500, output_dim=embed_dim, input_length=X.shape[1], dropout=0.1)(inputs)
x = LSTM(lstm_out, dropout_U=0.1, dropout_W=0.1)(x)
prediction = Dense(2,activation='softmax')(x)

model = Model(input=inputs,outputs=prediction)
model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])

Y = pd.get_dummies(data['sentiment']).values
X_train, X_valid, Y_train, Y_valid = train_test_split(X, Y, test_size=0.20, random_state=36)

model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=1, verbose=5)

score, acc = model.evaluate(X_valid, Y_valid, verbose=2, batch_size=batch_size)
print("Logloss score: %.2f" % (score))
print("Validation set Accuracy: %.2f" % (acc))

但它会引发以下错误:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

我应该如何修改我的代码?

1 个答案:

答案 0 :(得分:0)

问题出在您为输入图层指定的输入形状中。它必须改为shape=(X.shape[1],)(即删除多余的1)。此外,请删除input_length层的Embedding参数,因为它是多余的。