无效参数:reshape 的输入是具有 14155776 个值的张量,但请求的形状具有 262144

我正在尝试使用 ELMO 嵌入来用 LSTM 训练我的网络,但我对张量的形状有疑问 y-train with shape (67689,5) 用 1 个热向量编码(输出为 5 个类) x-train with shape (67689,) 是文本格式 这是代码:

from  tensorflow.keras.layers import Input,Lambda,Dense
from  tensorflow.keras.models import Model
import  tensorflow.keras.backend as K

batch_size=32
sequence_length=58
def ElmoEmbedding(x):
    return elmo_model(inputs={
                            "tokens": tf.squeeze(tf.cast(x,tf.string)),"sequence_len": tf.constant(batch_size*[sequence_length])
                      },signature="tokens",as_dict=True)["elmo"]

input_text = Input(shape=(1,),dtype=tf.string)
embedding = Lambda(ELMoEmbedding,output_shape=(58,1024,))(input_text)
reshape_2 = Reshape((1024,1,))(embedding)
lstm = Bidirectional(LSTM(units=100,recurrent_activation='relu',return_sequences=True,recurrent_dropout=0.2,dropout=0.2))(reshape_2)
reshape_3 = Reshape(( 5,))(lstm)
pred = Dense(5,activation='softmax')(reshape_3)
model = Model(inputs=[input_text],outputs=pred)
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
print(model.summary())
with tf.Session() as session:
    K.set_session(session)
    session.run(tf.global_variables_initializer())  
    session.run(tf.tables_initializer())


   
    history = model.fit(x_train,y_train,epochs=3,batch_size=256,validation_split = 0.2,shuffle=True)
    loss,accuracy = model.evaluate(x_train,verbose=False)
    print("Training accuracy: {:.4f}".format(accuracy))
    loss,accuracy = model.evaluate(x_test,y_test,verbose=False)
    print("Testing accuracy:  {:.4f}".format(accuracy))

我遇到了这个错误

InvalidArgumentError:发现 2 个根错误。 (0) 无效参数: 重塑的输入是一个具有 52428800 个值的张量,但请求的 形状有 1280 [[{{node reshape_45/Reshape}}]]
[[loss_49/mul/_847]] (1) 无效参数:reshape 的输入是一个 张量有 52428800 个值,但请求的形状有 1280
[[{{node reshape_45/Reshape}}]] 0 次成功操作。 0 派生 错误被忽略。

emergtech 回答:无效参数:reshape 的输入是具有 14155776 个值的张量,但请求的形状具有 262144

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/255415.html

大家都在问