Write a tensorflow-based CNN to classify the fasion-mnist dataset.
This is the fasion-mnist dataset.
First, run the code and analyze:
import tensorflow as tfimport pandas as pdimport numpy as npconfig = tf.ConfigProto()config.gpu_options.per_process_gpu_memory_fraction = 0.3train_data = pd.read_csv(‘test.csv‘)test_data = pd.read_csv(‘test.csv‘)def Weight(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial, tf.float32)def biases(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial, tf.float32)def conv(inputs, w): return tf.nn.conv2d(inputs, w, strides=[1, 1, 1, 1], padding=‘SAME‘)def pool(inputs): return tf.nn.max_pool(inputs, ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1], padding=‘SAME‘)x = tf.placeholder(tf.float32, [None, 784])y = tf.placeholder(tf.int64, [None])x_image = tf.reshape(x, [-1, 28, 28, 1])w1 = Weight([5, 5, 1, 32])b1 = biases([32])conv1 = tf.nn.relu(conv(x_image, w1) + b1)p1 = pool(conv1)w2 = Weight([5, 5, 32, 64])b2 = biases([64])conv2 = tf.nn.relu(conv(p1, w2) + b2)p2 = pool(conv2)flattended = tf.reshape(p2, [-1, 7 * 7 * 64])w_fc1 = Weight([7 * 7 * 64, 1024])b_fc1 = biases([1024])fc1 = tf.matmul(flattended, w_fc1) + b_fc1h_fc1 = tf.nn.relu(fc1)w_fc2 = Weight([1024, 10])b_fc2 = biases([10])logits = tf.matmul(h_fc1, w_fc2) + b_fc2cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y)correct_prediction = tf.equal(y, tf.argmax(logits, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)init = tf.global_variables_initializer()sess = tf.Session(config=config)sess.run(init)for i in range(10000): choice = np.random.choice(6000, 100) batch = train_data.iloc[choice] labels = np.array(batch.iloc[:, 0]) features = np.array(batch.iloc[:, 1:]).astype(np.float32) sess.run(train_step, feed_dict={x: features, y: labels}) if i % 50 == 0: test_batch = test_data.iloc[0:1000, :] test_labes = np.array(test_batch.iloc[:, 0]) test_features = np.array(test_batch.iloc[:, 1:]).astype(np.float32) print(sess.run(accuracy, feed_dict={x: test_features, y: test_labes}))sess.close()
1. Define weight, biases, Conv layer, pool Layer
def Weight(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial, tf.float32)def biases(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial, tf.float32)def conv(inputs, w): return tf.nn.conv2d(inputs, w, strides=[1, 1, 1, 1], padding=‘SAME‘)def pool(inputs): return tf.nn.max_pool(inputs, ksize=[1, 1, 1, 1], strides=[1, 2, 2, 1], padding=‘SAME‘)
In this Code, the stride of the convolution layer is 1, and the padding method of the same is used. The Stride of the pooling layer is 2 on the x y axis. In this way, each time the data passes through convolution and pooling, the width and length of the image will change to 1/2, that is, the original 28x28 image will change from 14x14 to 7x7.
2. Define placeholder
Tensorflow-based CNN convolutional neural network classifier for fasion-mnist Dataset