Skip to content

Commit

Permalink
Added the evaluation step at the training phase. Still need to create…
Browse files Browse the repository at this point in the history
… a test_dataset from the original dataset to evaluate at the server side.
  • Loading branch information
AlArgente committed Jan 22, 2024
1 parent eb8c7be commit 6bc637d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions flexnlp/notebooks/FederatedSS.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,25 @@ def copy_server_model_to_clients(server_flex_model: FlexModel):

# Prepare data for training phase

def create_input_examples_for_training(X_data_as_list):
def create_input_examples_for_training(X_data_as_list, X_test_as_list):
"""Function to create a DataLoader to train/finetune the model at client level
Args:
X_data_as_list (list): List containing the examples. Each example is a dict
with the following keys: query, pos, neg.
"""
train_examples = [InputExample(texts=[example['query'], example['pos'][0], example['neg'][0]]) for example in X_data_as_list]
return DataLoader(train_examples, shuffle=True, batch_size=16), train_examples
dev_examples = [InputExample(texts=[example['query'], example['pos'][0], example['neg'][0]]) for example in X_test_as_list]
return DataLoader(train_examples, shuffle=True, batch_size=16), dev_examples

def train(client_flex_model: FlexModel, client_data: Dataset):
print("Training client")
model = client_flex_model['model']
# client_train_dataset = client_data.to_numpy()
train_dataloader, dev_examples = create_input_examples_for_training(X_data_as_list=client_data.X_data.tolist())
X_data = client_data.X_data.tolist()
tam_train = int(len(X_data) * 0.75)
X_data, X_test = X_data[:tam_train], X_data[tam_train:]
train_dataloader, dev_examples = create_input_examples_for_training(X_data_as_list=X_data, X_test_as_list=X_test)
train_loss = losses.TripletLoss(model=model)
evaluator = TripletEvaluator.from_input_examples(dev_examples)
warmup_steps = int(len(train_dataloader) * 1 * 0.1) #10% of train data
Expand All @@ -120,6 +124,7 @@ def train(client_flex_model: FlexModel, client_data: Dataset):
evaluator=evaluator,
evaluation_steps=1000,
)
model.evaluate(evaluator, 'model_evaluation')
sentences = ['This is an example sentence', 'Each sentence is converted']
encodings = model.encode(sentences)
print(f"New encodings: {encodings}")
Expand Down

0 comments on commit 6bc637d

Please sign in to comment.