tensorflow53 《面向机器智能的TensorFlow实战》笔记-05-01 卷积基础
来源:互联网 发布:制作淘宝无缝拼接主图 编辑:程序博客网 时间:2024/06/04 19:11
01 get_shape
# 《面向机器智能的Tensor Flow实战》05 目标识别与分类# win10 Tensorflow-gpu1.1.0 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# 原书代码(tensorflow0.8):https://github.com/backstopmedia/tensorflowbook# tensorflow不同版本api变化:https://github.com/tensorflow/tensorflow/releases# filename:tfmi05.01.py # get_shapeimport tensorflow as tfimport numpy as npsess = tf.InteractiveSession()image_batch = tf.constant([ [ # First Image [[0, 255, 0], [0, 255, 0], [0, 255, 0]], [[0, 255, 0], [0, 255, 0], [0, 255, 0]] ], [ # Second Image [[0, 0, 255], [0, 0, 255], [0, 0, 255]], [[0, 0, 255], [0, 0, 255], [0, 0, 255]] ] ])image_batch.get_shape()sess.run(image_batch)[0][0][0]print(image_batch) # Tensor("Const:0", shape=(2, 2, 3, 3), dtype=int32)print(sess.run(image_batch)[0][0][0]) # [ 0 255 0]
02 卷积
# 《面向机器智能的Tensor Flow实战》05 目标识别与分类# win10 Tensorflow-gpu1.1.0 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# 原书代码(tensorflow0.8):https://github.com/backstopmedia/tensorflowbook# tensorflow不同版本api变化:https://github.com/tensorflow/tensorflow/releases# filename:tfmi05.02.py # 卷积import tensorflow as tfimport numpy as npsess = tf.InteractiveSession()input_batch = tf.constant([ [ # First Input [[0.0], [1.0]], [[2.0], [3.0]] ], [ # Second Input [[2.0], [4.0]], [[6.0], [8.0]] ] ])kernel = tf.constant([ [ [[1.0, 2.0]] ] ])conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 1, 1, 1], padding='SAME')sess.run(conv2d)print(conv2d.eval())'''[[[[ 0. 0.] [ 1. 2.]] [[ 2. 4.] [ 3. 6.]]] [[[ 2. 4.] [ 4. 8.]] [[ 6. 12.] [ 8. 16.]]]]'''lower_right_image_pixel = sess.run(input_batch)[0][1][1]lower_right_kernel_pixel = sess.run(conv2d)[0][1][1]lower_right_image_pixel, lower_right_kernel_pixelprint("lower_right_image_pixel: ", lower_right_image_pixel)print("lower_right_kernel_pixel: ", lower_right_kernel_pixel)print("lower_right_image_pixel: ", lower_right_image_pixel)'''lower_right_image_pixel: [ 3.]lower_right_kernel_pixel: [ 3. 6.]lower_right_image_pixel: [ 3.]'''input_batch = tf.constant([ [ # First Input (6x6x1) [[0.0], [1.0], [2.0], [3.0], [4.0], [5.0]], [[0.1], [1.1], [2.1], [3.1], [4.1], [5.1]], [[0.2], [1.2], [2.2], [3.2], [4.2], [5.2]], [[0.3], [1.3], [2.3], [3.3], [4.3], [5.3]], [[0.4], [1.4], [2.4], [3.4], [4.4], [5.4]], [[0.5], [1.5], [2.5], [3.5], [4.5], [5.5]], ], ])kernel = tf.constant([ # Kernel (3x3x1) [[[0.0]], [[0.5]], [[0.0]]], [[[0.0]], [[1.0]], [[0.0]]], [[[0.0]], [[0.5]], [[0.0]]] ])# NOTE: the change in the size of the strides parameter.conv2d = tf.nn.conv2d(input_batch, kernel, strides=[1, 3, 3, 1], padding='SAME')sess.run(conv2d)print(conv2d.eval())'''[[[[ 2.20000005] [ 8.19999981]] [[ 2.79999995] [ 8.80000019]]]]'''import matplotlib as mil#mil.use('svg')mil.use("nbagg")from matplotlib import pyplotfig = pyplot.gcf()fig.set_size_inches(4, 4)
03 激活函数/池化/归一化/高级层
# 《面向机器智能的Tensor Flow实战》05 目标识别与分类# win10 Tensorflow-gpu1.1.0 python3.5.3# CUDA v8.0 cudnn-8.0-windows10-x64-v5.1# 原书代码(tensorflow0.8):https://github.com/backstopmedia/tensorflowbook# tensorflow不同版本api变化:https://github.com/tensorflow/tensorflow/releases# filename:tfmi05.03.py # 激活函数/池化/归一化/高级层import tensorflow as tfimport numpy as npsess = tf.InteractiveSession()features = tf.range(-2, 3)# Keep note of the value for negative featuressess.run([features, tf.nn.relu(features)])print([features, tf.nn.relu(features)]) # [<tf.Tensor 'range:0' shape=(5,) dtype=int32>, <tf.Tensor 'Relu_1:0' shape=(5,) dtype=int32>]print(features.eval(), tf.nn.relu(features).eval()) # [-2 -1 0 1 2] [0 0 0 1 2]features = tf.to_float(tf.range(-1, 3))sess.run([features, tf.sigmoid(features)])print(features.eval(), tf.sigmoid(features).eval()) # [-1. 0. 1. 2.] [ 0.26894143 0.5 0.7310586 0.88079703]features = tf.to_float(tf.range(-1, 3))sess.run([features, tf.tanh(features)])print(features.eval(), tf.tanh(features).eval()) # [-1. 0. 1. 2.] [-0.76159418 0. 0.76159418 0.96402758]features = tf.constant([-0.1, 0.0, 0.1, 0.2])sess.run([features, tf.nn.dropout(features, keep_prob=0.5)])print(features.eval(), tf.nn.dropout(features, keep_prob=0.5).eval()) # [-0.1 0. 0.1 0.2] [-0. 0. 0. 0.40000001]batch_size=1input_height = 3input_width = 3input_channels = 1layer_input = tf.constant([ [ [[1.0], [0.2], [1.5]], [[0.1], [1.2], [1.4]], [[1.1], [0.4], [0.4]] ] ])# The strides will look at the entire input by using the image_height and image_widthkernel = [batch_size, input_height, input_width, input_channels]max_pool = tf.nn.max_pool(layer_input, kernel, [1, 1, 1, 1], "VALID")sess.run(max_pool)print(max_pool.eval()) # [[[[ 1.5]]]]batch_size=1input_height = 3input_width = 3input_channels = 1layer_input = tf.constant([ [ [[1.0], [1.0], [1.0]], [[1.0], [0.5], [0.0]], [[0.0], [0.0], [0.0]] ] ])# The strides will look at the entire input by using the image_height and image_widthkernel = [batch_size, input_height, input_width, input_channels]max_pool = tf.nn.avg_pool(layer_input, kernel, [1, 1, 1, 1], "VALID")sess.run(max_pool)print(max_pool) # Tensor("AvgPool:0", shape=(1, 1, 1, 1), dtype=float32)print(max_pool.eval()) # [[[[ 0.5]]]]layer_input = tf.constant([ [[[ 1.]], [[ 2.]], [[ 3.]]] ])lrn = tf.nn.local_response_normalization(layer_input)sess.run([layer_input, lrn])print(layer_input.eval(), lrn.eval())'''[[[[ 1.]] [[ 2.]] [[ 3.]]]] [[[[ 0.70710677]] [[ 0.89442718]] [[ 0.94868326]]]]'''image_input = tf.constant([ [ [[0., 0., 0.], [255., 255., 255.], [254., 0., 0.]], [[0., 191., 0.], [3., 108., 233.], [0., 191., 0.]], [[254., 0., 0.], [255., 255., 255.], [0., 0., 0.]] ] ])conv2d = tf.contrib.layers.convolution2d( image_input, num_outputs=4, kernel_size=(1,1), # It's only the filter height and width. activation_fn=tf.nn.relu, stride=(1, 1), # Skips the stride values for image_batch and input_channels. trainable=True)# It's required to initialize the variables used in convolution2d's setup.sess.run(tf.global_variables_initializer())sess.run(conv2d)print(conv2d.eval())'''[[[[ 0. 0. 0. 0. ] [ 9.16278458 0. 56.88573456 0. ] [ 0. 87.55300903 0. 11.87749958]] [[ 130.84643555 0. 48.07785034 0. ] [ 75.74397278 0. 106.45916748 0. ] [ 130.84643555 0. 48.07785034 0. ]] [[ 0. 87.55300903 0. 11.87749958] [ 9.16278458 0. 56.88573456 0. ] [ 0. 0. 0. 0. ]]]]'''features = tf.constant([[[1.2], [3.4]]])fc = tf.contrib.layers.fully_connected(features, num_outputs=2)sess.run(tf.global_variables_initializer())sess.run(fc)print(fc.eval())'''[[[ 0.24772798 0. ] [ 0.70189595 0. ]]]'''
阅读全文
0 0
- tensorflow53 《面向机器智能的TensorFlow实战》笔记-05-01 卷积基础
- tensorflow45 《面向机器智能的TensorFlow实战》笔记-02-01 测试tensorflow1.1.0可用
- tensorflow46 《面向机器智能的TensorFlow实战》笔记-03-01 TensorBoard基本操作
- tensorflow49 《面向机器智能的TensorFlow实战》笔记-04-01 线性回归
- tensorflow48 《面向机器智能的TensorFlow实战》笔记-03-03 tensorflow基本矢量图、summary、feed_dict
- tensorflow47 《面向机器智能的TensorFlow实战》笔记-03-02 占位符、名称空间
- tensorflow50 《面向机器智能的TensorFlow实战》笔记-04-02 Sigmoid
- tensorflow51 《面向机器智能的TensorFlow实战》笔记-04-03 softmax
- 面向机器智能的 TensorFlow 实践
- TensorFlow学习笔记6——《面向机器智能的TensorFlow实践》StanfordDog例程修改记录
- 牛逼哥TensorFlow资源分享:面向机器智能的TensorFlow实践
- tensorflow32《TensorFlow实战》笔记-05 TensorFlow实现卷积神经网络 code
- TensorFlow学习笔记11——《面向机器智能的tensorflow实践》第5.5节Stanford Dogs例程实现
- tensorflow29《TensorFlow实战》笔记-01 TensorFlow基础
- tensorflow12《TensorFlow实战Google深度学习框架》笔记-06-01卷积层、池化层样例code
- 基于Tensorflow的机器学习(6) -- 卷积神经网络
- Tensorflow实战学习(八)【机器学习基础 线性回归】
- tensorflow:2)卷积神经网络基础
- bzoj1751 [Usaco2005 qua]Lake Counting
- Deep Learning(深度学习)学习笔记整理系列之(七)
- 操作系统之进程同步
- Deep Learning(深度学习)学习笔记整理系列之(八)
- windows环境使用composer安装YII2.0
- tensorflow53 《面向机器智能的TensorFlow实战》笔记-05-01 卷积基础
- fl2440串口编程
- Java Web----实训项目第一阶段
- 创建位图索引
- cxf发布rest风格的webService
- linux find exec rm
- 在linux发布网上商城案例
- Unity ResUpdater研究使用1
- Linux特训之vi编辑器part 1(基本操作)