728x90
반응형
tf.Variable : tensorflow가 자체적으로 변경시키는 variable, trainable 한 variable.
reduce_mean : 평균을 내줌
ex) t = [1, 2, 3, 4]
tf.reduce_mean(t) ==> 2.5
linear_regression.py
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 29 30 31 32 33 | # Lab 2 Linear Regression import tensorflow as tf # X and Y data x_train = [1, 2, 3] y_train = [1, 2, 3] # H(x) = Wx + b W = tf.Variable(tf.random_normal([1]), name = 'weight') b = tf.Variable(tf.random_normal([1]), name = 'bias') # Our hypothesis XW + b hypothesis = x_train * W + b # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - y_train)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate = 0.01) train = optimizer.minimize(cost) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) # Fit the Line for step in range(2001): sess.run(train) if step % 20 == 0: print(step, sess.run(cost), sess.run(W), sess.run(b)) |
linear_regression_feed.py
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 29 30 | # Lab 2 Linear Regression import tensorflow as tf W = tf.Variable(tf.random_normal([1]), name = 'weight') b = tf.Variable(tf.random_normal([1]), name = 'bias') X = tf.placeholder(tf.float32, shape=[None]) Y = tf.placeholder(tf.float32, shape=[None]) # Our hypothesis XW+b hypothesis = X * W + b # cost/loss function cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize optimzier = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimzier.minimize(cost) # Launch the graph in a session. sess = tf.Session() # Initializes global variables in the graph. sess.run(tf.global_variables_initializer()) # Fit the line With new training data for step in range(2001): cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], feed_dict = {X: [1, 2, 3, 4, 5], Y: [2.1, 3.1, 4.1, 5.1, 6.1]}) if step % 20 == 0: print(step, cost_val, W_val, b_val) |
반응형
'Study > Machine&Deep Learning' 카테고리의 다른 글
[ML] Logistic Classification (0) | 2018.05.20 |
---|---|
[ML] TensorFlow로 파일에서 데이터 읽어오기 (2) | 2018.05.16 |
[ML] multi-variable linear regression (0) | 2018.05.08 |
[ML] Linear Regression의 cost 최소화 (0) | 2018.05.08 |
[ML] 기본적인 Machine Learning의 용어와 개념 (0) | 2018.05.04 |
댓글