I’m using Tensorflow 2.16.1. I have a list of Tensors I want to use as training data. They are varying shapes. It’s unclear how I can make this work. Here is some example code:
import tensorflow as tf
indices = [[0, 0], [1, 1]]
values = [0.5, 0.5]
tensor_1 = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=[2, 2])
tensor_1 = tf.sparse.reorder(tensor_1)
tensor_1 = tf.sparse.to_dense(tensor_1)
indices = [[0, 0], [1, 1], [2, 2]]
values = [0.5, 0.5, 0.5]
tensor_2 = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=[3, 3])
tensor_2 = tf.sparse.reorder(tensor_2)
tensor_2 = tf.sparse.to_dense(tensor_2)
tensor_list = [tensor_1, tensor_2]
tensors = tf.convert_to_tensor(tensor_list)
Running this results in: Shapes of all inputs must match: values[0].shape = [2,2] != values[1].shape = [3,3] [Op:Pack] name: packed
Ideally I wouldn’t be converting them to dense tensors, but that’s another issue. I can do something like this:
class InfoModel(tf.keras.Model):
def __init__(self):
super(InfoModel, self).__init__()
def call(self, inputs, training=False):
print(inputs)
return inputs
InfoModel()(tensor_list)
The above code works fine. However, I’m trying to generate these tensors with a custom tf.keras.utils.PyDataset
implementation. Here’s an example:
class TestGenerator(tf.keras.utils.PyDataset):
def __init__(self):
pass
def __getitem__(self, index):
indices = [[0, 0], [1, 1]]
values = [0.5, 0.5]
tensor_1 = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=[2, 2])
tensor_1 = tf.sparse.reorder(tensor_1)
tensor_1 = tf.sparse.to_dense(tensor_1)
indices = [[0, 0], [1, 1], [2, 2]]
values = [0.5, 0.5, 0.5]
tensor_2 = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=[3, 3])
tensor_2 = tf.sparse.reorder(tensor_2)
tensor_2 = tf.sparse.to_dense(tensor_2)
return tf.convert_to_tensor([tensor_1, tensor_2]), tf.convert_to_tensor([0.0, 0.0])
def __len__(self):
return 1
The above generator complains about shapes not matching. If I change this line:
return tf.convert_to_tensor([tensor_1, tensor_2]), tf.convert_to_tensor([0.0, 0.0])
to:
return [tensor_1, tensor_2], tf.convert_to_tensor([0.0, 0.0])
Then the error I get is:
TypeError: `output_signature` must contain objects that are subclass of `tf.TypeSpec` but found <class 'list'> which is not.