系统信息
- 是否编写了自定义代码(与使用TensorFlow提供的库存示例脚本相反):是
- OS平台和发行版(例如,Linux Ubuntu 16.04):Ubuntu 18.04.1
- 从哪里安装的TensorFlow(源代码或二进制文件):二进制文件
- TensorFlow版本(请使用以下命令):v2.3.0-54-gfcc4b966f1 2.3.1
- Python版本:3.6.9
- CUDA/cuDNN版本:10.1
- GPU型号和内存:GTX 2080 Ti
描述当前行为
从包含子层的层中加载权重,但在.weights
属性中未正确列出。这不仅会引起困惑,还会破坏对权重的进一步优化或修改。不过它们还是被正确恢复了。
当使用ListWrapper
时,问题不会出现。
描述预期行为
当再次加载模型时,List
中的子层变量应包含在.weights
中
独立代码重现问题
import tensorflow as tf
class TestLayer(tf.keras.layers.Layer):
def __init__(self, **kwargs):
super(TestLayer, self).__init__(**kwargs)
self.static_layer = tf.keras.layers.Dense(128)
self.my_layers = tf.python.training.tracking.data_structures.List()
for i in range(4):
layer = tf.keras.layers.Dense(128)
self.my_layers.append(layer)
def call(self, x):
x = self.static_layer(x)
for layer in self.my_layers:
x = layer(x)
return x
def get_config(self):
return super(TestLayer, self).get_config()
model = tf.keras.Sequential([TestLayer()])
x = tf.constant(42.0, shape=[1,1])
y1 = model(x)
model.save('my_test_model', save_format='tf')
model_loaded = tf.keras.models.load_model('my_test_model')
y2 = model_loaded(x)
# output
model.summary()
model_loaded.summary()
print('n vars: ', len(model.weights), ' ', len(model_loaded.weights))
print('diff: ', tf.norm(y1-y2))
输出
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
test_layer (TestLayer) (None, 128) 66304
=================================================================
Total params: 66,304
Trainable params: 66,304
Non-trainable params: 0
_________________________________________________________________
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
test_layer (TestLayer) (None, 128) 256
=================================================================
Total params: 256
Trainable params: 256
Non-trainable params: 0
_________________________________________________________________
n vars: 10 2
diff: tf.Tensor(0.0, shape=(), dtype=float32)
3条答案
按热度按时间kcugc4gi1#
我已经在Colab中尝试过使用TF版本2.3,并能够重现这个问题。请查看gist here.。然而,我在TF nightly版本中看到了以下错误信息(
AttributeError: module 'tensorflow' has no attribute 'python'
)。请查看gist here。谢谢!mklgxw1f2#
我能够复现
tf-nightly
的问题。here是我们的参考代码片段。谢谢!iyfjxgzm3#
在 TensorFlow 2.11 中能够重现您的问题,请查看以下代码片段:gist here。谢谢!