目的
將用戶(hù)自定義的layer結(jié)合tensorflow自帶的layer組成多層layer的計(jì)算圖。
實(shí)現(xiàn)功能
對(duì)2D圖像進(jìn)行滑動(dòng)窗口平均,并通過(guò)自定義的操作layer返回結(jié)果。
import tensorflow as tf import numpy as np sess = tf.Session() #將size設(shè)為[1, 4, 4, 1]是因?yàn)閠f中圖像函數(shù)是處理四維圖片的。 #這四維依次是: 圖片數(shù)量,高度, 寬度, 顏色通道 x_shape = [1,4,4,1] x_val = np.random.uniform(size = x_shape) #tf.nn.conv2d中name表明該layer命名為“Moving_Avg_Window” #該卷積核為[[0.25,0.25],[0.25,0.25]],所以是一個(gè)求平均操作 x_data = tf.placeholder(tf.float32, shape = x_shape) my_filter = tf.constant(0.25, shape = [2,2,1,1]) my_strides = [1,2,2,1] mov_avg_layer = tf.nn.conv2d(x_data, my_filter, my_strides, padding = 'SAME', name = 'Moving_Avg_Window') #自定義layer,對(duì)卷積操作之后的輸出做操作 def custom_layer(input_matrix): input_matrix_sqeeze = tf.squeeze(input_matrix) A = tf.constant([1.,2.],[-1.,3.]) b = tf.constant(1., shape = [2,2]) temp1 = tf.matmul(A, input_matrix_sqeeze) temp2 = tf.add(temp1, b) return(tf.sigmod(temp2)) #把剛剛自定義的layer加入到計(jì)算圖中,并給予自定義的命名(利用tf.name_scope()) with tf.name_scope('Custom_Layer') as scope: custom_layer1 = custom_layer(mov_avg_layer) #為占位符傳入4*4圖片,并執(zhí)行計(jì)算圖 print(sess.run(custom_layer, feed_dict= {x_data: x_val}))
分享標(biāo)題:tensorflow實(shí)現(xiàn)自定義layer并添加到計(jì)算圖中-創(chuàng)新互聯(lián)
本文來(lái)源:http://m.2m8n56k.cn/article16/esidg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、搜索引擎優(yōu)化、域名注冊(cè)、全網(wǎng)營(yíng)銷(xiāo)推廣、微信公眾號(hào)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容