Ошибка Numpy — ValueError: Не удалось преобразовать массив NumPy в тензор (Неподдерживаемый тип объекта int)

#python #numpy #tensorflow #machine-learning #keras

Вопрос:

Вот мои данные о поездах:

     Message_body                                      Label
0   Rofl. Its true to its name  0
1   The guy did some bitching but I acted like i'd...   0
2   Pity, * was in mood for that. So...any other s...   0
3   Will ?b going to esplanade fr home? 0
4   This is the 2nd time we have tried 2 contact u...   1
... ... ...
970 BangBabes Ur order is on the way. U SHOULD rec...   1
971 -PLS STOP bootydelious (32/F) is inviting you ...   1
972 You are a winner U have been specially selecte...   1
973 Customer service annoncement. You have a New Y...   1
974 FreeMsg Why haven't you replied to my text? I'...   1
 

Вот моя модель:

 X_train = np.array(train)
y_train = np.array(train['Label'])
X_test = test

from tensorflow.keras.layers import Dense, Conv1D, GlobalMaxPooling1D, Embedding, Dropout, MaxPooling1D
from tensorflow.keras.models import Sequential
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint

model = Sequential()
model.add(Embedding(vocab_num, 32))
model.add(Dropout(0.2))
model.add(Conv1D(32, 5, strides=1, padding='valid', activation='relu'))
model.add(GlobalMaxPooling1D())
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc'])

es = EarlyStopping(monitor = 'val_loss', mode = 'min', verbose = 1, patience = 3)
mc = ModelCheckpoint('best_model.h5', monitor = 'val_acc', mode = 'max', verbose = 1, save_best_only = True)

history = model.fit(X_train, y_train, epochs = 10, batch_size=64, validation_split=0.2, callbacks=[es, mc])
 

Вот в чем ошибка:

 ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-54-d78723a788e4> in <module>
----> 1 history = model.fit(X_train, y_train, epochs = 10, batch_size=64, validation_split=0.2, callbacks=[es, mc])

~anaconda3libsite-packagestensorflowpythonkerasenginetraining.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)
   1048          training_utils.RespectCompiledTrainableState(self):
   1049       # Creates a `tf.data.Dataset` and handles batch and epoch iteration.
-> 1050       data_handler = data_adapter.DataHandler(
   1051           x=x,
   1052           y=y,

~anaconda3libsite-packagestensorflowpythonkerasenginedata_adapter.py in __init__(self, x, y, sample_weight, batch_size, steps_per_epoch, initial_epoch, epochs, shuffle, class_weight, max_queue_size, workers, use_multiprocessing, model, steps_per_execution)
   1098 
   1099     adapter_cls = select_data_adapter(x, y)
-> 1100     self._adapter = adapter_cls(
   1101         x,
   1102         y,

~anaconda3libsite-packagestensorflowpythonkerasenginedata_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
    261                **kwargs):
    262     super(TensorLikeDataAdapter, self).__init__(x, y, **kwargs)
--> 263     x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
    264     sample_weight_modes = broadcast_sample_weight_modes(
    265         sample_weights, sample_weight_modes)

~anaconda3libsite-packagestensorflowpythonkerasenginedata_adapter.py in _process_tensorlike(inputs)
   1014     return x
   1015 
-> 1016   inputs = nest.map_structure(_convert_numpy_and_scipy, inputs)
   1017   return nest.list_to_tuple(inputs)
   1018 

~anaconda3libsite-packagestensorflowpythonutilnest.py in map_structure(func, *structure, **kwargs)
    657 
    658   return pack_sequence_as(
--> 659       structure[0], [func(*x) for x in entries],
    660       expand_composites=expand_composites)
    661 

~anaconda3libsite-packagestensorflowpythonutilnest.py in <listcomp>(.0)
    657 
    658   return pack_sequence_as(
--> 659       structure[0], [func(*x) for x in entries],
    660       expand_composites=expand_composites)
    661 

~anaconda3libsite-packagestensorflowpythonkerasenginedata_adapter.py in _convert_numpy_and_scipy(x)
   1009       if issubclass(x.dtype.type, np.floating):
   1010         dtype = backend.floatx()
-> 1011       return ops.convert_to_tensor_v2_with_dispatch(x, dtype=dtype)
   1012     elif scipy_sparse and scipy_sparse.issparse(x):
   1013       return _scipy_sparse_to_sparse_tensor(x)

~anaconda3libsite-packagestensorflowpythonutildispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

~anaconda3libsite-packagestensorflowpythonframeworkops.py in convert_to_tensor_v2_with_dispatch(value, dtype, dtype_hint, name)
   1402     ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
   1403   """
-> 1404   return convert_to_tensor_v2(
   1405       value, dtype=dtype, dtype_hint=dtype_hint, name=name)
   1406 

~anaconda3libsite-packagestensorflowpythonframeworkops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
   1408 def convert_to_tensor_v2(value, dtype=None, dtype_hint=None, name=None):
   1409   """Converts the given `value` to a `Tensor`."""
-> 1410   return convert_to_tensor(
   1411       value=value,
   1412       dtype=dtype,

~anaconda3libsite-packagestensorflowpythonprofilertrace.py in wrapped(*args, **kwargs)
    161         with Trace(trace_name, **trace_kwargs):
    162           return func(*args, **kwargs)
--> 163       return func(*args, **kwargs)
    164 
    165     return wrapped

~anaconda3libsite-packagestensorflowpythonframeworkops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
   1538 
   1539     if ret is None:
-> 1540       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1541 
   1542     if ret is NotImplemented:

~anaconda3libsite-packagestensorflowpythonframeworktensor_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 

~anaconda3libsite-packagestensorflowpythonframeworkconstant_op.py in constant(value, dtype, shape, name)
    262     ValueError: if called on a symbolic tensor.
    263   """
--> 264   return _constant_impl(value, dtype, shape, name, verify_shape=False,
    265                         allow_broadcast=True)
    266 

~anaconda3libsite-packagestensorflowpythonframeworkconstant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
    274       with trace.Trace("tf.constant"):
    275         return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 276     return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    277 
    278   g = ops.get_default_graph()

~anaconda3libsite-packagestensorflowpythonframeworkconstant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
    299 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
    300   """Implementation of eager constant."""
--> 301   t = convert_to_eager_tensor(value, ctx, dtype)
    302   if shape is None:
    303     return t

~anaconda3libsite-packagestensorflowpythonframeworkconstant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     96       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     97   ctx.ensure_initialized()
---> 98   return ops.EagerTensor(value, ctx.device_name, dtype)
     99 
    100 

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

X_train.shape — (961, 2), Y_train.shape — (961,).

dtype данных поезда является объектом и более подробным, у поезда[‘Message_body’] есть объект, а у поезда[‘Label’] есть int8.

Я нашел некоторые подсказки для любой другой подобной ошибки, погуглив.

Я изменил массив numpy на float32, np.int, … А также я использовал tf.convert_to_tensor для определения y_train, но они приводили к тем же ошибкам.

Я думаю, это потому, что мои данные являются строковыми.

Комментарии:

1. «они плохо работали»? Приводили ли они к аналогичным ошибкам? Если да, то что? ошибки тензорного потока часто более загадочны. Для начала расскажите нам о массиве(массивах) — форме и типе dtype,