12.convolution


CNN

  • CNNConvolutional Neural Network

  • CNN은 합성곱 계층convolusion layer과 풀링 계층pooling layer으로 구성

  • 지정한 크기의 영역을 window라 하며, 이 window를 오른쪽, 아래쪽으로 움직이면서 hidden layer를 완성
  • 몇 칸씩 움직이는 값을 stride스트라이드라 함

  • 이렇게 input layer의 window를 hidden layer의 뉴런 하나로 압축할 때, convolution 계층에서는 window 크기(ex 3x3이면 9개의 가중치)만큼의 가중치와 1개의 bias이 필요함
  • 이 때 window의 크기와 bias를 kernel 혹은 filter라고 하며, 이 kernel은 해당 hidden layer를 만들기 위한 모든 window에 공통으로 적용
  • 기본 신경망으로 모든 뉴런을 연결하면 784개의 가중치를 찾아내야 하지만, convolution에서는 3x3개인 9개의 가중치만 찾아내면 되므로 시간이 빠름
  • 알고리즘을 진행하는데 튜닝하는 파라미터를 하이퍼 파라미터라함

In [1]:
import tensorflow as tf
import pandas as pd
import numpy as np
In [4]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
Extracting ./mnist/data/train-images-idx3-ubyte.gz
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz


CNN 모델에서는 2차원 평면으로 구성하므로 조금 더 직관적인 형태로 구성할 수 있음.

  • X의 첫번째 차원인 None은 입력 데이터 갯수
  • 마지막 차원은 1, MNIST 데이터는 회색조 이미지라 색상이 한개 뿐이므로 depth=1을 사용
  • 출력값인 10개의 분류와, dropout, keep_prob를 정의
In [5]:
global_step = tf.Variable(0, trainable=False, name="global_step")
X = tf.placeholder(tf.float32, shape=[None, 28, 28, 1], name="X")
Y = tf.placeholder(tf.float32, shape=[None, 10], name="Y")
keep_prob = tf.placeholder(tf.float32, name="KEEP_PROB")


첫 번째 CNN 계층을 구성

  • 3x3 크기의 커널을 가진 convolution 계층을 구성
  • kernel에 사용할 가중치 변수와 tensorflow가 제공하는 tf.nn.conv2d()함수를 사용

In [6]:
# 3x3x1 크기의 커널과 (1)을 가지고32개의 커널
with tf.name_scope("layer1"):
    W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))

# 입력층 x, 첫 번째 계층의 가중치 W1, 오른쪽과 아래쪽으로 1칸 , 
# padding="SAME" -> 이미지의 가장 외곽에서 한 칸 밖으로 움직임
# strides=[1, 오른쪽, 아래쪽, 1], 양 끝은 반드시 1
# [batch_size, image_rows, image_cols, number_of_colors]
    L1 = tf.nn.conv2d(X, W1, strides=[1, 1, 1 ,1], padding="SAME")
    L1 = tf.nn.relu(L1)
# [batch_size, images_width, images_height, number_of_colors]
    L1 = tf.nn.max_pool(L1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

In [7]:
with tf.name_scope("layer2"):
# 3x3x1 -- 32개를 받아 64개로 convolution 계층 만듬
    W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
    L2 = tf.nn.conv2d(L1, W2, strides=[1, 1, 1, 1], padding="SAME")
    L2 = tf.nn.relu(L2)
    L2 = tf.nn.max_pool(L2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

In [8]:
with tf.name_scope("layer3"):
# 7x7x64크기의 1차원 계층을 만들고 중간단계인 256개의 뉴런으로 연결하는 신경망을 만들어줌
# fully connected layer
    W3 = tf.Variable(tf.random_normal([7 * 7 * 64, 256], stddev=0.01))
    L3 = tf.reshape(L2, [-1, 7 * 7 *64])
    L3 = tf.matmul(L3, W3)
    L3 = tf.nn.relu(L3)
    L3 = tf.nn.dropout(L3, keep_prob)
In [9]:
with tf.name_scope("layer4"):
    W4 = tf.Variable(tf.random_normal([256, 10], stddev=0.01))
    model = tf.matmul(L3, W4)
In [10]:
with tf.name_scope("cost"):
    cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(logits=model, labels=Y))
    opt = tf.train.AdamOptimizer(0.001).minimize(cost)
# opt = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
    tf.summary.scalar("cost", cost)
In [11]:
# batch_xs.reshape(-1, 28, 28, 1)
# mnist.test.images.reshape(-1, 28, 28, 1)
In [12]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_cnn", sess.graph)
cost_epoch = []


modeling

In [13]:
%%time
batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)

for epoch in range(15):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        batch_xs = batch_xs.reshape(-1, 28, 28, 1)
        
        _, cost_val = sess.run([opt, cost], feed_dict={X: batch_xs,
                                                       Y: batch_ys,
                                                       keep_prob: 0.8})
        
        total_cost += cost_val
        cost_epoch.append(total_cost)
        
        summary = sess.run(merged, feed_dict={X:batch_xs, Y: batch_ys, keep_prob:0.8})
        writer.add_summary(summary, global_step=sess.run(global_step))
        
    print("Epoch:", "%4d" % (epoch+1), 
          "Avg.Cost:", "%.4f" % (total_cost / total_batch))
    
print("optimization completed")    
Epoch:    1 Avg.Cost: 0.3136
Epoch:    2 Avg.Cost: 0.0977
Epoch:    3 Avg.Cost: 0.0681
Epoch:    4 Avg.Cost: 0.0531
Epoch:    5 Avg.Cost: 0.0438
Epoch:    6 Avg.Cost: 0.0356
Epoch:    7 Avg.Cost: 0.0310
Epoch:    8 Avg.Cost: 0.0275
Epoch:    9 Avg.Cost: 0.0233
Epoch:   10 Avg.Cost: 0.0193
Epoch:   11 Avg.Cost: 0.0198
Epoch:   12 Avg.Cost: 0.0155
Epoch:   13 Avg.Cost: 0.0151
Epoch:   14 Avg.Cost: 0.0130
Epoch:   15 Avg.Cost: 0.0120
optimization completed
CPU times: user 38min 12s, sys: 1min 17s, total: 39min 29s
Wall time: 14min 52s


costfunction

In [14]:
%matplotlib inline
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 8))
plt.plot(cost_epoch, "g")
plt.title("cost")
plt.show()


tensorgrapth

In [15]:
## jptensor.py 를 워킹디렉토리에 import
import jptensor as jp

tf_graph = tf.get_default_graph().as_graph_def()
jp.show_graph(tf_graph)
In [16]:
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy %.4f" % (sess.run(accuracy, feed_dict={
    X:mnist.test.images.reshape(-1, 28, 28, 1),
    Y:mnist.test.labels,
    keep_prob: 1
})))
accuracy 0.9909


labels

In [17]:
%matplotlib inline
labels = sess.run(model, feed_dict={X: mnist.test.images.reshape(-1, 28, 28, 1),
                                    Y: mnist.test.labels,
                                    keep_prob: 1})
fig = plt.figure()
for i in range(10):
    # (2, 5)의 그래프, i + 1번째 숫자 이미지 출력
    subplot = fig.add_subplot(2, 5, i+1)
    
    # x, y축 눈금 제거
    subplot.set_xticks([])
    subplot.set_yticks([])
    
    # 출력한 이미지 위에 예측한 숫자를 출력
    # np.argmax와 tf.argmax는 같은 기능
    # 결과값인 labels의 i번째 요소가 one-hot encoding으로 되어 있으므로
    # 해당 배열에서 가장 높은 값을 가진 인덱스를 예측한 숫자로 출력
    subplot.set_title("%d" % np.argmax(labels[i]))
    
    # 1차원 배열로 되어 있는 i번째 이미지 데이터를
    # 28 x 28형태의 2차원 배열로 변환
    subplot.imshow(mnist.test.images[i].reshape(28, 28))
plt.show()    
In [18]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))


'Deep_Learning' 카테고리의 다른 글

14.gan  (0) 2018.12.16
13.auto-encoder  (0) 2018.12.15
11.mnist_matplotlib_dropout_tensorgraph  (0) 2018.12.10
10.mnist_dropout  (0) 2018.12.10
00.write_csv  (0) 2018.12.09
11.mnist_matplotlib
In [1]:
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

plt.rcParams["axes.unicode_minus"] = False
plt.rcParams["figure.figsize"] = (12, 8)
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
Extracting ./mnist/data/train-images-idx3-ubyte.gz
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz


variable setting

In [4]:
global_step = tf.Variable(0, trainable=False, name="global_step")
X = tf.placeholder(tf.float32, shape=[None, 784], name="X")
Y = tf.placeholder(tf.float32, shape=[None,  10], name="Y")

W1 = tf.Variable(tf.random_normal([784, 256], mean=0, stddev=0.01), name="W1")
W2 = tf.Variable(tf.random_normal([256, 256], mean=0, stddev=0.01), name="W2")
W3 = tf.Variable(tf.random_normal([256,  10], mean=0, stddev=0.01), name="W3")

