tensorflow 설치 및 실행하기

# 참고 https://www.tensorflow.org
# pip 로 설치
pip install tensorflow

# Mac OS X, CPU only, Python 2.7 환경으로 설정
export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0rc0-py2-none-any.whlMac OS X, CPU only, Python 2.7

# 위 환경으로 다시 설치(업그레이드)
sudo pip install --upgrade $TF_BINARY_URL


# https://www.tensorflow.org/versions/r0.12/get_started/index.html 소스 복붙
#####
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]
#####


# 실행해보기
python tensorflow_test1.py
(0, array([-0.07611924], dtype=float32), array([ 0.53998518], dtype=float32))
(20, array([ 0.03687922], dtype=float32), array([ 0.33348444], dtype=float32))
(40, array([ 0.08317406], dtype=float32), array([ 0.30892587], dtype=float32))
(60, array([ 0.09551474], dtype=float32), array([ 0.30237937], dtype=float32))
(80, array([ 0.09880439], dtype=float32), array([ 0.30063426], dtype=float32))
(100, array([ 0.09968131], dtype=float32), array([ 0.30016908], dtype=float32))
(120, array([ 0.09991504], dtype=float32), array([ 0.30004507], dtype=float32))
(140, array([ 0.09997735], dtype=float32), array([ 0.30001202], dtype=float32))
(160, array([ 0.09999396], dtype=float32), array([ 0.3000032], dtype=float32))
(180, array([ 0.09999838], dtype=float32), array([ 0.30000088], dtype=float32))
(200, array([ 0.09999957], dtype=float32), array([ 0.30000025], dtype=float32))


# 텐서보드를 실행하면 http://localhost:6006 로 디버깅부터 각종 정보를 파악할 수 있다.
tensorboard --logdir ~/workspace/logdir

comments:

댓글 쓰기