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

基于Tensorflow一維卷積用法詳解

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

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

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為步長(zhǎng)
print(conv1.shape)#寬度計(jì)算(width-kernel_size+1)/strides ,(10-3+1)/2=4 (64,4,32)
conv2 = tf.nn.conv1d(input,w,2,'SAME') #步長(zhǎng)為2
print(conv2.shape)#寬度計(jì)算width/strides 10/2=5 (64,5,32)
conv3 = tf.nn.conv1d(input,w,1,'SAME') #步長(zhǎng)為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)時(shí),conv1的shape

以下是input_shape=(1,10,1), w = (3,1,3)時(shí),conv1的shape

補(bǔ)充知識(shí):tensorflow中一維卷積conv1d處理語(yǔ)言序列舉例

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是步長(zhǎng)。
 
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)

二維卷積實(shí)現(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]) #這一步必不可少,否則會(huì)報(bào)錯(cuò)說(shuō)維度不一致;
'''
[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) #池化后進(jìn)行拼接

以上這篇基于Tensorflow一維卷積用法詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

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

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

    python的繼承知識(shí)點(diǎn)總結(jié)

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

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

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

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

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

    Python的運(yùn)算符重載詳解

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

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

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

    python?lambda?表達(dá)式形式分析

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

    Python中除法使用的注意事項(xiàng)

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

最新評(píng)論

遂川县| 夹江县| 湘潭县| 子洲县| 沧源| 定南县| 安达市| 新昌县| 施秉县| 桂阳县| 内丘县| 丰县| 襄汾县| 依安县| 垣曲县| 名山县| 南雄市| 黑河市| 江北区| 临武县| 津市市| 教育| 宝兴县| 长葛市| 息烽县| 阳原县| 梁平县| 垫江县| 通道| 张家川| 赤壁市| 沙洋县| 罗山县| 舞阳县| 怀来县| 双柏县| 枣庄市| 达日县| 车险| 平遥县| 建宁县|