b1 = tf.zeros([256], name="bias1")
b2 = tf.zeros([256], name="bias2")
b3 = tf.zeros([10] , name="bais3")


model setting

In [5]:
keep_prob = tf.placeholder(tf.float32)

with tf.name_scope("layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.relu(L1)
    L1 = tf.nn.dropout(L1, keep_prob)
    
with tf.name_scope("layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.relu(L2)
    L2 = tf.nn.dropout(L2, keep_prob)
    
with tf.name_scope("layer3"):
    model = tf.add(tf.matmul(L2, W3), b3)
    
with tf.name_scope("cost"):
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))
    opt = tf.train.AdamOptimizer(0.001).minimize(cost, global_step=global_step)
    
    tf.summary.scalar("cost", cost)


model initialization

In [6]:
init = tf.global_variables_initializer()
sess = tf.Session()

sess.run(init)

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_matplotlib", sess.graph)
In [7]:
batch_size = 50
total_batch = int(mnist.train.num_examples / batch_size)
cost_epoch = []


model training

In [8]:
%%time
for epoch in range(20):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        
        _, cost_val = sess.run([opt, cost], feed_dict={X:batch_xs, Y: batch_ys, keep_prob:0.8})
        total_cost += cost_val
        cost_epoch.append(total_cost)
        
        summary = sess.run(merged, feed_dict={X:batch_xs, Y: batch_ys, keep_prob:0.8})
        writer.add_summary(summary, global_step=sess.run(global_step))
        
    print("epoch: %d, Avg.cost: %.4f" % (
        epoch+1, total_cost / total_batch
    ))
epoch: 1, Avg.cost: 0.3481
epoch: 2, Avg.cost: 0.1395
epoch: 3, Avg.cost: 0.1000
epoch: 4, Avg.cost: 0.0806
epoch: 5, Avg.cost: 0.0697
epoch: 6, Avg.cost: 0.0591
epoch: 7, Avg.cost: 0.0507
epoch: 8, Avg.cost: 0.0455
epoch: 9, Avg.cost: 0.0417
epoch: 10, Avg.cost: 0.0394
epoch: 11, Avg.cost: 0.0362
epoch: 12, Avg.cost: 0.0361
epoch: 13, Avg.cost: 0.0305
epoch: 14, Avg.cost: 0.0303
epoch: 15, Avg.cost: 0.0271
epoch: 16, Avg.cost: 0.0282
epoch: 17, Avg.cost: 0.0267
epoch: 18, Avg.cost: 0.0267
epoch: 19, Avg.cost: 0.0219
epoch: 20, Avg.cost: 0.0238
CPU times: user 3min 22s, sys: 43.7 s, total: 4min 6s
Wall time: 2min 27s


cost function

In [9]:
plt.figure(figsize=(20, 8))
plt.plot(cost_epoch, "g")
plt.title("cost_epoch")
plt.show()


tensor graph

In [10]:
## jptensor.py 를 워킹디렉토리에 import
import jptensor as jp

tf_graph = tf.get_default_graph().as_graph_def()
jp.show_graph(tf_graph)


test

In [11]:
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

accuracy_val = sess.run(accuracy, feed_dict={X: mnist.test.images, 
                                             Y: mnist.test.labels,
                                             keep_prob: 1})

print("accuracy: %.3f" % (accuracy_val))
accuracy: 0.980


labels

In [12]:
labels = sess.run(model, feed_dict={X: mnist.test.images,
                                    Y: mnist.test.labels,
                                    keep_prob: 1})
In [13]:
%matplotlib inline
fig = plt.figure()
for i in range(10):
    # (2, 5)의 그래프, i + 1번째 숫자 이미지 출력
    subplot = fig.add_subplot(2, 5, i+1)
    
    # x, y축 눈금 제거
    subplot.set_xticks([])
    subplot.set_yticks([])
    
    # 출력한 이미지 위에 예측한 숫자를 출력
    # np.argmax와 tf.argmax는 같은 기능
    # 결과값인 labels의 i번째 요소가 one-hot encoding으로 되어 있으므로
    # 해당 배열에서 가장 높은 값을 가진 인덱스를 예측한 숫자로 출력
    subplot.set_title("%d" % (np.argmax(labels[i])))
    
    # 1차원 배열로 되어 있는 i번째 이미지 데이터를
    # 28 x 28형태의 2차원 배열로 변환
    subplot.imshow(mnist.test.images[i].reshape((28, 28)))
plt.show()
In [14]:
from IPython.core.display import HTML, display

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

13.auto-encoder  (0) 2018.12.15
12.mnist_cnn  (0) 2018.12.12
10.mnist_dropout  (0) 2018.12.10
00.write_csv  (0) 2018.12.09
09.mnist_01_minibatch  (0) 2018.12.09
10.mnist_dropout
In [1]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))
In [2]:
import tensorflow as tf
import warnings
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (12, 8)
In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
WARNING:tensorflow:From <ipython-input-3-4dcbd946c02b>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting ./mnist/data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
In [4]:
global_step = tf.Variable(0, trainable=False, name="global_step")
X = tf.placeholder(tf.float32, shape=[None, 784], name="X") # None, 784
Y = tf.placeholder(tf.float32, shape=[None, 10], name="Y")

W1 = tf.Variable(tf.random_normal([784, 256], mean=0, stddev=0.01), name="W1")
W2 = tf.Variable(tf.random_normal([256, 256], mean=0, stddev=0.01), name="W2")
W3 = tf.Variable(tf.random_normal([256,  10], mean=0, stddev=0.011), name="W3")

b1 = tf.zeros([256], name="bias1")
b2 = tf.zeros([256], name="bias2")
b3 = tf.zeros([10],  name="bias3")


dropout

  • 학습시 전체 신경망 중 일부만 사용하도록 함 -> 과적합 방지
  • 시간이 오래걸리는 편

In [5]:
keep_prob = tf.placeholder(tf.float32)

with tf.name_scope("layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.relu(L1)
    L1 = tf.nn.dropout(L1, keep_prob)
    
with tf.name_scope("layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.relu(L2)
    L2 = tf.nn.dropout(L2, keep_prob)
    
with tf.name_scope("layer3"):
    model = tf.add(tf.matmul(L2, W3), b3)
In [6]:
with tf.name_scope("optimizer"):
    cost = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))
    opt = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost, global_step=global_step)
    tf.summary.scalar("cost", cost)
In [7]:
init = tf.global_variables_initializer()
sess = tf.Session()
# saver = tf.train.Saver(tf.global_variables())
sess.run(init)

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_dropout", sess.graph)
In [8]:
batch_size = 50
total_batch = int(mnist.train.num_examples/batch_size)
cost_epoch = []
In [9]:
%%time
for epoch in range(30):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        
        _, cost_val = sess.run([opt, cost], feed_dict={X:batch_xs, Y:batch_ys, keep_prob: 0.8})
        total_cost += cost_val
        cost_epoch.append(total_cost)
        
        summary = sess.run(merged, feed_dict={X:batch_xs, Y:batch_ys, keep_prob: 0.8})
        writer.add_summary(summary, global_step=sess.run(global_step))
        
    print("epoch: {}, Avg.cost: {}".format(epoch+1, total_cost / total_batch))
epoch: 1, Avg.cost: 0.3532606958614832
epoch: 2, Avg.cost: 0.1427451760597019
epoch: 3, Avg.cost: 0.10226461782120168
epoch: 4, Avg.cost: 0.08699281872800467
epoch: 5, Avg.cost: 0.06855666186279533
epoch: 6, Avg.cost: 0.05921383855340537
epoch: 7, Avg.cost: 0.0536975436815357
epoch: 8, Avg.cost: 0.04580990582659565
epoch: 9, Avg.cost: 0.04084625356087186
epoch: 10, Avg.cost: 0.040573723167723404
epoch: 11, Avg.cost: 0.035842695366584604
epoch: 12, Avg.cost: 0.03263294939398871
epoch: 13, Avg.cost: 0.03360669748346316
epoch: 14, Avg.cost: 0.030501310914848794
epoch: 15, Avg.cost: 0.028370174647349235
epoch: 16, Avg.cost: 0.02699218331828392
epoch: 17, Avg.cost: 0.026614617982999005
epoch: 18, Avg.cost: 0.027732884158863685
epoch: 19, Avg.cost: 0.02651893331764189
epoch: 20, Avg.cost: 0.024510102366322662
epoch: 21, Avg.cost: 0.024103802091576653
epoch: 22, Avg.cost: 0.021529521410293455
epoch: 23, Avg.cost: 0.024205624244715927
epoch: 24, Avg.cost: 0.021746395409784947
epoch: 25, Avg.cost: 0.02059082699589949
epoch: 26, Avg.cost: 0.02283201359495644
epoch: 27, Avg.cost: 0.021406652638101233
epoch: 28, Avg.cost: 0.022226517286706812
epoch: 29, Avg.cost: 0.019306987923368567
epoch: 30, Avg.cost: 0.020735127189004873
CPU times: user 4min 42s, sys: 1min, total: 5min 43s
Wall time: 3min 23s
In [10]:
plt.plot(cost_epoch, "g")
plt.title("cost_epoch")
plt.show()
In [11]:
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: {}".format(sess.run(accuracy, feed_dict={X: mnist.test.images,
                                                        Y: mnist.test.labels,
                                                        keep_prob: 1})))
