Oliver Wesche
Oliver Wesche

Reputation: 49

How can I get tf.data.Dataset.from_tensor_slices to accept my dtype?

# Convert to Tensor
imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
labels = tf.convert_to_tensor(labels, dtype=tf.int32)
# Build a TF Queue, shuffle data
image, label = tf.data.Dataset.from_tensor_slices([imagepaths, labels])

So the following code is what I am using with Tensorflow 2, I keep on changing my types that I convert to however it continuously gives me errors no matter which I use. Any Ideas? Below I have listed some of the errors I get:

tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute Pack as input #1(zero-based) was expected to be a string tensor but is a int32 tensor [Op:Pack] name: component_0


return ops.EagerTensor(value, handle, device, dtype)
TypeError: Cannot convert provided value to EagerTensor

Upvotes: 3

Views: 4218

Answers (1)

orrav
orrav

Reputation: 46

You can combine two tensors into one Dataset object by slicing a tuple of the two tensors. Like this:

# Convert to Tensor
imagepaths = tf.convert_to_tensor(imagepaths, dtype=tf.string)
labels = tf.convert_to_tensor(labels, dtype=tf.int32)
# Build a TF Queue, shuffle data
dataset = tf.data.Dataset.from_tensor_slices((imagepaths, labels))

Notice that the tensors should have the same size in their first dimension.

Upvotes: 2

Related Questions