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