accuracy: 0.9835000038146973
In [12]:
### tensorboard graph

from IPython.display import clear_output, Image, display, HTML

def strip_consts(graph_def, max_const_size=32):
    """Strip large constant values from graph_def."""
    strip_def = tf.GraphDef()
    for n0 in graph_def.node:
        n = strip_def.node.add() 
        n.MergeFrom(n0)
        if n.op == 'Const':
            tensor = n.attr['value'].tensor
            size = len(tensor.tensor_content)
            if size > max_const_size:
                tensor.tensor_content = "<stripped %d bytes>"%size
    return strip_def

def show_graph(graph_def, max_const_size=32):
    """Visualize TensorFlow graph."""
    if hasattr(graph_def, 'as_graph_def'):
        graph_def = graph_def.as_graph_def()
    strip_def = strip_consts(graph_def, max_const_size=max_const_size)
    code = """
        <script>
          function load() {{
            document.getElementById("{id}").pbtxt = {data};
          }}
        </script>
        <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()>
        <div style="height:600px">
          <tf-graph-basic id="{id}"></tf-graph-basic>
        </div>
    """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand()))

    iframe = """
        <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe>
    """.format(code.replace('"', '&quot;'))
    display(HTML(iframe))
In [13]:
show_graph(tf.get_default_graph().as_graph_def())
In [14]:
# ### tensorboard

# def TB(cleanup=False):
#     import webbrowser
#     webbrowser.open('http://127.0.0.1:6006')

#     !tensorboard --logdir="./logs/mnist_dropout/"

# TB()

'Deep_Learning' 카테고리의 다른 글

12.mnist_cnn  (0) 2018.12.12
11.mnist_matplotlib_dropout_tensorgraph  (0) 2018.12.10
00.write_csv  (0) 2018.12.09
09.mnist_01_minibatch  (0) 2018.12.09
08.tensorboard03_example  (0) 2018.12.09
00_write_csv
In [1]:
import pandas as pd
import numpy as np
In [2]:
# 털, 날개, 기타, 포유류, 조류
col_list = ["hair", "wing", "etc", "mammals", "bird"]
classification_ex = pd.DataFrame({
    "hair": [0, 1, 1, 0, 0, 0],
    "wing": [0, 0, 1, 0 ,0 ,1],
    "bird": [1, 0, 0, 1, 1, 0],
    "etc" : [0, 1, 0, 0, 0, 0],
    "mammals": [0, 0, 1 ,0 ,0 ,1]   
}, columns=col_list)

classification_ex.to_csv("./datas/classification_ex1.csv", 
                         encoding="utf-8",
                        index=False) # header=False
In [3]:
## iris_data
from sklearn.datasets import load_iris

iris = load_iris()
iris_values = np.hstack([iris.data, iris.target.reshape(-1, 1)])

col_names = ["sepal_length", "sepal_width", "petal_length", "petal_width", "species"]
iris_data = pd.DataFrame(data=iris_values, columns=col_names)

iris_data["species"].replace(to_replace=0.0, value="setosa", inplace=True)
iris_data["species"].replace(to_replace=1.0, value="versicolor", inplace=True)
iris_data["species"].replace(to_replace=2.0, value="virginica", inplace=True)

iris_data.to_csv("./datas/iris.csv", encoding="utf-8", index=False)
In [4]:
## breast_cancer
from sklearn.datasets import load_breast_cancer

cancer = load_breast_cancer()
cancer_values = np.hstack([cancer.data, cancer.target.reshape(-1, 1)])

col_names = np.hstack([cancer.feature_names, "result"])
cancer_data = pd.DataFrame(data=cancer_values, columns=col_names)

cancer_data["result"].replace(to_replace=0, value="malignant", inplace=True)
cancer_data["result"].replace(to_replace=1, value="benign", inplace=True)

cancer_data.to_csv("./datas/cancer.csv", encoding="utf-8", index=False)
In [5]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

11.mnist_matplotlib_dropout_tensorgraph  (0) 2018.12.10
10.mnist_dropout  (0) 2018.12.10
09.mnist_01_minibatch  (0) 2018.12.09
08.tensorboard03_example  (0) 2018.12.09
07.tensorboard02_example  (0) 2018.12.09
09_mnist_01_minibatch
In [1]:
import tensorflow as tf
import warnings
warnings.filterwarnings("ignore")


mnist data 준비

In [2]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
WARNING:tensorflow:From <ipython-input-2-4dcbd946c02b>:2: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.
Instructions for updating:
Please write your own downloading logic.
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting ./mnist/data/train-images-idx3-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.data to implement this functionality.
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use tf.one_hot on tensors.
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz
WARNING:tensorflow:From /anaconda3/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.
Instructions for updating:
Please use alternatives such as official/mnist/dataset.py from tensorflow/models.


model setting

  • image = 28X28, 784 attribute
  • label = 0, 1, ,2 ,3 ,4 ,5, 6, 7, 8, 9
In [3]:
global_step = tf.Variable(0, trainable=False, name="global_step")
X = tf.placeholder(tf.float32, [None, 784], name="X")
Y = tf.placeholder(tf.float32, [None, 10], name="Y")

minibatch

이미지를 하나씩 학습시키는 것보다 여러 개를 한꺼번에 학습시키는 쪽이 효과가 좋음.(많은 컴퓨팅 자원이 뒷받침 될 때)
따라서 일반적으로 데이터를 적당한크기로 잘라서 학습 --> 미니배치minibatch

  • placeholder에서 [None, 784]는 한 번에 학습시킬 이미지의 갯수를 지정 -- minibatch
  • 원하는 크기로 지정할 수도 있지만 학습할 갯수를 바꿔가면서 진행할 때는 "None"으로 넣어주면 tensorflow가 계산함

784(입력, 특징수) -> 256(first hidden layer) -> 256(second hidden layer) -> 10 (output 0-9 분류 갯수)

In [4]:
W1 = tf.Variable(tf.random_normal([784, 256], mean=0, stddev=1), name="var1")
W2 = tf.Variable(tf.random_normal([256, 256], mean=0, stddev=1), name="var2")
W3 = tf.Variable(tf.random_normal([256, 10],  mean=0, stddev=1), name="var3")

b1 = tf.zeros([256], name="bias1")
b2 = tf.zeros([256], name="bias2")
b3 = tf.zeros([10],  name="bias3")
In [5]:
with tf.name_scope("layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.relu(L1)

with tf.name_scope("layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.relu(L2)
    
with tf.name_scope("layer3"):
    model = tf.add(tf.matmul(L2, W3), b3)    
In [6]:
with tf.name_scope("opt"):
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01).minimize(cost, global_step=global_step)
    
    tf.summary.scalar("cost", cost)
In [7]:
init = tf.global_variables_initializer()
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())
sess.run(init)


merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_01", sess.graph)

batch_size = 100
total_batch = int(mnist.train.num_examples / batch_size)

MNIST는 데이터가 수만 개로 매우 크므로 학습에 미니배치 사용

  • 미니배치의 크기를 100개로 설정
  • mnist.train.num_examples를 배치크기로 나눠 미니배치가 총 몇 개인지를 저장

그리고 MNIST 데이터 전체를 학습하는 일을 총 15번 반복
학습 데이터 전체를 한 바퀴 도는 것을 에포치epoch라 함

In [8]:
for epoch in range(15):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        
        _, cost_val = sess.run([optimizer, cost], feed_dict={X:batch_xs, Y:batch_ys})
        total_cost += cost_val
        
        summary = sess.run(merged, feed_dict={X:batch_xs, Y:batch_ys})
        writer.add_summary(summary, global_step=sess.run(global_step))
        
    print("Epoch: {}, Avg.cost = {:.3f}".format(epoch+1, total_cost / total_batch))
Epoch: 1, Avg.cost = 53.018
Epoch: 2, Avg.cost = 8.887
Epoch: 3, Avg.cost = 4.864
Epoch: 4, Avg.cost = 3.215
Epoch: 5, Avg.cost = 2.577
Epoch: 6, Avg.cost = 2.208
Epoch: 7, Avg.cost = 2.240
Epoch: 8, Avg.cost = 1.909
Epoch: 9, Avg.cost = 1.490
Epoch: 10, Avg.cost = 1.574
Epoch: 11, Avg.cost = 1.440
Epoch: 12, Avg.cost = 1.100
Epoch: 13, Avg.cost = 0.913
Epoch: 14, Avg.cost = 1.022
Epoch: 15, Avg.cost = 0.742
In [9]:
# import os

# if not os.path.isdir("./model/mnist_01"):
#     os.mkdir("./model/mnist_01")

# saver.save(sess, "./model/mnist_01/dnn/ckpt", global_step=global_step)
# print("optimize complete!")
In [10]:
is_correct = tf.equal(tf.argmax(model, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: {:.4f}".format(sess.run(accuracy, feed_dict={X:mnist.test.images, 
                                                     Y:mnist.test.labels})))
