Skip to content Skip to sidebar Skip to footer

How To Move A Tensorflow.keras Model To Gpu

Let's say I have a keras model like this: with tf.device('/CPU'): model = tf.keras.Sequential([ # Adds a densely-connected layer with 64 units to the model: tf.keras.la

Solution 1:

I am answering my own question. If someone has a better solution. Kindly post it

This is a work around I found:

  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 againdel 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)

Post a Comment for "How To Move A Tensorflow.keras Model To Gpu"