Tensorflow:一维数据构造简单CNN

# Implementing Different Layers
# ---------------------------------------
#
# We will illustrate how to use different types
# of layers in TensorFlow
#
# The layers of interest are:
#  (1) Convolutional Layer卷积层
#  (2) Activation Layer激活层
#  (3) Max-Pool Layer池化层
#  (4) Fully Connected Layer 全连接层
#
# We will generate two different data sets for this
#  script, a 1-D data set (row of data) and
#  a 2-D data set (similar to picture)

import tensorflow as tf
import matplotlib.pyplot as plt
import csv
import os
import random
import numpy as np
import random
from tensorflow.python.framework import ops

ops.reset_default_graph()

# ---------------------------------------------------|
# -------------------1D-data-------------------------|
# ---------------------------------------------------|

# Create graph session 创建初始图结构
ops.reset_default_graph()
sess = tf.Session()

# parameters for the run运行参数
data_size = 25
conv_size = 5  # 卷积核宽度方向的大小
maxpool_size = 5  # 池化层核宽度方向上的大小
stride_size = 1  # 卷积核宽度方向上的步长

# ensure reproducibility 确保复现性
seed = 13
np.random.seed(seed)
tf.set_random_seed(seed)

# Generate 1D data 生成一维数据
data_1d = np.random.normal(size=data_size)

# Placeholder
x_input_1d = tf.placeholder(dtype=tf.float32, shape=[data_size])


# --------Convolution--------
def conv_layer_1d(input_1d, my_filter, stride):
    # TensorFlow's 'conv2d()' function only works with 4D arrays:
    # [batch, height, width, channels], we have 1 batch, and
    # width = 1, but height = the length of the input, and 1 channel.
    # So next we create the 4D array by inserting dimension 1's.
    # 关于数据维度的处理十分关键,因为tensorflow中卷积操作只支持四维的张量,
    # 所以要人为的把数据补充为4维数据[1,1,25,1]
    input_2d = tf.expand_dims(input_1d, 0)
    input_3d = tf.expand_dims(input_2d, 0)
    input_4d = tf.expand_dims(input_3d, 3)
    # Perform convolution with stride = 1, if we wanted to increase the stride,
    # to say '2', then strides=[1,1,2,1]
    convolution_output = tf.nn.conv2d(input_4d, filter=my_filter, strides=[1, 1, stride, 1], padding="VALID")
    # Get rid of extra dimensions 去掉多余的层数,只保留数字
    conv_output_1d = tf.squeeze(convolution_output)
    return (conv_output_1d)


# Create filter for convolution.
my_filter = tf.Variable(tf.random_normal(shape=[1, conv_size, 1, 1]))
# Create convolution layer
my_convolution_output = conv_layer_1d(x_input_1d, my_filter, stride=stride_size)


# --------Activation--------
def activation(input_1d):
    return (tf.nn.relu(input_1d))


# Create activation layer
my_activation_output = activation(my_convolution_output)


# --------Max Pool--------
def max_pool(input_1d, width, stride):
    # Just like 'conv2d()' above, max_pool() works with 4D arrays.
    # [batch_size=1, width=1, height=num_input, channels=1]
    # 因为在处理卷积层的结果时,使用squeeze函数对结果输出进行降维,所以此处要将最大池化层的维度提升为4维
    input_2d = tf.expand_dims(input_1d, 0)
    input_3d = tf.expand_dims(input_2d, 0)
    input_4d = tf.expand_dims(input_3d, 3)
    # Perform the max pooling with strides = [1,1,1,1]
    # If we wanted to increase the stride on our data dimension, say by
    # a factor of '2', we put strides = [1, 1, 2, 1]
    # We will also need to specify the width of the max-window ('width')
    pool_output = tf.nn.max_pool(input_4d, ksize=[1, 1, width, 1],
                                 strides=[1, 1, stride, 1],
                                 padding='VALID')
    # Get rid of extra dimensions
    pool_output_1d = tf.squeeze(pool_output)
    return (pool_output_1d)


my_maxpool_output = max_pool(my_activation_output, width=maxpool_size, stride=stride_size)


