3用于MNIST的卷积神经网络-3.1/3.2简单卷积神经网络的计算图设计(上/下)

一个简单的卷积神经网络
这里写图片描述

这里写图片描述

#3.1实现简单卷积神经网络对MNIST数据集进行分类:conv2d + activation + pool + fc

import csv
import tensorflow as tf
import os
from tensorflow.examples.tutorials.mnist import input_data
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#设置算法超参数
learning_rate_init = 0.001
training_epochs = 1
batch_size = 100
display_step = 10

#Network Parameters
n_input = 784 #MNIST data input(img shape:28*28)
n_class = 10 #MNIST total classes(0-9 digits)

#根据指定的维数返回初始化好的指定名称和权重 Variable
def WeightsVariable(shape,name_str,stddev=0.1):
    initial = tf.random_normal(shape=shape,stddev=stddev,dtype=tf.float32)
    return tf.Variable(initial,dtype=tf.float32,name=name_str)

#根据指定的维数返回初始化好的指定名称的偏置Variable
def BiasesVariable(shape,name_str,stddev=0.00001):
    initial = tf.random_normal(shape=shape, stddev=stddev, dtype=tf.float32)
    return tf.Variable(initial,dtype=tf.float32,name=name_str)

#2维卷积层(conv2d + bias)封装
def Conv2d(x,W,b,stride=1,padding='SAME'):
    with tf.name_scope('Wx_b'):
        y = tf.nn.conv2d(x,W,strides=[1,stride,stride,1],padding=padding)
        y = tf.nn.bias_add(y,b)
    return y

#非线性激活层的封装
def Activation(x,activation=tf.nn.relu,name='relu'):
    with tf.name_scope(name):
        y = activation(x)
    return y

#2维池化层pool的封装
def Pool2d(x,pool=tf.nn.max_pool,k=2,stride=2):
    return pool(x,ksize=[1,k,k,1],strides=[1,stride,stride,1],padding='VALID')

#全连接层activate(wx+b)的封装
def FullyConnnected(x,W,b,activate=tf.nn.relu,act_name='relu'):
    with tf.name_scope('Wx_b'):
        y = tf.matmul(x,W)
        y = tf.add(y,b)
    with tf.name_scope(act_name):
        y = activate(y)
    return y

#调用上面写的函数构造计算图
with tf.Graph().as_default():
    #计算图输入
    with tf.name_scope('Inputs'):
        X_origin = tf.placeholder(tf.float32,[None,n_input],name='X_origin')
        Y_true = tf.placeholder(tf.float32, [None, n_class], name='Y_true')
        #把图像数据从N*784的张量转换为N*28*28*1的张量
        X_image = tf.reshape(X_origin,[-1,28,28,1])
    #计算图向前推断过程
    with tf.name_scope('Inference'):
        #第一个卷积层(conv2d + biase)
        with tf.name_scope('Conv2d'):
            weights = WeightsVariable(shape=[5,5,1,16],name_str='weights')
            biases = BiasesVariable(shape=[16],name_str='biases')
            conv_out = Conv2d(X_image,weights,biases,stride=1,padding='VALID')

        #非线性激活层
        with tf.name_scope('Activate'):
            activate_out = Activation(conv_out,activation=tf.nn.relu,name='relu')

        #第一个池化层(max pool 2d)
        with tf.name_scope('Pool2d'):
            pool_out = Pool2d(activate_out,pool=tf.nn.max_pool,k=2,stride=2)

        #将二维特征图变换为一维特征向量
        with tf.name_scope('FeatsReshape'):
            features = tf.reshape(pool_out,[-1,12*12*16])

        #第一个全连接层(fully connected layer)
        with tf.name_scope('FC_Linear'):
            weights = WeightsVariable(shape=[12 * 12 * 16,n_class],name_str='weights')
            biases = BiasesVariable(shape=[n_class],name_str='biases')
            Ypred_logits = FullyConnnected(features,weights,biases,activate=tf.identity,act_name='identity')

    #定义损失层(loss layer)
    with tf.name_scope('Loss'):
        cross_entropy_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = Y_true,logits = Ypred_logits))

    #定义优化训练层(train layer)
    with tf.name_scope('Train'):
        learning_rate = tf.placeholder(tf.float32)
        optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
        trainer = optimizer.minimize(cross_entropy_loss)

    #定义模型评估层(evaluate layer)
    with tf.name_scope('Evaluate'):
        correct_pred = tf.equal(tf.arg_max(Ypred_logits,1),tf.arg_max(Y_true,1))
        accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))

    #添加所有变量的初始化节点
    init = tf.global_variables_initializer()

    print('把计算图写入事件文件,在TensorBoard里面查看')
    summary_writer = tf.summary.FileWriter(logdir='logs',graph=tf.get_default_graph())
    summary_writer.close()

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!Fashion-MNIST是一个流行的像分类数据集,用于训练和测试机器学习模型。卷积神经网络(Convolutional Neural Network,CNN)是一种常用的深度学习模型,特别适用于像相关的任务。 要使用卷积神经网络对Fashion-MNIST数据集进行分类,通常需要以下步骤: 1. 准备数据集:首先,您需要下载Fashion-MNIST数据集并加载到您的程序中。这个数据集包含了10个类别的服装像,每个类别有6000张训练像和1000张测试像。 2. 数据预处理:在训练模型之前,您需要对数据进行预处理。这通常包括将像素值归一化到0到1之间,将标签转换为独热编码(one-hot encoding),以及将数据划分为训练集和验证集。 3. 构建卷积神经网络模型:使用卷积层、池化层和全连接层来构建您的卷积神经网络模型。您可以选择不同的架构和超参数来优化模型性能。 4. 模型训练:使用训练集对模型进行训练。通过反向传播和梯度下降算法,不断调整模型参数以最小化损失函数。 5. 模型评估:使用测试集评估模型的性能。计算准确率、精确率、召回率等指标,了解模型在未见过的数据上的表现。 6. 模型优化:根据评估结果,对模型进行调整和优化。可以尝试不同的网络架构、正则化技术、优化算法等来提升模型性能。 这只是一个简要的概述,实际上在实现Fashion-MNIST卷积神经网络时,还需要考虑数据增强、调参等细节。如果您需要更具体的代码实现或深入的解释,请告诉我。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值