CNN(卷积神经网络)

CNN卷积神经网络

用mnist数据训练卷积神经网络。该卷积网络有两个卷积层,两个池化层,和两个全连接层。
输入:

x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])
input_x = tf.reshape(x,[-1,28,28,1])  #对其进行变形

卷积层和池化层

#卷积核的形状5,5,1,32
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_x,w1,strides=[1,1,1,1],padding='SAME'),b1))
pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#卷积核的形状
conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool1,w2,strides=[1,1,1,1],padding='SAME'),b2))
pool2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

全连接层

fc_input = tf.reshape(pool2,shape=[-1,7*7*64])

w3 = tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
b3 = tf.Variable(tf.constant(0.1,shape=[1024]))

w4 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
b4 = tf.Variable(tf.constant(0.1,shape=[10]))

fc1 = tf.nn.relu(tf.matmul(fc_input,w3)+b3)
fc1_dropout = tf.nn.dropout(fc1,0.25)

y = tf.nn.softmax(tf.matmul(fc1_dropout,w4)+b4)

损失函数和优化算法

loss = - tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))
optim = tf.train.AdamOptimizer(0.001).minimize(loss)

计算模型的准确率

prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accu = tf.reduce_mean(tf.cast(prediction,tf.float32))

附录

# -*- coding: utf-8 -*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
"""
def conv2d(input_,output_size,strides=2,name='deconv2d'):
    with tf.variable_scope(name,reuse=reuse):
        w = tf.get_variable('w',shape=[w,w,input_.shape[-1],output_size],initializer=tf.truncated_normal_initializer=stddev)
        net = tf.nn.conv2d(input_,w,strides=[1,strides,strides,1],padding='SAME')
    return net
"""
batch_size = 64
x = tf.placeholder(tf.float32,[None,784])
y_ = tf.placeholder(tf.float32,[None,10])

w1 = tf.Variable(tf.truncated_normal([5,5,1,32],stddev=0.1))
b1 = tf.Variable(tf.constant(0.1,shape=[32]))

w2 = tf.Variable(tf.truncated_normal([5,5,32,64],stddev=0.1))
b2 = tf.Variable(tf.constant(0.1,shape=[64]))

input_x = tf.reshape(x,[-1,28,28,1])
conv1 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(input_x,w1,strides=[1,1,1,1],padding='SAME'),b1))

pool1 = tf.nn.max_pool(conv1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

conv2 = tf.nn.relu(tf.nn.bias_add(tf.nn.conv2d(pool1,w2,strides=[1,1,1,1],padding='SAME'),b2))
pool2 = tf.nn.max_pool(conv2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

fc_input = tf.reshape(pool2,shape=[-1,7*7*64])

w3 = tf.Variable(tf.truncated_normal([7*7*64,1024],stddev=0.1))
b3 = tf.Variable(tf.constant(0.1,shape=[1024]))

w4 = tf.Variable(tf.truncated_normal([1024,10],stddev=0.1))
b4 = tf.Variable(tf.constant(0.1,shape=[10]))

fc1 = tf.nn.relu(tf.matmul(fc_input,w3)+b3)

fc1_dropout = tf.nn.dropout(fc1,0.25)

y = tf.nn.softmax(tf.matmul(fc1_dropout,w4)+b4)

loss = - tf.reduce_mean(tf.reduce_sum(y_*tf.log(y)))

prediction = tf.equal(tf.arg_max(y,1),tf.arg_max(y_,1))
accu = tf.reduce_mean(tf.cast(prediction,tf.float32))

optim = tf.train.AdamOptimizer(0.001).minimize(loss)

def train():
    with tf.Session() as sess:
        
        tf.global_variables_initializer().run()
        for i in range(10000):
            batch_x,batch_y = mnist.train.next_batch(batch_size)
            sess.run(optim,feed_dict={x:batch_x,y_:batch_y})
            if(i%300==0):
                accuracy = sess.run(accu,feed_dict={x:batch_x,y_:batch_y})
                print("after %d trains ,the accuracy is %g"%(i,accuracy))
                
def main(argv=None):
    train()
    
if __name__ == '__main__':
    tf.app.run()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

storm岚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值