0
votes

Here is the line, where it shows an error:

model.fit(trainX, trainY, batch_size=2, epochs=200, verbose=2)

This 'batch_index' variable is creating a problem.

I have tried everything from the internet (which is not so much), including looking inside the source code of tensorflow/keras for some hints. But nothing worked.

Here is some code of the function 'stock_prediction', that gives the error:

def stock_prediction(stock):

# Collect data points from csv
dataset = []

with open(data) as f:
    for n, line in enumerate(f):
        if n != 0:
            dataset.append(float(line.split(',')[1]))

dataset = np.array(dataset)

# Create dataset matrix (X=t and Y=t+1)
def create_dataset(dataset):
    dataX = [dataset[n+1] for n in range(len(dataset)-2)]
    return np.array(dataX), dataset[2:]

trainX, trainY = create_dataset(dataset)

# Create and fit Multilinear Perceptron model
model = Sequential()
model.add(Dense(8, input_dim=1, activation='relu'))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=200, batch_size=2, verbose=0)

# Our prediction for tomorrow
prediction = model.predict(np.array([dataset[0]]))
#result = '%s stock price will move from %s to %s' % (stock, dataset[0], prediction[0][0])
result = "%s's stock price will move from %s to %s on the next open day of the stock exchange." % (stock, dataset[0], prediction[0][0])
return result


---------------------------------------------------------------------------    
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-100-5a251bf349e2> in <module>
----> 1 print (stock_prediction(stock))
   2 os.remove(data)

<ipython-input-99-2b1e12631723> in stock_prediction(stock)
  23     model.add(Dense(1))
  24     model.compile(loss='mean_squared_error', optimizer='adam')
---> 25     model.fit(trainX, trainY, epochs=200, batch_size=2, verbose=0)
  26 
  27     # Our prediction for tomorrow

~/anaconda3/lib/python3.7/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
1237                                         steps_per_epoch=steps_per_epoch,
1238                                         validation_steps=validation_steps,
-> 1239                                         validation_freq=validation_freq)
1240 
1241     def evaluate(self,

~/anaconda3/lib/python3.7/site-packages/keras/engine/training_arrays.py in fit_loop(model, fit_function, fit_inputs, out_labels, batch_size, epochs, verbose, callbacks, val_function, val_inputs, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq)
 203                     break
 204 
--> 205             if batch_index == len(batches) - 1:  # Last batch.
 206                 if do_validation and should_run_validation(validation_freq, epoch):
 207                     val_outs = test_loop(model, val_function, val_inputs,

UnboundLocalError: local variable 'batch_index' referenced before assignment
1

1 Answers

1
votes

There is someone who had the same problem like you. Did you import the tensorflow and keras libraries correctly? See this answer:

Same question