Tensor+Flow 시작하기 (2)

출처 : aymericdamien Github

Multinomial Classification (softmax)

import tensorflow as tf
import numpy as np
xy = np.loadtext('train.txt', unpack=True, dtype='float32')
x_data = np.transpose(xy[0:3])
y_data = np.transpose(xy[3:])

X = tf.placeholder("float", [None, 3])
Y = tf.placeholder("float", [None, 3])
w = tf.Variable(tf.zeros([3, 3]))

hypothesis = tf.nn.softmax(tf.matmul(W,X)) #softmax

learning_rate = 0.001
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(hypothesis), reduction_indices=1))

#Minimize
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

#Minimize end / init before start (초기화)
init = tf.initialize_all_variables()
with tf.Session() as sess:
      sess.run(init)

      for step in xrange(2001):
        sess.run(train, feed_dict{X:x_data, Y:y_data})      #Minimize 실행
        if step % 200 == 0:   #20번 마다 결과값 출력 
         print step, sess.run(cost, feed_dict={X:x_data, Y:y_data}), sess.run(w)

print '--------------------------'
a = sess.run(hypothesis, feed_dict={X:[[1, 11, 7]]})
print a, sess.run(tf.arg_max(a, 1)) # 가장 큰 값 몇번째에 있는지
b = sess.run(hypothesis, feed_dict={X:[[1, 3, 4]]})
print b, sess.run(tf.arg_max(b, 1))
c = sess.run(hypothesis, feed_dict={X:[[1, 1, 0]]})
print c, sess.run(tf.arg_max(c, 1))
all = sess.run(hypothesis, feed_dict={X:[[1, 11, 7], [1, 3, 4] [1, 1, 0]]})
print all, sess.run(tf.arg max(all, 1))

XOR dataset

import numpy as np
import tensorflow as tf

xy = np.loadtxt('xor_dataset.txt', unpack=True)

# Need to change data structure. THESE LINES ARE DIFFERNT FROM Video BUT IT MAKES THIS CODE WORKS!
x_data = np.transpose( xy[0:-1] )
y_data = np.reshape( xy[-1], (4,1) )

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

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

b1 = tf.Variable(tf.zeros([2]), name="Bias1")
b2 = tf.Variable(tf.zeros([1]), name="Bias2")

# Hypotheses 
L2 =  tf.sigmoid(tf.matmul(X,W1)+b1)
hypothesis = tf.sigmoid( tf.matmul(L2,W2) + b2)

# Cost function 
cost = -tf.reduce_mean( Y*tf.log(hypothesis)+(1-Y)* tf.log(1.-hypothesis) )

# Minimize cost.
a = tf.Variable(0.1)
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)

# Initializa all variables.
init = tf.initialize_all_variables()


# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    
    for step in range(8001):
        sess.run(train, feed_dict={X:x_data, Y:y_data})
        
        if step % 1000 == 0:
            print(
                step, 
                sess.run(cost, feed_dict={X:x_data, Y:y_data}), 
                sess.run(W1),
                sess.run(W2)
            )
    
    # Test model
    correct_prediction = tf.equal( tf.floor(hypothesis+0.5), Y)
    accuracy = tf.reduce_mean(tf.cast( correct_prediction, "float" ) )
    
    # Check accuracy
    print( sess.run( [hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy], 
                   feed_dict={X:x_data, Y:y_data}) )
    print( "Accuracy:", accuracy.eval({X:x_data, Y:y_data}) )

XOR dataset (wide)

import numpy as np
import tensorflow as tf

xy = np.loadtxt('xor_dataset.txt', unpack=True)

x_data = np.transpose( xy[0:-1] )
y_data = np.reshape( xy[-1], (4,1) )

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

# Wide network: Use more neurons in each layer. 
W1 = tf.Variable(tf.random_uniform( [2,10], -1.0, 1.0))
W2 = tf.Variable(tf.random_uniform( [10,1], -1.0, 1.0))

b1 = tf.Variable(tf.zeros([10]), name="Bias1")
b2 = tf.Variable(tf.zeros([1]), name="Bias2")

# Hypotheses 
L2 =  tf.sigmoid(tf.matmul(X,W1)+b1)
hypothesis = tf.sigmoid( tf.matmul(L2,W2) + b2)

# Cost function 
cost = -tf.reduce_mean( Y*tf.log(hypothesis)+(1-Y)* tf.log(1.-hypothesis) )

# Minimize cost.
a = tf.Variable(0.1)
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)

# Initializa all variables.
init = tf.initialize_all_variables()


# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    
    for step in range(8001):
        sess.run(train, feed_dict={X:x_data, Y:y_data})
        
        if step % 1000 == 0:
            print(
                step, 
                sess.run(cost, feed_dict={X:x_data, Y:y_data}), 
                sess.run(W1),
                sess.run(W2)
            )
    
    # Test model
    correct_prediction = tf.equal( tf.floor(hypothesis+0.5), Y)
    accuracy = tf.reduce_mean(tf.cast( correct_prediction, "float" ) )
    
    # Check accuracy
    print( sess.run( [hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy], 
                   feed_dict={X:x_data, Y:y_data}) )
    print( "Accuracy:", accuracy.eval({X:x_data, Y:y_data}) )

XOR dataset (deep)

import numpy as np
import tensorflow as tf

xy = np.loadtxt('xor_dataset.txt', unpack=True)

x_data = np.transpose( xy[0:-1] )
y_data = np.reshape( xy[-1], (4,1) )

X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

# Deep network configuration.: Use more layers. 
W1 = tf.Variable(tf.random_uniform( [2,5], -1.0, 1.0))
W2 = tf.Variable(tf.random_uniform( [5,4], -1.0, 1.0))
W3 = tf.Variable(tf.random_uniform( [4,1], -1.0, 1.0))


b1 = tf.Variable(tf.zeros([5]), name="Bias1")
b2 = tf.Variable(tf.zeros([4]), name="Bias2")
b3 = tf.Variable(tf.zeros([1]), name="Bias3")


# Hypotheses 
L2 =  tf.sigmoid(tf.matmul(X,W1)+b1)
L3 =  tf.sigmoid(tf.matmul(L2,W2)+b2)
hypothesis = tf.sigmoid( tf.matmul(L3,W3) + b3)

# Cost function 
cost = -tf.reduce_mean( Y*tf.log(hypothesis)+(1-Y)* tf.log(1.-hypothesis) )

# Minimize cost.
a = tf.Variable(0.1)
optimizer = tf.train.GradientDescentOptimizer(a)
train = optimizer.minimize(cost)

# Initializa all variables.
init = tf.initialize_all_variables()


# Launch the graph
with tf.Session() as sess:
    sess.run(init)
    
    for step in range(20001):
        sess.run(train, feed_dict={X:x_data, Y:y_data})
        
        if step % 1000 == 0:
            print(
                step, 
                sess.run(cost, feed_dict={X:x_data, Y:y_data}), 
                sess.run(W1),
                sess.run(W2)
            )
    
    # Test model
    correct_prediction = tf.equal( tf.floor(hypothesis+0.5), Y)
    accuracy = tf.reduce_mean(tf.cast( correct_prediction, "float" ) )
    
    # Check accuracy
    print( sess.run( [hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy], 
                   feed_dict={X:x_data, Y:y_data}) )
    print( "Accuracy:", accuracy.eval({X:x_data, Y:y_data}) )

TensorFlow 그래프 시각화 작업

with tf.name_scope("cost") as scope:
    cost = -tf.reduce_mean(Y=tf.log(hypothesis) + (1-Y)*tf.log(1-hypothesis))
    cost_sum = tf.scalar_summary("cost", cost)  ## Add Scalar Variable

w1_hist = tf.histogram_summary("weight1", w1)  ## Add histogram

X = tf.placeholder(tf.float32, name = 'X-input')
Y = tf.placeholder(tf.float32, name = 'Y-input')
W = tf.Variable(tf.random_uniform([2, 2]), -1.0, 1.0), name = "Weight1")
b = tf.Variable(tf.zeros([2]), name = "Bias1")

with tf.Session() as sess:
     merged = tf.merge_all_summaries()
     writer = tf.train.SummaryWriter("./logs/xor_logs",sess.graph_def)

for step in xrange(2000):
     summary = sess.run([merged, train], feed_dict={X:x_data, Y=y_data})
     writer.add_summary(summary, step)

##시각화 Launch !
tensorboard --logdir=/tmp/mnist_logs

AWS 테스트 – g2.x2large(Oregon Region) 권장. 더 괜찮은 GPU EC2 찾으면 업데이트.

CUDA / CuDNN 설치(ubuntu 16.04)

wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_8.0.44-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1604_8.0.44-1_amd64.deb
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install -y opencl-headers build-essential protobuf-complier libprotoc-dev libboost-all-dev libleveldb-dev hdf5-tools libhdf5-serial-dev libopencv-core-dev libopencv-highgui-dev libsnappy-dev libsnappy1 libalatlas-base-dev cmake libstdc++6-4.8-dbg libgoogle-glog0 libgoogle-glog-dev libgflags-dev liblmdb-dev git gfortran
sudo apt-get install cuda
sudo apt-get install python-pip python-dev
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0rc0-cp27-none-linux_x86_64.whl
git clone https://github.com/nlintz/TensorFlow-Tutorials
cd TensorFlow-Tutorials/
vi ~/.profile # add PATH, LD PATH
source ~/.profile
python 06_autoencoder.py
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuba/lib64:$LD_LIBRARY_PATH

