欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Tensorflow一維卷積用法詳解

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

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

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)#寬度計(jì)算(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)#寬度計(jì)算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)時(shí),conv1的shape

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

補(bǔ)充知識(shí):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)

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

相關(guān)文章

  • python zip()函數(shù)使用方法解析

    python zip()函數(shù)使用方法解析

    這篇文章主要介紹了python zip()函數(shù)使用方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • python定義具名元組實(shí)例操作

    python定義具名元組實(shí)例操作

    在本篇文章里小編給大家分享的是一篇關(guān)于python定義具名元組實(shí)例操作內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2021-02-02
  • Python實(shí)現(xiàn)一行代碼自動(dòng)繪制藝術(shù)畫

    Python實(shí)現(xiàn)一行代碼自動(dòng)繪制藝術(shù)畫

    DiscoArt?是一個(gè)很牛的開源模塊,它能根據(jù)你給定的關(guān)鍵詞自動(dòng)繪畫。本文就將利用這一模塊實(shí)現(xiàn)一行代碼自動(dòng)繪制藝術(shù)畫,需要的可以參考一下
    2022-12-12
  • python3學(xué)習(xí)之Splash的安裝與實(shí)例教程

    python3學(xué)習(xí)之Splash的安裝與實(shí)例教程

    splash 是一個(gè)python語言編寫的用于配合scrapy解析js的庫,下面這篇文章主要給大家介紹了關(guān)于python3學(xué)習(xí)之Splash的安裝與使用的一些相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2018-07-07
  • 淺談spring boot 集成 log4j 解決與logback沖突的問題

    淺談spring boot 集成 log4j 解決與logback沖突的問題

    今天小編就為大家分享一篇淺談spring boot 集成 log4j 解決與logback沖突的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • python 快速排序代碼

    python 快速排序代碼

    閑來無事寫了個(gè)python快排序
    2009-11-11
  • 基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊

    基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊

    這篇文章主要介紹了基于PyQT實(shí)現(xiàn)區(qū)分左鍵雙擊和單擊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • Cython 三分鐘入門教程

    Cython 三分鐘入門教程

    根據(jù)一些我收到的反饋,大家似乎有點(diǎn)混淆——Cython是用來生成 C 擴(kuò)展到而不是獨(dú)立的程序的。所有的加速都是針對(duì)一個(gè)已經(jīng)存在的 Python 應(yīng)用的一個(gè)函數(shù)進(jìn)行的。
    2009-09-09
  • python快速排序的實(shí)現(xiàn)及運(yùn)行時(shí)間比較

    python快速排序的實(shí)現(xiàn)及運(yùn)行時(shí)間比較

    這篇文章主要介紹了python快速排序的實(shí)現(xiàn)及運(yùn)行時(shí)間比較,本文通過兩種方法給大家介紹,大家可以根據(jù)自己需要選擇適合自己的方法,對(duì)python實(shí)現(xiàn)快速排序相關(guān)知識(shí)感興趣的朋友一起看看吧
    2019-11-11
  • python實(shí)現(xiàn)圖像識(shí)別功能

    python實(shí)現(xiàn)圖像識(shí)別功能

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)圖像識(shí)別功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評(píng)論