我正在嘗試制作通過 reduce_sum(axis=-1) 減少通道的自定義層
例如當輸入形狀為 (32,32,128)
我想將輸入形狀從 (32,32,128) 更改為 (None,32,32,128) 和
如果通道的索引類似于 [0],[1],[2],[3].........[126],[127] 而我的自定義層想要添加 2 或 3 或 4...N渠道
假設我只想添加 2 個通道,這將是 [0] [1],[1] [2]….[126] [127] 并且輸出形狀將是 (32,32,64)
也(無,32,32,64)
有關更多詳細資訊,可以說我想添加 3 個通道,它們將是 [0] [1] [2],[3] [4] [4]... ... [123] [124] [125] ,[126] [127] 輸出形狀為 (32,32,44)
這里也是(無、32、32、44)
那么有可能實作嗎?
頻道中有索引嗎?如果是這樣的話,我想會很容易做到……
uj5u.com熱心網友回復:
x = tf.random.normal(shape=(32,32,128))
sum_dim = 2
x = tf.reduce_sum(tf.reshape(x, (x.shape[0], x.shape[1], -1, sum_dim)), -1)
#[32, 32, 64]
如果要寫keras層:
class ChannelSum(keras.layers.Layer):
def __init__(self, sum_dim=2):
super(ChannelSum, self).__init__()
self.sum_dim = sum_dim
def call(self, x ):
return tf.reduce_sum(tf.reshape(x, (-1, x.shape[1], x.shape[2], x.shape[3]//self.sum_dim, self.sum_dim)), -1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524096.html
標籤:张量流图像处理模型定制层
上一篇:聊聊計算機中的暫存器
下一篇:資料集形狀不正確的張量流
