如何使用Tensorflow 2.0中的图层列表



以下代码给我带来了一个错误" attributeError:cote nest属性"。我认为这是因为我试图将TensorFlow层放入普通列表中。

有人知道我如何解决这个问题并能够创建一层列表吗?我不想使用顺序,因为它的灵活性较小。

在pytorch中,他们有可以使用的调节师,而不是列表,我可以使用TensorFlow中的等效物吗?

!pip install tensorflow-gpu==2.0.0-alpha0
import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.layers = self.create_layers()
  def create_layers(self):    
    layers = [Conv2D(32, 3, activation='relu'), Flatten(), 
              Dense(128, activation='relu'), Dense(10, activation='softmax')]
    return layers
  def call(self, x):
    for layer in self.layers:
      x = layer(x)
    return x
model = MyModel()

完整问题

layers是模型层的保留名称。考虑为模型使用另一个属性。

import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten, Conv2D
from tensorflow.keras import Model
class MyModel(Model):
  def __init__(self):
    super(MyModel, self).__init__()
    self.layers_custom = self.create_layers()
  def create_layers(self):    
    layers = [Conv2D(32, 3, activation='relu'), Flatten(), 
              Dense(128, activation='relu'), Dense(10, activation='softmax')]
    return layers
  def call(self, x):
    for layer in self.layers_custom:
      x = layer(x)
    return x
model = MyModel()
print(model.layers)
print(model.layers_custom)

只需在TF2.0的教程中查看有关如何制作图层列表的示例https://www.tensorflow.org/tutorials/generative/pix2pix

最新更新