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:
- Create a state_dict like PyTorch
- Get the model architecture as JSON
- Clear the Keras session and delete the model instance
- Create a new model from the JSON within
tf.device
context - 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"