TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
来源:互联网 发布:小额网络个人投资收益 编辑:程序博客网 时间:2024/06/09 20:21
这次来看一看如何用layer等API来搭建神经网络。
from __future__ import print_function# Import MNIST datafrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=False)import tensorflow as tfimport matplotlib.pyplot as pltimport numpy as nplearning_rate = 0.1num_steps = 1000batch_size = 128display_step = 100# Network Parametersn_hidden_1 = 256 # 1st layer number of neuronsn_hidden_2 = 256 # 2nd layer number of neuronsnum_input = 784 # MNIST data input (img shape: 28*28)num_classes = 10 # MNIST total classes (0-9 digits)# Define the input function for traininginput_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 networkdef 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 Estimatormodel = tf.estimator.Estimator(model_fn)开始训练:
# Train the Modelmodel.train(input_fn, steps=num_steps)对训练好的模型进行评估:
# Evaluate the Model# Define the input function for evaluatinginput_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' methodmodel.evaluate(input_fn)
阅读全文
0 0
- TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
- TensorFlow学习笔记8:CNN搭建(layer,estimator等)
- TensorFlow学习笔记(九)tf搭建神经网络基本流程
- TensorFlow学习笔记(十)tf搭建神经网络可视化结果
- TensorFlow学习笔记5:神经网络搭建
- 莫烦 tensorflow 笔记 (一)搭建神经网络
- tensorflow 学习笔记4 搭建神经网络基本流程
- TensorFlow学习笔记(二):实现神经网络
- TensorFlow学习笔记(三):深层神经网络
- Tensorflow 搭建神经网络(单层)
- 学习笔记TF042:TF.Learn、分布式Estimator、深度学习Estimator
- tensorflow 学习笔记6 TensorBoard可视化神经网络过程
- Tensorflow深度学习笔记(九)--卷积神经网络(CNN)
- tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)
- 学习笔记:自编码神经网络 TensorFlow(代码)
- TensorFlow学习笔记(4)——深层神经网络
- TensorFlow 深度学习笔记 卷积神经网络
- TensorFlow深度学习笔记 循环神经网络实践
- CC26xx之内置温度传感器的使用+ 串口配置
- (转)Android之高德地图SDK配置及简单使用详解
- linux工作管理
- TCP端口状态说明
- Windows操作系统关机或者重启的诊断方法
- TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
- java8时间---java.time.LocalDate
- 字典学习与时间稀疏动态磁共振数据重建(三)
- [译]集群调度架构的变革
- javascript学习(十七)— jQuery选择器(一)
- android进阶篇之View——基础篇(View移动)
- 松柏先生·每日一例:搪瓷,终将以全新的面貌复兴
- Spring 框架简介
- 云计算安全防护和网络监控的应用