 Test_on_batch() gives the same loss output on different batches in a single run
Hi, 

Could you please provide reproducible code so that we can replicate the reported behavior. Thanks!  Hi,
Thank you looking into the issue. I will work on the reproducible code next week.
Regards,Garry
    On Thursday, August 24, 2023 at 11:16:01 AM PDT, Sachin Prasad ***@***.***> wrote:  
 
 


Hi,

Could you please provide reproducible code so that we can replicate the reported behavior. Thanks!

—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you authored the thread.Message ID: ***@***.***>
   @sachinprasadhs 
Hello, I uploaded the code onto github under the project name: test_on_batch_bug. It's the first time that I did it. I hope the problem can be duplicated.
Thank you!
Garry
 Thanks for your effort to create a debug code, while this code is too long to debug.
For us to debug, could you please create a single script with simple reproducible code(toy example) which can be run in google Colab. Thank you for your quick response. I will try to merge the files into a single file. Please make the code simpler. Thank you. It took me some time to get familiar with Colab. Currently, the code runs in Colab. It will take some additional time to simplify to code.  @sachinprasadhs 

Hi Sachin,
I merged the files into a single one and use random data to reproduce the problem. Hopefully it's good enough,  Thank you. Hi, I was able to reproduce the reported issue, please find the attached Gist here https://gist.github.com/sachinprasadhs/a14e4d71f9c967069fcd4fdc4aa3a967 I just proved my suspicion that train_on_batch( ) does the same that it uses the first batch of data only for training.

I used the random data in the code to train the model. As the data is random, the training should not converge. However, it converges.

