Search code examples
tensorflowtensorloss-function

how to get the shapes of a tensor inside a custom loss in tensorflow


I implemented a custom loss. I want to get the shape of input parameter, like y_true and y_pred. But whatever I have tried, I can't get a valid shape. The methods I tried including y_true.shape, int_shape(shape), y_true.get_shape returned (None, None).


Solution

  • You know TensorFlow version <= 2.0 does not run eagerly. So when you run the graph you'll get a valid shape. Know the output shape and implement your loss function base on that. tf.shape(tensor) returns a valid shape.