333
правки
Изменения
→Сверточная нейронная сеть
Реализация сверточной нейронной сети для классификации цифр из датасета MNIST:
'''from ''' __future__ '''import ''' division, print_function, absolute_import '''import ''' tensorflow '''as ''' tf
# Import MNIST data
'''from ''' tensorflow.examples.tutorials.mnist '''import ''' input_data mnist = input_data.read_data_sets("/tmp/data/", one_hot='''True''')
# Training Parameters
# tf Graph input
X = tf.placeholder(tf.float32, ['''None''', num_input]) Y = tf.placeholder(tf.float32, ['''None''', num_classes])
keep_prob = tf.placeholder(tf.float32) # dropout (keep probability)
# Create some wrappers for simplicity
'''def ''' conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
'''return ''' tf.nn.relu(x)
'''def ''' maxpool2d(x, k=2):
# MaxPool2D wrapper
'''return ''' tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')
# Create model
'''def ''' conv_net(x, weights, biases, dropout):
# MNIST data input is a 1-D vector of 784 features (28*28 pixels)
# Reshape to match picture format [Height x Width x Channel]
# Output, class prediction
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])
'''return ''' out
# Store layers weight & bias
# Start training
'''with ''' tf.Session() '''as ''' sess:
# Run the initializer
sess.run(init)
'''for ''' step '''in ''' '''range'''(1, num_steps+1):
batch_x, batch_y = mnist.train.next_batch(batch_size)
# Run optimization op (backprop)
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.8})
'''if ''' step % display_step == 0 '''or ''' step == 1:
# Calculate batch loss and accuracy
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,