Advertisement
Guest User

code

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. x = np.array([0,1,2,3,4,5,6,7,8,9,10])
  6. y = np.array([1,3,5,7,9,11,13,15,17,19,21])
  7.  
  8. plt.plot(x,y, "k")
  9. plt.show()
  10.  
  11. inputt = tf.placeholder(dtype = tf.float64, shape = None)
  12. output = tf.placeholder(dtype = tf.float64, shape = None)
  13.  
  14. slope = tf.Variable(4, dtype=tf.float64)
  15. b = tf.Variable(5 , dtype=tf.float64)
  16.  
  17. model_operation = slope * inputt + b
  18. error = output - model_operation
  19.  
  20. squared = error ** 2
  21.  
  22. loss = tf.reduce_mean(squared)
  23.  
  24.  
  25.  
  26. opt = tf.train.GradientDescentOptimizer(0.0005)
  27.  
  28. train = opt.minimize(loss)
  29.  
  30. init = tf.global_variables_initializer()
  31.  
  32. with tf.Session()as sess:
  33. sess.run(init)
  34. for i in range(20000):
  35. sess.run(train, feed_dict={inputt:x, output:y})
  36. if i % 10 ==0:
  37. print(sess.run([slope, b]))
  38. plt.plot(x, sess.run(model_operation, feed_dict={inputt:x}))
  39.  
  40. plt.plot(x,y, "k")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement