# -*- coding:utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
import argparse
import sys
DATA_DIR = os.path.join(‘.‘, ‘mnist_link‘)
# =======================================
# COMMON OPERATIONS
# =======================================
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=‘SAME‘)
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME‘)
def init_weight(shape):
init = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(init)
def init_bias(shape):
init = tf.constant(0.1, shape=shape)
return tf.Variable(init)
# =======================================
# BUILD CNN
# =======================================
def build_cnn(x):
‘‘‘
build the cnn model
‘‘‘
x_image = tf.reshape(x, [-1,28,28,1])
w1 = init_weight([5,5,1,32])
b1=init_bias([32])
conv1 = tf.nn.relu(conv2d(x_image, w1) + b1)
pool1 = max_pool_2x2(conv1)
w2 = init_weight([5,5,32,64])
b2 = init_bias([64])
conv2 = tf.nn.relu(conv2d(pool1, w2) + b2)
pool2 = max_pool_2x2(conv2)
# fc
w_fc1 = init_weight([7*7*64, 1024])
b_fc1 = init_bias([1024])
pool2_flat = tf.reshape(pool2, [-1, 7*7*64])
fc1 = tf.nn.relu(tf.matmul(pool2_flat, w_fc1) + b_fc1)
# dropout
keep_prob = tf.placeholder(tf.float32)
fc1_dropout = tf.nn.dropout(fc1, keep_prob)
# fc2
w_fc2 = init_weight([1024, 10])
b_fc2 = init_bias([10])
y_conv = tf.matmul(fc1_dropout, w_fc2) + b_fc2
return y_conv, keep_prob
# =======================================
# train and test
# =======================================
def main():
‘‘‘
feed data into cnn model, and train and test the model
‘‘‘
# import data
print(‘import data...‘)
mnist = input_data.read_data_sets(DATA_DIR, one_hot=True)
# create graph for cnn
x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
y_conv, keep_prob = build_cnn(x)
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y_, logits = y_conv))
optimizer = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_predictions = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))
init = tf.global_variables_initializer()
print(‘start training...‘)
with tf.Session() as sess:
sess.run(init)
for i in range(2000):
batch = mnist.train.next_batch(128)
optimizer.run(feed_dict={x:batch[0], y_:batch[1], keep_prob:0.5})
if i%100 == 0:
train_acc = accuracy.eval(
feed_dict = {x:batch[0], y_:batch[1], keep_prob:1.0})
print(‘step {}, accuracy is {}‘.format(i, train_acc))
test_acc = accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels, keep_prob:1.0})
print(‘test accuracy is {}‘.format(test_acc))
if __name__ == ‘__main__‘:
print(‘run main‘)
main()
原文地址:https://www.cnblogs.com/jiaxblog/p/9387755.html
时间: 2024-10-09 13:19:58