I am trying to use ELMO embedding to train my Network with LSTM but i have a problem with the shape of the tensor y-train with shape (67689, 5) encoded with 1 hot vector (the output is 5 classes) x-train with shape (67689,) is text formate this the code:
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, y_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))
and I got this error
InvalidArgumentError: 2 root error(s) found. (0) Invalid argument: Input to reshape is a tensor with 52428800 values, but the requested shape has 1280 [[{{node reshape_45/Reshape}}]]
[[loss_49/mul/_847]] (1) Invalid argument: Input to reshape is a tensor with 52428800 values, but the requested shape has 1280
[[{{node reshape_45/Reshape}}]] 0 successful operations. 0 derived errors ignored.