【问题标题】:How to change this Sequential model to Functional model?如何将此顺序模型更改为功能模型?
【发布时间】:2018-12-14 09:36:53
【问题描述】:

顺序模型:

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 中通过函数式 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

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

【问题讨论】:

标签: python machine-learning keras lstm


【解决方案1】:

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

【讨论】:

  • lstm 的时间步长参数是什么? X.shape[1]?
  • @user3526542 抱歉,我不清楚您的问题。您正在使用嵌入层,因此这些单词将用作时间步长轴。
猜你喜欢
  • 1970-01-01
  • 2020-07-22
  • 2017-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多