"TensorFlow 선형회귀"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source +<syntaxhighlight , -</source> +</syntaxhighlight>))
 
(다른 사용자 한 명의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
;텐서플로우 2차원 선형회귀
;텐서플로우 선형회귀분석


==2차원==
==단순==
{{참고|TensorFlow 단순선형회귀}}
<syntaxhighlight lang='python' notebook>
<source lang='python'>
import numpy as np
import tensorflow as tf
import tensorflow as tf


x_data = [1,2,3]
x_train = [1, 2, 3, 4]
y_data = [1,2,3]
y_train = [0, -1, -2, -3]
learning_rate = 0.05


W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
tf.model = tf.keras.Sequential()
b = tf.Variable(tf.zeros([1]))
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1))
y = W * x_data + b


loss = tf.reduce_mean(tf.square(y - y_data))
sgd = tf.keras.optimizers.SGD(lr=0.1)
train = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
tf.model.compile(loss='mse', optimizer=sgd)
tf.model.summary()
tf.model.fit(x_train, y_train, epochs=200)


sess = tf.Session()
y_predict = tf.model.predict(np.array([5, 4]))
sess.run( tf.global_variables_initializer() )
print(y_predict)
</syntaxhighlight>


for step in range(2001):
==다중==
    sess.run(train)
<syntaxhighlight lang='python'>
    if step % 200 == 0:
        print(step, sess.run(W), sess.run(b))
# 0 [ 0.02381748] [ 0.36606845]
# 200 [ 0.97388893] [ 0.05935668]
# 400 [ 0.99767464] [ 0.00528606]
# 600 [ 0.99979293] [ 0.00047073]
# 800 [ 0.99998158] [  4.19568278e-05]
# 1000 [ 0.99999833] [  3.76215985e-06]
# 1200 [ 0.99999964] [  7.06425851e-07]
# 1400 [ 0.99999964] [  6.58742181e-07]
# 1600 [ 0.99999964] [  6.58742181e-07]
# 1800 [ 0.99999964] [  6.58742181e-07]
# 2000 [ 0.99999964] [  6.58742181e-07]
</source>
 
==3차원==
<source lang='python'>
import tensorflow as tf
import tensorflow as tf


58번째 줄: 42번째 줄:
         if step % 200 == 0:
         if step % 200 == 0:
             print(step, cost_val, W_val, b_val)
             print(step, cost_val, W_val, b_val)
</source>
</syntaxhighlight>
{{소스헤더|실행결과}}
{{소스헤더|실행결과}}
<source lang='text'>
<syntaxhighlight lang='text'>
0 0.630719 [[ 1.96448588]
0 0.630719 [[ 1.96448588]
  [-1.46632338]] [ 0.50560945]
  [-1.46632338]] [ 0.50560945]
83번째 줄: 67번째 줄:
2000 1.29013e-06 [[ 2.21474481]
2000 1.29013e-06 [[ 2.21474481]
  [-1.21604705]] [ 0.0029954]
  [-1.21604705]] [ 0.0029954]
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[텐서플로우 다차원 선형회귀]]
* [[TensorFlow 단순선형회귀]]
* [[텐서플로우]]
* [[선형회귀]]


[[분류: TensorFlow]]
[[분류: TensorFlow]]
[[분류: 회귀분석]]

2021년 4월 20일 (화) 21:45 기준 최신판

텐서플로우 선형회귀분석

1 단순[ | ]

import numpy as np
import tensorflow as tf

x_train = [1, 2, 3, 4]
y_train = [0, -1, -2, -3]

tf.model = tf.keras.Sequential()
tf.model.add(tf.keras.layers.Dense(units=1, input_dim=1))

sgd = tf.keras.optimizers.SGD(lr=0.1)
tf.model.compile(loss='mse', optimizer=sgd)
tf.model.summary()
tf.model.fit(x_train, y_train, epochs=200)

y_predict = tf.model.predict(np.array([5, 4]))
print(y_predict)

2 다중[ | ]

import tensorflow as tf

x_data = [[1,1],[2,2],[3,3]]
y_data = [[1],[2],[3]]
X = tf.placeholder(tf.float32, shape=[None,2])
Y = tf.placeholder(tf.float32, shape=[None,1])
W = tf.Variable(tf.random_normal([2,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))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(2001):
        cost_val, W_val, b_val, _ = sess.run([cost, W, b, train], feed_dict={X: x_data, Y: y_data})
        if step % 200 == 0:
            print(step, cost_val, W_val, b_val)
실행결과
0 0.630719 [[ 1.96448588]
 [-1.46632338]] [ 0.50560945]
200 0.0159201 [[ 2.1430881 ]
 [-1.28772187]] [ 0.33275333]
400 0.00558863 [[ 2.17255759]
 [-1.25825167]] [ 0.19715267]
600 0.00196186 [[ 2.19001746]
 [-1.24079025]] [ 0.11681108]
800 0.000688694 [[ 2.20036244]
 [-1.23044467]] [ 0.06920907]
1000 0.000241762 [[ 2.20649195]
 [-1.22431529]] [ 0.04100555]
1200 8.48685e-05 [[ 2.21012259]
 [-1.22068286]] [ 0.02429538]
1400 2.97948e-05 [[ 2.21227384]
 [-1.21853077]] [ 0.01439506]
1600 1.04602e-05 [[ 2.21354699]
 [-1.21725464]] [ 0.00852949]
1800 3.67325e-06 [[ 2.2142992 ]
 [-1.21649647]] [ 0.00505449]
2000 1.29013e-06 [[ 2.21474481]
 [-1.21604705]] [ 0.0029954]

3 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}