I converted a file to dataframe with pandas and now I would like to train a Deep Learning model via TensorFlow. I don't succeed to train the model: after dividing in training and test set, when I go to compile the model it tells me
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type
numpy.ndarray).
I thought the problem was that the numpy arrays had different sizes, but despite performing padding (in this way all the arrays had the same dimension inside the column), the problem was not solved. Below I insert an example of a column I have inside the dataset: if I wanted to transform this into a tensor, how should I do it?
df = pd.read_parquet('example.parquet')
df['column']
0 [0, 1, 1, 1, 0, 1, 0, 1, 0]
1 [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0]
2 [0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1]
3 [0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1]
4 [0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0]
...
115 [0, 1, 0, 0, 1, 1, 1, 1, 1]
116 [0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, ...
117 [0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]
118 [0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, ...
119 [0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1]
Clearly I have inserted the original column, not the one on which I padded unsuccessfully.
These are the steps that I did to train the model, if they can be useful
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
Y = label_encoder.fit_transform(Y)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size = 0.3, random_state = 42)
#create model
model = Sequential()
#add model layers
model.add(BatchNormalization())
model.add(Dense(20, activation='softmax', input_shape=(X_train.shape)))
# compile model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=50)
UPDATE: Complete traceback
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported
object type numpy.ndarray).
---------------------------------------------------------------------
------
ValueError Traceback (most recent call
last)
~\AppData\Local\Temp/ipykernel_16380/3421148994.py in <module>
1 from livelossplot import PlotLossesKeras
2
----> 3 model.fit(X_train, y_train, validation_data=(X_test, y_test),
epochs=50, callbacks=[PlotLossesKeras()])
~\AppData\Local\Programs\Python\Python39\lib\site-
packages\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)
1132 training_utils.RespectCompiledTrainableState(self):
1133 # Creates a `tf.data.Dataset` and handles batch and epoch
iteration.
-> 1134 data_handler = data_adapter.get_data_handler(
1135 x=x,
1136 y=y,
~\AppData\Local\Programs\Python\Python39\lib\site-
packages\keras\engine\data_adapter.py in get_data_handler(*args,
**kwargs)
1381 if getattr(kwargs["model"], "_cluster_coordinator", None):
1382 return _ClusterCoordinatorDataHandler(*args, **kwargs)
-> 1383 return DataHandler(*args, **kwargs)
1384
1385
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\data_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, distribute)
1136
1137 adapter_cls = select_data_adapter(x, y)
-> 1138 self._adapter = adapter_cls(
1139 x,
1140 y,
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\data_adapter.py in __init__(self, x, y, sample_weights, sample_weight_modes, batch_size, epochs, steps, shuffle, **kwargs)
228 **kwargs):
229 super(TensorLikeDataAdapter, self).__init__(x, y, **kwargs)
--> 230 x, y, sample_weights = _process_tensorlike((x, y, sample_weights))
231 sample_weight_modes = broadcast_sample_weight_modes(
232 sample_weights, sample_weight_modes)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\data_adapter.py in _process_tensorlike(inputs)
1029 return x
1030
-> 1031 inputs = tf.nest.map_structure(_convert_numpy_and_scipy, inputs)
1032 return tf.__internal__.nest.list_to_tuple(inputs)
1033
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
867
868 return pack_sequence_as(
--> 869 structure[0], [func(*x) for x in entries],
870 expand_composites=expand_composites)
871
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
867
868 return pack_sequence_as(
--> 869 structure[0], [func(*x) for x in entries],
870 expand_composites=expand_composites)
871
~\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\data_adapter.py in _convert_numpy_and_scipy(x)
1024 if issubclass(x.dtype.type, np.floating):
1025 dtype = backend.floatx()
-> 1026 return tf.convert_to_tensor(x, dtype=dtype)
1027 elif _is_scipy_sparse(x):
1028 return _scipy_sparse_to_sparse_tensor(x)
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\util\dispatch.py in wrapper(*args, **kwargs)
204 """Call target, and fall back on dispatchers if there is a TypeError."""
205 try:
--> 206 return target(*args, **kwargs)
207 except (TypeError, ValueError):
208 # Note: convert_to_eager_tensor currently raises a ValueError, not a
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor_v2_with_dispatch(value, dtype, dtype_hint, name)
1428 ValueError: If the `value` is a tensor not of given `dtype` in graph mode.
1429 """
-> 1430 return convert_to_tensor_v2(
1431 value, dtype=dtype, dtype_hint=dtype_hint, name=name)
1432
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\ops.py in convert_to_tensor_v2(value, dtype, dtype_hint, name)
1434 def convert_to_tensor_v2(value, dtype=None, dtype_hint=None, name=None):
1435 """Converts the given `value` to a `Tensor`."""
-> 1436 return convert_to_tensor(
1437 value=value,
1438 dtype=dtype,
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\profiler\trace.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
~\AppData\Local\Programs\Python\Python39\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)
1564
1565 if ret is None:
-> 1566 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1567
1568 if ret is NotImplemented:
~\AppData\Local\Programs\Python\Python39\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
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py in constant(value, dtype, shape, name)
269 ValueError: if called on a symbolic tensor.
270 """
--> 271 return _constant_impl(value, dtype, shape, name, verify_shape=False,
272 allow_broadcast=True)
273
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast)
281 with trace.Trace("tf.constant"):
282 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
--> 283 return _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
284
285 g = ops.get_default_graph()
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py in _constant_eager_impl(ctx, value, dtype, shape, verify_shape)
306 def _constant_eager_impl(ctx, value, dtype, shape, verify_shape):
307 """Creates a constant on the current device."""
--> 308 t = convert_to_eager_tensor(value, ctx, dtype)
309 if shape is None:
310 return t
~\AppData\Local\Programs\Python\Python39\lib\site-packages\tensorflow\python\framework\constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
104 dtype = dtypes.as_dtype(dtype).as_datatype_enum
105 ctx.ensure_initialized()
--> 106 return ops.EagerTensor(value, ctx.device_name, dtype)
107
108
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray).