Home > OS >  from_tensor_slices returns ValueError when passing two numpy arrays as arguments
from_tensor_slices returns ValueError when passing two numpy arrays as arguments

Time:11-10

I've got two numpy arrays: images_ar (data) and categorical_y_ar (labels).

Both consist of dtype('uint8').

Shape of categorical_y_ar is (978, 126).

Shape of images_ar is (978, 224, 224, 3)

When trying to build a dataset with:

dataset = tf.data.Dataset.from_tensor_slices(images_ar, categorical_y_ar)

I get the following traceback:

ValueError Traceback (most recent call last) Input In [50], in <cell line: 1>() ----> 1 dataset = tf.data.Dataset.from_tensor_slices(images_ar, categorical_y_ar) 2 dataset = dataset.repeat().batch(batch_size) 4 val_size = int( num_classes* 0.2)

File ~.conda\envs\sklearn-env\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py:814, in DatasetV2.from_tensor_slices(tensors, name) 736 @staticmethod 737 def from_tensor_slices(tensors, name=None): 738 """Creates a Dataset whose elements are slices of the given tensors. 739 740 The given tensors are sliced along their first dimension. This operation (...) 812 Dataset: A Dataset. 813 """ --> 814 return TensorSliceDataset(tensors, name=name)

File ~.conda\envs\sklearn-env\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py:4728, in TensorSliceDataset.init(self, element, is_files, name) 4719 for t in self._tensors[1:]: 4720
batch_dim.assert_is_compatible_with( 4721
tensor_shape.Dimension( 4722
tensor_shape.dimension_value(t.get_shape()[0]))) 4724 variant_tensor = gen_dataset_ops.tensor_slice_dataset( 4725
self._tensors, 4726
output_shapes=structure.get_flat_tensor_shapes(self._structure),
4727 is_files=is_files, -> 4728 metadata=self._metadata.SerializeToString()) 4729 super(TensorSliceDataset, self).init(variant_tensor)

File ~.conda\envs\sklearn-env\lib\site-packages\tensorflow\python\data\ops\dataset_ops.py:668, in DatasetV2._metadata(self) 666 """Helper for generating dataset metadata.""" 667 metadata = dataset_metadata_pb2.Metadata() --> 668 if self._name: 669 metadata.name = _validate_and_encode(self._name) 670 return metadata

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Please help me to figure out why this error pops up.

CodePudding user response:

Try to pass the data as a set or list not as a single entity to the tf.data.Dataset.from_tensor_slice(), as shown below

x_train = np.random.randn(978, 224, 224, 3)
y_train = np.random.randint(0,127, size=(978, 127))

dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))

print(next(iter(dataset.take(1))))

Output:

<tf.Tensor: shape=(224, 224, 3), dtype=float64, numpy= ...
  • Related