accuracy: 0.9612
In [11]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

10.mnist_dropout  (0) 2018.12.10
00.write_csv  (0) 2018.12.09
08.tensorboard03_example  (0) 2018.12.09
07.tensorboard02_example  (0) 2018.12.09
06.tensorboard01_example  (0) 2018.12.09
08_tensorboard03_example
In [1]:
import tensorflow as tf
import pandas as pd
import numpy as np

import warnings
warnings.filterwarnings("ignore")

import os
In [2]:
# set working directory
HOME = os.getenv("HOME")
WORKDIR = os.path.join(HOME, "python", "deep_learning", "tensorflow")
os.chdir(WORKDIR)
In [3]:
# load_data
cancer = pd.read_csv("./datas/cancer.csv")

# cancer_target_names = np.unique(cancer.result.values)
# cancer_target_names = cancer_target_names[[1, 0]] 

# for i, n in enumerate(cancer_target_names):
#     cancer.replace(to_replace=cancer_target_names[i], value=i, inplace=True)
In [4]:
# data split
from sklearn.model_selection import train_test_split

train_set, test_set = train_test_split(cancer, test_size=0.2, random_state=0)
train_labels = train_set["result"].values
test_labels  = test_set["result"].values
In [5]:
# preprocessing
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.impute import SimpleImputer
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion

class DataframeSelector(BaseEstimator, TransformerMixin):
    def __init__(self, attr_list):
        self.attr_list = attr_list
        
    def fit(self, X, y=None):
        return self
    
    def transform(self, X):
        return X.iloc[:, self.attr_list].values
In [6]:
n_list = range(train_set.shape[1]-1)
c_list = [train_set.shape[1]-1]
In [7]:
num_pipeline = Pipeline([
    ["selector", DataframeSelector(n_list)],
    ["imputer", SimpleImputer(strategy="median")],
    ["scaler", StandardScaler()]
])

cat_pipeline = Pipeline([
    ["selector", DataframeSelector(c_list)],
    ["encoder", OneHotEncoder(sparse=False)]
])

full_pipeline = FeatureUnion(transformer_list=[
    ["nums", num_pipeline],
    ["cats", cat_pipeline]
])
In [8]:
scaled_train = full_pipeline.fit_transform(train_set)
scaled_test  = full_pipeline.fit_transform(test_set)
In [9]:
x_train, y_train = scaled_train[:, :30], scaled_train[:, 30:] 
x_test, y_test = scaled_test[:, :30], scaled_test[:, 30:]
In [10]:
x_train = x_train.astype("float32")
y_train = y_train.astype("float32")
In [11]:
global_step = tf.Variable(0, trainable=False, name="global_step")
In [12]:
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")
In [13]:
# 30, 2
W1 = tf.Variable(tf.random_normal([30, 10], mean=0, stddev=1))
W2 = tf.Variable(tf.random_normal([10, 100], mean=0, stddev=1))
W3 = tf.Variable(tf.random_normal([100, 500], mean=0, stddev=1))
W4 = tf.Variable(tf.random_normal([500, 2], mean=0, stddev=1))

b1 = tf.zeros([10])
b2 = tf.zeros([100])
b3 = tf.zeros([500])
b4 = tf.zeros([2])
In [14]:
with tf.name_scope("layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.sigmoid(L1)

with tf.name_scope("layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.sigmoid(L2)

with tf.name_scope("layer3"):
    L3 = tf.add(tf.matmul(L2, W3), b3)
    L3 = tf.nn.sigmoid(L3)   
    
with tf.name_scope("layer4"):
    model = tf.add(tf.matmul(L3, W4), b4)    
In [15]:
with tf.name_scope("optimizer"):
    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, global_step=global_step)
    
    tf.summary.scalar("cost", cost)
#     tf.summary.scalar("W1", W1)
#     tf.summary.scalar("W2", W2)
#     tf.summary.scalar("W3", W3)
#     tf.summary.scalar("W4", W4)

#     tf.summary.scalar("b1", b1)
#     tf.summary.scalar("b2", b2)
#     tf.summary.scalar("b3", b3)
#     tf.summary.scalar("b4", b4)            
In [16]:
# tf.reset_default_graph()
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())
In [17]:
ckpt = tf.train.get_checkpoint_state("./model/cancer")
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    saver.restore(sess, ckpt.model_checkpoint_path)
    
else:
    init = tf.global_variables_initializer()
    sess.run(init)
INFO:tensorflow:Restoring parameters from ./model/cancer/dnn.ckpt-5000
In [18]:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/cancer", sess.graph)
In [19]:
for step in range(5000):
    sess.run(train_op, feed_dict={X:x_train, Y:y_train})
    
    if (step+1) % 200 == 0:
        print("step: {}, cost: {}".\
             format(sess.run(global_step),
                    sess.run(cost, feed_dict={X:x_train, Y:y_train})))
        
    summary = sess.run(merged, feed_dict={X:x_train, Y:y_train})   
    writer.add_summary(summary, global_step=sess.run(global_step))
    
saver.save(sess, "./model/cancer/dnn.ckpt", global_step=global_step)    
step: 5200, cost: 2.7666922619573597e-07
step: 5400, cost: 2.444436688620044e-07
step: 5600, cost: 2.1798199156819464e-07
step: 5800, cost: 1.9518827798492566e-07
step: 6000, cost: 1.747525146811313e-07
step: 6200, cost: 1.5667471586766624e-07
step: 6400, cost: 1.4069287601614633e-07
step: 6600, cost: 1.247110219537717e-07
step: 6800, cost: 1.1213514028440841e-07
step: 7000, cost: 1.0191723021080179e-07
step: 7200, cost: 9.196132566557935e-08
step: 7400, cost: 8.200541401492956e-08
step: 7600, cost: 7.51934692289069e-08
step: 7800, cost: 6.759553627944115e-08
step: 8000, cost: 6.052158596503432e-08
step: 8200, cost: 5.554362658699574e-08
step: 8400, cost: 4.925567154145938e-08
step: 8600, cost: 4.427770861070712e-08
step: 8800, cost: 4.139572951089576e-08
step: 9000, cost: 3.694175632062979e-08
step: 9200, cost: 3.353578392761847e-08
step: 9400, cost: 3.065380482780711e-08
step: 9600, cost: 2.7509825528682086e-08
step: 9800, cost: 2.541383992138435e-08
step: 10000, cost: 2.3317852537729777e-08
Out[19]:
'./model/cancer/dnn.ckpt-10000'
In [20]:
prediction = tf.argmax(model, 1)
target = tf.argmax(Y, 1)
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))

print("=================================")
print("train_prediction: \n{}".format(sess.run(prediction, feed_dict={X:x_train, Y:y_train})))
print("train_target: \n{}".format(sess.run(target, feed_dict={X:x_train, Y:y_train})))
print("train_accuracy: \n{:.3f}".format(sess.run(accuracy*100, feed_dict={X:x_train, Y:y_train})))

print("\n=================================")
print("test_prediction: \n{}".format(sess.run(prediction, feed_dict={X:x_test, Y:y_test})))
print("test_target: \n{}".format(sess.run(target, feed_dict={X:x_test, Y:y_test})))
print("test_accuracy: \n{:.3f}".format(sess.run(accuracy*100, feed_dict={X:x_test, Y:y_test})))
=================================
train_prediction: 
[0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 0
 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0
 1 0 0 1 1 0 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0
 0 0 1 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 0 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 1 1 0 1 1 0
 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1
 1 1 1 0 1 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 0 1 0 1
 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0
 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 1
 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 0 1 0 0 1
 1 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1
 0 0 0 0 0 1 1 1 0 0 0]
train_target: 
[0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 1 0 0 1 0 1 0 0 0
 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1 0 0 0 0
 1 0 0 1 1 0 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 0 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0
 0 0 1 0 1 0 0 0 1 0 0 0 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 0 1
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 1 0 1 1 0 1 1 0
 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 0 1 0 1 0 0 0 0 1
 1 1 1 0 1 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 1 0 0 0 1 1 0 0 1 0 0 0 1 0 1 0 1
 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 1 0 0 1 0 0 0 1 1 0 0 0 0 0
 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 1
 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 0 1 1 0 0 0 1 0 0 0 0 1 0 1 1 1 0 1 0 1 0 1
 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 1 1 1 0 1 0 0 1
 1 1 1 0 1 1 1 0 1 0 1 0 0 1 1 0 1 0 0 0 0 1 0 0 1 0 0 0 1 1 0 0 0 1 0 0 1
 0 0 0 0 0 1 1 1 0 0 0]
train_accuracy: 
100.000

=================================
test_prediction: 
[1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0
 1 0 1 1 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 0 1
 1 0 0 0 0 0 1 1 1 0 1 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 0
 1 1 0]
test_target: 
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 1 0
 1 0 1 1 0 1 0 0 1 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1 1 0 0 1 0 1 1 1 0 0 1 0 1
 1 0 0 0 0 0 1 1 1 0 1 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 1 0
 1 1 0]
test_accuracy: 
98.246
In [21]:
# tensorboard --logdir=./logs/cancer
In [22]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