The same issue exists in predict_on_batch( ). I got the same output for different batches.  I simplified the code even further by generating the training data directly without the signal processing involved. I got the same result: none of these functions, train_on_batch( ), Test_on_batch( ), and predict_on_batch( ). is working as expected. They all just use the first batch of data. @sachinprasadhs 
Hi Sachin, I am trying to apply deep neural network to audios. To me, it makes more sense to train the NN through train_on_batch( ), so that the data in are sequence. 
I am little surprised to find the bug. train_on_batch( ) has been there for years. So it's a bug newly introduced? It should be a quick fix, right? Do you have an estimate when it will be fixed? Thank you! Can you print out `vadModel.predict(mfccs3, steps=1)` output, the `logs` are based on predictions and true values.  Although predictions are expected to change with inputs, its not guaranteed if there are other issues in how the network is structured.  I would also suggest you print out the true values `vadLabel`.  I suspect that `vadLabel` is same value for all the data points and model predictions are also same regardless of the inputs and that could explain why the `test_on_batch` is giving you the same results. @sampathweb 
Thank you very much for your comments. Here is the link of the simplified code. I did what you suggested. I did find a bug on generating vadLabel in the test_31_hub.py, which doesn't affect the original bug in train_on_batch(), test_on_batch(), and predict_on_batch(). 
[test_32_hub.py](https://github.com/garryyan2/best_on_batch_bug/blob/main/proj9_hub/test_32_hub_e.py) Here is the output of predict_on_patch( ) with the corresponding vadLabels. Only the first 3 values are displayed. You can see the predictions are the same for different mfccs3 values (and the actual vadLabel values).

 mfccs3 value =  [[ 0.19199993 -0.04735164 -0.09702517  0.0700368  -0.07838414  0.03527357
  -0.13301742 -0.03066867 -0.08744524  0.00235493  0.14291315  0.00133576]]
vadLabel value =  [[0.]
 [1.]
 [0.]]
string logs =  [[0.20285858]
 [0.20722845]
 [0.20002772]]
mfccs3 value =  [[-0.10515928 -0.02034175 -0.11670403  0.042041    0.01306251 -0.08155169
  -0.04368642 -0.06203794  0.10585806 -0.00957588 -0.07321575  0.031962  ]]
vadLabel value =  [[0.]
 [1.]
 [1.]]
string logs =  [[0.20285858]
 [0.20722845]
 [0.20002772]]
mfccs3 value =  [[-0.17972131 -0.02062319  0.113969    0.13202399 -0.04922952  0.13873391
  -0.01433952 -0.22431006  0.00374917 -0.02184209  0.05709298 -0.16851307]]
vadLabel value =  [[0.]
 [1.]
 [0.]]
string logs =  [[0.20285858]
 [0.20722845]
 [0.20002772]]
 @sachinprasadhs 
Hi Sachin，

The bug has been reported for a while. This is a serious bug: none of these three functions rain_on_batch(), test_on_batch(), and predict_on_batch(), are working as they are supposed to work. All three of the functions only use the first batch of data instead of using all batches in a loop. 
My question is how we can expedite the bug fixing. 
Is there anyone working on the issue right now? Is there a priority number assigned to the bug? How can we prioritize the problem? 
Thank you for your help. @SuryanarayanaY 

Hi Surya, 

I just verified through Colab that the bugs are in the latest tensorflow version 2.14.0 as well. Could you please remove the label TF 2.10?

My code is in github [test_32_hub_e.py](https://github.com/garryyan2/best_on_batch_bug/blob/main/proj9_hub/test_32_hub_e.py)

I am really puzzled that how such a bug could be there. Did I do something wrong?
Thank you for your help.

 It looks like the problem happens only in  models generated through tf.keras.Sequential( ) . 

The sequential model is problematic. I also encounter issues when I try to use keras.optimizers.Adam( ) optimizer. 
### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes


### TensorFlow version

tf 2.10.0

### Custom code

Yes

### OS platform and distribution

windows 10

### Python version

3.10.12

### Current behavior?

I noticed the problem when I got a straight horizontal line on plotting the test results on the trained network. I used the sequential models.

I use train_on_batch(), which gives converging losses. When I switch to test_on_batch(), the losses remain the same for different batches. When I restart the test with different test files, it will give a different loss value, which remains the same for all the batches. In other words, the loss from test_on_batch() remains the some for all batches in a single run.

It's a sequential model.

Here is the code of the section:

    print('mfccs3 value = ', tf.keras.backend.eval(mfccs3[1,:]) )               
    #logs = vadModel.train_on_batch(mfccs3, vadLabel)
    logs = vadModel.test_on_batch(mfccs3, vadLabel) 
    print('string logs = ', str(logs))
The result is:
index = 1

```
mfccs3 value = [[-8.2793800e+01 -5.9538417e+00 9.8302096e-01 -3.5255635e-01
3.0392697e-01 -6.4597696e-01 2.2358397e-02 2.5344249e-02
-6.8171650e-01 -3.7053981e-01 -3.4044239e-01 -8.1056818e-02]]
```

string logs = 0.2398043

index = 2

```
mfccs3 value = [[-69.159195 -2.2269542 4.2501264 -1.3486748 0.62957734
-3.2606528 -3.253118 -3.5308673 -1.1313365 -1.1839466
-2.330786 -1.6313086 ]]
```

string logs = 0.2398043

index = 3

```
mfccs3 value = [[-64.894104 -1.892648 0.11392474 -0.81098145 -1.4640433
-1.1901256 -1.7744782 -0.85753983 -0.9694403 -0.8149232
-1.0680746 -1.0442001 ]]
```

string logs = 0.2398043

You can see that the inputs for test_on_batch() have changed. However, the loss remains the same. I use the same code for train_on_batch(), which gives converging losses.



### Standalone code to reproduce the issue

```shell
logs = vadModel.train_on_batch(mfccs3, vadLabel)
""" vs.  """
    logs = vadModel.test_on_batch(mfccs3, vadLabel) 

it's just these two lines for a sequential model.
```


### Relevant log output

```shell
I even tried latest Tensorflow version. It has the same problem.

tensorflow version: 2.15.0-dev20230817
listOfFiles 1681
2023-08-17 15:42:12.560549: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
model length =  7
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 lstm (LSTM)                 multiple                  2640      
                                                                 
 dense (Dense)               multiple                  210       
                                                                 
 dense_1 (Dense)             multiple                  11        
                                                                 
=================================================================
Total params: 2861 (11.18 KB)
Trainable params: 2861 (11.18 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
```
