如何使keras直接操作数据数组作为拟合的一部分

时间:2019-05-20 17:42:16

标签: tensorflow keras

我需要一个keras模型,以便能够在对图像进行处理的同时制作图像。也许我已经想过了,但是我想出的方法是定义自己的图层,在其输入中添加可训练的张量。模型输入的一部分将是一个零的常量数组,当将其传递到我的图层时,它将在其中添加可训练的张量。然后,当拟合完成时,我提取这些层的权重作为拟合数组。

作为工作流程示例,我们将其称为自定义层Image2d。对于踢球,我要添加卷积和密集层,但不要注意这些。我本质上需要允许keras在做事情的同时制作图像。

class Image2d(Layer):
  def __init__(self,**kwargs):
    super(MyLayer, self).__init__(**kwargs)

  def build(self,input_shape):
    self.kernel = self.add_weight(name='kernel', 
                                  shape=input_shape,
                                  trainable=True)
    super(MyLayer, self).build(input_shape)  

  def call(self, x):
    return x + self.kernel 

  def compute_output_shape(self, input_shape):
    return input_shape

# now for the model
input=Input(shape=input_shape)
x=Image2d()(input)
x=Conv2d(5,5)(x)
output=Dense(1)(x)
model=K.models.Model(inputs=np.zeros(input_shape), outputs=output)

我敢肯定还有更多聪明的主意。有人要分享吗?

0 个答案:

没有答案