-
lec2_linearRegressionTensorflow끄적끄적/기초배우기(sung kim) 2020. 4. 26. 14:48
'''
Regression(data)
X(feature data) | Y
1 |1
2 |2
3 |3
(Linear)Hypothesis(선형가설)
1. H(x) = Wx + b
2. which hypothesis is better?
Cost(loss) function : How fit the line to our (training)data
(H(x)-y)^2 --> give more panalty when it get more gap!
you don t need to consider about sign(minus/plus)
for i in len(data):
cost = ((H(x(i))-y(i))**2)/len(data)
Goal : Minimize cost
cost<=cost(W,b)
'''
'''
1.Build graph using TensorFlow operations
2.feed data and run graph(operation)
sess.fun(op,feed_dict={x:x_data})
3.update variables in the graph(and return values)
'''
import tensorflow as tf
# X and Y data
x_train = [1,2,3]
y_train = [1,2,3]
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))
# reduce_mean : 평균
#Minimize(GradientDescent)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)
# Lanch 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))
더보기w = tf.Variable(tf.random_normal([1]),name='weight')
tf.Variable() : 텐서플로우의 변수 생성
tf.random_normal()
tf.Tensor는 벡터와 행렬을 일반화 한것으로 고차원으로 확장 가능하다.
- 속성 : 자료형(dtype)<기본값은 tf.float32>, 형태(shape)
- 종류 : tf.Variable(변수형), : 세션 내에서 값이 바꿀 수 있는 텐서
tf.constant(상수형), : 미리 주어진 값으로 고정된 텐서
tf.placeholder, : 고정된 값을 가지지만 값이 미리 주어지지 않고 나중에 넣을 수 있는 텐서
신경망 모형의 경우 batch단위 학습이기 때문에 학습용 데이터는 플레이스 홀더에 넣어준다.
tf.SparseTensor
- ndarray와 다른점 : ndarray는 직접 데이터를 저장하기 위한 자료형이지만 tensor는 그래프 안에서 다차원 데이터를 표현하는 객체이다.
'Tensorflow끄적끄적 > 기초배우기(sung kim)' 카테고리의 다른 글
lec5_LogisticClassification (0) 2020.04.27 lec4_mutiVariableLinearRegression (0) 2020.04.27 lec3_HowtoMinimize (0) 2020.04.27 lec1_basic (0) 2020.04.26 소개 (0) 2020.04.26