TensorFlow学习笔记8:CNN搭建(layer,estimator等)
来源:互联网 发布:阿里妈妈淘宝客优惠券 编辑:程序博客网 时间:2024/05/22 17:00
同样的,学习一下用layer等API来搭建CNN。
代码来源:https://github.com/aymericdamien/TensorFlow-Examples
首先,设置相关参数。
from __future__ import division, print_function, absolute_import# 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 np# Training Parameterslearning_rate = 0.001num_steps = 2000batch_size = 128# Network Parametersnum_input = 784 # MNIST data input (img shape: 28*28)num_classes = 10 # MNIST total classes (0-9 digits)dropout = 0.75 # Dropout, probability to keep units接下来,搭建神经网络。
# Create the neural networkdef 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如上述代码,第一个卷积层有32个5*5的filter,池化,第二个卷积层有64个3*3的filter,池化,flatten层转换为1维向量,经过全连接层,最后输出。
接下来,建立Estimator。
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 Estimatormodel = tf.estimator.Estimator(model_fn)定义了损失函数,进行优化,评估模型的准确率时直接调用了API。
接下来就可以进行训练和评估了。
# 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)# 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学习笔记8:CNN搭建(layer,estimator等)
- TensorFlow学习笔记6:神经网络搭建(layer,estimator等)
- TensorFlow学习笔记7:CNN搭建
- 深度学习---TensorFlow学习笔记:搭建CNN模型
- tensorflow学习笔记(五):cnn
- 使用tensorflow搭建CNN网络(3)---《深度学习》
- Tensorflow中的CNN layer参数(即用tensorflow框架实现简单CNN)
- Tensorflow深度学习笔记(九)--卷积神经网络(CNN)
- TensorFlow学习笔记(3)----CNN识别MNIST手写数字
- tensorflow学习笔记五:mnist实例--卷积神经网络(CNN)
- tensorflow学习笔记(六):cnn过程可视化
- TensorFlow学习笔记(8)----CNN分类CIFAR-10数据集
- 学习笔记TF042:TF.Learn、分布式Estimator、深度学习Estimator
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记--CNN精要及实现
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记2:构建CNN模型
- TensorFlow学习笔记2:构建CNN模型
- 登录验证
- Linux相关指令
- 11.28学习计划
- linux终端运行.sh文件
- MAC Safari Xcode VSCode Iterm常用快捷键
- TensorFlow学习笔记8:CNN搭建(layer,estimator等)
- groovy之closure
- dedecms织梦后台模板layui框架-20171126更新
- HDU 1274 展开字符串(递归)
- c语言函数
- 自己整理的知识点 “算法排序和查找”
- [python]使用channels库时遇到的一些问题
- 链式栈实现迷宫寻径
- Hibernate 级联删除异常 deleted object would be re-saved by cascade