我正在尝试通过以下链接重现示例:
https://www.depends-on-the-definition.com/named-entity-recognition-with-residual-lstm-and-elmo/
简而言之,我正在尝试将ELMo嵌入用于序列标记任务。我正在关注本教程,但是当我尝试拟合模型时
ValueError: Error when checking input: expected input_1 to have shape (50,) but got array with shape (1,)
给我错误的代码是这样的:
from keras.layers.merge import add
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Lambda
input_text = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 1024))(input_text)
x = Bidirectional(LSTM(units=512, return_sequences=True,
recurrent_dropout=0.2, dropout=0.2))(embedding)
x_rnn = Bidirectional(LSTM(units=512, return_sequences=True,
recurrent_dropout=0.2, dropout=0.2))(x)
x = add([x, x_rnn]) # residual connection to the first biLSTM
out = TimeDistributed(Dense(n_tags, activation="softmax"))(x)
model = Model(input_text, out)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["categorical_accuracy"])
X_tr, X_val = X_tr[:1213*batch_size], X_tr[-135*batch_size:]
y_tr, y_val = y_tr[:1213*batch_size], y_tr[-135*batch_size:]
y_tr = y_tr.reshape(y_tr.shape[0], y_tr.shape[1], 1)
y_val = y_val.reshape(y_val.shape[0], y_val.shape[1], 1)
history = model.fit(np.array(X_tr), y_tr, validation_data=(np.array(X_val), y_val),batch_size=batch_size, epochs=3, verbose=1)
当我尝试拟合模型时,该错误与该代码的最后一行有关。 有人可以帮助我了解如何解决此问题吗?
答案 0 :(得分:0)
您的输入形状指定为(50,),但是您np.array(X_tr)的当前输出是数组(1,)的单行。鉴于有关您的数据的信息有限,我将检查数组(X_tr)的长度,如果长度为50,只需使用.T
进行转置X_tr_arr = np.array(X_tr)
X_tr_t = X_tr_arr.T
https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.ndarray.T.html