【发布时间】:2019-08-12 21:38:02
【问题描述】:
我需要重新排列张量值,然后在 Keras 中对其进行重塑,但是我正在努力寻找在 Keras 中使用 Tensorflow 后端重新排列张量的正确方法。
此自定义层/函数将遍历值,然后通过步幅公式重新排列值 这似乎没有权重,所以我假设无状态并且不会影响反向传播。
但它需要列表切片:
out_array[b,channel_out, row_out, column_out] = in_array[b,i,j,k]
这只是我正在努力解决的组件之一。
这里是函数/层
def reorg(tensor, stride):
batch,channel, height, width = (tensor.get_shape())
out_channel = channel * (stride * stride)
out_len = length//stride
out_width = width//stride
#create new empty tensor
out_array = K.zeros((batch, out_channel, out_len, out_width))
for b in batch:
for i in range(channel):
for j in range(height):
for k in range(width):
channel_out = i + (j % stride) * (channel * stride) + (k % stride) * channel
row_out = j//stride
column_out = k//stride
out_array[b,channel_out, row_out, column_out] = K.slice(in_array,[b,i,j,k], size = (1,1,1,1))
return out_array.astype("int")
我没有太多在 Keras 中创建自定义函数/层的经验, 所以不太确定我是否在正确的轨道上。
这是代码位根据步幅所做的事情(这里是 2):
【问题讨论】:
-
您能否编辑您的问题并解释逻辑或这种“重新排列”?也许可以使用向量/矩阵运算来完成。
-
@today 很抱歉我添加了一张图片
标签: python tensorflow keras