Skip to content

Instantly share code, notes, and snippets.

@bzamecnik
Last active November 26, 2021 11:00
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bzamecnik/bd3786a074f8cb891bc2a397343070f1 to your computer and use it in GitHub Desktop.
Save bzamecnik/bd3786a074f8cb891bc2a397343070f1 to your computer and use it in GitHub Desktop.
CuDNN-compatible GRU in Keras
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bzamecnik
Copy link
Author

In the resulting PR it's even possible to convert GRU weights to CuDNNGRU and LSTM weights to CuDNNLSTM!

@SatishDivakarla
Copy link

I am getting following error when trying to load_weights that are saved from cudnngru model.

AttributeError: 'Dataset' object has no attribute 'reshape'

Architecture code on GPU machine:
input_layer = Input(shape=(sequence_length,))
embedding_layer = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix], trainable=False)(input_layer)
x = Bidirectional(CuDNNGRU(recurrent_units, return_sequences=True))(embedding_layer)
x = Dropout(dropout_rate)(x)
x = Bidirectional(CuDNNGRU(recurrent_units, return_sequences=True))(x)
x_max = GlobalMaxPool1D()(x)
x_avg = GlobalAveragePooling1D()(x)
x = concatenate([x_max, x_avg])
# x = Dense(dense_size, activation="relu")(x)
output_layer = Dense(6, activation="sigmoid")(x)
model = Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='binary_crossentropy', optimizer=RMSprop(clipvalue=1, clipnorm=1), metrics=['accuracy'])

Code to save the weights on GPU machine:
model.save_weights("model0_weights.h5")

Architecture code on CPU machine:
input_layer = Input(shape=(sequence_length,))
embedding_layer = Embedding(embedding_matrix.shape[0], embedding_matrix.shape[1],
weights=[embedding_matrix], trainable=False)(input_layer)
x = Bidirectional(GRU(recurrent_units, reset_after=True, recurrent_activation='sigmoid', return_sequences=True))(embedding_layer)
x = Dropout(dropout_rate)(x)
x = Bidirectional(GRU(recurrent_units, reset_after=True, recurrent_activation='sigmoid', return_sequences=True))(x)
x_max = GlobalMaxPool1D()(x)
x_avg = GlobalAveragePooling1D()(x)
x = concatenate([x_max, x_avg])
output_layer = Dense(6, activation="sigmoid")(x)
model = Model(inputs=input_layer, outputs=output_layer)
model.compile(loss='binary_crossentropy', optimizer=RMSprop(clipvalue=1, clipnorm=1), metrics=['accuracy'])

Code to reload the saved weights:
model_0_weights = model.load_weights("model0_weights.h5")

Please let me know if I am doing something wrong.

@SatishDivakarla
Copy link

@bzamecnik, Could you please respond to my comment. I have a trained model with CudNNGRU trained on GPU, but I am not able to make them use on CPU environment for predictions. Thanks in advance.

@leowang16
Copy link

Having the same error:
AttributeError: 'Dataset' object has no attribute 'reshape'

@bzamecnik
Copy link
Author

@SatishDivakarla: Sorry, I missed your comment... Don't you have the original stack trace? If I remember correctly I saw something similar in Keras issues and the cause was that the weights were a h5py Dataset instead of a numpy array. I'm not sure about resolution.

@bzamecnik
Copy link
Author

bzamecnik commented Apr 23, 2018

@SatishDivakarla: Aah, keras-team/keras#9112 (comment). Fixed in keras-team/keras#9662 (2018-03-15). Will be released in Keras 2.1.6.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment