Advertisement
proffreda

tensorflow examples

Jan 19th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. # Linear Regression Ex 1
  2. # makes up some data in two dimensions, and then fits a line to it.
  3.  
  4. import tensorflow as tf
  5. import numpy as np
  6.  
  7. # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
  8. x_data = np.random.rand(100).astype(np.float32)
  9. y_data = x_data * 0.1 + 0.3
  10.  
  11. # Try to find values for W and b that compute y_data = W * x_data + b
  12. # (We know that W should be 0.1 and b 0.3, but TensorFlow will
  13. # figure that out for us.)
  14. W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  15. b = tf.Variable(tf.zeros([1]))
  16. y = W * x_data + b
  17.  
  18. # Minimize the mean squared errors.
  19. loss = tf.reduce_mean(tf.square(y - y_data))
  20. optimizer = tf.train.GradientDescentOptimizer(0.5)
  21. train = optimizer.minimize(loss)
  22.  
  23. # Before starting, initialize the variables. We will 'run' this first.
  24. init = tf.global_variables_initializer()
  25.  
  26. # Launch the graph.
  27. sess = tf.Session()
  28. sess.run(init)
  29.  
  30. # Fit the line.
  31. for step in range(201):
  32. sess.run(train)
  33. if step % 20 == 0:
  34. print(step, sess.run(W), sess.run(b))
  35.  
  36. # Learns best fit is W: [0.1], b: [0.3]
  37.  
  38. #Linear Regression Ex 2 with plotting
  39.  
  40. import numpy as np
  41. import matplotlib.pyplot as plt
  42. import tensorflow as tf
  43. number_of_points = 200
  44. x_point = []
  45. y_point = []
  46. a = 0.22
  47. b = 0.78
  48. for i in range(number_of_points):
  49. x = np.random.normal(0.0,0.5)
  50. y = a*x + b +np.random.normal(0.0,0.1)
  51. x_point.append([x])
  52. y_point.append([y])
  53. plt.plot(x_point,y_point, 'o', label='Input Data')
  54. plt.legend()
  55. plt.show()
  56. A = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  57. B = tf.Variable(tf.zeros([1]))
  58. y = A * x_point + B
  59. cost_function = tf.reduce_mean(tf.square(y - y_point))
  60. optimizer = tf.train.GradientDescentOptimizer(0.5)
  61. train = optimizer.minimize(cost_function)
  62. model = tf.initialize_all_variables()
  63. with tf.Session() as session:
  64. session.run(model)
  65. for step in range(0,21):
  66. session.run(train)
  67. if (step % 5) == 0:
  68. plt.plot(x_point,y_point,'o',
  69. label='step = {}'
  70. .format(step))
  71. plt.plot(x_point,
  72. session.run(A) *
  73. x_point +
  74. session.run(B))
  75. plt.legend()
  76. plt.show()
  77.  
  78.  
  79.  
  80. x = tf.constant(1.0, name='input')
  81. w = tf.Variable(0.8, name='weight')
  82. y = tf.mul(w, x, name='output')
  83. y_ = tf.constant(0.0, name='correct_value')
  84. loss = tf.pow(y - y_, 2, name='loss')
  85. train_step = tf.train.GradientDescentOptimizer(0.025).minimize(loss)
  86.  
  87. for value in [x, w, y, y_, loss]:
  88. tf.scalar_summary(value.op.name, value)
  89.  
  90. summaries = tf.merge_all_summaries()
  91.  
  92. sess = tf.Session()
  93. summary_writer = tf.train.SummaryWriter('log_simple_stats', sess.graph)
  94.  
  95. sess.run(tf.initialize_all_variables())
  96. for i in range(100):
  97. summary_writer.add_summary(sess.run(summaries), i)
  98. sess.run(train_step)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement