TensorflowTutorial_一维数据构造简单CNN
使用一维数据构造简单卷积神经网络




神经网络对于一维数据非常重要,时序数据集、信号处理数据集和一些文本嵌入数据集都是一维数据,会频繁的使用到神经网络。我们在此利用一组一维数据构造卷积层-最大池化层-全连接层的卷积神经网络。希望给大家使用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]
参考文献
TensorFlow机器学习实战指南
TensorflowTutorial_一维数据构造简单CNN的更多相关文章
- TensorflowTutorial_二维数据构造简单CNN
使用二维数据构造简单卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 图像和一些时序数据集都可以用二维数据的形式表现,我们此次使用随机分布的二位数据构造一个简单的CNN-网络卷积- ...
- TersorflowTutorial_MNIST数据集上简单CNN实现
MNIST数据集上简单CNN实现 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow机器学习实战指南 源代码请点击下方链接欢迎加星 Tesorflow实现基于MNI ...
- Tensorflow简单CNN实现
觉得有用的话,欢迎一起讨论相互学习~Follow Me 少说废话多写代码~ """转换图像数据格式时需要将它们的颜色空间变为灰度空间,将图像尺寸修改为同一尺寸,并将标签依 ...
- 图解Redis之数据结构篇——简单动态字符串SDS
图解Redis之数据结构篇--简单动态字符串SDS 前言 相信用过Redis的人都知道,Redis提供了一个逻辑上的对象系统构建了一个键值对数据库以供客户端用户使用.这个对象系统包括字符串对象 ...
- LeetCode总结 -- 一维数据合并篇
合并是一维数据结构中非经常见的操作, 一般是排序, 分布式算法中的子操作. 这篇总结主要介绍LeetCode中关于合并的几个题目: Merge Two Sorted ListsMerge Sorted ...
- pytorch批训练数据构造
这是对莫凡python的学习笔记. 1.创建数据 import torch import torch.utils.data as Data BATCH_SIZE = 8 x = torch.linsp ...
- 通过 UDP 发送数据的简单范例
package j2se.core.net.udp; import java.io.IOException;import java.net.DatagramPacket;import java.net ...
- MapReduce实例-NASA博客数据频度简单分析
环境: Hadoop1.x,CentOS6.5,三台虚拟机搭建的模拟分布式环境,gnuplot, 数据:http://ita.ee.lbl.gov/html/contrib/NASA-HTTP.htm ...
- RapidMiner的基本使用(一个医疗数据的简单决策树算法分析)
RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) RapidMiner的基本使用(一个医疗数据的简单决策树算法分析) 需要分析的文件: 右键分别创建读取excel数据,选择属性,设置 ...
随机推荐
- Flask-SQLAlchemy
Flask-SQLAlchemy SQLAlchemy 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言 ...
- 读了前半本<Thinking in Java>
读了1-14章.这本书真的不适合初学者,可能比较适合有一两年Java经验的人来读.学习真的是一个螺旋递进的过程.刚开始学Java基本语法,书上看到的很多东西觉得过于细枝末节,没见过,用不上,导致书看不 ...
- <魔域>按键精灵脚本
用了三天时间才写完,实现了通过图片识别读取坐标数值,自动寻路等简单功能. 主要的难点在于游戏中的坐标系,和电脑屏幕的坐标系存在三维旋转关系,难以换算. //全局变量:第一个数左上右下坐标 Global ...
- Java中import及package的用法
有些人写了一阵子 Java,可是对於 Java 的 package 跟 import 还是不 太了解很多人以為原始码 .java 档案中的 import 会让编译器把所 import 的程式通通写到编 ...
- kafka数据迁移实践
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者:mikealzhou 本文重点介绍kafka的两类常见数据迁移方式:1.broker内部不同数据盘之间的分区数据迁移:2.不同broker ...
- cdn和反向代理
都是用来加速网站访问速度 cdn和反向代理的基本原理都是使用缓存,区别在于cdn部署在网络提供商的机房,使用户在请求网络服务时,可以从距离自己最近的网络提供商机房获取数据:而反向代理则部署在网站的中心 ...
- CXF之"@XmlType.name 和 @XmlType.namespace 为类分配不同的名称"错误
CXF 的 wsdl2java.bat 生产的代码,拷贝到目录,进行调研 web service接口时,抛出错误: Exception in thread "main" javax ...
- windows下如何创建没有名字的.htaccess文件
http://www.mdaima.com/jingyan/35.html WINDOWS下建立空名的.htaccess文件 ? 大家都知道,在windows环境下是不能直接建立没有名字的文件的,那我 ...
- 学习总结:libevent--简单入门
libevent--简单入门 一.简介 libevent是一个c语言写的事件驱动库,轻量级,专注于网络,跨平台特性好,支持多种 I/O 多路复用.支持I/O,定时器和信号等事件,允许设置注册事件优先级 ...
- Django之Model组件
Model组件在django基础篇就已经提到过了,本章介绍更多高级部分. 一.回顾 1.定义表(类) ##单表 from django.db import models class user(mode ...