tensorflow 读取cifar_TensorFlow实战4——TensorFlow实现Cifar10识别

1 importcifar10, cifar10_input2 importtensorflow as tf3 importnumpy as np4 importtime5 importmath6

7 max_steps = 3000

8 batch_size = 128

9 data_dir = '/tmp/cifar10_data/cifar-10-batches-bin'

10

11

12 defvariable_with_weight_loss(shape, stddev, w1):13 '''定义初始化weight函数,使用tf.truncated_normal截断的正态分布,但加上L2的loss,相当于做了一个L2的正则化处理'''

14 var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))15 '''w1:控制L2 loss的大小,tf.nn.l2_loss函数计算weight的L2 loss'''

16 if wl is notNone:17 weight_loss = tf.multiply(tf.nn.l2_loss(var), w1, name='weight_loss')18 '''tf.add_to_collection:把weight losses统一存到一个collection,名为losses'''

19 tf.add_to_collection('losses', weight_loss)20

21 returnvar22

23

24 #使用cifar10类下载数据集并解压展开到默认位置

25 cifar10.maybe_download_and_extract()26

27 '''distored_inputs函数产生训练需要使用的数据,包括特征和其对应的label,28 返回已经封装好的tensor,每次执行都会生成一个batch_size的数量的样本'''

29 images_train, labels_train = cifar10_input.distored_inputs(data_dir=data_dir,30 batch_size=batch_size)31

32 images_test, l

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值