有什么方法可以创建可自定义权重的自定义Keras图层?

时间:2020-02-13 07:56:57

标签: python python-3.x tensorflow keras deep-learning

我试图创建某种“字典”,它是形状的张量,假设(8,16,32)我想封装在自定义Keras层中。我要使用此层的方式是从数据集中获取两个数字作为输入,并使用这些数字作为索引来查找字典并以(32,)的形式输出。基本上,我正在尝试做类似的事情,但是有一个Keras层,所以我可以训练字典:

import numpy as np
a = np.zeros((8, 16, 32)) # The dictionary 
output = a[5,14,:] #5 and 14 will be the input index numbers from my dataset
output.shape #(32,)

我以前没有写过任何自定义的Keras图层。我遵循了this link,这就是我最终的目的

class GridLayer(layers.Layer):

    def __init__(self, len, width, depth):
        super(GridLayer, self).__init__()
        self.depth = depth
        w_init = tf.random_normal_initializer()
        self.w = tf.Variable(initial_value=w_init(shape=(len, width, depth),
                                                  dtype='float32'),
                                                  trainable=True)
    def call(self, inputs):
        # The input will be of shape ((None, 2, 1)) 
        return tf.slice(self.w, [inputs[1][0], inputs[1][1], 0], [1,1,self.depth])

当我尝试使用图层时

# Create the layer 
grid_len = 8
grid_width = 16
grid_depth = 32
grid_layer = GridLayer(grid_len, grid_width, grid_depth)

# Feed input to the custom layer 
input_layer = Input((2,1))
output_layer = grid_layer(input_layer)
model = Model(inputs = input_layer, outputs = output_layer)

我最终遇到此错误

Traceback (most recent call last):
  File "lstm_practice.py", line 39, in <module>
    output_layer = grid_layer(input_layer)
  File "C:\Users\s3702199\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 773, in __call__
    outputs = call_fn(cast_inputs, *args, **kwargs)
  File "C:\Users\s3702199\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\autograph\impl\api.py", line 237, in wrapper
    raise e.ag_error_metadata.to_exception(e)
ValueError: in converted code:

    lstm_practice.py:28 call  *
        return tf.slice(self.w, [inputs[1][0], inputs[1][1], 0], [1,1,self.depth])
    C:\Users\s3702199\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\ops\array_ops.py:951 slice
        return gen_array_ops._slice(input_, begin, size, name=name)
    C:\Users\s3702199\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\ops\gen_array_ops.py:8451 _slice
        "Slice", input=input, begin=begin, size=size, name=name)
    C:\Users\s3702199\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow_core\python\framework\op_def_library.py:486 _apply_op_helper
        (input_name, err))

    ValueError: Tried to convert 'begin' to a tensor and failed. Error: Shapes must be equal rank, but are 1 and 0
        From merging shape 1 with other shapes. for 'grid_layer/Slice/packed' (op: 'Pack') with input shapes: [1], [1], [].

如何使用自定义Keras图层进行这种张量切片?谢谢!

0 个答案:

没有答案