tensorflow CNN常用函数

tf.variable_scope

命名域(name scope):通过tf.name_scope()来实现;
变量域(variable scope):通过tf.variable_scope()来实现;可以通过设置reuse 标志以及初始化方式来影响域下的变量。
这两种作用域都会给tf.Variable()创建的变量加上词头,而tf.name_scope对tf.get_variable()创建的变量没有词头影响,

# Define a scope for reusing the variables
    with tf.variable_scope('ConvNet', reuse=reuse):

tf.layers.conv2d和tf.nn.conv2d
tf.nn.conv2d的filter是确切的矩阵:W_conv1 = weight_varible([5, 5, 1, 32])#卷积核
tf.layers.conv2d的filter是32。一个整数,代表滤波器的个数,也就是卷积核数或者输出空间的维数

# Convolution Layer with 32 filters and a kernel size of 5
conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

tf.layers.max_pooling2d
inputs: 进行池化的数据。
pool_size: 池化的核大小(pool_height, pool_width),如[3,3]. 如果长宽相等,也可以直接设置为一个数,如pool_size=3.
strides: 池化的滑动步长。可以设置为[1,1]这样的两个整数. 也可以直接设置为一个数,如strides=2。

# Max Pooling (down-sampling) with strides of 2 and kernel size of 2
conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

tf.contrib.layers.flatten
Class Flatten:在保留第0轴的情况下对输入的张量进行Flatten(扁平化)-[? , 1]
tensor ‘t’ is [1, 2, 3, 4, 5, 6, 7, 8, 9]
tensor ‘t’ 的形状就是 [9] reshape(t, [3, 3]) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

pass ‘[-1]’ 扁平化 ‘t’
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]

# Flatten the data to a 1-D vector for the fully connected layer
fc1 = tf.contrib.layers.flatten(conv2)

tf.layers.dense
1024-输出的维数

        # Fully connected layer (in tf contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)#输入和输出的维度

tf.layers.dropout
将Dropout(退出率)应用于输入.
Dropout包括在每次更新期间随机将输入单位的分数rate设置为0,这有助于防止过度拟合(overfitting).保留的单位按比例1 / (1 - rate)进行缩放,以便在训练时间和推理时间内它们的总和不变.

fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

tf.layers.dense
该层实现了操作:outputs = activation(inputs * kernel + bias),其中activation是作为activation参数传递的激活函数(如果不是None),是由层创建的权重矩阵,kernel是由层创建的权重矩阵,并且bias是由层创建的偏差向量(只有use_bias为True时).

out = tf.layers.dense(fc1, n_classes)

tf.argmax
此函数是对矩阵按行或列计算最大值

参数
input:输入Tensor
axis:0表示按列,1表示按行

pred_classes = tf.argmax(logits_test, axis=1)

tf.nn.softmax
作用:softmax函数的作用就是归一化。
输入: 全连接层(往往是模型的最后一层)的值,一般代码中叫做logits
输出: 归一化的值,含义是属于该位置的概率,一般代码叫做probs。

 pred_probas = tf.nn.softmax(logits_test)

tf.estimator.ModeKeys.PREDICT

# If prediction mode, early return
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

当Estimator的predict方法被调用时,model_fn会接收mode=ModeKeys.PREDICT。在这种情况下,model_fn必须返回一个包含prediction的tf.estimator.EstimatorSpec。

模型必须在做出预测之前被训练。被训练后的模型存储在model_dir目录下,该目录可以在你实例化Estimator时确定。

tf.estimator.EstimatorSpec
mode:A ModeKeys。指定这是训练,评估还是预测。
predictions:预测张量或张量的dict。
loss:训练损失张量。必须为标量或形状为[1]。
train_op:训练步骤的操作。
eval_metric_ops:Dict of metric results keyed by name。
export_outputs:描述要输出到SavedModel并在服务期间使用的输出签名。
name:此输出的任意名称。
output:ExportOutput对象,
training_chief_hooks:tf.train.SessionRunHook对象的迭代可在培训期间在首席工作者上运行。
training_hooks:tf.train.SessionRunHook对象可迭代,以在培训期间在所有工作程序上运行。
scaffold:一个tf.train.Scaffold对象,可用于设置初始化,保护程序以及更多用于训练的对象。
Evaluation_hooks:tf.train.SessionRunHook对象的迭代可在评估期间运行。
projection_hooks:tf.train.SessionRunHook对象的可迭代性,可在预测期间运行。

if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

tf.reduce_mean
输入的待降维的tensor;

loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
        logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))

tf.nn.sparse_softmax_cross_entropy_with_logits
labels的每一行labels[i]必须为一个概率分布
我们可以把logist理解为原生态的、未经缩放的,可视为一种未归一化的l“概率替代物”,如[4, 1, -2]。它可以是其他分类器(如逻辑回归等、SVM等)的输出。

tf.cast
用于改变某个张量的数据类型

tf.train.AdamOptimizer
是一个寻找全局最优点的优化算法。
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)

