步骤和上一篇一样,step0~step5
【step0】
准备工作:下载并检测数据集,tensorflow.tutorial自带minist数据集,如果下载不成功,可以网页手动下载后放到代码已经为你创建的文件夹里。
mnist = input_data.read_data_sets('./mnist', one_hot=True)
test_x = mnist.test.images[:2000]
test_y = mnist.test.labels[:2000]
# plot one example,to see if it's right
print(mnist.train.images.shape) # (55000, 28 * 28)
print(mnist.train.labels.shape) # (55000, 10)
plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
plt.title('%i' % np.argmax(mnist.train.labels[0]))
plt.show()
别忘了定义batch_size和学习速率
BATCH_SIZE = 50
LR = 0.001
【step1】
定义输入:batchsize不确定,所以用到None,-1等参数,见tf.placeholder()函数和tf.reshape()函数,注意经过reshape后的参数image才是网络真正的输入
tf_x = tf.placeholder(tf.float32, [None, 28*28]) / 255
image = tf.reshape(tf_x, [-1, 28, 28, 1]) # (batch, height, width, channel)
tf_y = tf.placeholder(tf.int32, [None, 10]) # input y
【step2】
定义学习参数:最重要的一步,学习参数是我们的卷积核参数,很经典的结构,卷+池+卷+池+全
代码如下:
conv1 = tf.layers.conv2d( # shape (28, 28, 1)
inputs=image,
filters=16,
kernel_size=5,
strides=1,
padding='same',
activation=tf.nn.relu
) # -> (28, 28, 16)
pool1 = tf.layers.max_pooling2d(
conv1,
pool_size=2,
strides=2,
) # -> (14, 14, 16)
conv2 = tf.layers.conv2d(pool1, 32, 5, 1, 'same', activation=tf.nn.relu) # -> (14, 14, 32)
pool2 = tf.layers.max_pooling2d(conv2, 2, 2) # -> (7, 7, 32)
flat = tf.reshape(pool2, [-1, 7*7*32]) # -> (7*7*32, )
output = tf.layers.dense(flat, 10) # output layer
【step3】
定义学习方法:最重要的是新学到的softmax函数,它把网络的输出,10维转化成1维,其原理是,在这10维度中找到维度最大的那一维(说明落到该维度可能性最大),并因此得到一个1维参数,就是维度序号。
Adam优化器适合解决更复杂的问题
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # compute cost
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
accuracy = tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables
labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
【step4】
初始化模型并训练:比线性回归的训练回合要多
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
sess.run(init_op) # initialize var in graph
for step in range(600):
b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
_, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y})
if step % 50 == 0:
accuracy_, flat_representation = sess.run([accuracy, flat], {tf_x: test_x, tf_y: test_y})
print('Step:', step, '| train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)
【step5】
测试:测试模型我们在step0就确定好了喔
test_output = sess.run(output, {tf_x: test_x[:10]})
pred_y = np.argmax(test_output, 1)
print(pred_y, 'prediction number')
print(np.argmax(test_y[:10], 1), 'real number')
网友评论