00.write_csv  (0) 2018.12.09
09.mnist_01_minibatch  (0) 2018.12.09
07.tensorboard02_example  (0) 2018.12.09
06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
07_tensorboard02_example
In [1]:
#!/anaconda3/envs/py36/bin/python3
import numpy as np
import pandas as pd
import tensorflow as tf

import warnings
warnings.filterwarnings("ignore")
In [2]:
iris = pd.read_csv("./datas/iris.csv")
In [3]:
iris_target_names = np.unique(iris["species"].values)
In [4]:
for i, n in enumerate(iris_target_names):
    iris.replace(to_replace=iris_target_names[i], value=i, inplace=True)
In [5]:
from sklearn.model_selection import train_test_split

train_set, test_set = train_test_split(iris, test_size=0.2, random_state=42)

train_labels = train_set["species"].values
test_labels = test_set["species"].values
In [6]:
import matplotlib.pyplot as plt

%matplotlib inline
train_set.plot(kind="scatter", x="sepal_length", y="sepal_width", alpha=0.4, figsize=(10, 8), c="species", cmap=plt.get_cmap("jet"), colorbar=True, sharex=False)
plt.show()
In [7]:
train_set.hist(bins=50, figsize=(20, 15))
plt.show()
In [8]:
from sklearn.preprocessing import OneHotEncoder, StandardScaler, LabelEncoder
from sklearn.impute import SimpleImputer
from sklearn.base import BaseEstimator, TransformerMixin
from sklearn.pipeline import Pipeline, FeatureUnion

class DataFrameSelector(BaseEstimator, TransformerMixin):
    def __init__(self, lists):
        self.lists = lists
        
    def fit(self, X, y=None):
        return self
    
    def transform(self, X):
        return X.iloc[:, self.lists].values
    
# class LabelEncoders(BaseEstimator, TransformerMixin):
#     def __init__(self):
#         self.encoder = LabelEncoder()
        
#     def fit(self, X, y=None):
#         self.encoder.fit(X)
#         return self
    
#     def transform(self, X, y=None):
#         return self.encoder.transform(X)
In [9]:
n_list = [0, 1, 2, 3]
e_list = [4]
In [10]:
train_set["species"] = train_set["species"].astype(str)
In [11]:
num_pipeline = Pipeline([
    ["selector", DataFrameSelector(lists=n_list)],
    ["imputer" , SimpleImputer(strategy="median")],
    ["scaler"  , StandardScaler()]
])

encoding_pipeline = Pipeline([
    ["selector", DataFrameSelector(lists=e_list)],
    ["encoder", OneHotEncoder(sparse=False, categories="auto")]
])

full_pipeline = FeatureUnion(transformer_list=[
    ["nums", num_pipeline],
    ["encoding", encoding_pipeline]
])
In [12]:
scaled_train = full_pipeline.fit_transform(train_set)
scaled_test  = full_pipeline.fit_transform(test_set)
In [13]:
x_train, y_train = scaled_train[:, :4], scaled_train[:, 4:]
x_test, y_test = scaled_test[:, :4], scaled_test[:, 4:]
In [14]:
global_step = tf.Variable(0, trainable=False, name="global_step")
In [15]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)
In [16]:
# 4, 1
W1 = tf.Variable(tf.random_normal([4, 10], mean=0, stddev=1))
W2 = tf.Variable(tf.random_normal([10, 100], mean=0, stddev=1))
W3 = tf.Variable(tf.random_normal([100, 3], mean=0, stddev=1))

b1 = tf.zeros([10])
b2 = tf.zeros([100])
b3 = tf.zeros([3])
In [17]:
# Layer 1
with tf.name_scope("Layer1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.sigmoid(L1)
    
with tf.name_scope("Layer2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.sigmoid(L2)
    
with tf.name_scope("Layer3"):
    model = tf.add(tf.matmul(L2, W3), b3)
    model = tf.nn.sigmoid(model)
In [18]:
with tf.name_scope("optiimizer"):
    cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model) + (1-Y)*tf.log(1-model)))
    optimizer = tf.train.AdadeltaOptimizer(learning_rate=0.01)
    train_op = optimizer.minimize(cost, global_step=global_step)
    
    tf.summary.scalar("cost", cost)
    tf.summary.histogram("Weight1", W1)
    tf.summary.histogram("Weight2", W2)
    tf.summary.histogram("Weight3", W3)
    tf.summary.histogram("bias1", b1)
    tf.summary.histogram("bias2", b2)
    tf.summary.histogram("bias3", b3)
In [19]:
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())
In [20]:
ckpt = tf.train.get_checkpoint_state("./model/iris")
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    saver.restore(sess, ckpt.model_checkpoint_path)
    
else:
    sess.run(tf.global_variables_initializer())
INFO:tensorflow:Restoring parameters from ./model/iris/dnn.ckpt-5000
In [21]:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/iris", sess.graph)
In [22]:
for step in range(5000):
    sess.run(train_op, feed_dict={X:x_train, Y:y_train})
    
    if (step+1) % 200 == 0:
        print("step: {}, cost: {:.5f}".\
             format(sess.run(global_step),
                    sess.run(cost, feed_dict={X:x_train, Y:y_train})))
        
    summary = sess.run(merged, feed_dict={X:x_train, Y:y_train})
    writer.add_summary(summary, global_step=sess.run(global_step))
    
saver.save(sess, "./model/iris/dnn.ckpt", global_step=global_step)
step: 5200, cost: 550.79474
step: 5400, cost: 511.44778
step: 5600, cost: 472.62866
step: 5800, cost: 434.81265
step: 6000, cost: 398.32742
step: 6200, cost: 362.83173
step: 6400, cost: 326.22653
step: 6600, cost: 287.97693
step: 6800, cost: 256.19949
step: 7000, cost: 231.45538
step: 7200, cost: 211.00574
step: 7400, cost: 193.48444
step: 7600, cost: 178.29463
step: 7800, cost: 165.20464
step: 8000, cost: 154.06615
step: 8200, cost: 144.64468
step: 8400, cost: 136.65207
step: 8600, cost: 129.83101
step: 8800, cost: 123.97330
step: 9000, cost: 118.91176
step: 9200, cost: 114.51005
step: 9400, cost: 110.65558
step: 9600, cost: 107.25186
step: 9800, cost: 104.21504
step: 10000, cost: 101.47087
Out[22]:
'./model/iris/dnn.ckpt-10000'
In [23]:
# print("{}".format(sess.run(model, feed_dict={X:x_scaled_test, Y:y_test})))

prediction = tf.argmax(model, 1)
target = tf.argmax(Y, 1)
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))


print("====================================")
print("train_prediction: \n{}".format(sess.run(prediction, feed_dict={X: x_train})))
print("train_target: \n{}\n".format(sess.run(target, feed_dict={Y:y_train})))
print("\naccuracy: \n{:.3f}%".format(sess.run(accuracy*100, 
                                              feed_dict={X: x_train, Y:y_train})))

print("\n====================================")
print("test_prediction: \n{}".format(sess.run(prediction, feed_dict={X: x_test})))
print("test_target: \n{}".format(sess.run(target, feed_dict={Y:y_test})))


print("\naccuracy: \n{:.3f}%".format(sess.run(accuracy*100, 
                                              feed_dict={X: x_test, Y:y_test})))
====================================
train_prediction: 
[0 0 2 0 0 2 2 0 0 0 2 2 2 0 0 1 2 2 2 2 1 2 1 0 2 1 0 0 0 1 2 0 0 0 1 0 1
 2 0 1 2 0 2 2 1 1 2 1 0 1 2 0 0 1 2 0 2 0 0 2 1 2 1 1 2 1 0 0 1 2 0 0 0 1
 2 0 2 2 0 1 2 2 2 2 0 2 1 2 1 1 2 1 2 1 0 1 2 2 0 1 2 2 0 2 0 2 2 2 1 2 1
 1 1 2 0 1 1 0 1 2]
train_target: 
[0 0 1 0 0 2 1 0 0 0 2 1 1 0 0 1 2 2 1 2 1 2 1 0 2 1 0 0 0 1 2 0 0 0 1 0 1
 2 0 1 2 0 2 2 1 1 2 1 0 1 2 0 0 1 1 0 2 0 0 1 1 2 1 2 2 1 0 0 2 2 0 0 0 1
 2 0 2 2 0 1 1 2 1 2 0 2 1 2 1 1 1 0 1 1 0 1 2 2 0 1 2 2 0 2 0 1 2 2 1 2 1
 1 2 2 0 1 2 0 1 2]


accuracy: 
85.833%

====================================
test_prediction: 
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 2 2 1 1 2 0 2 0 2 2 2 2 2 0 0]
test_target: 
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]

accuracy: 
96.667%
In [24]:
# tensorboard --logdir=./logs/iris
In [25]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

09.mnist_01_minibatch  (0) 2018.12.09
08.tensorboard03_example  (0) 2018.12.09
06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
06_tensorboard01_example
In [1]:
import numpy as np
import pandas as pd
import tensorflow as tf
In [2]:
data = pd.read_csv("./datas/classification_ex.csv", dtype="float32").values
In [3]:
x_data = data[:, 0:2]
y_data = data[:, 2:]


