从零开始深度学习0509——tensorflow基本用法及mnist实例

0509

 

https://blog.csdn.net/goldxwang/article/details/78790797

 

 

tf.equal(tf.argmax(y, 1),tf.argmax(y_, 1))用法

https://blog.csdn.net/ZHANGHUIHUIA/article/details/83784943

利用tf.argmax()按行求出真实值y_、预测值y最大值的下标,用tf.equal()求出真实值和预测值相等的数量,也就是预测结果正确的数量,tf.argmax()和tf.equal()一般是结合着用。

 

tf.cast

强制转换类型  

tf.cast(data_1,dtype) 

将data_1(可以为int,bool,float,python列表,python元组

输出转换后的数据,原数据不变  转换成指定dtype类型

 

 

两行结合 计算准确率 accuracy

corect_prediction = tf.equal(tf.argmax(ys, 1), tf.argmax(logits, 1))  

acc = tf.reduce_mean(tf.cast(corect_prediction, tf.float32))

 

 

 

cnn_minist.py

可去pycharm 查看代码  ./TF_Foundation/MINST_data/3_cnn_minst.py

import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_data



"""

place_holder+conv2d+pool2d+circle+fcn(reshape)+out+get logits+ loss + optimizer+sess(train)+acc

"""

minst = input_data.read_data_sets('MINST_data', one_hot=True)



learning_rate = 0.001 #学习率

batch_size = 128  #每次取出128个batch

num_step = 10  #迭代训练多少次

display_step = 100 #最后每100次输出一下

input_size = 784 #28*28   总共有784个像素点

num_labels = 10 #输出样本的分类

drop_out = 0.9  #drop_out 保留率



xs = tf.placeholder(tf.float32, [batch_size, input_size])  # # batch_size 是每次训练多少个  input_size 数据集中 h*w*c 计算的张量

ys = tf.placeholder(tf.float32, [batch_size, num_labels])  #  10个输出

keep_prob = tf.placeholder(tf.float32) #保留率的占位符



## 卷积层 wx+b

def conv2d(input, W, b, stride=1):

    conv = tf.nn.conv2d(input, W, strides=[1, stride, stride, 1], padding='SAME')

    return tf.nn.bias_add(conv, b)



## 池化层

def maxpool2d(input, stride=2):

    return tf.nn.max_pool(input, ksize=[1, stride, stride, 1], strides=[1, stride, stride, 1], padding='SAME')



def inference(x, W, b, dropout):



    #minist 读取出来是 [128,784]  前面是设定每次训练的batch_size  后面是读取的张量 28*28*1

    x = tf.reshape(x, shape=[-1, 28, 28, 1]) # 把784的向量转换为[28,28,1]的矩阵,通道数为1  就是黑白图像

    conv1 = conv2d(x, W['wc1'], b['bc1'])  #第一次卷积完 feature map 是 128*26*26*32   x[28*28*1] w[5*5*1] (28-5+2)/1+1=26  用了32个卷积核 batch=128  所以为128*26*26*32

    pool1 = maxpool2d(conv1) #第一次 池化层 降采样 完 是 128*14*14*32  池化操作与w无关, feature map[26*26] ksize[2*2] stride=2 (26-2+2)/2+1=14  batch=128 所以降维为128*14*14*32

    conv2 = conv2d(pool1, W['wc2'], b['bc2'])  #第一次卷积完 feature map 是 128*12*12*64

    pool2 = maxpool2d(conv2) #128*7*7*64

    print(pool2.get_shape().as_list())

    pool2 = tf.reshape(pool2, shape=[batch_size, -1]) #128*3136   -1表示懒得计算由程序根据其他位置的数计算  本来是[128,7,7,64]  7*7*64=3136 所以为[128,3136]

    print(pool2.shape)

    dim = pool2.shape.as_list()



    Weight_fc = tf.Variable(tf.random_normal([int(dim[1]), 1024])) # [3136,1024]  后一个参数为输出全连接层的输出,一般设为2的n次方

    bia_fc = tf.Variable(tf.random_normal([1024]))

    fc = tf.nn.relu(tf.add(tf.matmul(pool2, Weight_fc), bia_fc)) # [128,1024]

    fc = tf.nn.dropout(fc, dropout) #根据设置的保留率 进行drop out

    out = tf.add(tf.matmul(fc, W['out']), b['out'])



    return out



Weights = {

    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),   # 5*5*1  32个卷积核

    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),  # 5*5*32  64个卷积核

    'out': tf.Variable(tf.random_normal([1024, 10]))       #

}



bias= {

    'bc1': tf.Variable(tf.random_normal([32])),

    'bc2': tf.Variable(tf.random_normal([64])),

    'out': tf.Variable(tf.random_normal([10]))

}





logits = inference(xs, Weights, bias, keep_prob)



cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=ys) # 最后全连接输出层输出后 是softmax进行归一化变成概率 + 交叉熵损失函数

loss = tf.reduce_mean(cross_entropy) #求平均



train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #反向传播 梯度下降 根据学习率优化 adam优化方法



#两行配合  计算准确率

corect_prediction = tf.equal(tf.argmax(ys, 1), tf.argmax(logits, 1))  # 利用tf.argmax()按行求出真实值ys、预测值logits最大值的下标,用tf.equal()求出真实值和预测值相等的数量,也就是预测结果正确的数量

acc = tf.reduce_mean(tf.cast(corect_prediction, tf.float32)) # tf.cast 强制类型转换





with tf.Session() as sess:

    tf.global_variables_initializer().run()

    for i in range(num_step):

        x_input, y_input = minst.train.next_batch(batch_size) #按照设定的batch 每次读取

        sess.run(train_op, feed_dict={xs: x_input, ys: y_input, keep_prob: drop_out})

        if i % display_step == 0 or i == 1:

            ac, los = sess.run([acc, loss], feed_dict={xs: x_input, ys: y_input, keep_prob: 1})

            print("acc is "+"{:.3f}".format(ac)+"   loss is   "+"{:.3f}".format(los))

    print('over')

    test_ac = sess.run(acc, feed_dict={xs: minst.test.images[:128], ys: minst.test.labels[:128], keep_prob: 1})

    print("test acc is ", test_ac)

 

 

这篇讲的很详细

https://blog.csdn.net/u010312436/article/details/78616918

 

 

tensorflow 保存模型时

只能保存参数,不能保存整个神经网络

所以保存后,restore时,还需要定义整个网络,然后再restore把参数放进来

 

完成mnist手写体识别

参见 ComputerVision  - - - >  AI-woniu  - - - >  MINST_TheNeuralNetwork

训练完后,保存参数

先对自己上传的图片 进行 灰度处理,像素裁剪到28*28  再进行二值化处理

再进行test测试

 

常用网络模型

 

 

 

 

RNN

 

神经元与上下都有关系

使用w3 来保留中间这层的信息

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值