'TypeError: Failed to convert elements of SparseTensor to Tensor

TypeError: Failed to convert elements of SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(None, 2), dtype=int64), values=Tensor("DeserializeSparse:1", shape=(None,), dtype=float32), dense_shape=Tensor("stack:0", shape=(2,), dtype=int64)) to Tensor. Consider casting elements to a supported type. See https://www.tensorflow.org/api_docs/python/tf/dtypes for supported TF dtypes.

I am training a model for classification using images: there are two classes.

while executing this line model.fit(train_x, train_y, batch_size = 32, epochs = 6) error mentioned above appears.

here is the code:

https://github.com/mughal1918/ResNet50.git



Solution 1:[1]

The shape is incorrect if you are looking into the Fn inputs but further input must tobe correct and it indicates input output matching problems.

Input and Output :

My_SerializeSparse = tf.raw_ops.SerializeSparse(
    sparse_indices=sparse_indices, sparse_values=sparse_values, sparse_shape=sparse_shape, out_type=tf.dtypes.string,
    name=None
)

print( My_SerializeSparse )
#tf.Tensor(
#[b'\x08\t\x12\x08\x12\x02\x08\x02\x12\x02\x08\x02" \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'
# b'\x08\x03\x12\x04\x12\x02\x08\x02"\x08\x01\x00\x00\x00\x02\x00\x00\x00'
# b'\x08\t\x12\x04\x12\x02\x08\x02"\x10\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00'], shape=(3,), dtype=string)


# sparse_indices=[None, 2]
# sparse_values=[None]
# sparse_shape=[2,]

sparse_indices=[[1, 2]]
sparse_values=[1]
sparse_shape=[2, 1]

My_SerializeSparse = tf.raw_ops.SerializeSparse(
    sparse_indices=sparse_indices, sparse_values=sparse_values, sparse_shape=sparse_shape, out_type=tf.dtypes.string,
    name=None
)
print( My_SerializeSparse )
# tf.Tensor(
# [b'\x08\t\x12\x08\x12\x02\x08\x01\x12\x02\x08\x02"\x10\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00'
# b'\x08\x03\x12\x04\x12\x02\x08\x01"\x04\x01\x00\x00\x00'
# b'\x08\t\x12\x04\x12\x02\x08\x02"\x10\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00'], shape=(3,), dtype=string)

Sample problem

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martijn Pieters