GradiantDescentAlgorithm

Toma·2022년 2월 11일

GradiantDescentAlgorithm 의 식은 w 값을 조정해가며 cost 의 최솟값을 찾아간다. tensorflow 에서는 w 에 값을 할당할 때 equal 연산자(=)는 사용이 안되며 assign 이라는 함수를 통해 w 에 할당 가능하다.

import tensorflow as tf

X = [1, 2, 3]
Y = [1, 2, 3]

W = tf.Variable(5.0)

hypothesis = X * W

cost = tf.reduce_mean(tf.square(hypothesis - Y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train = optimizer.minimize(cost)

sess = tf.Session()

sess.run(tf.global_variables_initializer())

for step in range(10):
print(step, sess.run(W))
sess.run(train)

학습 할수록 찾고자 하는 w 값에 가까워지는 것을 볼 수 있다.

profile
Don't forget the grace

0개의 댓글