Reputation: 1285
I'm new to TensorFlow and creating linear regressions, but I'm not seeing my issue here. I followed this guide to create my first simple (univariate) linear regression model, but am running into the error:
UPDATE WITH NEW PRINT OUTPUTS
_x : -1000
_y : 10
_x : -900
_y : 12
_x : -950
_y : 12
...
c : nan
c dtype : float32
Epoch 50 : cost = nan W = nan b = nan
_x : -1000
_y : 10
_x : -900
_y : 12
_x : -950
_y : 12
...
c : nan
c dtype : float32
Epoch 100 : cost = nan W = nan b = nan
...
Epoch 150 : cost = nan W = nan b = nan
...
Epoch 200 : cost = nan W = nan b = nan
...
Epoch 450 : cost = nan W = nan b = nan
...
Epoch 500 : cost = nan W = nan b = nan
training_cost : nan
training_cost dtype : float32
weight : nan
weight : float32
bias : nan
bias dtype : float32
Traceback (most recent call last):
File "editabletoworkouterrors.py", line 77, in <module>
new_green_dur = green_light_duration_new(current_reward, current_green)
File "editabletoworkouterrors.py", line 66, in green_light_duration_new
green_light_duration_new = weight * x + bias
TypeError: can't multiply sequence by non-int of type 'numpy.float32'
So I have a bunch of nan's for some reason? When I tried this in the program I'm working on which has a large list of green light durations and rewards, sometimes it'll start with having numbers in the weight, x and b values as it should, but then it starts displaying nan's again during the later epochs, if that makes sense?
I'm making a traffic simulator, and I'd like it to be able to determine what the best "green light duration" would be, based on a previous reward (car wait time change for the lane in the reward). I've created a simple working code for someone to replicate:
UPDATED WITH NEW PRINTS IN CODE
import numpy as np
import random
import matplotlib.pyplot as plt
import tensorflow as tf
import warnings
warnings.simplefilter(action='once', category=FutureWarning) # future warnings annoy me
# set the epsilon for this episode
# Start with empty lists
reward = []
green_light = []
# add in a couple of rewards and light durations
current_reward = [-1000,-900,-950]
current_green = [10,12,12]
# Pass in reward and green_light
def green_light_duration_new(current_reward, current_green):
# Predicting the best light duration based on previous rewards.
# predict the best duration based on previous step's reward value, using simple linear regression model
x = current_reward
y = current_green
n = len(x)
# Plot of Training Data
plt.scatter(x, y)
plt.xlabel('Reward')
plt.ylabel('Green Light Duration')
plt.title("Training Data")
plt.show()
X = tf.placeholder("float")
Y = tf.placeholder("float")
W = tf.Variable(np.random.randn(), name = "W")
b = tf.Variable(np.random.randn(), name = "b")
learning_rate = 0.01
training_epochs = 500
# Hypothesis
y_pred = tf.add(tf.multiply(X, W), b)
print('y_pred : ', y_pred)
print('y_pred dtype : ', y_pred.dtype)
# Mean Squared Error Cost Function
cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)
print('cost : ', cost)
print('cost dtype: ', cost.dtype)
# Gradient Descent Optimizer
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
# Global Variables Initializer
init = tf.global_variables_initializer()
# Starting the Tensorflow Session
with tf.Session() as sess:
# Initializing the Variables
sess.run(init)
# Iterating through all the epochs
for epoch in range(training_epochs):
# Feeding each data point into the optimizer using Feed Dictionary
for (_x, _y) in zip(x, y):
print('_x : ',_x)
print('_y : ',_y)
sess.run(optimizer, feed_dict = {X : _x, Y : _y})
# Displaying the result after every 50 epochs
if (epoch + 1) % 50 == 0:
# Calculating the cost a every epoch
c = sess.run(cost, feed_dict = {X : x, Y : y})
print('c : ', c)
print('c dtype : ', c.dtype)
print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))
# Storing necessary values to be used outside the Session
training_cost = sess.run(cost, feed_dict ={X: x, Y: y})
print('training_cost : ', training_cost)
print('training_cost dtype : ', training_cost.dtype)
weight = sess.run(W)
print('weight : ', weight)
print('weight : ', weight.dtype)
bias = sess.run(b)
print('bias : ', bias)
print('bias dtype : ', bias.dtype)
# Calculating the predictions
green_light_duration_new = weight * x + bias
print("Training cost =", training_cost, "Weight =", weight, "bias =", bias, '\n')
# Plotting the Results
plt.plot(x, y, 'ro', label ='Original data')
plt.plot(x, green_light_duration_new, label ='Fitted line')
plt.title('Linear Regression Result')
plt.legend()
plt.show()
return green_light_duration_new
# Go to the training function
new_green_dur = green_light_duration_new(current_reward, current_green)
# Append the predicted green light to its list
green_light.append(new_green_dur)
# Go on to run the rest of the simulation with the new green light duration,
# and append its subsequent reward to current_reward list to run again later.
Also, if there's a way for the regression model to keep the green light duration to be between a minimum of 10 (seconds) and a maximum value of 120 seconds, that would be spectacular too, I just don't know how to work that in. Thanks a lot!
Upvotes: 0
Views: 132
Reputation: 77860
... and there's the problem! Current values of items are
x = [-1000,-900,-950] # type list
weight = nan # type numpy.float32
bias = nan # type numpy.float32
x
is a simple Python list, not a numpy array. Python doesn't have vectorized operations such as float * list
Upvotes: 1