tensorflow icon indicating copy to clipboard operation
tensorflow copied to clipboard

how to solve the exception with "UnboundLocalError: local variable 'batch_index' referenced before assignment"

Open Roych13 opened this issue 1 year ago • 4 comments
trafficstars

tensorflow: 1.14.0 keras: 2.2.5 when I run : model.fit(x, y, epochs=epochs, batch_size=batch_size, verbose=2, shuffle=False) and then catch the exception as below: UnboundLocalError: local variable 'batch_index' referenced before assignment

I guess it maybe concern with epochs or batch_size, or shape of my dataset? Can someone tell me what's the relation between epochs and batch_size?Is it the epoches must be divided by batch_size?

Roych13 avatar Mar 22 '24 10:03 Roych13

Hey! For issues related to general coding questions, stackoverflow is a great resource, this issue tracker is primarily for bugs that directly impact the project

Maybe this post on stackoverflow helps: https://stackoverflow.com/questions/63364588/unboundlocalerror-local-variable-batch-outputs-referenced-before-assignment If it doesn't solve your problem, feel free to reply back and I will try my best to help you

Aloqeely avatar Mar 24 '24 18:03 Aloqeely

@Roych13, We see that you are using tf version 1.14, 1.x is not actively supported, please update to 2.x and let us know if you are facing the same issue. Could you please provide the complete code to reproduce the issue.

Also the batch size is the number of samples in each batch. Each batch process is referred to as one step and An epoch is completed when all the data in the training set is used for training. The smaller the batch_size is, the larger number of batches is processed per one epoch.

The ideal combination of epochs and batch size depends on your specific dataset, model architecture, and computational resources. Also better way is to start from smaller batches to find wide and plain minima, which are better from the generalization point of view, and then increase batch size for steadier convergence. Thank you!

tilakrayal avatar Mar 25 '24 08:03 tilakrayal

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Apr 02 '24 01:04 github-actions[bot]

Hi @Roych13 ,

UnboundLocalError: local variable 'batch_index' referenced before assignment

In general the above error arises when you are using a variable (here eg. "batch_index") passed as argument or assigning to some other variable before actually assigning a value to it. Search if your code have batch_index anywhere and check whetehr it is instantiated before using somewhere else.

svkgn4DL avatar Apr 02 '24 14:04 svkgn4DL

This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Apr 11 '24 01:04 github-actions[bot]

This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further.

github-actions[bot] avatar Apr 19 '24 01:04 github-actions[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Apr 19 '24 01:04 google-ml-butler[bot]