Softmax classifier for MNIST

# tf Graph input
x = tf.placeholder("float", [None, 784]) #mnist data image of shape 28*28=784
y = tf.placeholder("float", [None, 10]) #0-9 digits recognition->10classes
#Create model
#set model wights
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
#Construct model
activation = tf.nn.softmax(tf.matmul(x,W)+b) #Softmax
#Mizimize error using cross entropy
cost = tf.reduce_mean(-tf.reduce_sum(y=tf.log(activation), reduction_indicies=1)) #Cross entropy
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) #Gradient Descent

#Initializing Variables
init = tf.initialize_all_variables()
#Launch Graph
with tf.Session() as sess:
    sess.run(init)
    #Training cycle
    for epoch in range(training_epochs):
      avg_cost = 0.
      total_batch = int(mnist.train.num_examples/batch_size)
      #loop over all batches
      for i in range(total_batch):
          batch_xs, batch_ys = minst.train.next_batch(batch_size)
          #Fit training using batch data
          sess.run(optimizer, fee_dict={x: batch_xs, y: batch_ys})
          #Compute average loss
          avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys})
      #Display logs per epoch step
      if epoch % display_step ==0:
          print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)
    print "Optimization Finished"
    #Test mode 
    correct_prediction = tf.equal(tf.argmax(activation, 1), tf.argmax(y,1))
    #Calculate accuracy
    accurac = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})

NN(Neural Nets) for MNIST

#Parameters
learning_rate = 0.001
training_epochs = 15
batch_size =100
display_step =1
#tf Graph input
X = tf.placeholder("float", [None, 784])
Y = tf.placeholder("float", [None, 10])
#Store layers weight & bias
W1 = tf.Variable(tf.random_normal([784,256]))
W2 = tf.Variable(tf.random_normal([256,256]))
W3 = tf.Variable(tf.random_normal([256,10]))

B1 = tf.Variable(tf.random_normal([256]))
B2 = tf.Variable(tf.random_normal([256]))
B3 = tf.Variable(tf.random_normal([10]))

L1 = tf.nn.relu(tf.add(tf.matmul(X,W1),B1))
L2 = tf.nn.relu(tf.add(tf.matmul(L1,W2),B2)) #hidden layer
hypothesis = tf.add(tf.add(tf.matmul(L2,W3),B3)) #no need softmax here
#Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(hypothesis, Y)) #Softmax loss
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost) # 

More Deep & dropout

#Construct Model

dropout_rate = tf.placeholder("float")
L1 = tf.nn.relu(tf.add(tf.matmul(X,W1),B1))
L2 = tf.nn.dropout(_L1, dropout_rate)
L1 = tf.nn.relu(tf.add(tf.matmul(X,W1),B1))
L2 = tf.nn.dropout(_L1, dropout_rate)
L1 = tf.nn.relu(tf.add(tf.matmul(X,W1),B1))
L2 = tf.nn.dropout(_L1, dropout_rate)
L1 = tf.nn.relu(tf.add(tf.matmul(X,W1),B1))
L2 = tf.nn.dropout(_L1, dropout_rate)
hypothesis = tf.add(tf.matmul(L4, W5), B5)

for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
#Fit training using batch data
sess.run(optimizer, feed_dict={X: batch_xs, Y: batch_ys, dropout_rate: 0.7})

#Test model
correct_prediction = tf.equal(tf.argmax(hypothesis, 1), tf.argmax(Y,1))
#Calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))
print "Accuracy:", accuracy.eval({X: mnist.test.images, Y: mnist.test.labels, dropout_rate: 1})

참고 :

Data Standardization / Regularization

X_std[:,0] = (X[:,-] – X[:,0].mean()) / X[:,0].std()

l2reg = 0.001 * tf.reduce_sum(tf.square(W))

Advertisements

답글 남기기

아래 항목을 채우거나 오른쪽 아이콘 중 하나를 클릭하여 로그 인 하세요:

WordPress.com 로고

WordPress.com의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Google+ photo

Google+의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Twitter 사진

Twitter의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

Facebook 사진

Facebook의 계정을 사용하여 댓글을 남깁니다. 로그아웃 /  변경 )

%s에 연결하는 중