model save

  • global_step -> trainable=False
In [4]:
global_step = tf.Variable(0, trainable=False, name="global_step")


model setting

In [5]:
# ground truth
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

# weight, bias
W1 = tf.Variable(tf.random_normal([2, 10],  mean=0, stddev=1))
W2 = tf.Variable(tf.random_normal([10, 20], mean=0, stddev=1))
W3 = tf.Variable(tf.random_normal([20, 3],  mean=0, stddev=1))

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

# Layer 1
with tf.name_scope("Layer_1"):
    L1 = tf.add(tf.matmul(X, W1), b1)
    L1 = tf.nn.sigmoid(L1)

# Layer 2
with tf.name_scope("Layer_2"):
    L2 = tf.add(tf.matmul(L1, W2), b2)
    L2 = tf.nn.sigmoid(L2)

# Layer 3
with tf.name_scope("Layer_3"):
    model = tf.add(tf.matmul(L2, W3), b3)
    model = tf.nn.softmax(model)

    
# cost function
with tf.name_scope("optimizer"):
    cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model) + (1-Y)*tf.log(1-model)))
    optimizer = tf.train.AdamOptimizer(learning_rate=0.01)
    train_op = optimizer.minimize(cost, global_step=global_step)
    tf.summary.scalar("cost", cost)
    tf.summary.histogram("Weight1", W1)
    tf.summary.histogram("Weight2", W2)
    tf.summary.histogram("Weight3", W3)
    tf.summary.histogram("bais1", b1)
    tf.summary.histogram("bais2", b2)
    tf.summary.histogram("bais3", b3)


create session

In [6]:
sess = tf.Session()
saver = tf.train.Saver(tf.global_variables())

tf.global_variables()는 앞서 정의한 변수들을 가져오는 함수

make checkpoint

In [7]:
ckpt = tf.train.get_checkpoint_state("./model")
if ckpt and tf.train.checkpoint_exists(ckpt.model_checkpoint_path):
    saver.restore(sess, ckpt.model_checkpoint_path)
    
else:
    sess.run(tf.global_variables_initializer())

tf.summary.merge_all 함수로 앞서 지정한 텐서들을 수집한 다음 tf.summary.FileWriter 함수를 이용해 그래프와 텐서들의 값을 저장할 디렉토리를 설정

In [8]:
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs", sess.graph)


running model

In [9]:
for step in range(1000):
    sess.run(train_op, feed_dict={X:x_data, Y:y_data})
    
    if (step+1) % 50 == 0:    
        print("step: {}, cost: {:.5f}".\
              format(sess.run(global_step), 
                     sess.run(cost, feed_dict={X:x_data, Y:y_data})))
        
    summary = sess.run(merged, feed_dict={X:x_data, Y:y_data})
    writer.add_summary(summary, global_step=sess.run(global_step))
step: 50, cost: 6.33244
step: 100, cost: 2.69545
step: 150, cost: 0.85424
step: 200, cost: 0.35161
step: 250, cost: 0.19246
step: 300, cost: 0.12306
step: 350, cost: 0.08628
step: 400, cost: 0.06428
step: 450, cost: 0.04998
step: 500, cost: 0.04012
step: 550, cost: 0.03300
step: 600, cost: 0.02768
step: 650, cost: 0.02359
step: 700, cost: 0.02037
step: 750, cost: 0.01779
step: 800, cost: 0.01568
step: 850, cost: 0.01393
step: 900, cost: 0.01246
step: 950, cost: 0.01122
step: 1000, cost: 0.01016
In [10]:
saver.save(sess, "./model/dnn.ckpt", global_step=global_step)
Out[10]:
'./model/dnn.ckpt-1000'
In [11]:
prediction = tf.argmax(model, 1)
target = tf.argmax(Y, 1)
print("prediction: \t{}".format(sess.run(prediction, feed_dict={X: x_data})))
print("target: \t{}".format(sess.run(target, feed_dict={Y:y_data})))

is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("\naccuracy: \t{:.3f}%".format(sess.run(accuracy*100, feed_dict={X: x_data, Y: y_data})))
prediction: 	[2 0 1 2 2 1]
target: 	[2 0 1 2 2 1]

accuracy: 	100.000%
In [12]:
# tensorboard --logdir=./logs
In [13]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))


'Deep_Learning' 카테고리의 다른 글

08.tensorboard03_example  (0) 2018.12.09
07.tensorboard02_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
05_deep_neural_net_Costfun2

review

In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# data_define

# data_define
# [털, 날개]
x_data = np.array([
    [0, 0],
    [1, 0],
    [1, 1],
    [0, 0],
    [0, 0],
    [0, 1]
])

# [기타, 포유류, 조류]
y_data = np.array([
    [1, 0, 0], # 기타
    [0, 1, 0], # 포유류
    [0, 0, 1], # 조류
    [1, 0, 0], # 기타
    [1, 0, 0], # 기타
    [0, 0, 1]  # 조류
])


model setting

In [3]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

W1 = tf.Variable(tf.random_normal([2, 10], mean=0, stddev=1))
W2 = tf.Variable(tf.random_normal([10, 3], mean=0, stddev=1))

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

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

model = tf.add(tf.matmul(L1, W2), b2)
model = tf.nn.softmax(model)


cost function

  • one-hot encoding을 이용한 대부분의 모델에서는 cross-entropy를 사용
    $E(w,\quad b)\quad =\quad -\sum _{ n=1 }^{ N } \left\{ { t_{ n }logy_{ n }+(1-t_{ n })log(1-y_{ n }) } \right\} $
In [4]:
cost = tf.reduce_mean(
    tf.nn.softmax_cross_entropy_with_logits_v2(labels=Y, logits=model))


modeling

In [5]:
optimizier = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizier.minimize(cost)
In [6]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for step in range(1000):
    sess.run(train_op, feed_dict={X: x_data, Y:y_data})
    
    if (step+1) % 50 == 0:
        print("{}, {:.5f}".format(step+1, sess.run(cost, feed_dict={X: x_data, Y:y_data})))
50, 1.15160
100, 1.14459
150, 1.13681
200, 1.12806
250, 1.11820
300, 1.10710
350, 1.09478
400, 1.08139
450, 1.06730
500, 1.05302
550, 1.03913
600, 1.02610
650, 1.01422
700, 1.00354
750, 0.99399
800, 0.98538
850, 0.97755
900, 0.97032
950, 0.96356
1000, 0.95717


output

In [7]:
sess.run(model, feed_dict={X: x_data, Y:y_data})
Out[7]:
array([[0.64512706, 0.08173119, 0.2731418 ],
       [0.37277853, 0.06834452, 0.55887693],
       [0.3646471 , 0.05929578, 0.57605714],
       [0.64512706, 0.08173119, 0.2731418 ],
       [0.64512706, 0.08173116, 0.2731418 ],
       [0.51281446, 0.0757072 , 0.41147828]], dtype=float32)
In [8]:
prediction = tf.argmax(model, axis=1)
target = tf.argmax(Y, axis=1)

print("prediction: \t{}".format(sess.run(prediction, feed_dict={X:x_data})))
print("target: \t{}".format(sess.run(target, feed_dict={Y:y_data})))
prediction: 	[0 2 2 0 0 0]
target: 	[0 1 2 0 0 2]


accuracy

In [9]:
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: \t{:.3f}".format(sess.run(accuracy, feed_dict={X:x_data, Y:y_data})))
accuracy: 	0.667
In [10]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

07.tensorboard02_example  (0) 2018.12.09
06.tensorboard01_example  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
04_deep_neural_net_Costfun1

review

In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# data_define
# [털, 날개]
x_data = np.array([
    [0, 0],
    [1, 0],
    [1, 1],
    [0, 0],
    [0, 0],
    [0, 1]
])

# [기타, 포유류, 조류]
y_data = np.array([
    [1, 0, 0], # 기타
    [0, 1, 0], # 포유류
    [0, 0, 1], # 조류
    [1, 0, 0], # 기타
    [1, 0, 0], # 기타
    [0, 0, 1]  # 조류
])

## model setting

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


add layer

In [3]:
# weighted
W1 = tf.Variable(tf.random_normal([2, 10], mean=0, stddev=1)) # [특징, hidden_layer]
W2 = tf.Variable(tf.random_normal([10, 3], mean=0, stddev=1)) # [hidden_layer, output]

# biased
b1 = tf.Variable(tf.zeros([10])) # ["hidden layer"]
b2 = tf.Variable(tf.zeros([3]))  # [output]

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

model = tf.add(tf.matmul(L1, W2), b2)
model = tf.nn.softmax(model)


cost function

  • one-hot encoding을 이용한 대부분의 모델에서는 cross-entropy를 사용
    $E(w,\quad b)\quad =\quad -\sum _{ n=1 }^{ N } \left\{ { t_{ n }logy_{ n }+(1-t_{ n })log(1-y_{ n }) } \right\} $
In [4]:
cost = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model) + (1-Y)*tf.log(1-model), axis=1))


modeling

