keras-team/tf-keras

shape issue for y_pred for a custom made loss function

Closed this issue · 3 comments

TensorFlow version (you are using): 2.10.0
Are you willing to contribute it (Yes/No) : Yes

Describe the feature and the current behavior/state.

Hello GitHub,

I am relatively new to TensorFlow and GitHub. I was trying to implement a custom made loss function for solving ODEs in TensorFlow. I am using the method suggested by a relatively old paper published in 1997 (https://arxiv.org/abs/physics/9705023). The loss function is supposed to estimate the residual of an ODE based on predicted values from a DNN. I tested the loss function without the DNN and it does what it is supposed to do. The only problem that arises is when I use it in combination with the DNN, Here I get the following error message when I run it:
2 root error(s) found.
(0) INVALID_ARGUMENT: Input to reshape is a tensor with 3200 values, but the requested shape has 100
[[{{node loss/Reshape_1}}]]
[[sequential/dense_5/BiasAdd/_8]]
(1) INVALID_ARGUMENT: Input to reshape is a tensor with 3200 values, but the requested shape has 100
[[{{node loss/Reshape_1}}]]

I am confused here because the input shape of the loss function matches the shape of y_pred (100, 1) when I test it outside the DNN. But when the DNN passes y_pred to the loss function it seems to have a shape of (3200, 1).

Code attached

Will this change the current api? How? No

Who will benefit from this feature? Me

  • Do you want to contribute a PR? (yes/no):no

ode_solver.txt

@saluisto,
The error is because you adapt your code from a code with the original input image size 24*24. The tensor shape after two convolution and two max-pooling layers is [-1, 6, 6, 64]. However, as your input image shape is 150*150, the intermediate shape becomes [-1, 38, 38, 64].

The image size and the model's input shape were different. Could you please check your image size again.
Also take a look at this comment for the similar error. Thank you!

Thank you for your answer. But I am not sure if I understand your answer correctly: In my script I am not using a convolution layer neither a max-pooling layer. I only use dense layer architecture (see below). Input shape should be (-1,100)

Layer (type) Output Shape Param #

dense (Dense) (None, 100) 200

dense_1 (Dense) (None, 200) 20200

dense_2 (Dense) (None, 300) 60300

dense_3 (Dense) (None, 300) 90300

dense_4 (Dense) (None, 200) 60200

dense_5 (Dense) (None, 100) 20100

=================================================================
Total params: 251,300
Trainable params: 251,300
Non-trainable params: 0

I resolved the issue. Thank you for your help