In [9]:
import tensorflow as tf
x1_data = [73., 93., 89., 96., 73.]
x2_data = [80., 88., 91., 98., 66.]
x3_data = [75., 93., 90., 100., 70.]
y_data = [152., 185., 180., 196., 142.]
x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
x3 = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
w1 = tf.Variable(tf.random_normal([1]), name='weight1')
w2 = tf.Variable(tf.random_normal([1]), name='weight2')
w3 = tf.Variable(tf.random_normal([1]), name='weight3')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = x1 * w1 + x2 * w2 + x3 * w3 + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
feed_dict={x1: x1_data, x2: x2_data, x3: x3_data, Y: y_data})
if step % 500 == 0:
print("Step : ", step, "\nCost : ", cost_val, "\nPrediction : \n", hy_val, "\nOriginal : \n", y_data)
print("----------------------------------------------")
Matrix 를 사용한 코드¶
In [11]:
import tensorflow as tf
x_data = [[73., 80., 75.],
[93., 88., 93.],
[89., 91., 90.],
[96., 98., 100.],
[73., 66., 70.]]
y_data = [[152.],
[185.],
[180.],
[196.],
[142.]]
X = tf.placeholder(tf.float32, shape=[None, 3]) # None : row 가 몇일지 모르는 상태 (numpy 의 경우 -1)
Y = tf.placeholder(tf.float32, shape=[None, 1])
W = tf.Variable(tf.random_normal([3, 1]), name='weight')
b = tf.Variable(tf.random_normal([1]), name='bias')
hypothesis = tf.matmul(X, W) + b
cost = tf.reduce_mean(tf.square(hypothesis - Y))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)
train = optimizer.minimize(cost)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
cost_val, hy_val, _ = sess.run([cost, hypothesis, train],
feed_dict={X: x_data, Y: y_data})
if step % 500 == 0:
print("Step : ", step, "\nCost : ", cost_val, "\nPrediction : \n", hy_val, "\nOriginal : \n", y_data)
print("----------------------------------------------")
'Dev > 딥러닝' 카테고리의 다른 글
05. Tensor Flow 로 Classification 예제 (binary) (0) | 2018.08.17 |
---|---|
04-2. Tensor Flow 에서 csv 파일 읽기 및 queue 사용법 (0) | 2018.08.17 |
03. TensorFlow Linear Regression 의 cost 최소화 구현 (0) | 2018.08.17 |
02. Tensor Flow로 linear regression 예제 (0) | 2018.08.16 |
01. Tensor Flow 의 기본적인 operations (0) | 2018.08.16 |
댓글