In [5]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(cost)
In [6]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for step in range(1000):
    sess.run(train_op, feed_dict={X:x_data, Y:y_data})
    
    if (step+1) % 50 == 0:
        print("{}, {:.5f}".format(step+1, 
                                 sess.run(cost, feed_dict={X: x_data, Y: y_data})))
50, 1.66444
100, 1.45811
150, 1.34925
200, 1.26442
250, 1.19221
300, 1.12926
350, 1.07365
400, 1.02398
450, 0.97921
500, 0.93851
550, 0.90123
600, 0.86685
650, 0.83496
700, 0.80520
750, 0.77730
800, 0.75101
850, 0.72614
900, 0.70253
950, 0.68003
1000, 0.65854


output

In [7]:
sess.run(model, feed_dict={X:x_data})
Out[7]:
array([[0.86290747, 0.08460085, 0.05249172],
       [0.408934  , 0.25658685, 0.33447912],
       [0.03734298, 0.09417698, 0.86848   ],
       [0.86290747, 0.08460085, 0.05249172],
       [0.86290747, 0.08460083, 0.05249173],
       [0.12566108, 0.106323  , 0.7680159 ]], dtype=float32)
In [8]:
prediction = tf.argmax(model, axis=1)
target = tf.argmax(Y, axis=1)

print("prediction: \t{}".format(sess.run(prediction, feed_dict={X:x_data})))
print("target: \t{}".format(sess.run(target, feed_dict={Y:y_data})))
prediction: 	[0 0 2 0 0 2]
target: 	[0 1 2 0 0 2]


accuracy

In [9]:
is_correct = tf.equal(prediction, target)
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: {:.3f}".format(sess.run(accuracy, feed_dict={X:x_data, Y:y_data})))
accuracy: 0.833
In [10]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
03.classification  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09
03_classification

활성화함수

@activation_fucntion
activation function은 인공신경망을 통과해온 값을 최종적으로 어떤 값으로 만들지를 결정 대표적으로 Sigmoid, ReLU, tanh함수가 있음


backpropagation

@backpropagation
출력층이 내놓은 결과의 오차를 입력층까지 전달하며 가중치를 계산


classification model

In [1]:
import tensorflow as tf
import numpy as np
In [2]:
# [털, 날개]
x_data = np.array([
    [0, 0],
    [1, 0],
    [1, 1],
    [0, 0],
    [0, 0],
    [0, 1]
])

# [기타, 포유류, 조류]
y_data = np.array([
    [1, 0, 0], # 기타
    [0, 1, 0], # 포유류
    [0, 0, 1], # 조류
    [1, 0, 0], # 기타
    [1, 0, 0], # 기타
    [0, 0, 1]  # 조류
])

#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [1, 0] -> [0, 1, 0] # 털o, 날개x -> 포유류
#     [1, 1] -> [0, 0, 1] # 털o, 날개o -> 조류
#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [0, 0] -> [1, 0, 0] # 털x, 날개x -> 기타
#     [0, 1] -> [0, 0, 1] # 털x, 날개o -> 조류


neuralnet model setting

X, Y는 실측값groud truth를 넣어서 학습 -> placeholder

In [3]:
X = tf.placeholder(tf.float32)
Y = tf.placeholder(tf.float32)

# weith와 bias 설정
W = tf.Variable(tf.random_uniform([2, 3], -1., 1.)) # 입력, 출력
b = tf.Variable(tf.zeros([3]))

L = tf.add(tf.matmul(X, W), b)
L = tf.nn.relu(L)

# 출력값을 softmax 함수를 이용하여 다듬어줌
# softmax는 결과값들을 전체 합이 1이 되도록 만들어줌 ==> 확률로 해석 가능
model = tf.nn.softmax(L)


cost function

  • one-hot encoding을 이용한 대부분의 모델에서는 cross-entropy를 사용
    $E(w,\quad b)\quad =\quad -\sum _{ n=1 }^{ N } \left\{ { t_{ n }logy_{ n }+(1-t_{ n })log(1-y_{ n }) } \right\} $
In [4]:
cost =tf.reduce_mean(-tf.reduce_sum(Y*tf.log(model) + (1-Y)*tf.log(1 - model), axis=1))


modeling

In [5]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(cost)

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)

for step in range(1000):
    sess.run(optimizer, feed_dict={X:x_data, Y:y_data})
    
    if (step+1) % 50 == 0:
        print("{}, {}".format(step+1, 
                             sess.run(cost, feed_dict={X:x_data, Y:y_data})))
50, 1.9566283226013184
100, 1.9450751543045044
150, 1.9336179494857788
200, 1.9222602844238281
250, 1.911005973815918
300, 1.899857997894287
350, 1.888819694519043
400, 1.8778947591781616
450, 1.8670859336853027
500, 1.856396198272705
550, 1.8458284139633179
600, 1.835384726524353
650, 1.8250678777694702
700, 1.8148797750473022
750, 1.804822325706482
800, 1.7948967218399048
850, 1.7851048707962036
900, 1.7754473686218262
950, 1.7659250497817993
1000, 1.7565385103225708


output

예측값인 model을 바로 출력하면 확률로 나오기 때문에 요소중 가장 큰 값의 인덱스를 찾아주는 argmax()를 이용

In [6]:
sess.run(model, feed_dict={X:x_data})
Out[6]:
array([[0.3319709 , 0.3319709 , 0.33605826],
       [0.2515205 , 0.45800376, 0.29047582],
       [0.21185754, 0.42796907, 0.36017334],
       [0.3319709 , 0.3319709 , 0.33605826],
       [0.3319709 , 0.3319709 , 0.33605826],
       [0.29241043, 0.2897259 , 0.4178636 ]], dtype=float32)
In [7]:
prediction = tf.argmax(model, axis=1)
target = tf.argmax(Y, axis=1)

print("prediction: {}".format(sess.run(prediction, feed_dict={X:x_data})))
print("ground_truth: {}".format(sess.run(target, feed_dict={Y:y_data})))
prediction: [2 1 1 2 2 2]
ground_truth: [0 1 2 0 0 2]


accuracy

In [8]:
is_correct = tf.equal(prediction, target)

# tf.cast : True, False -> 1, 0
accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32))
print("accuracy: {:.3f}".format(sess.run(accuracy, feed_dict= {X:x_data, Y:y_data})))
accuracy: 0.333
In [9]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09
02_linear_regression

선형 회귀|center

  • 주어진 x와 y 값을 가지고 서로 간의 관계를 파악
  • 새로운 x값이 주어졌을 때 y값을 쉽게 알 수 있음
In [1]:
import tensorflow as tf
In [2]:
x_data = [1, 2, 3]
y_data = [1, 2, 3]


data 생성

In [3]:
W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.random_uniform([1], -1.0, 1.0))


placeholder 설정

In [4]:
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")


model 설정

In [5]:
model = W*X + b


cost function

In [6]:
cost = tf.reduce_mean(tf.square(model- Y))


gradient desent

In [7]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1)
train_op = optimizer.minimize(cost)

@경사하강법|center


modeling

In [8]:
sess =  tf.Session()
sess.run(tf.global_variables_initializer())
for step in range(500):
    _, cost_val = sess.run([train_op, cost], feed_dict={X: x_data, Y: y_data})
    if step % 25 == 0:
        print("step: {}, cost_val: {:.5f}, W: {}, b: {}".format(step, cost_val, sess.run(W), sess.run(b)))
step: 0, cost_val: 0.45853, W: [0.71138144], b: [0.7272724]
step: 25, cost_val: 0.02285, W: [0.828638], b: [0.389546]
step: 50, cost_val: 0.00677, W: [0.9067342], b: [0.2120151]
step: 75, cost_val: 0.00201, W: [0.9492389], b: [0.11539178]
step: 100, cost_val: 0.00059, W: [0.97237265], b: [0.06280342]
step: 125, cost_val: 0.00018, W: [0.9849635], b: [0.03418154]
step: 150, cost_val: 0.00005, W: [0.99181616], b: [0.01860373]
step: 175, cost_val: 0.00002, W: [0.99554586], b: [0.01012532]
step: 200, cost_val: 0.00000, W: [0.9975758], b: [0.00551084]
step: 225, cost_val: 0.00000, W: [0.9986806], b: [0.00299932]
step: 250, cost_val: 0.00000, W: [0.99928194], b: [0.00163241]
step: 275, cost_val: 0.00000, W: [0.9996092], b: [0.00088852]
step: 300, cost_val: 0.00000, W: [0.9997873], b: [0.00048356]
step: 325, cost_val: 0.00000, W: [0.9998842], b: [0.00026316]
step: 350, cost_val: 0.00000, W: [0.99993694], b: [0.00014328]
step: 375, cost_val: 0.00000, W: [0.9999657], b: [7.794145e-05]
step: 400, cost_val: 0.00000, W: [0.99998134], b: [4.2393247e-05]
step: 425, cost_val: 0.00000, W: [0.9999898], b: [2.3081342e-05]
step: 450, cost_val: 0.00000, W: [0.99999446], b: [1.2575029e-05]
step: 475, cost_val: 0.00000, W: [0.99999696], b: [6.8529835e-06]


