-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: yes <[email protected]>
- Loading branch information
Showing
5 changed files
with
84 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
tensorflow==2.13 | ||
keras==3.6.0 | ||
tensorflow==2.18.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"""Copyright (C) 2020-2021 Intel Corporation | ||
SPDX-License-Identifier: Apache-2.0 | ||
Licensed subject to the terms of the separately executed evaluation | ||
license agreement between Intel Corporation and you. | ||
""" | ||
import keras as ke | ||
|
||
from openfl.federated import KerasTaskRunner | ||
|
||
|
||
class KerasNLP(KerasTaskRunner): | ||
"""A basic convolutional neural network model.""" | ||
|
||
def __init__(self, latent_dim, **kwargs): | ||
""" | ||
Init taskrunner. | ||
Args: | ||
**kwargs: Additional parameters to pass to the function | ||
""" | ||
super().__init__(**kwargs) | ||
|
||
self.model = self.build_model(latent_dim, | ||
self.data_loader.num_encoder_tokens, | ||
self.data_loader.num_decoder_tokens, | ||
**kwargs) | ||
|
||
self.initialize_tensorkeys_for_functions() | ||
|
||
self.model.summary(print_fn=self.logger.info) | ||
|
||
self.logger.info(f'Train Set Size : {self.get_train_data_size()}') | ||
|
||
def build_model(self, latent_dim, num_encoder_tokens, num_decoder_tokens, **kwargs): | ||
""" | ||
Define the model architecture. | ||
Args: | ||
input_shape (numpy.ndarray): The shape of the data | ||
num_classes (int): The number of classes of the dataset | ||
Returns: | ||
tensorflow.python.keras.engine.sequential.Sequential: The model defined in Keras | ||
""" | ||
encoder_inputs = ke.Input(shape=(None, num_encoder_tokens)) | ||
encoder = ke.layers.LSTM(latent_dim, return_state=True) | ||
encoder_outputs, state_h, state_c = encoder(encoder_inputs) | ||
|
||
# We discard `encoder_outputs` and only keep the states. | ||
encoder_states = [state_h, state_c] | ||
|
||
# Set up the decoder, using `encoder_states` as initial state. | ||
decoder_inputs = ke.Input(shape=(None, num_decoder_tokens)) | ||
|
||
# We set up our decoder to return full output sequences, | ||
# and to return internal states as well. We don't use the | ||
# return states in the training model, but we will use them in inference. | ||
decoder_lstm = ke.layers.LSTM(latent_dim, return_sequences=True, return_state=True) | ||
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states) | ||
decoder_dense = ke.layers.Dense(num_decoder_tokens, activation='softmax') | ||
decoder_outputs = decoder_dense(decoder_outputs) | ||
|
||
# Define the model that will turn | ||
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data` | ||
model = ke.Model([encoder_inputs, decoder_inputs], decoder_outputs) | ||
|
||
model.compile( | ||
optimizer="RMSprop", | ||
loss='categorical_crossentropy', metrics=['accuracy'] | ||
) | ||
|
||
return model |