带有LSTM ValueError的NER:无法将NumPy数组转换为张量(不受支持的对象类型int)

时间:2020-08-09 21:10:08

标签: pandas lstm tf.keras ner

我正在使用LSTM和带有tensorflow后端的keras学习NER。 Tensorflow == 2.20r4和keras == 2.3.1。

我一直在研究一些示例,但是遇到了一个我似乎无法弄清的错误。错误消息是:ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int)."

我已将列表输入转换为Numpy数组,如下所示:X = np.asarray(X), Y = np.asarray(Y)

然后我使用from tensorflow.keras.utils import to_categorical对Y进行编码。前置编码和后置编码 形状以及模型说明如下:

数据处理

# print pre-encoding x_train, y_train shapes
print(X_train.shape, y_train.shape)
(8296, 1302) (8296, 1302)

# convert y_train to_categorical 
y_train = to_categorical(y_train)

# print post-encoding x_train, y_train shapes
print(X_train.shape, y_train.shape)
(8296, 1302) (8296, 1302, 32)

# print pre-encoding x_test, y_test shapes
print(X_test.shape, y_test.shape)
(1464, 1302) (1464, 1302)

# convert y_test to categorical 
y_test = to_categorical(y_test)

# print post-encoding X_test, y_test
print(X_test.shape, y_test.shape)
(1464, 1302) (1464, 1302, 32)

模型声明

input = Input(shape=(max_len,))
model = Embedding(input_dim=num_words, output_dim=50, input_length=max_len)(input)
model = SpatialDropout1D(0.1)(model)
model = Bidirectional(LSTM(units=100, return_sequences=True, recurrent_dropout=0.1))(model)
output = TimeDistributed(Dense(num_tags, activation="softmax"))(model)

model = Model(input, output)
model.summary()

model.compile(optimizer="adam",
    loss="categorical_crossentropy",
    metrics=["accuracy"])

训练模型

checkpointer = ModelCheckpoint(filepath = 'model.h5',
                       verbose = 0,
                       mode = 'auto',
                       save_best_only = True,
                       monitor='val_loss')

# the model
early_stopping = EarlyStopping(monitor='val_accuracy', patience=1, verbose=0, mode='max', restore_best_weights=False)
callbacks =[checkpointer, early_stopping]

history = model.fit(
    X_train,
    y_train,
    validation_split=0.2,
    batch_size=32,
    epochs=3,
    verbose=1,
    callbacks=callbacks
)

当我尝试训练模型时,出现以下错误消息: ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

我已经提供了详细的错误消息,以获取可能提供的任何见解

Error message
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-107-482ccaecbe1c> in <module>
      3 callbacks =[checkpointer, early_stopping]
      4 
----> 5 history = model.fit(
      6     X_train,
      7     y_train,

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\keras\engine\training.py in _method_wrapper(self, *args, **kwargs)
     64   def _method_wrapper(self, *args, **kwargs):
     65     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
---> 66       return method(self, *args, **kwargs)
     67 
     68     # Running inside `run_distribute_coordinator` already.

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
    793       # `Tensor` and `NumPy` input.
    794       (x, y, sample_weight), validation_data = (
--> 795           data_adapter.train_validation_split((x, y, sample_weight),
    796                                               validation_split=validation_split,
    797                                               shuffle=False))

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in train_validation_split(arrays, validation_split, shuffle)
   1335     return array_ops.gather_v2(t, indices)
   1336 
-> 1337   train_arrays = nest.map_structure(
   1338       functools.partial(_split, indices=train_indices), arrays)
   1339   val_arrays = nest.map_structure(

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
    615 
    616   return pack_sequence_as(
--> 617       structure[0], [func(*x) for x in entries],
    618       expand_composites=expand_composites)
    619 

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
    615 
    616   return pack_sequence_as(
--> 617       structure[0], [func(*x) for x in entries],
    618       expand_composites=expand_composites)
    619 

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\keras\engine\data_adapter.py in _split(t, indices)
   1332     if t is None:
   1333       return t
-> 1334     t = ops.convert_to_tensor_v2(t)
   1335     return array_ops.gather_v2(t, indices)
   1336 

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
   1276     ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
   1277   """
-> 1278   return convert_to_tensor(
   1279       value=value,
   1280       dtype=dtype,

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1339 
   1340     if ret is None:
-> 1341       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1342 
   1343     if ret is NotImplemented:

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\tensor_conversion_registry.py in _default_conversion_function(***failed resolving arguments***)
     50 def _default_conversion_function(value, dtype, name, as_ref):
     51   del as_ref  # Unused.
---> 52   return constant_op.constant(value, dtype, name=name)
     53 
     54 

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
    259     ValueError: if called on a symbolic tensor.
    260   """
--> 261   return _constant_impl(value, dtype, shape, name, verify_shape=False,
    262                         allow_broadcast=True)
    263 

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    268   ctx = context.context()
    269   if ctx.executing_eagerly():
--> 270     t = convert_to_eager_tensor(value, ctx, dtype)
    271     if shape is None:
    272       return t

c:\users\dt\appdata\local\programs\python38\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     95   ctx.ensure_initialized()
---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
     97 
     98 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).

0 个答案:

没有答案