我正在尝试ELMO
通过神经网络中的张量流实现嵌入。这是我的网络的代码片段:
def get_elmo_embeds_model():
input_text = tf.keras.layers.Input(shape=(1,), dtype=tf.string)
embedding = tf.keras.layers.Lambda(ELMoEmbedding, output_shape=(1024, ))(input_text)
print(embedding.shape)
conv_1d_layer = tf.keras.layers.Conv1D(256,5,activation='relu')(embedding)
max_pool_1 = tf.keras.layers.MaxPooling1D(5)(conv_1d_layer)
x = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(256,name="BiLSTM")) (max_pool_1)
dropout_2 = tf.keras.layers.Dropout(0.2)(x)
flatten_1 = tf.keras.layers.Flatten()(dropout_2)
pred = tf.keras.layers.Dense(1, activation='sigmoid')(flatten_1)
model = tf.keras.models.Model(inputs=[input_text], outputs=pred)
return model
text_only_model = get_elmo_embeds_model()
text_only_model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',precision_mat,recall_mat,f1_mat])
text_only_model.summary()
with tf.Session() as session:
K.set_session(session)
session.run(tf.global_variables_initializer())
session.run(tf.tables_initializer())
history = text_only_model.fit_generator(text_elmo_train,epochs=EPOCHS, validation_steps=VALIDATION_STEPS,
steps_per_epoch=STEPS_PER_EPOCH,validation_data = text_elmo_valid)
运行此模型时,我收到以下错误:
steps_per_epoch=STEPS_PER_EPOCH,validation_data = text_elmo_valid)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_v1.py", line 796, in fit
use_multiprocessing=use_multiprocessing)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_generator_v1.py", line 586, in fit
steps_name='steps_per_epoch')
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_generator_v1.py", line 306, in model_iteration
steps_name='validation_steps')
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_generator_v1.py", line 252, in model_iteration
batch_outs = batch_function(*batch_data)
File "/home/.local/lib/python3.7/site-packages/keras/engine/training_v1.py", line 1152, in test_on_batch
outputs = self.test_function(inputs) # pylint: disable=not-callable
File "/home/.local/lib/python3.7/site-packages/keras/backend.py", line 4187, in __call__
run_metadata=self.run_metadata)
File "/home/.conda/envs/test_multimod/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1485, in __call__
run_metadata_ptr)
tensorflow.python.framework.errors_impl.UnimplementedError: TensorArray has size zero, but element shape [?,256] is not fully defined. Currently only static shapes are supported when packing zero-size TensorArrays.
我检查了正在传递的数据,其中没有空值,但在运行此函数时仍然存在此错误。