I'm new to python I found some answers to this question, but nothing could really help me.
here a the part of code with which I have problem:
batch_index=0 # initializing a globale variable, I've tried with global batch_index too
..................................
def next_Training_Batch():
if batch_index < len(train_data):
batch_index = batch_index + 1
return train_data[batch_index-1], train_labels[batch_index-1]
else :
shuffle_data()
batch_index = 0
return train_data[batch_index], train_labels[batch_index]
when I call the function I get the following :
UnboundLocalError: local variable 'batch_index' referenced before assignment
I don't want to use a parameter in the function(as suggested in similar question) and to be honest I'm using number of "global" variable without have any errors, I don't get why I'm not allowed to evaluate it in the if statement? thanks for any hint !
Add global batch_index
to the beginning of the function and it will know that you're referring to the global variable not a local one.
batch_index=0 # initializing a globale variable, I've tried with global batch_index too
...
def next_Training_Batch():
global batch_index
if batch_index < len(train_data):
batch_index = batch_index + 1
return train_data[batch_index - 1], train_labels[batch_index - 1]
else:
shuffle_data()
batch_index = 0
return tain_data[batch_index], train_labels[batch_index]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With