更新时间:2023-12-02 17:24:10
我正在回答自己的问题.如果有人有更好的解决方案.请张贴
I am answering my own question. If someone has a better solution. Kindly post it
这是我发现的解决方法:
This is a work around I found:
tf.device
上下文中使用JSON创建新模型tf.device
context
state_dict = {}
for layer in model.layers:
for weight in layer.weights:
state_dict[weight.name] = weight.numpy()
model_json_config = model.to_json()
tf.keras.backend.clear_session() # this is crucial to get previous names again
del model
with tf.device("/GPU:0"):
new_model = tf.keras.models.model_from_json(model_json_config)
for layer in new_model.layers:
current_layer_weights = []
for weight in layer.weights:
current_layer_weights.append(state_dict[weight.name])
layer.set_weights(current_layer_weights)