简体   繁体   English

如何使用自定义层 Tensorflow2 保存自定义 model

[英]How to save a custom model with custom layer Tensorflow2

I am trying to save weight of model: model.save_weights('filepath')我正在尝试节省 model 的重量: model.save_weights('filepath')

My model is我的 model 是

class CLayer(Layer):
    def __init__(self, hidden_units=[]):
        super(PartCoder, self).__init__()
        # self.layers = NoDependency([])
        # self.__dict__['layers'] = []
        self.layers = []
        for u in hidden_units:
            self.layers.append(keras.layers.Dense(u))

class CModel(Model):
    def __init__(self, hidden_units):
        super(CustomModel, self).__init__()
        self.cLayers = CLayer(hidden_units=hidden_units)


model_1 = CModel(hidden_units=[2,4])  
model_1.save_weights("filepath_1")  #--> worked

model_2 = CModel(hidden_units=[2,4])
model_2.load_weights("filepaht_1")  #--> worked

#train model_2
model_2.save_weights("filepath_2") #--> Crash here

Result:结果:

Epoch 5: Loss = 72.76963806152344
Epoch 10: Loss = 68.98507690429688
Epoch 15: Loss = 65.25872039794922
Epoch 20: Loss = 61.60311508178711
Epoch 25: Loss = 58.087005615234375

But with errors:但有错误:

ValueError: Unable to save the object ListWrapper([<tensorflow.python.keras.layers.core.Dense object at 0x7f032829d4e0>, <tensorflow.python.keras.layers.core.Dense object at 0x7f032829da90>, <tensorflow.python.keras.layers.core.Dense object at 0x7f032ad38e80>, <tensorflow.python.keras.layers.core.Dense object at 0x7f0328165048>]) (a list wrapper constructed to track trackable TensorFlow objects). A list element was replaced (__setitem__, __setslice__), deleted (__delitem__, __delslice__), or moved (sort). In order to support restoration on object creation, tracking is exclusively for append-only data structures.

If you don't need this list checkpointed, wrap it in a tf.contrib.checkpoint.NoDependency object; it will be automatically un-wrapped and subsequently ignored.

After I find on google, I tried to use:在谷歌上找到后,我尝试使用:

self.__dict__['layers'] = []
#OR: self.layers = NoDependency([])

for saving list of layer in CLayer class.用于在CLayer class 中保存层列表。 This solution is works but the weights in layer is not updated.此解决方案有效,但未更新层中的权重。

Epoch 5: Loss = 76.57701873779297
Epoch 10: Loss = 76.57701873779297
Epoch 15: Loss = 76.57701873779297
Epoch 20: Loss = 76.57701873779297

Link: Tried solution链接:尝试的解决方案

My question is how to save the model with custom layers as the above example model without error.我的问题是如何保存带有自定义层的 model 作为上述示例 model 没有错误。

After long time I search and fix in all way that I can.很长一段时间后,我尽我所能搜索和修复。 Then I have moved to Pytorch.然后我搬到了 Pytorch。 It is easier to saving model without error.没有错误地保存 model 更容易。 Finally, I solved by using Pytorch and it worked.最后,我通过使用 Pytorch 解决了它并且它有效。

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

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