글자가 깨질지 모르므로 안돌아가면 첨부파일로^^;;
아래 튜토리얼만 돌려도 메모리 모자르다고 투덜대긴 하지만...
Dropout 은 하던 안하던 상관은 없을 것다고 한다;;
W tensorflow/core/common_runtime/bfc_allocator.cc:213] Ran out of memory trying to allocate 2.59GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
W tensorflow/core/common_runtime/bfc_allocator.cc:213] Ran out of memory trying to allocate 3.90GiB. The caller indicates that this is not a failure, but may mean that there could be performance gains if more memory is available.
test accuracy 0.9926
# tags : Tensorflow tensor flow 텐저플로우 텐서플로우
# Tensorflow Tutorial for CNN Mnist
# URL https://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.html#convolution-and-pooling
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
# Loading data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
# Making session .. But what is session??
sess = tf.InteractiveSession()
######################
# Layer architecture #
######################
# This is layer??
# what is placeholder??
# Tutorial:
# We start building the computation graph by creating nodes for the input images and target
# output classes.
# Here x and y_ aren't specific values. Rather, they are each a placeholder -- a value that
# we'll input when we ask TensorFlow to run a computation.
#
# The input images x will consist of a 2d tensor of floating point numbers. Here we assign it
# a shape of [None, 784], where 784 is the dimensionality of a single flattened 28 by 28 pixel
# MNIST image, and None indicates that the first dimension, corresponding to the batch size, can
# be of any size. The target output classes y_ will also consist of a 2d tensor, where each row
# is a one-hot 10-dimensional vector indicating which digit class (zero through nine) the
# corresponding MNIST image belongs to.
#
# The shape argument to placeholder is optional, but it allows TensorFlow to automatically
# catch bugs stemming from inconsistent tensor shapes.
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
# Reshape x to 4d tensor: second and third dimensions correspoinding to image width and height, the final dimension correspoinding to the number of color channls
x_image = tf.reshape(x, [-1,28,28,1])
##
# Function Definitions
# Initialization
# return variable in the specified shape with stddev 0.1
def weight_variable(shape): # shape is a kind of array?? can be expanded???? Cool!
initial = tf.truncated_normal(shape, stddev=0.1) # Initialize w/ stddev 0.1 normal distribution
return tf.Variable(initial)
# return constant bias in the specified shape
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape) # Initilaize w/ 0.1
return tf.Variable(initial)
# 2D convolution, stride news 1111
# SAME is valid ??
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# Max pooling
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
##
# First Convolution Layer
# Weight and bias Variables
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
# Applying Max pooling Convolution
# We convolve x_image w/ the weight tensor, add the bias, apply the ReLU function, and final\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0ly maxpool
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
##
# Second Convolutional Layer
# In order to build a deep network, we stack several layers of this type.
# Weight and bias Variables
# The second layer will have 64 features for each 5x5 patch.
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
# Applying Max pooling Convolution
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
##
# Densely Connected Layer : Fully connected layer w/ 1024 neurons
# Now that the image size has been reduced to 7x7
W_fc1 = weight_variable([7 * 7 * 64, 1024])
# we add a fully-connected layer with 1024 neurons to allow processing on the entire image.
b_fc1 = bias_variable([1024])
# We reshape the tensor from the pooling layer into a batch of vectors,
h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
# multiply by a weight matrix, add a bias, and a ReLU
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
##
# Dropout: To reduce overfitting, we will apply dropout before the readout layer.
# We create a placeholder for the probability that a neuron's output is kept during dropout.
# This allows us to turn dropout on during training, and turn it off during testing.
keep_prob = tf.placeholder(tf.float32)
# TensorFlow's tf.nn.dropout op automatically handles scaling neuron outputs in addition
# to masking them, so dropout just works without any additional scaling.
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
# For this small convolutional network, performance is actually nearly identical with and
# without dropout. Dropout is often very effective at reducing overfitting, but it is most
# useful when training very large neural networks.
##
# Readout Layer
# Finally, we add a layer, just like for the one layer softmax regression above.
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
################################
# Train and Evaluate the Model #
################################
# Finally, we can do something with the layer built up
# To train and evaluate it we will use code that is nearly identical to that for the simple
# one layer SoftMax network above.
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_conv, y_))
# The differences are that:
# 1) We will replace the steepest gradient descent optimizer with the more sophisticated ADAM opti.
# 2) We will include the additional parameter keep_prob in feed_dict to control the dropout rate.
# 3) We will add logging to every 100th iteration in the training process.
# ADAM optimizer rather than the steepest gradient descent optimizer
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# Diff btw. obs and label
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
# calculate accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
sess.run(tf.initialize_all_variables())
# Feel free to go ahead and run this code, but it does 20,000 training iterations
# and may take a while (possibly up to half an hour), depending on your processor.
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={
x:batch[0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\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0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
# Print out : I hope the final test set accruacy after runnign this code will be 99.2%
print("test accuracy %g"%accuracy.eval(feed_dict={
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))