Изменения

Перейти к: навигация, поиск
Сверточная нейронная сеть
Реализация сверточной нейронной сети для классификации цифр из датасета MNIST:
<code style="display:inline-block">
'''from''' __future__ '''import''' division, print_function, absolute_import
'''import''' tensorflow '''as''' tf
<pre stylefont color="color: green"># Import MNIST data</prefont>
'''from''' tensorflow.examples.tutorials.mnist '''import''' input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot='''True''')
<pre stylefont color="color: green"># Training Parameters</prefont>
learning_rate = 0.001
num_steps = 200
display_step = 10
<pre stylefont color="color: green"># Network Parameters</prefont>
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)
dropout = 0.75 # Dropout, probability to keep units
<pre stylefont color="color: green"># tf Graph input</prefont>
X = tf.placeholder(tf.float32, ['''None''', num_input])
Y = tf.placeholder(tf.float32, ['''None''', num_classes])
keep_prob = tf.placeholder(tf.float32) <pre stylefont color="color: green"># dropout (keep probability)</prefont>
<pre stylefont color="color: green"># Create some wrappers for simplicity</prefont>
'''def''' conv2d(x, W, b, strides=1):
<pre stylefont color="color: green"># Conv2D wrapper, with bias and relu activation</prefont>
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
'''def''' maxpool2d(x, k=2):
<pre stylefont color="color: green"># MaxPool2D wrapper</prefont>
'''return''' tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')
<pre stylefont color="color: green"># Create model</prefont>
'''def''' conv_net(x, weights, biases, dropout):
<pre stylefont color="color: green"># MNIST data input is a 1-D vector of 784 features (28*28 pixels)
# Reshape to match picture format [Height x Width x Channel]
# Tensor input become 4-D: [Batch Size, Height, Width, Channel]</prefont>
x = tf.reshape(x, shape=[-1, 28, 28, 1])
<pre stylefont color="color: green"># Convolution Layer</prefont>
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
<pre stylefont color="color: green"># Max Pooling (down-sampling)</prefont>
conv1 = maxpool2d(conv1, k=2)
<pre stylefont color="color: green"># Convolution Layer</prefont>
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
<pre stylefont color="color: green"># Max Pooling (down-sampling)</prefont>
conv2 = maxpool2d(conv2, k=2)
<pre stylefont color="color: green"># Fully connected layer # Reshape conv2 output to fit fully connected layer input</prefont>
fc1 = tf.reshape(conv2, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)
<pre stylefont color="color: green"># Apply Dropout</prefont>
fc1 = tf.nn.dropout(fc1, dropout)
<pre stylefont color="color: green"># Output, class prediction</prefont>
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
'''return''' out
<pre stylefont color="color: green"># Store layers weight & bias</prefont>
weights = {
<pre stylefont color="color: green"># 5x5 conv, 1 input, 32 outputs</prefont>
'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
<pre stylefont color="color: green"># 5x5 conv, 32 inputs, 64 outputs</prefont>
'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
<pre stylefont color="color: green"># fully connected, 7*7*64 inputs, 1024 outputs</prefont>
'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
<pre stylefont color="color: green"># 1024 inputs, 10 outputs (class prediction)</prefont>
'out': tf.Variable(tf.random_normal([1024, num_classes]))
}
}
<pre stylefont color="color: green"># Construct model</prefont>
logits = conv_net(X, weights, biases, keep_prob)
prediction = tf.nn.softmax(logits)
<pre stylefont color="color: green"># Define loss and optimizer</prefont>
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits, labels=Y))
train_op = optimizer.minimize(loss_op)
<pre stylefont color="color: green"># Evaluate model</prefont>
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
<pre stylefont color="color: green"># Initialize the variables (i.e. assign their default value)</prefont>
init = tf.global_variables_initializer()
<pre stylefont color="color: green"># Start training</prefont>
'''with''' tf.Session() '''as''' sess:
<pre stylefont color="color: green"># Run the initializer</prefont>
sess.run(init)
'''for''' step '''in''' '''range'''(1, num_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
<pre stylefont color="color: green"># Run optimization op (backprop)</prefont>
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.8})
'''if''' step % display_step == 0 '''or''' step == 1:
<pre stylefont color="color: green"># Calculate batch loss and accuracy</prefont>
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
Y: batch_y,
"{:.3f}".format(acc))
print("Optimization Finished!")
<pre stylefont color="color: green"># Calculate accuracy for 256 MNIST test images</prefont>
print("Testing Accuracy:", \
sess.run(accuracy, feed_dict={X: mnist.test.images[:256],
Y: mnist.test.labels[:256],
keep_prob: 1.0}))
</code>
==Keras==
333
правки

Навигация