Skip to content

Instantly share code, notes, and snippets.

@kwakseonghun
Created August 10, 2017 00:54
Show Gist options
  • Save kwakseonghun/e4f3f80ba8aa4f139c165adcf7e22629 to your computer and use it in GitHub Desktop.
Save kwakseonghun/e4f3f80ba8aa4f139c165adcf7e22629 to your computer and use it in GitHub Desktop.
import numpy as np
number_of_points=500 #수행횟수
x_point=[] # X랜덤변수 배열
y_point=[]
a=0.22
b=0.78
x=-1.5
x_po=[]
for i in range(number_of_points):
x=x+0.006
x_po.append([x])
y=a*x+b+np.random.normal(0.0,0.1)
x_point.append([x])
y_point.append([y])
import matplotlib.pyplot as plt
plt.plot(x_point,y_point, 'o', label='Input Data')
plt.legend()
plt.show()
import tensorflow as tf
A=tf.Variable(tf.random_uniform([1],-1.0,1.0))
B=tf.Variable(tf.zeros([1]))
y=A*x_point+B
cost_function=tf.reduce_mean(tf.square(y-y_point))
optimizer=tf.train.GradientDescentOptimizer(0.5)
train=optimizer.minimize(cost_function)
model=tf.global_variables_initializer()
with tf.Session() as session:
session.run(model)
for step in range(0,101):
session.run(train)
if (step % 5) ==0:
plt.plot(x_point,y_point,'o',label='step={}'.format(step))
plt.plot(x_po,session.run(A)*x_po+session.run(B))
plt.legend()
plt.show()
print(session.run(A),session.run(B))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment