tensorflow学习(五)基于卷积神经网络mnist数据集分类

    xiaoxiao2022-07-07  194

    import numpy as np import tensorflow as tf import input_data mnist = input_data.read_data_sets('data/',one_hot=True) train_img = mnist.train.images train_label = mnist.train.labels test_img = mnist.test.images test_label = mnist.test.labels #设置输出输出层数 n_input = 784 n_output = 10 w = { 'wc1': tf.Variable(tf.random_normal([3,3,1,64],stddev=0.1)), #[w,n,in_channel,out_channel] 'wc2': tf.Variable(tf.random_normal([3,3,64,128],stddev=0.1)), 'wd1': tf.Variable(tf.random_normal([7*7*128,1024],stddev=0.1)), #[输入,输出] 'wd2': tf.Variable(tf.random_normal([1024,n_output],stddev=0.1)) } b = { 'bc1': tf.Variable(tf.random_normal([64],stddev=0.1)), 'bc2': tf.Variable(tf.random_normal([128],stddev=0.1)), 'bd1': tf.Variable(tf.random_normal([1024],stddev=0.1)), 'bd2': tf.Variable(tf.random_normal([n_output],stddev=0.1)), } def conv_basic(_input, _w, _b, _kratio): _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1]) #转化为一个四维数据 [n_batch_size, h, w, channel] -1 自动判断输入的batch_size _conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1,1,1,1], padding='SAME') #strides [batch_size, h, w, channel] same 补零 valide 去除末尾的数据 _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1'])) _pool1 = tf.nn.max_pool(_conv1, ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #ksize [batch_siez,w, h, channel] strides = [batch_size, w, h, channel] _pool1_dr1 = tf.nn.dropout(_pool1,_kratio) # conv2 _conv2 = tf.nn.conv2d(_pool1_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME') # strides [batch_size, h, w, channel] same 补零 valide 去除末尾的数据 _conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2'])) _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # ksize [batch_siez,w, h, channel] strides = [batch_size, w, h, channel] _pool_dr2 = tf.nn.dropout(_pool2, _kratio) #vectorize _dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]]) #full1 _fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1'])) _fc_dr1 = tf.nn.dropout(_fc1, _kratio) #full2 _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2']) out = { 'input_r':_input_r, 'conv1':_conv1, 'pool1':_pool1, 'pool1_dr1':_pool1_dr1, 'conv2':_conv2, 'pool2':_pool2, 'pool_dir2':_pool_dr2, 'densel':_dense1, 'fc1':_fc1, 'fc1_dr1': _fc_dr1, 'out':_out } return out x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) kratio = tf.placeholder(tf.float32) #对于设置的所有placeholder都需要通过字典赋值 _pred = conv_basic(x, w, b, kratio)['out'] #计算损失 需要输入labels 和logits loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=_pred)) optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) _corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1)) accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) batch_size = 16 for epoch in range(15): avg_loss =0 total_batch = 10 for i in range(10): batch_xs, batch_ys = mnist.train.next_batch(16) sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, kratio: 0.7}) avg_loss += sess.run(loss, feed_dict={x:batch_xs, y:batch_ys, kratio:1.} )/10 print('epoch: d/d cost: %.9f' % (epoch,15, avg_loss )) train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, kratio : 1.}) print('accuracy %.3f'% (train_acc))

    添加保存神经网络和读取神经网络参数。当do_train为1时训练保存神经网络当do_train为0是读取神经网络

    import numpy as np import tensorflow as tf import input_data mnist = input_data.read_data_sets('data/',one_hot=True) train_img = mnist.train.images train_label = mnist.train.labels test_img = mnist.test.images test_label = mnist.test.labels #设置输出输出层数 n_input = 784 n_output = 10 w = { 'wc1': tf.Variable(tf.random_normal([3,3,1,64],stddev=0.1)), #[w,n,in_channel,out_channel] 'wc2': tf.Variable(tf.random_normal([3,3,64,128],stddev=0.1)), 'wd1': tf.Variable(tf.random_normal([7*7*128,1024],stddev=0.1)), #[输入,输出] 'wd2': tf.Variable(tf.random_normal([1024,n_output],stddev=0.1)) } b = { 'bc1': tf.Variable(tf.random_normal([64],stddev=0.1)), 'bc2': tf.Variable(tf.random_normal([128],stddev=0.1)), 'bd1': tf.Variable(tf.random_normal([1024],stddev=0.1)), 'bd2': tf.Variable(tf.random_normal([n_output],stddev=0.1)), } def conv_basic(_input, _w, _b, _kratio): _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1]) #转化为一个四维数据 [n_batch_size, h, w, channel] -1 自动判断输入的batch_size _conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1,1,1,1], padding='SAME') #strides [batch_size, h, w, channel] same 补零 valide 去除末尾的数据 _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1'])) _pool1 = tf.nn.max_pool(_conv1, ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME') #ksize [batch_siez,w, h, channel] strides = [batch_size, w, h, channel] _pool1_dr1 = tf.nn.dropout(_pool1,_kratio) # conv2 _conv2 = tf.nn.conv2d(_pool1_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME') # strides [batch_size, h, w, channel] same 补零 valide 去除末尾的数据 _conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2'])) _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') # ksize [batch_siez,w, h, channel] strides = [batch_size, w, h, channel] _pool_dr2 = tf.nn.dropout(_pool2, _kratio) #vectorize _dense1 = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]]) #full1 _fc1 = tf.nn.relu(tf.add(tf.matmul(_dense1, _w['wd1']), _b['bd1'])) _fc_dr1 = tf.nn.dropout(_fc1, _kratio) #full2 _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2']) out = { 'input_r':_input_r, 'conv1':_conv1, 'pool1':_pool1, 'pool1_dr1':_pool1_dr1, 'conv2':_conv2, 'pool2':_pool2, 'pool_dir2':_pool_dr2, 'densel':_dense1, 'fc1':_fc1, 'fc1_dr1': _fc_dr1, 'out':_out } return out x = tf.placeholder(tf.float32, [None, n_input]) y = tf.placeholder(tf.float32, [None, n_output]) kratio = tf.placeholder(tf.float32) #对于设置的所有placeholder都需要通过字典赋值 _pred = conv_basic(x, w, b, kratio)['out'] #计算损失 需要输入labels 和logits loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=_pred)) optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) _corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1)) accr = tf.reduce_mean(tf.cast(_corr, tf.float32)) init = tf.global_variables_initializer() save_step = 1 saver = tf.train.Saver(max_to_keep=3) do_train = 0 with tf.Session() as sess: sess.run(init) batch_size = 16 if do_train ==1: for epoch in range(15): avg_loss =0 total_batch = 10 for i in range(10): batch_xs, batch_ys = mnist.train.next_batch(16) sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys, kratio: 0.7}) avg_loss += sess.run(loss, feed_dict={x:batch_xs, y:batch_ys, kratio:1.} )/10 print('epoch: d/d cost: %.9f' % (epoch,15, avg_loss )) train_acc = sess.run(accr, feed_dict={x: batch_xs, y: batch_ys, kratio : 1.}) print('accuracy %.3f'% (train_acc)) saver.save(sess, 'saver/nets/cnn_minist.ckpt-' + str(epoch) ) if do_train ==0: epoch = 15 -1 saver.restore(sess, 'saver/nets/cnn_minist.ckpt-' + str(epoch) ) test_acc = sess.run(accr,feed_dict={x: test_img, y:test_label, kratio:1.}) print('testing accuray %.3f' % (test_acc))
    最新回复(0)