# --------Fully Connected--------
def fully_connected(input_layer, num_outputs):
    # First we find the needed shape of the multiplication weight matrix:
    # The dimension will be (length of input) by (num_outputs)
    weight_shape = tf.squeeze(tf.stack([tf.shape(input_layer), [num_outputs]]))
    # squeeze函数用于去掉维度为1的维度。保留数据。

    # Initialize such weight
    # 初始化weight
    weight = tf.random_normal(weight_shape, stddev=0.1)
    # Initialize the bias
    # 初始化bias
    bias = tf.random_normal(shape=[num_outputs])
    # Make the 1D input array into a 2D array for matrix multiplication
    # 将一维的数组添加一维成为2维数组
    input_layer_2d = tf.expand_dims(input_layer, 0)
    # Perform the matrix multiplication and add the bias
    full_output = tf.add(tf.matmul(input_layer_2d, weight), bias)
    # Get rid of extra dimensions
    # 去掉多余的维度只保留数据
    full_output_1d = tf.squeeze(full_output)
    return (full_output_1d)


my_full_output = fully_connected(my_maxpool_output, 5)

# Run graph
# Initialize Variables
init = tf.global_variables_initializer()
sess.run(init)

feed_dict = {x_input_1d: data_1d}

print('>>>> 1D Data <<<<')

# Convolution Output
print('Input = array of length %d'%(x_input_1d.shape.as_list()[0]))  # 25
print('Convolution w/ filter, length = %d, stride size = %d, results in an array of length %d:'%
      (conv_size, stride_size, my_convolution_output.shape.as_list()[0]))  # 21
print(sess.run(my_convolution_output, feed_dict=feed_dict))

# Activation Output
print('\nInput = above array of length %d'%(my_convolution_output.shape.as_list()[0]))  # 21
print('ReLU element wise returns an array of length %d:'%(my_activation_output.shape.as_list()[0]))  # 21
print(sess.run(my_activation_output, feed_dict=feed_dict))

# Max Pool Output
print('\nInput = above array of length %d'%(my_activation_output.shape.as_list()[0]))  # 21
print('MaxPool, window length = %d, stride size = %d, results in the array of length %d'%
      (maxpool_size, stride_size, my_maxpool_output.shape.as_list()[0]))  # 17
print(sess.run(my_maxpool_output, feed_dict=feed_dict))

# Fully Connected Output
print('\nInput = above array of length %d'%(my_maxpool_output.shape.as_list()[0]))  # 17
print('Fully connected layer on all 4 rows with %d outputs:'%
      (my_full_output.shape.as_list()[0]))  # 5
print(sess.run(my_full_output, feed_dict=feed_dict))

# >>>> 1D Data <<<<
# Input = array of length 25
# Convolution w/ filter, length = 5, stride size = 1, results in an array of length 21:
# [-2.63576341 -1.11550486 -0.95571411 -1.69670296 -0.35699379  0.62266493
#   4.43316031  2.01364899  1.33044648 -2.30629659 -0.82916248 -2.63594174
#   0.76669347 -2.46465087 -2.2855041   1.49780679  1.6960566   1.48557389
#  -2.79799461  1.18149185  1.42146575]
#
# Input = above array of length 21
# ReLU element wise returns an array of length 21:
# [ 0.          0.          0.          0.          0.          0.62266493
#   4.43316031  2.01364899  1.33044648  0.          0.          0.
#   0.76669347  0.          0.          1.49780679  1.6960566   1.48557389
#   0.          1.18149185  1.42146575]
#
# Input = above array of length 21
# MaxPool, window length = 5, stride size = 1, results in the array of length 17
# [ 0.          0.62266493  4.43316031  4.43316031  4.43316031  4.43316031
#   4.43316031  2.01364899  1.33044648  0.76669347  0.76669347  1.49780679
#   1.6960566   1.6960566   1.6960566   1.6960566   1.6960566 ]
#
# Input = above array of length 17
# Fully connected layer on all 4 rows with 5 outputs:
# [ 1.71536088 -0.72340977 -1.22485089 -2.5412786  -0.16338299]
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,402评论 6 499
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,377评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,483评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,165评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,176评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,146评论 1 297
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,032评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,896评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,311评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,536评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,696评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,413评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,008评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,659评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,815评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,698评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,592评论 2 353

推荐阅读更多精彩内容