【tensorflow】Cifar10卷积神经网络实时训练过程

本文记录了使用TensorFlow在Cifar10数据集上进行卷积神经网络(CNN)的实时训练过程。训练中详细展示了准确率和损失曲线,并讨论了训练集和测试集的不同处理方式。最终达到的最好准确率为0.741,可能存在欠拟合问题。
摘要由CSDN通过智能技术生成

执行环境,可能出现
AttributeError: module 'tensorflow.python.ops.image_ops' has no attribute 'per_image_whitening'
需要将cifar10_input.py 的182行改为per_image_whitening 改为 per_image_standardization

cifar10_input.pycifar10.py 下载地址

#coding=utf-8
import cifar10,cifar10_input
from matplotlib import pyplot as plt
import tensorflow as tf
import numpy as np
import time
import math

max_steps = 4500
batch_size = 128

data_dir = './cifar10_data/cifar-10-batches-bin'
#下载好的数据集所在的文件夹

def variable_with_weight_loss(shape, stddev, wl):
    var = tf.Variable(tf.truncated_normal(shape, stddev=stddev))
    if wl is not None:
        weight_loss = tf.multiply(tf.nn.l2_loss(var), wl, name='weight_loss')
        ## wl 控制l2损失的比重
        tf.add_to_collection('losses', weight_loss) 
        ## 参数的l2损失加入到损失集合中
    return var


def loss(logits, labels):

    labels = tf.cast(labels, tf.int64)
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits, labels=labels, name='cross_entropy_per_example')
    # logits为[batch_size,num_classes]
    # labels为[batch_size,]的一维向量,其中每一个元素代表对应样本的类别
    # 先对网络的输出 Logits 进行 Softmax 概率化
    # Cross-Entropy 每个样本的交叉熵(损失)
    cross_entropy_mean = tf.reduce_mean(cross_entropy, name='cross_entropy')
    # 一个 batch 内样本求平均损失
    tf.add_to_collection('losses', cross_entropy_mean)
    
    return tf.add_n(tf.get_collection('losses'), name='total_loss') 
    # get 损失集合中所有损失,并相加后返回损失总和
  

# cifar10.maybe_download_and_extract()
# 如果没有下载,则需要将该行注释取消,
# 当然检查到 data_dir 目录下已经下载好的,则自动取消下载

images_train, labels_train = cifar10_input.distorted_inputs(data_dir=data_dir,
                                                            batch_size=batch_size)

images_test, labels_test = cifar10_input.inputs(eval_data=True,
                                                data_dir=data_dir,
                                                batch_size=batch_size)                                                  
#images_train, labels_train = cifar10.distorted_inputs()
#images_test, labels_test = cifar10.inputs(eval_data=True)

image_holder = tf.placeholder(tf.float32, [batch_size, 24, 24, 3])
label_holder = tf.placeholder(tf.int32, [batch_size])
#原始图像是32*32*3,distorted_inputs函数随机裁剪,旋转成24*24*3的尺寸
#inputs用于测试集,只在正中间裁剪成24*24*3的尺寸
#logits = inference(image_holder)

#############################第一层卷积层###################################
weight1 = variable_with_weight_loss(shape=[5, 5, 3, 64], stddev=5e-2, wl=0.0)
kernel1 = tf.nn.conv2d(image_holder, weight1, [1, 1, 1, 1], padding='SAME')
bias1 = tf.Variable(tf.constant(0.0, shape=[64]))
conv1 = tf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值