日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值

發(fā)布時(shí)間:2025/4/16 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在看Segnet的時(shí)候發(fā)現(xiàn)使用了帶有索引的最大池化(max_pool),在tensorflow的實(shí)現(xiàn)中,對(duì)應(yīng)的函數(shù)為tf.nn.max_pool_with_argmax(),其返回值為所取最大值位置的索引,但采用了一種指數(shù)的計(jì)算方式進(jìn)行表示

這里為官方注釋

The indices in `argmax` are flattened, so that a maximum value at position`[b, y, x, c]` becomes flattened index`((b * height + y) * width + x) * channels + c`.

可以看到對(duì)索引的表示增加了一些運(yùn)算操作,使得我們無(wú)法直接使用這個(gè)索引對(duì)應(yīng)的位置

這里自己寫(xiě)了一個(gè)拆解方法的代碼,記錄一下(只考慮了通道影響,沒(méi)有考慮batch的影響,可能后續(xù)會(huì)加以改進(jìn)說(shuō)明)

def unravel_corr(list,shape):b,h,w,c = list.get_shape().as_list()cc = []for i in range(c):a = tf.constant(i,dtype=tf.int64)a = [a]aa = (tf.tile(tf.tile(a,[h])[:,tf.newaxis],[1,w])[:,:,tf.newaxis])cc.append(aa)aa = tf.concat(cc,2)aa = tf.tile(aa[tf.newaxis,:,:,:],[b,1,1,1])_,height,width,_ = shapelist_y = (list-aa)/c%widthlist_x = (list-aa)/c//width%heightreturn [list_x,list_y]

整體測(cè)試代碼如下

import tensorflow as tf a = tf.constant([[5,8,2,1],[4,3,5,7],[0,7,9,1],[2,3,9,7]]) a = tf.reshape(a,[1,4,4,1]) c=tf.constant([[1,5,7,1],[4,8,5,7],[0,7,9,12],[15,3,9,7]]) c = tf.reshape(c,[1,4,4,1]) a = tf.concat([a,c],3) b,list = tf.nn.max_pool_with_argmax(a,[1,2,2,1],[1]+[2,2]+[1],padding='VALID') def unravel_corr(list,shape):b,h,w,c = list.get_shape().as_list()cc = []for i in range(c):a = tf.constant(i,dtype=tf.int64)a = [a]aa = (tf.tile(tf.tile(a,[h])[:,tf.newaxis],[1,w])[:,:,tf.newaxis])cc.append(aa)aa = tf.concat(cc,2)aa = tf.tile(aa[tf.newaxis,:,:,:],[b,1,1,1])_,height,width,_ = shapelist_y = (list-aa)/c%widthlist_x = (list-aa)/c//width%heightreturn [list_x,list_y] cor = unravel_corr(list,[1,4,4,2])with tf.Session() as sess:print('a:',sess.run(a))print('b:',sess.run(b))print('list:',sess.run(cor))

經(jīng)計(jì)算,得到的位置索引與手動(dòng)計(jì)算得到的結(jié)果相符

總結(jié)

以上是生活随笔為你收集整理的tensorflow tf.nn.max_pool_with_argmax返回最大池化对应索引值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。