티스토리 뷰
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import tensorflow as tf x_data = [1,2,3] y_data = [1,2,3] W = tf.Variable(tf.random_normal([1]), name = 'weight') X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) hypothesis = W * X cost = tf.reduce_sum(tf.square(hypothesis -Y)) learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(21): sess.run(update, feed_dict={X: x_data, Y: y_data}) print(step, sess.run(cost, feed_dict{X: x_data, Y: y_data}), sess.run(W)) | cs |
위 코드는 경사하강법 알고리즘을 구현한 예제이다.
1 2 | optimizer = tf.train.GradientDesecdentOptimizer(learning rate = 0.1) train = optimizer.minimize(cost) |
물론 이렇게 쉽게
1 2 3 4 | learning_rate = 0.1 gradient = tf.reduce_mean((W * X - Y) * X) descent = W - learning_rate * gradient update = W.assign(descent) | cs |
이거 대신 사용 할 수 있다
'Programming > Deep Learning' 카테고리의 다른 글
Gradient Descent Algorithm(경사하강법 알고리즘) (0) | 2018.03.21 |
---|---|
다층 퍼셉트론으로 XOR 게이트 구현 (0) | 2018.03.20 |
퍼셉트론으로 AND, OR ,NAND 게이트 구현 (using numpy, introduce bias) (0) | 2018.03.20 |
퍼셉트론 AND , OR , NAND게이트 (0) | 2018.03.20 |
퍼셉트론 (0) | 2018.03.20 |
댓글