1. 그래프 빌드

2. sess.run통해서 그래프 실행

3. 실행 결과 돌려줌 

 

 

1. H(x) = Wx + b

x와 y값 주어짐

x_train = [1,2,3]

y_train = [1,2,3]

 

Variable 이란 -> 텐서플로우가 자체적으로 변경해주는 값, 학습해주는 과정에서 변경해준다. 

shap이 무엇이냐, -> 처음에는 보통 랜덤값으로 준다. 

 

 

 

 

minimize -> 자기 스스로 x,y조절하면서 

 

 

matplotlib를 사용한다.

 

cmd창에서 설치를 한 뒤

 

>>> import tensorflow.compat.v1 as tf
>>> X = [1,2,3]
>>> Y = [1,2,3]

>>> W = tf.placeholder(tf.float32)

했는데 

 

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 3023, in placeholder
    raise RuntimeError("tf.placeholder() is not compatible with "
RuntimeError: tf.placeholder() is not compatible with eager execution

 

오류가 났다. 

 

 

 

또 버전 문제인가 해서 

 W = tf.compat.v1.placeholder(tf.compt.v1.float32)

이걸 사용해 봤지만 여전히 오류가 났다. 

 

 

 

 

갑자기 실습 1 때 했던 것이 기억나서 

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

v1버전을 임포트 해오고

v2버전을 비활성화하는 내용이다. 

 

 

 

어쨋든 넘어가고

 

 

 

 

>>> hypothesis = X * W
>>>
>>> cost = tf.reduce_mean(tf.square(hypothesis - Y))
>>> sess = tf.Session()

또, 오류 생겼다. 

Session 할 때마다 생기는 것 같아서 구글랭 해봤는데

일부가 유지보수가 중단 되어서 v1를 사용해라는 권고메시지였다.

 

 

http://blog.daum.net/ejleep1/928

 

1-2 TensorFlow 2.0 업그레이드 후 출현하는 에러 메시지 처리 요령

텐서플로우 2.0 으로 업그레이드 후 1.6 버전에서 실행에 아무런 문제가 없던 코드를 실행한 결과 다음의 메시지가 출현했다. 예를 들면 tf.placeholder 가 유지보수가 중단(deprecated)되었으므로 tf.compa

blog.daum.net

 

 

 

 

 

>>> sess = tf.compat.v1.Session()
>>> sess.run(tf.global_variables_initializer())
>>>
>>> W_val = []
>>> cost_val = []
>>>

-> 그래프를 구동하기 위해 Session을 열고,

-> sess.run(tf.globla_variables_initializer() -> varialbe 초기화

-> W값, cost의 값 저장할 리스트 만들기 (W_val = [], cost_val = [] )

 

 

 

 

>>> for i in range(-30, 50):
...     feed_W = i * 0.1
...     curr_cost, curr_W = sess.run([cost, W], feed_dict = {W: feed_W})

-> -3에서 5까지 0.1간격으로 짧게 움직이겠다

-> feed_dict = {W: feed_W} : W값 넘기면서

-> cost와 W가 어떻게 변하는지 curr_cost에 넣기

 

 

 

for문 하던 도중에 에러가 생기면

고치고 다시 for문 시작해야 한다.

python은 tab을 따지기 때문에,

다시 안하면,

curr_cost, curr_W = sess.run([cost, W], feed_dict = {W: feed_W})
  File "<stdin>", line 1
    curr_cost, curr_W = sess.run([cost, W], feed_dict = {W: feed_W})
    ^
IndentationError: unexpected indent

이런 오류 생긴다.

for문 할때는 ...있을 때!

 

 

 

 

다시 for문 입력해서 

>>> for i in range(-30, 50):
...     feed_W = i * 0.1
...     curr_cost, curr_W = sess.run([cost, W], feed_dict = {W: feed_W})
...     W_val.append(curr_W)
...     cost_val.append(curr_cost)
...

 

 

 

plot를 이용해서 x축이 W이고, y축이 cost인 그래프 그려보자,

>>> plt.plot(W_val, cost_val)
[<matplotlib.lines.Line2D object at 0x000002D5C5A1E2C8>]
>>> plt.show()

 

 

 

최소화 하는 값은 W가 1인 값,

 

 

+ Recent posts