tensorflow cnn
2017-12-07 本文已影响0人
Do_More
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/', one_hot=True)
n_output_layer = 10
def convolutional_neural_network(data):
weights = {
'w_conv1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
'w_conv2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
'w_fc': tf.Variable(tf.random_normal([7 * 7 * 64, 1024])),
'out': tf.Variable(tf.random_normal([1024, n_output_layer]))
}
biases = {
'b_conv1': tf.Variable(tf.random_normal([32])),
'b_conv2': tf.Variable(tf.random_normal([64])),
'b_fc': tf.Variable(tf.random_normal([1024])),
'out': tf.Variable(tf.random_normal([n_output_layer]))
}
data = tf.reshape(data, [-1, 28, 28, 1])
conv1 = tf.nn.relu(tf.add(tf.nn.conv2d(data, weights['w_conv1'], strides=[1, 1, 1, 1], padding='SAME'), biases['b_conv1']))
conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
conv2 = tf.nn.relu(tf.add(tf.nn.conv2d(conv1, weights['w_conv2'], strides=[1, 1, 1, 1], padding='SAME'), biases['b_conv2']))
conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
fc = tf.reshape(conv2, [-1, 7 * 7 * 64])
fc = tf.nn.relu(tf.add(tf.matmul(fc, weights['w_fc']), biases['b_fc']))
# fc = tf.nn.dropout(fc, 0.8)
output = tf.add(tf.matmul(fc, weights['out']), biases['out'])
return output
batch_size = 100
X = tf.placeholder('float', [None, 28 * 28])
Y = tf.placeholder('float')
def train_neural_network(X, Y):
predict = convolutional_neural_network(X)
cost_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predict, labels=Y))
optimizer = tf.train.AdamOptimizer().minimize(cost_func)
epochs = 1
with tf.Session() as session:
session.run(tf.global_variables_initializer())
epoch_loss = 0
for epoch in range(epochs):
for i in range(int(mnist.train.num_examples / batch_size)):
x, y = mnist.train.next_batch(batch_size)
_, c = session.run([optimizer, cost_func], feed_dict={X: x, Y: y})
epoch_loss += c
print(epoch, ' : ', epoch_loss)
correct = tf.equal(tf.argmax(predict, 1), tf.argmax(Y, 1))
acurracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('acurracy: ', acurracy.eval({
X: mnist.test.images,
Y: mnist.test.labels
}))
train_neural_network(X, Y)
result:
(0, ' : ', 1624912.0736694336)
('acurracy: ', 0.9483)