optimizer.minimize
loss:包含最小化值的张量。
global_step:可选变量,在变量已更新后增加一。

    train_op = optimizer.minimize(loss_op,
                                  global_step=tf.train.get_global_step())

tf.train.get_global_step
tf.train.get_global_step()仅使您获得定义为存储全局步长的当前变量(如果已定义)。

tf.metrics.accuracy
计算predictions匹配labels的情况。

labels:ground truth值,Tensor,其形状匹配 predictions。

predictions:预测值,任何形状的Tensor。

acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)

tf.estimator.Estimator
Build the Estimator

model = tf.estimator.Estimator(model_fn)

tf.estimator.inputs.numpy_input_fn
产生输入管道

input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'images': mnist.test.images}, y=mnist.test.labels,
    batch_size=batch_size, shuffle=False)

batch_size=batch_size
batch: batch是批 比如50算一批,100算一批

model.train
model.eval(),pytorch会自动把BN和DropOut固定住,不会取平均,而是用训练好的值。不然的话,一旦test的batch_size过小,很容易就会被BN层导致生成图片颜色失真极大;在模型测试阶段使用

model.train() 让model变成训练模式,此时 dropout和batch normalization的操作在训练q起到防止网络过拟合的问题

拓展:一般来说,我们在可能发生过拟合的情况下才会使用dropout等正则化技术。那什么时候可能会发生呢?比如神经网络过深,或训练时间过长,或没有足够多的数据时。那为什么dropout能有效防止过拟合呢?可以理解为,我们每次训练迭代时,随机选择一批单元不参与训练,这使得每个单元不会依赖于特定的前缀单元,因此具有一定的独立性;同样可以看成我们拿同样的数据在训练不同的网络,每个网络都有可能过拟合,但迭代多次后,这种过拟合会被抵消掉。要注意的是,dropout是体现在训练环节,训练完成后,我们认为所有的单元都被训练好了,在验证或测试阶段,我们是拿完整的神经网络去验证或测试。

https://blog.csdn.net/hjimce/article/details/50866313

model.train(input_fn, steps=num_steps)

model.evaluate

# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'images': mnist.test.images}, y=mnist.test.labels,
    batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
e = model.evaluate(input_fn)

完整代码

from __future__ import division, print_function, absolute_import

# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=False)

import tensorflow as tf

# Training Parameters
learning_rate = 0.001
num_steps = 2000
batch_size = 128

# Network Parameters,
#代替之前的placeholder
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)
dropout = 0.25 # Dropout, probability to drop a unit


# Create the neural network
def conv_net(x_dict, n_classes, dropout, reuse, is_training):
    # Define a scope for reusing the variables
    with tf.variable_scope('ConvNet', reuse=reuse):
        # TF Estimator input is a dict, in case of multiple inputs
        x = x_dict['images']

        # MNIST data input is a 1-D vector of 784 features (28*28 pixels)
        # Reshape to match picture format [Height x Width x Channel]
        # Tensor input become 4-D: [Batch Size, Height, Width, Channel]
        x = tf.reshape(x, shape=[-1, 28, 28, 1])

        # Convolution Layer with 32 filters and a kernel size of 5
        conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv1 = tf.layers.max_pooling2d(conv1, 2, 2)

        # Convolution Layer with 64 filters and a kernel size of 3
        conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
        # Max Pooling (down-sampling) with strides of 2 and kernel size of 2
        conv2 = tf.layers.max_pooling2d(conv2, 2, 2)

        # Flatten the data to a 1-D vector for the fully connected layer
        fc1 = tf.contrib.layers.flatten(conv2)

        # Fully connected layer (in tf contrib folder for now)
        fc1 = tf.layers.dense(fc1, 1024)#输入和输出的维度
        # Apply Dropout (if is_training is False, dropout is not applied)
        fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)

        # Output layer, class prediction
        out = tf.layers.dense(fc1, n_classes)

    return out


# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):
    # Build the neural network
    # Because Dropout have different behavior at training and prediction time, we
    # need to create 2 distinct computation graphs that still share the same weights.
    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)

    # Predictions
    pred_classes = tf.argmax(logits_test, axis=1)
    pred_probas = tf.nn.softmax(logits_test)

    # If prediction mode, early return
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)

        # Define loss and optimizer
    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())

    # Evaluate the accuracy of the model
    acc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)

    # TF Estimators requires to return a EstimatorSpec, that specify
    # the different ops for training, evaluating, ...
    estim_specs = tf.estimator.EstimatorSpec(
        mode=mode,
        predictions=pred_classes,
        loss=loss_op,
        train_op=train_op,
        eval_metric_ops={'accuracy': acc_op})

    return estim_specs

# Build the Estimator
model = tf.estimator.Estimator(model_fn)

# Define the input function for training
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)
# Train the Model
model.train(input_fn, steps=num_steps)

# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'images': mnist.test.images}, y=mnist.test.labels,
    batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
e = model.evaluate(input_fn)

print("Testing Accuracy:", e['accuracy'])
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值