我想在模型中修改gumbel-softmax。我发现作者实现了张量流。我将其带到我的源代码中,并在lambda层中使用它替换了现有的softmax行为层。它起作用了,但是当该模型克隆其他模型时出现了问题,但出现了错误。有解决此问题的解决方案或经验吗?
感谢阅读。
Traceback (most recent call last):
File "D:/dev/workspace/starcraftrlchallenge/python/saida_agent_example/zealotVsZealot/zealot_vs_zealot_v0_Proposed_discreted_gumbel_mb.py", line 269, in <module>
agent.compile([Adam(lr=5e-5), Adam(lr=5e-5)], metrics=['mae'])
File "D:\dev\workspace\starcraftrlchallenge\python\core\algorithm\MAProposed.py", line 321, in compile
self.actor_train_on_batch = self.actor_optimizer()
File "D:\dev\workspace\starcraftrlchallenge\python\core\algorithm\MAProposed.py", line 163, in actor_optimizer
self.target_actor = keras.models.clone_model(self.actor)
File "C:\Users\SDS-1\Anaconda3\lib\site-packages\keras\models.py", line 251, in clone_model
return _clone_functional_model(model, input_tensors=input_tensors)
File "C:\Users\SDS-1\Anaconda3\lib\site-packages\keras\models.py", line 139, in _clone_functional_model
layer(computed_tensor, **kwargs))
File "C:\Users\SDS-1\Anaconda3\lib\site-packages\keras\engine\base_layer.py", line 457, in __call__
output = self.call(inputs, **kwargs)
File "C:\Users\SDS-1\Anaconda3\lib\site-packages\keras\layers\core.py", line 687, in call
return self.function(inputs, **arguments)
File "D:\dev\workspace\starcraftrlchallenge\python\saida_agent_example\zealotVsZealot\model\ac_networks_gumbel_mb.py", line 22, in <lambda>
action_logit = Lambda(lambda h : gumbel_softmax(h, hard=True))(h)
NameError: name 'gumbel_softmax' is not defined
答案 0 :(得分:0)
看看clone_model
source code似乎使用了与模型加载相同的from_config
方法。在那里(当加载模型时)要特别注意自定义对象。如Keras FAQ中所述:
如果要加载的模型包含自定义图层或其他自定义类或函数,则可以通过custom_objects参数将它们传递给加载机制。
尽管clone_model
没有这样的论点。一个简单的解决方法是保存和加载模型以便克隆它:
from keras.models import load_model
def gumbel_softmax:
....
model.save('tmp.h5')
clone = load_model('tmp.h5', custom_objects={'gumbel_softmax': gumbel_softmax})
os.remove('tmp.h5')