[英]Proper way to create a custom layer/function to rearrange layer values (Keras with Tensorflow)
I need to rearrange a tensor values and then reshape it in Keras, however I am struggling with the proper way to to rearrange a tensor in Keras with Tensorflow backend. 我需要重新排列张量值,然后在Keras中对其进行重塑,但是我正在努力尝试使用Tensorflow后端在Keras中重新排列张量的正确方法。
This custom layer/function will iterate through the values, and then rearrange the values via a stride formula This doesn't seem to have weights, so I am assuming stateless and won't affect back propagation. 此自定义图层/函数将迭代这些值,然后通过一个跨步公式重新排列这些值。这似乎没有权重,因此我假设是无状态的,不会影响反向传播。
It requires list slicing though: 但是它需要列表切片:
out_array[b,channel_out, row_out, column_out] = in_array[b,i,j,k]
and this is just one of the components I am struggling with. out_array[b,channel_out, row_out, column_out] = in_array[b,i,j,k]
,这只是我正在努力的组件之一。
Here is the function/layer 这是功能/层
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")
I don't have much experience creating custom functions/layers in Keras, so not quite sure If I am on the right track. 我在Keras中创建自定义函数/图层的经验不足,因此不太确定我是否步入正轨。
Here is what the code bit is doing depending on the stride (here it's 2): 这是根据步幅(这里是2)而执行的代码位的操作:
https://towardsdatascience.com/training-object-detection-yolov2-from-scratch-using-cyclic-learning-rates-b3364f7e4755 https://towardsdatascience.com/training-object-detection-yolov2-from-scratch-using-cyclic-learning-rates-b3364f7e4755
When you say re-arrange, do you mean change the order of your axes? 当您说重新排列时,您的意思是更改轴的顺序吗? There is a function called tf.transpose which you can use inside a custom layer.
有一个名为tf.transpose的函数,可以在自定义图层中使用。 There is also tf.keras.layers.Permute which can be used without any custom code to re-order a tensor.
还有tf.keras.layers.Permute ,无需任何自定义代码即可使用它来重新排序张量。
If you are asking how you can create a custom layer, there are some methods you'll need to implement. 如果您询问如何创建自定义图层,则需要实现一些方法。 The docs explain it pretty well here: Custom Layers
文档在这里对此进行了很好的解释: 自定义层
from tensorflow.keras import layers
import tensorflow as tf
class Linear(layers.Layer):
def __init__(self, units=32):
super(Linear, self).__init__()
self.units = units
def build(self, input_shape):
self.w = self.add_weight(shape=(input_shape[-1], self.units),
initializer='random_normal',
trainable=True)
self.b = self.add_weight(shape=(self.units,),
initializer='random_normal',
trainable=True)
def call(self, inputs):
return tf.matmul(inputs, self.w) + self.b
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.