且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将tensorflow.keras模型移动到GPU

更新时间: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:

  1. 创建一个像PyTorch这样的state_dict
  2. 获取模型架构为JSON
  3. 清除Keras会话并删除模型实例
  4. tf.device上下文中使用JSON创建新模型
  5. 从state_dict加载先前的权重
  1. Create a state_dict like PyTorch
  2. Get the model architecture as JSON
  3. Clear the Keras session and delete the model instance
  4. Create a new model from the JSON within tf.device context
  5. Load the previous weights from state_dict

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)