use*_*360 7 tensorflow tensorflow2.0
下面的代码向我抛出错误“AttributeError:无法设置属性”。我认为这是因为我试图将 TensorFlow 层放入普通列表中。
有谁知道我如何解决这个问题并能够创建图层列表?我不想使用顺序,因为它不太灵活。
在 PyTorch 中,他们有 ModuleLists,您可以使用它来代替列表,我可以使用 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()
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8867 次 |
| 最近记录: |