인공지능/딥러닝

텐서플로를 이용한 신경망 구현

해피밀세트 2020. 6. 12. 00:03
반응형

 

 

 

1.  선형회귀 예

입력(x) 출력(y)
1 2
2 4
3 6
4 8
5 10
6 12

Q. 7을 입력하면 출력값은?

 

# 데이터 입력

x_data = [1,2,3,4,5,6]
y_data = [2,4,6,8,10,12]
 
# 변수 만들기
# seed=0 : 난수값 고정
# tf.random_normal : 정규분포에 해당하는 난수값 리턴

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
w = tf.Variable(tf.random_normal([1],seed=0),name='weight')
b = tf.Variable(tf.random_normal([1],seed=0),name='bias')

 
# hypothesis 정의

hypothesis = w * x + b
 
# MSE로 손실값 구하기

cost = tf.reduce_mean(tf.square(hypothesis - y))
 
# 학습하면서 GradientDescent한다.(학습률 0.001)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
 

# cost optimizer minimize한다.

train = optimizer.minimize(cost)

 

# 세션열고 실행

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(2001):
    cost_v,w_v,b_v,_ = sess.run([cost,w,b,train],
                                feed_dict={x:x_data, y:y_data})
    if step % 200 == 0 :
        print(step,cost_v,w_v,b_v)

# 7을 입력하면 출력값은?

print(sess.run(hypothesis,feed_dict={x:7}))

 

 

 

2. 공부시간 대비 점수 예측

공부시간 점수
2 71
4 83
6 91
8 97

Q. 7시간 공부했을때 점수는?

 

# 데이터 입력

x_data = [2,4,6,8]
y_data = [71,83,91,97]

 

# 변수 만들기

x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
w = tf.Variable(tf.random_normal([1],seed=0),name='weight')
b = tf.Variable(tf.random_normal([1],seed=0),name='bias')

 
# hypothesis 정의

hypothesis = w * x + b
 
# MSE로 손실값 구하기

cost = tf.reduce_mean(tf.square(hypothesis - y))
 
# 학습하면서 GradientDescent한다.(학습률 0.001)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
 
# cost optimizer minimize한다.

train = optimizer.minimize(cost)
 

# 세션열고 실행

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(8001):
    cost_v,w_v,b_v,_ = sess.run([cost,w,b,train],
                                feed_dict={x:x_data, y:y_data})
    if step % 1000 == 0 :
        print(step,cost_v,w_v,b_v)

# 7시간 공부했을때 성적은?

print(sess.run(hypothesis,feed_dict={x:7}))

 

 

 

3. 독립변수가 여러개일 때

x1 x2 x3 y
70 80 75 152
93 88 93 185
89 91 90 180
96 98 100 196
73 66 70 142

# 데이터 입력

x1_data = [70,93,89,96,73]
x2_data = [80,88,91,98,66]
x3_data = [75,93,90,100,70]
y_data = [152,185,180,196,142]

 

# 변수 만들기

x1 = tf.placeholder(tf.float32)
x2 = tf.placeholder(tf.float32)
x3 = tf.placeholder(tf.float32)
y =  tf.placeholder(tf.float32)
w1 = tf.Variable(tf.random_normal([1],seed=0,name="weight1"))
w2 = tf.Variable(tf.random_normal([1],seed=0,name="weight2"))
w3 = tf.Variable(tf.random_normal([1],seed=0,name="weight3"))
b = tf.Variable(tf.random_normal([1],seed=0,name="bias"))

 
# hypothesis 정의

hypothesis = w1*x1 + w2*x2 + w3*x3 + b
 
# MSE로 손실값 구하기

cost = tf.reduce_mean(tf.square(hypothesis - y))
 
# 학습하면서 GradientDescent한다.(학습률 0.00001)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001)
 
# cost optimizer minimize한다.

train = optimizer.minimize(cost)
 

# 세션열고 실행

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(100001):
    cost_v,h_v,_ =sess.run([cost,hypothesis,train],
                              feed_dict={x1:x1_data,
                                         x2:x2_data,
                                         x3:x3_data,
                                         y:y_data})
    if step % 10000 == 0:
        print(step,cost_v,h_v)

# 예측하기

print("
당신의 점수는 ",sess.run(hypothesis, feed_dict = {x1:100,x2:70,x3:60}))

 

 

4. 시험 데이터 사용

# 데이터 불러오기

import numpy as np

xy = np.loadtxt("C:/data/ex.csv",

                delimiter=",",dtype = np.float32)

xy

# 데이터 나누기

x_data = xy[:,0:-1]

y_data = xy[:,-1]

x_data.shape

y_data.shape

# 데이터 형식이 (5,1)로 맞아야함

x_data = xy[:,0:-1]

y_data = xy[:,[-1]]

x_data.shape

y_data.shape

# 행값을 무한으로 고정시키고 싶을땐 None이라고 한다.

x = tf.placeholder(tf.float32, shape=[None,3])

y = tf.placeholder(tf.float32, shape=[None,1])

w = tf.Variable(tf.random_normal([3,1],seed=0,name='weight'))

b = tf.Variable(tf.random_normal([1],seed=0,name='bias'))

 
# hypothesis 정의

hypothesis = tf.matmul(x,w)+b
 
# MSE로 손실값 구하기

cost = tf.reduce_mean(tf.square(hypothesis - y))
 
# 학습하면서 GradientDescent한다.(학습률 0.00001)

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.00001)
 
# cost optimizer minimize한다.

train = optimizer.minimize(cost)
 

# 세션열고 실행

sess = tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(200001):
    cost_v,h_v,_ =sess.run([cost,hypothesis,train],
                              feed_dict={x:x_data, y:y_data})
    if step % 40000 == 0:
        print(step,cost_v,h_v)

# 예측하기

print("
당신의 점수는 ",
      sess.run(hypothesis, feed_dict = {x:[[100,70,60]]}))   

 

 

 

반응형