使用tf.estimator创建一个简单的神经网络(对所用到的所有知识点进行讲解)

温馨提示

所有的知识点在后面的代码中都有用到哈,有的知识点在看得时候如果不理解也没事,看了最后的代码就会明白

知识点

下面开始讲解知识点

tf.estimator.inputs.numpy_input_fn

tf.estimator.inputs.numpy_input_fn(
    x,
    y=None,
    batch_size=128,
    num_epochs=1,
    shuffle=None,
    queue_capacity=1000,
    num_threads=1
)
'''
Args:
	x: numpy array object or dict of numpy array objects. If an array, the array will be treated as a single feature.
	y: numpy array object or dict of numpy array object. None if absent.
	batch_size: Integer, size of batches to return.
	num_epochs: Integer, number of epochs to iterate over data. If None will run forever.
	shuffle: Boolean, if True shuffles the queue. Avoid shuffle at prediction time.
	queue_capacity: Integer, size of queue to accumulate.
	num_threads: Integer, number of threads used for reading and enqueueing. In order to have predicted and repeatable order of reading and enqueueing, such as in prediction and evaluation mode, num_threads should be 1.

Returns:
        Function, that has signature of ()->(dict of features, targets)
'''

说明

返回一个函数,将numpy类型的数据 输入到 模型中
返回函数将基于 numpy arrays字典 输出 : features 和 targets ,
features 是一个字典 和 参数 x 具有相同的keys
如果y是一个词典 那么targets 和 y 也具有相同的keys(注意 y也可以不是词典)

例子

age = np.arange(4) * 1.0
height = np.arange(32, 36)
x = {'age': age, 'height': height}
y = np.arange(-32, -28)

with tf.Session() as session:
  input_fn = numpy_io.numpy_input_fn(
      x, y, batch_size=2, shuffle=False, num_epochs=1)

tf.layers.dense

tf.layers.dense(
    inputs,
    units,
    activation=None,
    use_bias=True,
    kernel_initializer=None,
    bias_initializer=tf.zeros_initializer(),
    kernel_regularizer=None,
    bias_regularizer=None,
    activity_regularizer=None,
    kernel_constraint=None,
    bias_constraint=None,
    trainable=True,
    name=None,
    reuse=None
)
'''
Arguments:
		inputs: Tensor input.
		units: Integer or Long, dimensionality of the output space.
		activation: Activation function (callable). Set it to None to maintain a linear activation.
		use_bias: Boolean, whether the layer uses a bias.
		kernel_initializer: Initializer function for the weight matrix. If None (default), weights are initialized using the default initializer used by tf.get_variable.
		bias_initializer: Initializer function for the bias.
		kernel_regularizer: Regularizer function for the weight matrix.
		bias_regularizer: Regularizer function for the bias.
		activity_regularizer: Regularizer function for the output.
		kernel_constraint: An optional projection function to be applied to the kernel after being updated by an Optimizer (e.g. used to implement norm constraints or value constraints for layer weights). The function must take as input the unprojected variable and must return the projected variable (which must have the same shape). Constraints are not safe to use when doing asynchronous distributed training.
		bias_constraint: An optional projection function to be applied to the bias after being updated by an Optimizer.
		trainable: Boolean, if True also add variables to the graph collection GraphKeys.TRAINABLE_VARIABLES (see tf.Variable).
		name: String, the name of the layer.
		reuse: Boolean, whether to reuse the weights of a previous layer by the same name.


Returns:
		Output tensor the same shape as inputs except the last dimension is of size units
'''

说明:

实现的操作是:
outputs = activation(inputs * kernel + bias)
返回的是一个tensor,也就是网络的输出
输出张量和输入张量形状相同,除了最后一维度的大小是 units
例如:inputs=[h1,h2,h3] units=h4
输出的是 [h1,h2,h4]

tf.nn.sparse_softmax_cross_entropy_with_logits

tf.nn.sparse_softmax_cross_entropy_with_logits(
    _sentinel=None,
    labels=None,
    logits=None,
    name=None
)
'''
Args:
	_sentinel: Used to prevent positional parameters. Internal, do not use.
	labels: Tensor of shape [d_0, d_1, ..., d_{r-1}] (where r is rank of labels and result) and dtype int32 or int64. Each entry in labels must be an index in [0, num_classes). Other values will raise an exception when this op is run on CPU, and return NaN for corresponding loss and gradient rows on GPU.
	logits: Unscaled log probabilities of shape [d_0, d_1, ..., d_{r-1}, num_classes] and dtype float16, float32, or float64.
	name: A name for the operation (optional).
'''

说明

logits 的类型必须是: float16, float32, or float64,
labels 的类型必须是: int32 or int64.

例子

我们这里为了理解就举一个3分类的例子
labels=2 这是真实的标签
logits=[1,5,4] #这是神经网络最后一层的输出
sparse_softmax_cross_entropy_with_logits

sparse的作用:
       将labels=2  变为 labels=[0,0,1]
softmax的作用:
       将logits=[1,5,4] 转化为一个概率分布 logits=[0.1,0.5,0.4]  
cross_entropy_with_logits的作用
       就是 计算[0,0,1] 和 [0.1,0.5,0.4] 的交叉熵

tf.train.GradientDescentOptimizer

详情请看本人这篇博客

tf.estimator.EstimatorSpec

详情请看本人这篇博客

tf.metrics.accuracy

详情请看本人这篇博客

tf.estimator.Estimator

详情请看本人这篇博客

以上知识点全部都在下面这个代码中用到

代码来源

#!/usr/bin/env python
# coding: utf-8

from __future__ import print_function

# 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
import matplotlib.pyplot as plt
import numpy as np

learning_rate = 0.1
num_steps = 1000
batch_size = 128
display_step = 100

# Network Parameters
n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)

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)

def neural_net(x_dict):
    x=x_dict['images']
    layer_1=tf.layers.dense(x,n_hidden_1)
    layer_2=tf.layers.dense(layer_1,n_hidden_2)
    out_layer=tf.layers.dense(layer_2,num_classes)
    return out_layer

   
def mode_fn(features,labels,mode):
    
    #这个函数返回是一个 tf.estimator.EstimatorSpec()
    logits=neural_net(features)
    
    pred_classes=tf.argmax(logits,axis=1)
    pred_probas=tf.nn.softmax(logits)
    
    #PREDICTS
    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,labels=tf.cast(labels,tf.int32)))
    optimizer=tf.train.GradientDescentOptimizer(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)
    
    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
    
   
model=tf.estimator.Estimator(mode_fn)

model.train(input_fn,steps=num_steps)

# Ealuate 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

model.evaluate(input_fn)  #注意这里的是动态了 因为这里的model都是一个model ,其参数是训练模型训练好的
# Predict single images
n_images = 4
# Get images from test set
test_images = mnist.test.images[:n_images]
# Prepare the input data
input_fn = tf.estimator.inputs.numpy_input_fn(
    x={'images': test_images}, shuffle=False)
# Use the model to predict the images class
preds = list(model.predict(input_fn))

# Display
for i in range(n_images):
    plt.imshow(np.reshape(test_images[i], [28, 28]), cmap='gray')
    plt.show()
    print("Model prediction:", preds[i])





猜你喜欢

转载自blog.csdn.net/qq_32806793/article/details/84994588