I am asking a question very similar to that of TensorFlow broadcasting of RaggedTensor
Basically, I am doing machine learning, and one data sample consists of a list of lists of coordinates, where each list of coordinates represents a drawing stroke on a canvas. One such sample might be,
[
[[2,3], [2,4], [3,6], [4,8]],
[[7,3], [10,9]],
[[10,12], [14,17], [13,15]]
]
I would like to normalize these coordinates by subtracting by the mean and dividing by the standard deviation. Specifically, I want to find the mean and standard deviation of all the x-coordinates (index=0) and y-coordinates (index=1), respectively. I got these values by
list_points=tf.ragged.constant(list_points)
STD=tf.math.reduce_std(list_points, axis=(0,1))
mean=tf.reduce_mean(list_points, axis=(0,1))
STD and mean both have shape of (2,)
Now, I want to subtract the mean from list_points (this is the sample list of lists of coordinates), but it seems that for ragged_rank=3, I can only subtract by a scalar or a tensor that covers every single data point. Is there an easy way that I can simply subtract the RaggedTensor by a Tensor of shape (2,)?
I have tried to simply subtract mean from list_points directly, but whatever I do, I get this error:
ValueError: pylist has scalar values depth 3, but ragged_rank=3 requires scalar value depth greater than 3
In your case,
ragged_rankis 1 in fact. Thustf.reduce_meancan be used as followsWe can subtract (add, multiply, etc) from a ragged tensor of ragged rank 1 an ordinary tensor of rank 1 if their first dimensions coincides.
This is possible because under the hood of the raged tensor of ragged rank 1 we have an ordinary tensor
For
ragged_rank > 1case we can attracttf.math.segment_meanthat is more tricky.