TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
2017-11-28 14:57
716 查看
这次来看一看如何用layer等API来搭建神经网络。
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)
# 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)基本的参数设置和上一篇博客中没有区别。
这里,输入函数numpy_input_fn(x, y=None, batch_size=128, num_epochs=1, shuffle=None, queue_capacity=1000, num_threads=1) 中x必须为一个字典,y为数组或者None。
接下来定义神经网络。
# Define the neural network
def neural_net(x_dict):
# TF Estimator input is a dict, in case of multiple inputs
x = x_dict['images']
# Hidden fully connected layer with 256 neurons
layer_1 = tf.layers.dense(x, n_hidden_1)
# Hidden fully connected layer with 256 neurons
layer_2 = tf.layers.dense(layer_1, n_hidden_2)
# Output fully connected layer with a neuron for each class
out_layer = tf.layers.dense(layer_2, num_classes)
return out_layer可以看到,与上一篇博客不同,这里用tf.layers.dense 直接建立了全连接层。
接下来,建立estimator。先定义model_fn, 建立神经网络,定义损失函数,优化,并且进行相关的评估工作。
model = tf.estimator.Estimator(model_fn) 可以建立estimator。
# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):
# Build the neural network
logits = neural_net(features)
# Predictions
pred_classes = tf.argmax(logits, axis=1)
pred_probas = tf.nn.softmax(logits)
# 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, labels=tf.cast(labels, dtype=tf.int32)))
optimizer = tf.train.GradientDescentOptimizer(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
# 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
model.evaluate(input_fn)
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)
# 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)基本的参数设置和上一篇博客中没有区别。
这里,输入函数numpy_input_fn(x, y=None, batch_size=128, num_epochs=1, shuffle=None, queue_capacity=1000, num_threads=1) 中x必须为一个字典,y为数组或者None。
接下来定义神经网络。
# Define the neural network
def neural_net(x_dict):
# TF Estimator input is a dict, in case of multiple inputs
x = x_dict['images']
# Hidden fully connected layer with 256 neurons
layer_1 = tf.layers.dense(x, n_hidden_1)
# Hidden fully connected layer with 256 neurons
layer_2 = tf.layers.dense(layer_1, n_hidden_2)
# Output fully connected layer with a neuron for each class
out_layer = tf.layers.dense(layer_2, num_classes)
return out_layer可以看到,与上一篇博客不同,这里用tf.layers.dense 直接建立了全连接层。
接下来,建立estimator。先定义model_fn, 建立神经网络,定义损失函数,优化,并且进行相关的评估工作。
model = tf.estimator.Estimator(model_fn) 可以建立estimator。
# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):
# Build the neural network
logits = neural_net(features)
# Predictions
pred_classes = tf.argmax(logits, axis=1)
pred_probas = tf.nn.softmax(logits)
# 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, labels=tf.cast(labels, dtype=tf.int32)))
optimizer = tf.train.GradientDescentOptimizer(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)开始训练:
# 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
model.evaluate(input_fn)
相关文章推荐
- 【懒懒的Tensorflow学习笔记三之搭建简单的神经网络模型】
- TensorFlow学习笔记5:神经网络搭建
- TensorFlow学习笔记8:CNN搭建(layer,estimator等)
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- tensorflow 学习笔记(四) - mnist实例--用简单的神经网络来训练和测试
- tensorflow 学习笔记11 最简单的循环神经网络(RNN)
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- 学习笔记:自编码神经网络 TensorFlow(代码)
- 莫烦 tensorflow 笔记 (一)搭建神经网络
- tensorflow 学习笔记12 循环神经网络RNN LSTM结构实现MNIST手写识别
- 学习笔记TF055:TensorFlow神经网络简单实现一元二次函数
- TensorFlow深度学习笔记 循环神经网络实践
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- 对比学习用 Keras 搭建 CNN RNN 等常用神经网络和其后端切换(Theano和TensorFlow)
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- tensorflow 学习笔记7 普通神经网络实现mnist手写识别
- TensorFlow 深度学习笔记 TensorFlow实现与优化深度神经网络
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试(MNIST For ML Beginners)
- 深度学习——利用学习框架TensorFlow搭建深层神经网络DNN
- 学习笔记TF052:卷积网络,神经网络发展,AlexNet的TensorFlow实现