OKdevTV Docs
Home OKdevTV
ESC
tensorflow 6 min read 10.9 KB Raw Markdown
On this page (33)

TensorFlow

Prerequisite

install v0.12.1

sh
sudo pip install --upgrade virtualenv
virtualenv --system-site-packages ~/tensorflow
source ~/tensorflow/bin/activate
TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.1-py2-none-any.whl
pip install --upgrade $TF_BINARY_URL
  • exit virtualenv mode
sh
deactivate
sh
pip install --upgrade virtualenv
virtualenv --system-site-packages -p python.exe tensorflow
tensorflow\Scripts\activate
pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.1-cp35-cp35m-win_amd64.whl
  • exit virtualenv mode
text
deactivate

install v1.0.0

  • warning: most samples are not updated.
sh
sudo pip install --upgrade virtualenv
virtualenv --system-site-packages ~/tensorflow
source ~/tensorflow/bin/activate
pip install --upgrade tensorflow
deactivate

first example

sh
import tensorflow as tf
import numpy as np

# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
x_data = np.random.rand(100).astype(np.float32)
y_data = x_data * 0.1 + 0.3

# Try to find values for W and b that compute y_data = W * x_data + b
# (We know that W should be 0.1 and b 0.3, but TensorFlow will
# figure that out for us.)
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))
y = W * x_data + b

# Minimize the mean squared errors.
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

# Before starting, initialize the variables.  We will 'run' this first.
init = tf.global_variables_initializer()

# Launch the graph.
sess = tf.Session()
sess.run(init)

# Fit the line.
for step in range(201):
    sess.run(train)
    if step % 20 == 0:
        print(step, sess.run(W), sess.run(b))

# Learns best fit is W: [0.1], b: [0.3]

Linear Regression

Gradient Descent Method

  • 경사하강법
  • tf.train.GradientDescentOptimizer()

Logistic Regression

  • 0 or 1
  • True of False

Sigmoid

Perceptron

  • the perceptron is an algorithm for learning a binary classifier

softmax

  • http://pythonkim.tistory.com/19
  • “softmax는 데이터를 2개 이상의 그룹으로 나누기 위해 binary classification을 확장한 모델이다.”
  • “통계에서 가장 큰 값을 찾는 개념을 hardmax라고 부른다. softmax는 새로운 조건으로 가장 큰 값을 찾는 개념을 말한다. 일반적으로는 큰 숫자를 찾는 것이 hardmax에 해당하고, 숫자를 거꾸로 뒤집었을 경우에 대해 가장 큰 숫자를 찾는다면 softmax에 해당한다. 여기서는 우리가 알고 있는 큰 숫자를 찾는 것이 아니라는 뜻으로 쓰인다.”

Cross Entropy

NCE loss

Rectifier Linear Unit

  • ReLU

CNN

  • Convolutional Neural Network
  • 이미지를 작게 쪼개어서 분석하는 기법
  • Convolutional Layer + Pooling Layer
  • 참고: http://sanghyukchun.github.io/75/
  • sparse weight, tied weight, equivariant representation

Convolutional Layer

  • 합성곱
  • CNN Architecture
    • Convolutional Layers
    • Sub-sample Layers

Pooling Layer

  • 더 dimension이 낮은 feature map을 얻기 위하여 Subsampling
  • convolution layer의 feature map을 조금 더 줄여주는 역할

RNN

LSTM

NLU

CRF

GRU

GAN

TensorFlow Term

  • rank : dimension of tensor
  • shape : rows and columns of tensor
  • type : data type of tensor
  • mlp : MultiLayer Perceptron
py
import numpy as np
tensor_1d = np.array([1.3, 1, 4.0, 23.99])

print tensor_1d

print tensor_1d[0]

tensor_1d.ndim

tensor_1d.shape

tensor_1d.type

import tensorflow as tf
tf_tensor = tf.convert_to_tensor(tensor_1d, dtype=tf.float64)
py
tensor_2d = np.array([(1,2,3,4), (5,6,7,8), (9,10,11,12), (13,14,15,16)])
print tensor_2d

tensor_2d[0:2,0:2]

Random functions

  • random_uniform() : Uniform Distribution Funtion
    • random_uniform(shape, minval, maxval, dtype, seed, name)
    • Uniform Distribution : 주어진 범위 내의 모든 수가 동일한 분포를 갖는 형태
  • random_normal() : Normal Distribution Function
    • random_normal(shape, mean, stddev, name)

term

- image from : http://www.saedsayad.com/artificial_neural_network_bkp.htm * - image from : http://blog.refu.co/?p=931 - one-hot : 벡터에서 하나만 1이고 나머지는 0으로 채워진 경우 [0,0,0,1,0,0,0,0,0,0] == 3

tasks

  • 선형 회귀
    • 합격 여부 예측
  • RNN
    • Chatbot
  • CNN
    • 비슷한 이미지 찾기

서적

tensorboard

  • TensorFlow 시각화 기능

code

  • name="a"
  • merged = tf.merge_all_summaries()
  • writer = tf.train.SummaryWriter("/tmp/tensorflowlog", session.graph)

tensorboard 실행

text
tensorboard --logdir=/temp/tensorflowlogs

GPU

py
import tensorflow as tf
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))
  • output
text
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: Tesla K40c, pci bus
id: 0000:05:00.0
b: /job:localhost/replica:0/task:0/gpu:0
a: /job:localhost/replica:0/task:0/gpu:0
MatMul: /job:localhost/replica:0/task:0/gpu:0
[[ 22.  28.]
 [ 49.  64.]]

VGA 확인

text
sudo dnf install pciutils
lspci | grep -i vga
  • set gpu
py
import tensorflow as tf
# Creates a graph.
with tf.device('/gpu:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)

# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print(sess.run(c))

ref