Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply penalty on duplicated models. #55

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions healthcare/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from math import floor
from typing import Callable, Any
from functools import lru_cache, update_wrapper
import numpy as np
from tensorflow.keras.models import load_model


# LRU Cache with TTL
Expand Down Expand Up @@ -110,3 +112,27 @@ def ttl_get_block(self) -> int:
Note: self here is the miner or validator instance
"""
return self.subtensor.get_current_block()

def compare_keras_models(model1_path: str, model2_path: str) -> bool:
"""
Compares two Keras models based on their weights.

Args:
model1_path (str): The path to the first model.
model2_path (str): The path to the second model.

Returns:
bool: True if the models are equal, False otherwise.
"""
weights1 = load_model(model1_path).get_weights()
weights2 = load_model(model2_path).get_weights()

if len(weights1) != len(weights2):
return False
else:
are_equal = all([np.array_equal(w1, w2) for w1, w2 in zip(weights1, weights2)])

if are_equal:
return True
else:
return False
25 changes: 23 additions & 2 deletions healthcare/validator/reward.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from tensorflow.keras.models import load_model
from healthcare.dataset.dataset import load_dataset, load_and_preprocess_image
from constants import BASE_DIR
from healthcare.utils.misc import compare_keras_models


@contextmanager
Expand Down Expand Up @@ -128,8 +129,28 @@ def get_rewards(
alpha = 0.98 # Step size used for calculating reward movement

# Rank of models
loss_indices = list(enumerate(loss_of_models)) # Combine loss values with their corresponding indices
sorted_indices = sorted(loss_indices, key=lambda x: (x[1], commit_blocks[x[0]])) # Loss first, then time
loss_indices = list(enumerate(zip(loss_of_models, model_paths))) # Combine loss values with their corresponding indices
sorted_indices = sorted(loss_indices, key=lambda x: (x[1][0], commit_blocks[x[0]])) # Loss first, then time

current_loss = float('inf')
valid_model_paths = []
for idx, (loss, model_path) in sorted_indices:
if loss != current_loss:
# if loss is not equal to current loss, the model is not duplicated
current_loss = loss
valid_model_paths = [model_path]
continue
for valid_model_path in valid_model_paths[0:]:
if compare_keras_models(valid_model_path, model_path):
# if models are equal, we will set the loss of the next model to infinity
loss_of_models[idx] = float('inf')
else:
# if models are not equal, we will add the model to the valid model paths
valid_model_paths.append(model_path)
# sort again after setting loss of the duplicated model to infinity
loss_indices = list(enumerate(loss_of_models))
sorted_indices = sorted(loss_indices, key=lambda x: (x[1], commit_blocks[x[0]]))

ranks = {} # A dictionary to store ranks

# Assign ranks to the sorted indices
Expand Down