应用TensorFlow高级API构建卷积神经网络(1)--代码

应用TensorFlow高级API构建卷积神经网络

  • 两个卷积层,两个全连接层
  • 输入 [sample * 28 * 28 * 1 ] (灰度图)
  • [ 28 * 28 1 ] --> (32个卷积核,每个大小551,sample方式卷积) --> [ 28 * 28 * 32] --> (池化 22 ,步长2)–> [14 *14 *32]
  • [ 14 * 14 32] --> (64个卷积核,每个大小55*32,sample方式卷积) --> [14 * 14 64] --> (池化 22 ,步长2)–> [7 * 7 *64]
  • [ 7 * 7 * 64] --> reshape 成列向量 --> (7 * 7 * 64)
  • [sample * (7764)] 全连接层1 weights:[7764 , 1024] --> [sample * 1024]
  • [sample * 1024] 全连接层2 weights:[1024,10] --> [sample *10]
  • 输出:10个分类
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np

mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)
# 参数
learning_rate = 0.001
num_steps = 2000
batch_size = 128
# 网络参数
num_input = 784
num_classes = 10
dropout = 0.25#丢弃的比例
# 定义卷积网络
def conv_net(x_dict, n_classes, dropout, reuse, is_training):
    with tf.variable_scope('ConvNet', reuse=reuse):
        x = x_dict['images']
        # 4-D: [Batch Size, Height, Width, Channel]
        x= tf.reshape(x, shape=[-1,28,28,1])
        conv1 = tf.layers.conv2d(x,32,5,activation=tf.nn.relu)
        conv1 = tf.layers.max_pooling2d(conv1,2,2)
        
        conv2 = tf.layers.conv2d(conv1,64,3,activation=tf.nn.relu)
        conv2 = tf.layers.max_pooling2d(conv2,2,2)
        
        # fc1 = tf.contrib.layers.flatten(conv2)
        # flattened = tf.reshape(x, [tf.shape(x)[0], -1])
        fc1 = tf.layers.flatten(conv2)
        fc1 = tf.layers.dense(fc1,1024)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
        
        out = tf.layers.dense(fc1, n_classes)
        
    return out
# 定义模型函数
def model_fn(features, labels, mode):
    logits_train = conv_net(features,num_classes,dropout,reuse=False,is_training=True)
    logits_test = conv_net(features,num_classes,dropout,reuse=True,is_training=False)
    
    pred_classes = tf.argmax(logits_test, axis=1)
    pred_probas = tf.nn.softmax(logits_test)
    # 预测模式
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
    # 训练模式
    # 损失函数及优化器
    loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
    # 准确率
    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)
    
    return tf.estimator.EstimatorSpec(
            mode=mode,
            predictions=pred_classes,
            loss=loss_op,
            train_op=train_op,
            eval_metric_ops={'accuracy':acc_op}
            )
# 构建评价器    
model = tf.estimator.Estimator(model_fn)
# 训练的输入函数
input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'images':mnist.train.images},
        y=mnist.train.labels,
        batch_size=batch_size,
        num_epochs=None,shuffle=True
        )
# 训练模型
model.train(input_fn, steps=num_steps)

# 评价模型
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'images': mnist.test.images}, y=mnist.test.labels,
    batch_size=batch_size, shuffle=False)
model.evaluate(input_fn)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值