我正在构建一个命名实体识别模型。在进行预处理(建模和验证集中的标记化、填充和分离)之后,我在训练模型时出错。据我了解,错误是由于训练集和测试集的尺寸造成的。
我的模型如下
model = tf.keras.Sequential([
tf.keras.layers.Embedding(input_dim=2109,output_dim=64),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(16, activation='relu'),
#tf.keras.layers.Dropout(rate, noise_shape=None, seed=None, **kwargs)
tf.keras.layers.Dense(3, activation='softmax')
])
我的训练数据具有以下格式。您看到的第一个包含随机数和 -1 的向量是我的向量 x,而您看到的仅包含 1 和 0 的第二个向量是我的向量,它采用一种热编码格式
print(train_pad_seq[1])
print('\n')
print(train_tag[1])
print('\n')
print(len(test_pad_seq[1]))
print('\n')
print(len(test_tag[1]))
[269 2 9 6 28 21 2 53 3 142 368 23 132 12 23 698 2 88
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1]
[[1. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
我的训练和测试集的维度如下
print('lad dimensiones de train_seq es: ')
print(train_pad_seq.shape)
print('lad dimensiones de train_tag es: ')
print(train_tag.shape)
print('\n')
print('lad dimensiones de test_seq es: ')
print(test_seq.shape)
print('lad dimensiones de test_tag es: ')
print(test_tag.shape)
lad dimensiones de train_seq es:
(1104, 158)
lad dimensiones de train_tag es:
(1104,)
lad dimensiones de test_seq es:
(368,)
lad dimensiones de test_tag es:
(368,)
我编译模型的代码如下
#ajustamos los parametros de la red creada y entrenamos el modelo
num_epochs = 10
#model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
#detencion temprana
history = model.fit(train_pad_seq,train_pad_tag, epochs=num_epochs, validation_data=(test_pad_seq,test_pad_tag))
最后抛出我的错误如下
Epoch 1/10
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-3ba21e640097> in <module>()
5 model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
6 #detencion temprana
----> 7 history = model.fit(train_pad_seq,train_pad_tag, epochs=num_epochs, validation_data=(test_pad_seq,test_pad_tag))
9 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
992 except Exception as e: # pylint:disable=broad-except
993 if hasattr(e, "ag_error_metadata"):
--> 994 raise e.ag_error_metadata.to_exception(e)
995 else:
996 raise
ValueError: in user code:
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:853 train_function *
return step_function(self, iterator)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:842 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1286 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2849 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3632 _call_for_each_replica
return fn(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:835 run_step **
outputs = model.train_step(data)
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py:789 train_step
y, y_pred, sample_weight, regularization_losses=self.losses)
/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py:201 __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:141 __call__
losses = call_fn(y_true, y_pred)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:245 call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/losses.py:1666 categorical_crossentropy
y_true, y_pred, from_logits=from_logits, axis=axis)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper
return target(*args, **kwargs)
/usr/local/lib/python3.7/dist-packages/keras/backend.py:4839 categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/tensor_shape.py:1161 assert_is_compatible_with
raise ValueError("Shapes %s and %s are incompatible" % (self, other))
ValueError: Shapes (None, 158, 3) and (None, 3) are incompatible
有人可以帮我理解这个错误的原因吗?