test

In [9]:
class prediction:
            
    def run(self, input):  
        self.input = input
        output = sess.run(model, feed_dict={X: self.input})
        print("X: {}, Y-result: {}".format(self.input, output))
        
pred = prediction()
In [10]:
pred.run(2.5)
pred.run(5)
pred.run(10)
X: 2.5, Y-result: [2.4999995]
X: 5, Y-result: [4.999995]
X: 10, Y-result: [9.999987]
In [11]:
from IPython.core.display import HTML, display

display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
01.tesnsor_and_graph  (0) 2018.12.09
01_tesnsor_and_graph
In [1]:
import tensorflow as tf
In [2]:
hello = tf.constant("hello, Tensorflow")
print(hello)
Tensor("Const:0", shape=(), dtype=string)

1. 텐서의 그래프 실행


hello 변수의 값을 출력한 결과로, hello가 텐서플로의 텐서Tensor라는 자료형이고, 상수를 담고 있음
텐서는 텐서플로에서 다양한 수학식을 계산하기 위한 가장 기본적이고 중요한 자료형이며, rank와 shape라는 개념을 가지고 있음

In [3]:
3 # rank=0, shape=[]
[1., 2., 3.] # rank=1, shape=[3]
[[1., 2., 3.], [4., 5., 6.]] # rank=2, shape=[2, 3]
[[[1., 2., 3.,]], [[7., 8., 9.]]] # rank=3, shape=[2, 1, 3]
Out[3]:
[[[1.0, 2.0, 3.0]], [[7.0, 8.0, 9.0]]]
In [4]:
a = tf.constant(10)
b = tf.constant(32)
c = tf.add(a, b)
print(c)
Tensor("Add:0", shape=(), dtype=int32)

텐서플로 프로그램 구성|center

그래프: 텐서들의 연산 모음
필요할 때 연산을 실행하는 코드를 넣어 '원하는 시점'에서 실제 연산을 수행하도록 함 --> 지연 실행lazy evaluation

In [5]:
sess = tf.Session()

print(sess.run(hello))
print(sess.run([a, b, c]))

sess.close()
b'hello, Tensorflow'
[10, 32, 42]
In [6]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important}</style>"))

'Deep_Learning' 카테고리의 다른 글

06.tensorboard01_example  (0) 2018.12.09
05.deep_neural_net_Costfun2  (0) 2018.12.09
04.deep_neural_net_Costfun1  (0) 2018.12.09
03.classification  (0) 2018.12.09
02.linear_regression  (0) 2018.12.09
07_handling_dataframe(bool-apply)
In [1]:
import pandas as pd
from collections import OrderedDict
In [2]:
scientists = pd.read_csv("./data/scientists.csv")


열의 자료형 바꾸기와 새로운 열 추가

In [3]:
print("Born type: {}".format(scientists["Born"].dtype))
print("Died type: {}".format(scientists["Died"].dtype))
Born type: object
Died type: object
In [4]:
born_datetime = pd.to_datetime(scientists["Born"], format="%Y-%m-%d")
print("born_datetime: \n{}".format(born_datetime))

print("\n===================================\n")
died_datetime = pd.to_datetime(scientists["Died"], format="%Y-%m-%d")
print("died_datetime: \n{}".format(died_datetime))
born_datetime: 
0   1920-07-25
1   1876-06-13
2   1820-05-12
3   1867-11-07
4   1907-05-27
5   1813-03-15
6   1912-06-23
7   1777-04-30
Name: Born, dtype: datetime64[ns]

===================================

died_datetime: 
0   1958-04-16
1   1937-10-16
2   1910-08-13
3   1934-07-04
4   1964-04-14
5   1858-06-16
6   1954-06-07
7   1855-02-23
Name: Died, dtype: datetime64[ns]
In [5]:
scientists["born_dt"], scientists["died_dt"] = [born_datetime, died_datetime]
scientists.head()
Out[5]:
Name Born Died Age Occupation born_dt died_dt
0 Rosaline Franklin 1920-07-25 1958-04-16 37 Chemist 1920-07-25 1958-04-16
1 William Gosset 1876-06-13 1937-10-16 61 Statistician 1876-06-13 1937-10-16
2 Florence Nightingale 1820-05-12 1910-08-13 90 Nurse 1820-05-12 1910-08-13
3 Marie Curie 1867-11-07 1934-07-04 66 Chemist 1867-11-07 1934-07-04
4 Rachel Carson 1907-05-27 1964-04-14 56 Biologist 1907-05-27 1964-04-14
In [6]:
scientists["age_days"] = (scientists["died_dt"] - scientists["born_dt"])
scientists.head()
Out[6]:
Name Born Died Age Occupation born_dt died_dt age_days
0 Rosaline Franklin 1920-07-25 1958-04-16 37 Chemist 1920-07-25 1958-04-16 13779 days
1 William Gosset 1876-06-13 1937-10-16 61 Statistician 1876-06-13 1937-10-16 22404 days
2 Florence Nightingale 1820-05-12 1910-08-13 90 Nurse 1820-05-12 1910-08-13 32964 days
3 Marie Curie 1867-11-07 1934-07-04 66 Chemist 1867-11-07 1934-07-04 24345 days
4 Rachel Carson 1907-05-27 1964-04-14 56 Biologist 1907-05-27 1964-04-14 20777 days
In [7]:
import random

print(scientists["Age"])
print("\n===================================\n")
random.seed(42)
random.shuffle(scientists["Age"])
print(scientists["Age"])
0    37
1    61
2    90
3    66
4    56
5    45
6    41
7    77
Name: Age, dtype: int64

===================================

/anaconda3/lib/python3.6/random.py:277: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  x[i], x[j] = x[j], x[i]
0    66
1    56
2    41
3    77
4    90
5    45
6    37
7    61
Name: Age, dtype: int64


열 삭제

In [8]:
print(scientists.columns)

print("\n===================================\n")

scientists_dropped = scientists.drop(["Age"], axis=1)
print(scientists_dropped.columns)
Index(['Name', 'Born', 'Died', 'Age', 'Occupation', 'born_dt', 'died_dt',
       'age_days'],
      dtype='object')

===================================

Index(['Name', 'Born', 'Died', 'Occupation', 'born_dt', 'died_dt', 'age_days'], dtype='object')
In [9]:
from IPython.core.display import display, HTML

display(HTML("<style> .container{width:100% !important;}</style>"))

'pandas > basic' 카테고리의 다른 글

06.handling_dataframe(bool)  (0) 2018.12.09
05.handling_series(apply)  (0) 2018.12.09
04.handling_series(basic)  (0) 2018.12.09
03.create_data_frame  (0) 2018.12.09
02.basic_statistic  (0) 2018.12.09
06_handling_dataframe(bool)
In [1]:
import pandas as pd
from collections import OrderedDict
In [2]:
scientists = pd.read_csv("./data/scientists.csv")


boolean 추출과 broadcasting

In [3]:
bool_idx = scientists["Age"] > scientists["Age"].mean()
scientists[bool_idx]
Out[3]:
Name Born Died Age Occupation
1 William Gosset 1876-06-13 1937-10-16 61 Statistician
2 Florence Nightingale 1820-05-12 1910-08-13 90 Nurse
3 Marie Curie 1867-11-07 1934-07-04 66 Chemist
7 Johann Gauss 1777-04-30 1855-02-23 77 Mathematician
In [4]:
## 문자열 2배, 값2배 -- broadcasting
print(scientists*2)
                                       Name                  Born  \
0        Rosaline FranklinRosaline Franklin  1920-07-251920-07-25   
1              William GossetWilliam Gosset  1876-06-131876-06-13   
2  Florence NightingaleFlorence Nightingale  1820-05-121820-05-12   
3                    Marie CurieMarie Curie  1867-11-071867-11-07   
4                Rachel CarsonRachel Carson  1907-05-271907-05-27   
5                        John SnowJohn Snow  1813-03-151813-03-15   
6                    Alan TuringAlan Turing  1912-06-231912-06-23   
7                  Johann GaussJohann Gauss  1777-04-301777-04-30   

                   Died  Age                            Occupation  
0  1958-04-161958-04-16   74                        ChemistChemist  
1  1937-10-161937-10-16  122              StatisticianStatistician  
2  1910-08-131910-08-13  180                            NurseNurse  
3  1934-07-041934-07-04  132                        ChemistChemist  
4  1964-04-141964-04-14  112                    BiologistBiologist  
5  1858-06-161858-06-16   90                    PhysicianPhysician  
6  1954-06-071954-06-07   82  Computer ScientistComputer Scientist  
7  1855-02-231855-02-23  154            MathematicianMathematician  
In [5]:
from IPython.core.display import display, HTML
display(HTML("<style> .container{width:100% !important;}</style>"))

'pandas > basic' 카테고리의 다른 글

07.handling_dataframe(bool-apply)  (0) 2018.12.09
05.handling_series(apply)  (0) 2018.12.09
04.handling_series(basic)  (0) 2018.12.09
03.create_data_frame  (0) 2018.12.09
02.basic_statistic  (0) 2018.12.09

+ Recent posts