본문 바로가기

반응형

잡다한 IT/머신러닝 & 딥러닝

03-4 Minimizing Cost tf gradient ■ 직접구한 Minimizing cost 와 텐서플로우 사용 Minimizing cost 비교 gvs = optimizer.compute_gradients(cost,[W])- gradient 값을 얻어오는 함수 apply_graidents = optimizer.apply_gradients(gvs)- gradient 값을 통하여 W 값을 얻어내기 위한 그래프를 생성함 sess.run(apply_gradients)- apply_gradients를 실행하여 W 값을 얻어옴 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717.. 더보기
03-3 Minimizing Cost tf optimizer ■ 텐서플로우에서 제공하는 Minimizing Cost 함수를 이해한다. ▶ 내장 함수를 사용하여 목적함수를 구함optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)train = optimizer.minimize(cost)- tf->train->GradientDescentOptimizer->minimize 와 같은 계통으로 사용을 함- learning_rate=0.1 과 cost 함수의 인자로 사용하는 위치를 익힌다. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051# Lab03-3 Minimizing Cost tf optim.. 더보기
03-2 Minimizing Cost without tensorflow APIs ■ 텐서플로우 함수를 사용하지 않고 Cost Minimizing 기능을 구현한다. ▶ 내장함수를 사용하지 않고 편미분으로 직접 구현함learning_rate = 0.1- 학습률을 설정한다 gradient = tf.reduce_mean((W*X-Y)*X)- Cost 함수를 W에 대한 편미분을 했을 때의 값- 편미분을 통하여 기울기 값을 구한다. descent = W - learning_rate*gradient- 현재의 W값에서 학습율과 기울기의 곱을 한 값을 뺀다. update = W.assign(descent)- 그 값을 W에 업데이트 한다. 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950.. 더보기
03-1 Minimizing Cost show graph ■ Cost 함수를 그래프로 표시한다. ▶Matplotlib를 이용한 그래프 그리기 import matplotlib.pyplot as plt- matplotlib를 임포트한다. W_history=[]cost-history=[]- x축과 y축에 사용할 배열을 할당한다.- x축은 W_history , y축은 cost_history 이다. curr_cost = sess.run(cost,feed_dict={W:curr_W})W_history.append(curr_W)cost_history.append(curr_cost)- sess.run() 을 통하여 cost ops를 실행하고 각각의 값을 넣는다. plt.plot(W_history, cost_history)plt.show()- 그래프를 그리고 화면에 출력한다... 더보기
02-3 Linear Regression : feed from variable ■ Place hold 사용법을 익힌다. (파이썬 변수를 feed 처리함) ▶Place hold 사용 - 변수 대입x_train = [1,2,3,4]y_train = [0,-1,-2,-3]- place hold 에 feed_dict로 연결할 변수 생성 curr_W,curr_b,curr_loss = sess.run([W,b,loss],{x:x_train,y:y_train})-세션을 실행할 때 place hold에 변수를 대입한다.이때 사용되는 것이 feed_dict이다 123456789101112131415161718192021222324252627282930313233import tensorflow as tf W=tf.Variable([.3],tf.float32)b=tf.Variable([-.3],tf... 더보기
02-2 Linear Regression : feed ■ Place hold 를 입력으로 사용하여 간단한 Linear regression 구현 ▶Place hold 사용 - 상수 대입X = tf.placeholder(tf.float32, shape=[None])Y = tf.placeholder(tf.float32, shape=[None])- rank = 1 이고 type이 tf.float32 인 place hold 를 생성한다.- None은 해당 축의 원소 갯수를 임의로 선정한다는 의미이다. sess.run([cost,W,b,train],feed_dict={X:[1,2,3],Y:[1,2,3]})- 세션을 실행할 때 place hold에 변수를 대입한다.-이때 사용되는 것이 feed_dict 이다. 123456789101112131415161718192021.. 더보기
02-1 Linear Regression ■ Linear Regression ▶ 난수의 사용tf.set_random_seed(777)- 그래프 수준의 난수 시들르 설정- 난수 시드에 의존하는 연산들은 실제로 그래프 수준과 연산 수준의 두 가지 시드로부터 시드를 얻음- 이 연산은 그래프 수준의 시드를 설정함 tf.random_normal([1])- 정규분포로부터의 난수값을 반환합니다.- 괄호 안의 [1] 은 rank 값을 의미함 ▶ 변수W = tf.Variable(tf.random_normal([1]),name='weight')- 변수의 선언- 1열(shape [1])의 텐서의 속성을 가지는 변수 선언 tf.global_variables_initializer()- 반드시 변수는 초기화를 해야함- 이 때 사용되는 변수 초기화 함수가 tf.globa.. 더보기
01 Tensorflow basic ■ 기본적인 텐서플로우를 동작시키기 위한 구조 파악 import tensorflow as tf- 텐서플로우 API 사용을 하려면 tensorflow를 임포트 tf.__version__- version check hello = tf.constant("Hello, TensorFlow!")- 상수 텐서를 생성- 1차원 텐서를 생성하고, 1차원 텐서 hello 에는 Hello,TensorFlow 라는 스트링이 존재 sess = tf.Session()- Session 객체는 Operation 객체가 실행되고 Tensor 객체가 계산되는 환경을 캡슐화한다.- class tf.Session는 TensorFlow 연산들을 실행하기 위한 클래스 sess.run(hello)- fetches에서 연산과 텐서를 실행합니다. .. 더보기

반응형