Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
2,403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. Epoch 50 : cost = nan W = nan b = nan
  2. Epoch 100 : cost = nan W = nan b = nan
  3. Epoch 150 : cost = nan W = nan b = nan
  4. Epoch 200 : cost = nan W = nan b = nan
  5. Epoch 250 : cost = nan W = nan b = nan
  6. Epoch 300 : cost = nan W = nan b = nan
  7. Epoch 350 : cost = nan W = nan b = nan
  8. Epoch 400 : cost = nan W = nan b = nan
  9. Epoch 450 : cost = nan W = nan b = nan
  10. Epoch 500 : cost = nan W = nan b = nan
  11. Traceback (most recent call last):
  12. File "editabletoworkouterrors.py", line 77, in <module>
  13. new_green_dur = green_light_duration_new(current_reward, current_green)
  14. File "editabletoworkouterrors.py", line 66, in green_light_duration_new
  15. green_light_duration_new = weight * x + bias
  16. TypeError: can't multiply sequence by non-int of type 'numpy.float32'
  17.  
  18. import numpy as np
  19. import random
  20. import matplotlib.pyplot as plt
  21. import tensorflow as tf
  22. import warnings
  23.  
  24. warnings.simplefilter(action='once', category=FutureWarning) # future warnings annoy me
  25.  
  26. # set the epsilon for this episode
  27.  
  28. # Start with empty lists
  29. reward = []
  30. green_light = []
  31.  
  32. # add in a couple of rewards and light durations
  33. current_reward = [-1000,-900,-950]
  34. current_green = [10,12,12]
  35.  
  36. # Pass in reward and green_light
  37. def green_light_duration_new(current_reward, current_green):
  38. # Predicting the best light duration based on previous rewards.
  39. # predict the best duration based on previous step's reward value, using simple linear regression model
  40. x = current_reward
  41. y = current_green
  42. n = len(x)
  43. # Plot of Training Data
  44. plt.scatter(x, y)
  45. plt.xlabel('Reward')
  46. plt.ylabel('Green Light Duration')
  47. plt.title("Training Data")
  48. plt.show()
  49.  
  50. X = tf.placeholder("float")
  51. Y = tf.placeholder("float")
  52. W = tf.Variable(np.random.randn(), name = "W")
  53. b = tf.Variable(np.random.randn(), name = "b")
  54. learning_rate = 0.01
  55. training_epochs = 500
  56. # Hypothesis
  57. y_pred = tf.add(tf.multiply(X, W), b)
  58. # Mean Squared Error Cost Function
  59. cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)
  60. # Gradient Descent Optimizer
  61. optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
  62. # Global Variables Initializer
  63. init = tf.global_variables_initializer()
  64. # Starting the Tensorflow Session
  65. with tf.Session() as sess:
  66. # Initializing the Variables
  67. sess.run(init)
  68. # Iterating through all the epochs
  69. for epoch in range(training_epochs):
  70. # Feeding each data point into the optimizer using Feed Dictionary
  71. for (_x, _y) in zip(x, y):
  72. sess.run(optimizer, feed_dict = {X : _x, Y : _y})
  73. # Displaying the result after every 50 epochs
  74. if (epoch + 1) % 50 == 0:
  75. # Calculating the cost a every epoch
  76. c = sess.run(cost, feed_dict = {X : x, Y : y})
  77. print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))
  78. # Storing necessary values to be used outside the Session
  79. training_cost = sess.run(cost, feed_dict ={X: x, Y: y})
  80. weight = sess.run(W)
  81. bias = sess.run(b)
  82. # Calculating the predictions
  83. green_light_duration_new = weight * x + bias
  84. print("Training cost =", training_cost, "Weight =", weight, "bias =", bias, 'n')
  85. # Plotting the Results
  86. plt.plot(x, y, 'ro', label ='Original data')
  87. plt.plot(x, green_light_duration_new, label ='Fitted line')
  88. plt.title('Linear Regression Result')
  89. plt.legend()
  90. plt.show()
  91. return green_light_duration_new
  92.  
  93. # Go to the training function
  94. new_green_dur = green_light_duration_new(current_reward, current_green)
  95.  
  96. # Append the predicted green light to its list to run regression later again
  97. green_light.append(new_green_dur)
  98.  
  99. # Go on to run the rest of the simulation with the new green light duration,
  100. # and append its subsequent reward to current_reward list to run again later.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement