简体   繁体   English

ValueError:检查输入时出错:预期 input_1 具有形状 (50,),但使用 ELMo 嵌入和 LSTM 获得形状为 (1,) 的数组

[英]ValueError: Error when checking input: expected input_1 to have shape (50,) but got array with shape (1,) with ELMo embeddings and LSTM

I'm trying to reproduce the example at this link:我正在尝试在此链接上重现示例:
https://www.depends-on-the-definition.com/named-entity-recognition-with-residual-lstm-and-elmo/ https://www.depends-on-the-definition.com/named-entity-recognition-with-residual-lstm-and-elmo/
In few words, I'm trying to use the ELMo embeddings for the Sequence tagging task.简而言之,我正在尝试将 ELMo 嵌入用于 Sequence 标记任务。 I'm following this tutorial but when I try to fit the model我正在关注本教程,但是当我尝试拟合模型时

ValueError: Error when checking input: expected input_1 to have shape (50,) but got array with shape (1,)

The code that gives me the error is this:给我错误的代码是这样的:

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)

The error is related to the last line of this code, when I try to fit the model.当我尝试拟合模型时,该错误与此代码的最后一行有关。 Can someone help me in understand how to solve this problem?有人可以帮助我了解如何解决这个问题吗?

your input shape is specified as (50, ), but your current output of your np.array(X_tr) is a single line of array (1,).您的输入形状指定为 (50, ),但您 np.array(X_tr) 的当前输出是单行数组 (1,)。 Given limited information about your data, I would check the length of the array (X_tr) and if it's 50, just transpose it using .T鉴于有关您的数据的信息有限,我会检查数组 (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 https://docs.scipy.org/doc/numpy-1.15.0/reference/generated/numpy.ndarray.T.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到形状为 (1221, 50, 50, 1) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (1221, 50, 50, 1) Keras LSTM输入-ValueError:检查输入时出错:预期input_1具有3维,但数组的形状为(1745,1) - Keras LSTM Input - ValueError: Error when checking input: expected input_1 to have 3 dimensions, but got array with shape (1745, 1) ValueError:检查输入时出错:预期 input_1 的形状为 (168, 5),但数组的形状为 (5808, 5) - ValueError: Error when checking input: expected input_1 to have shape (168, 5) but got array with shape (5808, 5) ValueError:检查输入时出错:预期lstm_1_input具有3个维,但数组的形状为(393613,50) - ValueError: Error when checking input: expected lstm_1_input to have 3 dimensions, but got array with shape (393613, 50) ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到了形状为(无、无、无)的数组 - ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (None, None, None) ValueError:检查输入时出错:期望input_1有4个维度,但得到的数组有形状(6243,256,256) - ValueError: Error when checking input: expected input_1 to have 4 dimensions, but got array with shape (6243, 256, 256) ValueError:检查输入时出错:预期 input_1 有 5 个维度,但得到了形状为 (10000, 32, 3, 32) 的数组 - ValueError: Error when checking input: expected input_1 to have 5 dimensions, but got array with shape (10000, 32, 3, 32) ValueError:检查输入时出错:预期 input_1 具有形状 (224, 224, 3) 但得到形状为 (3, 224, 224) 的数组 - ValueError: Error when checking input: expected input_1 to have shape (224, 224, 3) but got array with shape (3, 224, 224) ValueError:检查输入时出错:预期 lstm_11_input 具有形状 (1, 1) 但得到形状为 (1, 3841) 的数组 - ValueError: Error when checking input: expected lstm_11_input to have shape (1, 1) but got array with shape (1, 3841) ValueError:检查输入时出错:预期 lstm_6_input 具有形状 (87482, 1) 但得到的数组具有形状 (87482, 3) - ValueError: Error when checking input: expected lstm_6_input to have shape (87482, 1) but got array with shape (87482, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM