我就废话不多说了,大家还是直接看代码吧!
import tensorflow as tf import numpy as np input = tf.constant(1,shape=(64,10,1),dtype=tf.float32,name='input')#shape=(batch,in_width,in_channels) w = tf.constant(3,shape=(3,1,32),dtype=tf.float32,name='w')#shape=(filter_width,in_channels,out_channels) conv1 = tf.nn.conv1d(input,w,2,'VALID') #2为步长 print(conv1.shape)#宽度计算(width-kernel_size+1)/strides ,(10-3+1)/2=4 (64,4,32) conv2 = tf.nn.conv1d(input,w,2,'SAME') #步长为2 print(conv2.shape)#宽度计算width/strides 10/2=5 (64,5,32) conv3 = tf.nn.conv1d(input,w,1,'SAME') #步长为1 print(conv3.shape) # (64,10,32) with tf.Session() as sess: print(sess.run(conv1)) print(sess.run(conv2)) print(sess.run(conv3))
以下是input_shape=(1,10,1), w = (3,1,1)时,conv1的shape
以下是input_shape=(1,10,1), w = (3,1,3)时,conv1的shape
补充知识:tensorflow中一维卷积conv1d处理语言序列举例
tf.nn.conv1d:
函数形式: tf.nn.conv1d(value, filters, stride, padding, use_cudnn_on_gpu=None, data_format=None, name=None):
程序举例:
import tensorflow as tf import numpy as np sess = tf.InteractiveSession() # --------------- tf.nn.conv1d ------------------- inputs=tf.ones((64,10,3)) # [batch, n_sqs, embedsize] w=tf.constant(1,tf.float32,(5,3,32)) # [w_high, embedsize, n_filers] conv1 = tf.nn.conv1d(inputs,w,stride=2 ,padding='SAME') # conv1=[batch, round(n_sqs/stride), n_filers],stride是步长。 tf.global_variables_initializer().run() out = sess.run(conv1) print(out)
注:一维卷积中padding='SAME'只在输入的末尾填充0
tf.layters.conv1d:
函数形式:tf.layters.conv1d(inputs, filters, kernel_size, strides=1, padding='valid', data_format='channels_last', dilation_rate=1, activation=None, use_bias=True,...)
程序举例:
import tensorflow as tf import numpy as np sess = tf.InteractiveSession() # --------------- tf.layters.conv1d ------------------- inputs=tf.ones((64,10,3)) # [batch, n_sqs, embedsize] num_filters=32 kernel_size =5 conv2 = tf.layers.conv1d(inputs, num_filters, kernel_size,strides=2, padding='valid',name='conv2') # shape = (batchsize, round(n_sqs/strides),num_filters) tf.global_variables_initializer().run() out = sess.run(conv2) print(out)
二维卷积实现一维卷积:
import tensorflow as tf sess = tf.InteractiveSession() def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1,1,1,1], padding='SAME') def max_pool_1x2(x): return tf.nn.avg_pool(x, ksize=[1,1,2,1], strides=[1,1,2,1], padding='SAME') ''' ksize = [x, pool_height, pool_width, x] strides = [x, pool_height, pool_width, x] ''' x = tf.Variable([[1,2,3,4]], dtype=tf.float32) x = tf.reshape(x, [1,1,4,1]) #这一步必不可少,否则会报错说维度不一致; ''' [batch, in_height, in_width, in_channels] = [1,1,4,1] ''' W_conv1 = tf.Variable([1,1,1],dtype=tf.float32) # 权重值 W_conv1 = tf.reshape(W_conv1, [1,3,1,1]) # 这一步同样必不可少 ''' [filter_height, filter_width, in_channels, out_channels] ''' h_conv1 = conv2d(x, W_conv1) # 结果:[4,8,12,11] h_pool1 = max_pool_1x2(h_conv1) tf.global_variables_initializer().run() print(sess.run(h_conv1)) # 结果array([6,11.5])x
两种池化操作:
# 1:stride max pooling convs = tf.expand_dims(conv, axis=-1) # shape=["htmlcode">kernel_sizes = [3,4,5] # 分别用窗口大小为3/4/5的卷积核 with tf.name_scope("mul_cnn"): pooled_outputs = [] for kernel_size in kernel_sizes: # CNN layer conv = tf.layers.conv1d(embedding_inputs, self.config.num_filters, kernel_size, name='conv-%s' % kernel_size) # global max pooling layer gmp = tf.reduce_max(conv, reduction_indices=[1], name='gmp') pooled_outputs.append(gmp) self.h_pool = tf.concat(pooled_outputs, 1) #池化后进行拼接以上这篇基于Tensorflow一维卷积用法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
标签:
Tensorflow,一维卷积
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com
暂无“基于Tensorflow一维卷积用法详解”评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]