IT Log

TensorFlow 본문

기타

TensorFlow

newly0513 2019. 5. 7. 13:33
728x90
반응형

TensorFlow란?

  • 다양한 작업에 대해 데이터 흐름 프로그래밍을 위한 오픈소스 소프트웨어 라이브러리
  • 심볼릭 수학 라이브러리이자, 뉴럴 네트워크 같은 기계학습 응용프로그램에도 사용
  • 구글 브레인팀이 만들었고 2015년 11월 9일 아파치 2.0 오픈소스 라이센스로 공개

 

Anaconda 설치

  1. https://www.anaconda.com/distribution/에 접속
  2. Windows / Mac / Linux 중 해당 OS 선택
  3. Python 3.7 version 다운로드
  4. 프로그램 실행하여 설치 완료 (default값으로 설치 진행)

 

Anaconda Prompt

1. 환경 구축

conda create -n test python=3.7 numpy scipy matplotlib spyder pandas seaborn scikit-learn h5py tensorflow keras

 

2. 환경 활성화

activate test

 

Jupyter Notebook 실행

  1. Anaconda3 폴더 > Jupyter Notebook 클릭

 

 

  2. 실행되면 아래와 같이 웹브라우저가 실행되어짐

 

 

  3. New > Python3 클릭

 

  4. import tensorflow as tf를 입력하고 Ctrl + Enter로 실행하면 아래와 같음

 

TensorFlow 설치

  • Anaconda Prompt 실행
// 아래 2가지 명령을 Prompt에서 다 완료한 후
conda update conda
pip install tensorflow
  • Jupyter Notebook에서 다시 tensorflow 작성
import tensorflow as tf

base = tf.Variable(1, name="counter")
plus = tf.constant(2)
value = tf.add(base, plus)
update = tf.assign(base, value)   
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
  sess.run(init_op)
  print(sess.run(base))
  
  for _ in range(5):
    sess.run(update)
    print(sess.run(base))
// 결과
1
3
5
7
9
11

 

간단한 분류 모델 구현하기

import tensorflow as tf
import numpy as np

xdata = np.array([[0,0],[1,0],[1,1],[0,0],[0,0],[0,1]])
ydata = np.array([
			[1,0,0],
            [0,1,0],
            [0,0,1],
            [1,0,0],
            [1,0,0],
            [0,0,1]])
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W1 = tf.Variable(tf.random_uniform([2,10], -1., 1.))
W2 = tf.Variable(tf.random_uniform([10,3], -1., 1.))

b1 = tf.Variable(tf.zeros([10]))
b2 = tf.Variable(tf.zeros([3]))

L1 = tf.add(tf.matmul(X, W1), b1)
L1 = tf.nn.relu(L1)

model = tf.add(tf.matmul(L1,W2),b2)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))

optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(cost)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for step in range(100):
	sess.run(train_op, feed_dict={X:xdata, Y:ydata})
	if (step + 1) % 10 == 0:
		print(step + 1, sess.run(cost, feed_dict={X:xdata,Y:ydata}))
        
prediction = tf.argmax(model,1)
target = tf.argmax(Y,1)
print('예측', sess.run(prediction, feed_dict={X:xdata,Y:ydata}))
print('실제', sess.run(target, feed_dict={Y:ydata}))

is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print('정확도: %.2f' % sess.run(accuracy * 100, feed_dict={X:xdata,Y:ydata}))
            
10 0.7053401
20 0.54776055
30 0.43680218
40 0.35588858
50 0.29502556
60 0.24649827
70 0.20603375
80 0.17146368
90 0.14168642
100 0.11606511
예측 [0 1 2 0 0 2]
실제 [0 1 2 0 0 2]
정확도: 100.00
728x90
반응형

'기타' 카테고리의 다른 글

플랫폼(Platform)이란?  (0) 2020.08.08
Tensorflow (2)  (0) 2019.05.08
Redis  (0) 2019.05.07
Cassandra  (0) 2019.05.07
MongoDB  (0) 2019.05.07
Comments