简体   繁体   English

使用 tf.keras.layers.concatenate() 作为 tensorflow 中的自定义层

[英]Use tf.keras.layers.concatenate() as custom layer in tensorflow

I want to make U-net using custom layers in tensorflow.我想在 tensorflow 中使用自定义层制作 U-net。 I need use tf.keras.layers.concatenate there and that is my problem.我需要在那里使用 tf.keras.layers.concatenate ,这就是我的问题。 Input tensors for all other layers I can add to layer in method call.我可以在方法调用中添加到层的所有其他层的输入张量。 But syntax for concatenate layer is tf.keras.layers.concatenate(input, axis) and I need something like this tf.keras.layers.concatenate(axis)(input), but it does not work.但是连接层的语法是 tf.keras.layers.concatenate(input,axis),我需要这样的东西 tf.keras.layers.concatenate(axis)(input),但它不起作用。 Can anybody help me please?有人可以帮我吗?
Thank you.谢谢你。

My code is something like this:我的代码是这样的:

class MyModel(tf.keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.block1 = Conv2D(.....)
    self.block2 = BatchNormalization()
    ....etc.....
    self.decoder_concat = tf.keras.layers.concatenate(axis=-1) #that i need but it does not work

  def call(self, inputs):
     x = self.block1(inputs)
     x = self.block2(x)
     ....etc......
     x = self.decoder_concat([x, concatLayer]) #that i need but it does not work

Providing the solution here (Answer Section), even though it is present in the Comment Section, for the benefit of the community.在这里提供解决方案(答案部分),即使它出现在评论部分,也是为了社区的利益。

After changing tf.keras.layers.concatenate to tf.keras.layers.Concatenate has resolved the issue.tf.keras.layers.concatenate更改为tf.keras.layers.Concatenate后,问题已解决。

tf.keras.layers.Concatenate which is used as a layer that concatenates list of inputs in Tensorflow, where as tf.keras.layers.concatenate acts as functional interface to the Concatenate layer. tf.keras.layers.Concatenate用作连接 Tensorflow 中的输入列表的层,其中tf.keras.layers.concatenate充当连接层的功能接口。 Please refer more details here在此处参考更多详细信息

Please refer updated code in below请参考下面的更新代码

class MyModel(tf.keras.Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.block1 = Conv2D(.....)
    self.block2 = BatchNormalization()
    ....etc.....
    self.decoder_concat = tf.keras.layers.Concatenate(axis=-1) #that i need but it does not work

  def call(self, inputs):
     x = self.block1(inputs)
     x = self.block2(x)
     ....etc......
     x = self.decoder_concat([x, concatLayer]) #that i need but it does not work

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM