最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Tensorflow一維卷積用法詳解

 更新時間:2020年05月22日 10:14:54   作者:星夜孤帆  
這篇文章主要介紹了基于Tensorflow一維卷積用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

我就廢話不多說了,大家還是直接看代碼吧!

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:

函數(shù)形式: 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:

函數(shù)形式: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)

二維卷積實現(xiàn)一維卷積:

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) # 權(quán)重值
W_conv1 = tf.reshape(W_conv1, [1,3,1,1]) # 這一步同樣必不可少
'''
[filter_height, filter_width, in_channels, out_channels]
'''
h_conv1 = conv2d(x, W_conv1) # 結(jié)果:[4,8,12,11]
h_pool1 = max_pool_1x2(h_conv1)
tf.global_variables_initializer().run()
print(sess.run(h_conv1)) # 結(jié)果array([6,11.5])x

兩種池化操作:

# 1:stride max pooling
convs = tf.expand_dims(conv, axis=-1) # shape=[?,596,256,1]
smp = tf.nn.max_pool(value=convs, ksize=[1, 3, self.config.num_filters, 1], strides=[1, 3, 1, 1],
     padding='SAME') # shape=[?,299,256,1]
smp = tf.squeeze(smp, -1) # shape=[?,299,256]
smp = tf.reshape(smp, shape=(-1, 199 * self.config.num_filters))
 
# 2: global max pooling layer
gmp = tf.reduce_max(conv, reduction_indices=[1], name='gmp')

不同核尺寸卷積操作:

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一維卷積用法詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python os模塊和fnmatch模塊的使用介紹

    python os模塊和fnmatch模塊的使用介紹

    這篇文章主要介紹了python os模塊和fnmatch模塊的使用介紹,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-03-03
  • python字符串切割:str.split()與re.split()的對比分析

    python字符串切割:str.split()與re.split()的對比分析

    今天小編就為大家分享一篇python字符串切割:str.split()與re.split()的對比分析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python中的字典操作及字典函數(shù)

    python中的字典操作及字典函數(shù)

    本篇文章給大家介紹了python中的字典,包括字典的操作,字典函數(shù)實現(xiàn)代碼,需要的朋友參考下吧
    2018-01-01
  • python的繼承知識點總結(jié)

    python的繼承知識點總結(jié)

    在本文里小編整理的是關(guān)于python的繼承知識點總結(jié)內(nèi)容,學(xué)習(xí)到關(guān)于繼承的讀者們可以參考一下。
    2018-12-12
  • Python實現(xiàn)的用戶登錄系統(tǒng)功能示例

    Python實現(xiàn)的用戶登錄系統(tǒng)功能示例

    這篇文章主要介紹了Python實現(xiàn)的用戶登錄系統(tǒng)功能,涉及Python流程控制及字符串判斷等相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • Python實現(xiàn)使用request模塊下載圖片demo示例

    Python實現(xiàn)使用request模塊下載圖片demo示例

    這篇文章主要介紹了Python實現(xiàn)使用request模塊下載圖片,結(jié)合完整實例形式分析了Python基于requests模塊的流傳輸文件下載操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-05-05
  • Python的運算符重載詳解

    Python的運算符重載詳解

    這篇文章主要介紹了Python的運算符重載詳解,文中有非常詳細的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • Python操作Redis之設(shè)置key的過期時間實例代碼

    Python操作Redis之設(shè)置key的過期時間實例代碼

    這篇文章主要介紹了Python操作Redis之設(shè)置key的過期時間實例代碼,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • python?lambda?表達式形式分析

    python?lambda?表達式形式分析

    這篇文章主要介紹了python?lambda?表達式形式分析,?lambda??表達式會創(chuàng)建一個函數(shù)對象,可以對其賦值并如同普通函數(shù)一樣使用,下面通過定義了一個求平方的?lambda?表達式展開主題內(nèi)容,需要的朋友可以參考一下
    2022-04-04
  • Python中除法使用的注意事項

    Python中除法使用的注意事項

    這篇文章主要介紹了Python中除法使用的注意事項,是Python程序設(shè)計很重要的技巧,需要的朋友可以參考下
    2014-08-08

最新評論

宣城市| 呼伦贝尔市| 祥云县| 巧家县| 深圳市| 沈阳市| 靖州| 诸城市| 宣恩县| 安化县| 广宗县| 桐乡市| 高阳县| 盐亭县| 浏阳市| 梁平县| 昌邑市| 历史| 措美县| 福安市| 广灵县| 永和县| 枞阳县| 夏津县| 蓬莱市| 漳州市| 梅河口市| 同仁县| 巧家县| 锡林浩特市| 绩溪县| 疏勒县| 专栏| 乐昌市| 嵩明县| 政和县| 比如县| 普格县| 博乐市| 益阳市| 黑山县|