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

Neural network multi-agent system #58

Merged
merged 48 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
1a2386a
The test NeuralNetwork with target architecture
StannisMod Aug 22, 2023
9db1442
First version of dataset generator
StannisMod Aug 23, 2023
13eacd8
Merge branch 'main' into feature/nn
StannisMod Aug 23, 2023
ea5c01b
Made dataset loader
StannisMod Aug 23, 2023
b8d0bb9
Received first nn results
StannisMod Aug 23, 2023
ede54f9
Added neural network to multi-agency comparison
StannisMod Aug 23, 2023
285827a
Made NeuralNet module
StannisMod Aug 24, 2023
ca4f8a3
New graph metrics, fully configurable nn
StannisMod Aug 24, 2023
d585699
Added new features
vanoha01 Aug 30, 2023
5494722
Parallel generation introduced
vanoha01 Aug 30, 2023
23d545f
Parallel generation introduced
vanoha01 Aug 31, 2023
6b126f7
Parallel generation introduced
vanoha01 Aug 31, 2023
e1724fd
Parallel generation introduced
vanoha01 Aug 31, 2023
21e084e
Merge remote-tracking branch 'origin/main' into feature/nn
vanoha01 Sep 1, 2023
8a8eefd
Merge remote-tracking branch 'origin/main' into feature/nn
StannisMod Sep 1, 2023
3cd083b
WorkGraph and GraphNode delete function introduced
vanoha Sep 3, 2023
c3c1fc2
-
StannisMod Sep 4, 2023
853e4d5
Merge branch 'feature/nn' of https://github.com/Industrial-AI-Researc…
StannisMod Sep 4, 2023
e5fd20e
Merge remote-tracking branch 'origin/feature/nn' into feature/nn
vanoha01 Sep 4, 2023
a78a257
Cosmetic changes
vanoha01 Sep 4, 2023
676a4b5
Cosmetic changes
vanoha01 Sep 4, 2023
a37357a
Cosmetic changes
vanoha01 Sep 5, 2023
db31efd
Delete sklearn from requirements.txt due to 'pip install' deny to ins…
vanoha01 Sep 5, 2023
335dcbe
Normalized metrics
vanoha01 Sep 11, 2023
13fd021
Change hyperparameters of net
vanoha01 Sep 12, 2023
d443b0e
Found good hyperparameters.
vanoha01 Sep 13, 2023
649ba95
Hyper parameters search was constructed with the Ray Tune framework.
vanoha01 Sep 22, 2023
3ad132a
Pre-trained models are added
vanoha01 Sep 22, 2023
d083306
Added dataset with 10.000 objects
vanoha01 Sep 26, 2023
da04ad8
Comparison of neural network and multi agency algo presented
vanoha01 Oct 10, 2023
3605cd9
New neural network presented.
vanoha01 Oct 10, 2023
77c06bb
Refactored neural manager and added grid search for a regression task
vanoha01 Oct 16, 2023
5895738
Refactored grid search for classification in accordance to new arhite…
vanoha01 Oct 16, 2023
72c7b03
Refactored code
vanoha01 Oct 16, 2023
6b0dfe6
Refactored grid search.
vanoha01 Oct 18, 2023
a8fe281
Upgraded experiments neural manager and merely manager of MA
vanoha01 Oct 20, 2023
c851a3c
Upgraded experiments neural manager and merely manager of MA
vanoha01 Oct 20, 2023
5d82f96
Merge remote-tracking branch 'origin/feature/nn' into feature/nn
vanoha01 Oct 20, 2023
e731cdf
Upgraded experiments neural manager and merely manager of MA
vanoha01 Oct 20, 2023
a26d470
Cosmetic changes
vanoha01 Oct 23, 2023
be86e94
Cosmetic changes
vanoha01 Oct 23, 2023
05e51a9
Merge remote-tracking branch 'origin/main' into feature/nn
vanoha01 Oct 24, 2023
f96ec8f
Merge remote-tracking branch 'origin/main' into feature/nn
vanoha01 Oct 24, 2023
392865f
Cosmetic changes
vanoha01 Oct 25, 2023
fb69437
Merge remote-tracking branch 'origin/feature/nn' into feature/nn
vanoha01 Oct 25, 2023
38e7b3c
Added datasets
vanoha01 Oct 25, 2023
3b16d08
Cosmetic changes based on GitHub requests
vanoha01 Oct 26, 2023
b9c3cad
Merge branch 'main' into feature/nn
Timotshak Oct 31, 2023
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
1,037 changes: 522 additions & 515 deletions examples/visualization.ipynb

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
1,001 changes: 1,001 additions & 0 deletions experiments/neural_network/classification.ipynb

Large diffs are not rendered by default.

20,001 changes: 20,001 additions & 0 deletions experiments/neural_network/datasets/wg_algo_dataset_10k.csv

Large diffs are not rendered by default.

100,001 changes: 100,001 additions & 0 deletions experiments/neural_network/datasets/wg_contractor_dataset_100000_objs.csv

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions experiments/neural_network/grid_search_cv_classification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import os

import pandas as pd
import torch
import torchmetrics.classification
from ray import tune
from ray.air import session, Checkpoint
from ray.tune.schedulers import ASHAScheduler
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

from sampo.scheduler.selection.neural_net import NeuralNetTrainer, NeuralNet, NeuralNetType
from sampo.scheduler.selection.validation import cross_val_score

path = os.path.join(os.getcwd(), 'datasets/wg_algo_dataset_10k.csv')
vanoha marked this conversation as resolved.
Show resolved Hide resolved
dataset = pd.read_csv(path, index_col='index')
for col in dataset.columns[:-1]:
dataset[col] = dataset[col].apply(lambda x: float(x))

x_tr, x_ts, y_tr, y_ts = train_test_split(dataset.drop(columns=['label']), dataset['label'],
stratify=dataset['label'])

scaler = StandardScaler()
scaler.fit(x_tr)
scaled_dataset = scaler.transform(x_tr)
scaled_dataset = pd.DataFrame(scaled_dataset, columns=x_tr.columns)
x_tr = scaled_dataset

scaler = StandardScaler()
scaler.fit(x_ts)
scaled_dataset = scaler.transform(x_ts)
scaled_dataset = pd.DataFrame(scaled_dataset, columns=x_ts.columns)
x_ts = scaled_dataset


def train(config: dict) -> None:
"""
Training function for ray tune process

:param config: search space of the model's hyperparameters
"""
model = NeuralNet(input_size=13,
layer_size=config['layer_size'],
layer_count=config['layer_count'],
task_type=NeuralNetType.CLASSIFICATION)
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=config['lr'])
scorer = torchmetrics.classification.BinaryAccuracy()
net = NeuralNetTrainer(model, criterion, optimizer, scorer, 2)

x_train, x_test, y_train, y_test = x_tr, x_ts, y_tr, y_ts
score, best_loss, best_trainer = cross_val_score(X=x_train,
y=y_train,
model=net,
epochs=config['epochs'],
folds=config['cv'],
shuffle=True,
type_task=NeuralNetType.CLASSIFICATION)
# Checkpoint - structure of the saved model
checkpoint_data = {
'model_state_dict': best_trainer.model.state_dict(),
'optimizer_state_dict': best_trainer.optimizer.state_dict()
}
checkpoint = Checkpoint.from_dict(checkpoint_data)
# Report loss and score immediate metrics
session.report({'loss': best_loss, 'score': score}, checkpoint=checkpoint)
print('accuracy:', score)
print('Finished Training')
print('------------------------------------------------------------------------')


def best_test_score(best_trained_model: NeuralNetTrainer) -> None:
x_train, x_test, y_train, y_test = x_tr, x_ts, y_tr, y_ts

predicted = best_trained_model.predict_proba([torch.Tensor(v) for v in x_test.values])
array = []
label_test = y_test.to_numpy()
for i in range(len(predicted)):
flag = 0 if predicted[i][0] > predicted[i][1] else 1
array.append(int(flag == label_test[i]))
print('Best trial test set accuracy:', sum(array) / len(array))


def main():
# Dict represents the search space by model's hyperparameters
config = {
'layer_size': tune.grid_search([i for i in range(10, 11)]),
'layer_count': tune.grid_search([i for i in range(5, 6)]),
'lr': tune.loguniform(1e-5, 1e-3),
'epochs': tune.grid_search([2]),
'cv': tune.grid_search([5])
}

scheduler = ASHAScheduler(
metric='loss',
mode='min',
max_t=10,
grace_period=1,
reduction_factor=2
)

reporter = tune.CLIReporter(
metric_columns=['loss', 'score']
)

# Here you can change the number of CPU's you want to use for tuning
result = tune.run(
train,
resources_per_trial={'cpu': 6},
vanoha marked this conversation as resolved.
Show resolved Hide resolved
config=config,
num_samples=1,
scheduler=scheduler,
progress_reporter=reporter,
)

# Receive the trial with the best results
best_trial = result.get_best_trial('loss', 'min', 'last')
best_checkpoint = best_trial.checkpoint.to_air_checkpoint()
best_checkpoint_data = None
try:
best_checkpoint_data = best_checkpoint.to_dict()
except Exception as e:
Exception(f'{best_checkpoint} with {e}')

# Construct the best trainer based on the best checkpoint data
best_trained_model = NeuralNet(input_size=13,
layer_size=best_trial.config['layer_size'],
layer_count=best_trial.config['layer_count'],
out_size=2,
task_type=NeuralNetType.CLASSIFICATION)
best_trained_model.load_state_dict(best_checkpoint_data['model_state_dict'])
best_trained_optimizer = torch.optim.Adam(best_trained_model.model.parameters(), lr=best_trial.config['lr'])
best_trained_optimizer.load_state_dict(best_checkpoint_data['optimizer_state_dict'])
best_trainer = NeuralNetTrainer(best_trained_model,
torch.nn.CrossEntropyLoss(),
best_trained_optimizer,
scorer=torchmetrics.classification.BinaryAccuracy(),
batch_size=2)

# Print score of the best trainer on test sample
best_test_score(best_trainer)

best_trainer.save_checkpoint(os.path.join(os.getcwd(), 'checkpoints/'), '1.pth')

print(f'Best trial config: {best_trial.config}')
print(f'Best trial validation loss: {best_trial.last_result["loss"]}')
print(f'Best trial final validation accuracy: {best_trial.last_result["score"]}')


if __name__ == '__main__':
main()
136 changes: 136 additions & 0 deletions experiments/neural_network/grid_search_cv_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import os

import pandas as pd
import torch
import torchmetrics
from ray import tune
from ray.air import session, Checkpoint
from ray.tune import CLIReporter
from ray.tune.schedulers import ASHAScheduler
from sklearn.model_selection import train_test_split

from sampo.scheduler.selection.neural_net import NeuralNetTrainer, NeuralNet, NeuralNetType
from sampo.scheduler.selection.validation import cross_val_score

path = os.path.join(os.getcwd(), 'datasets/wg_contractor_dataset_100000_objs.csv')
dataset = pd.read_csv(path, index_col='index')
for col in dataset.columns[:-1]:
dataset[col] = dataset[col].apply(lambda x: float(x))

dataset['label'] = dataset['label'].apply(lambda x: [int(i) for i in x.split()])

x_tr, x_ts, y_tr, y_ts = train_test_split(dataset.drop(columns=['label']), dataset['label'])


def train(config: dict) -> None:
"""
Training function for ray tune process

:param config: search space of the model's hyperparameters
"""
model = NeuralNet(input_size=13,
layer_size=config['layer_size'],
layer_count=config['layer_count'],
out_size=6,
task_type=NeuralNetType.REGRESSION)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=config['lr'])
scorer = torchmetrics.regression.MeanSquaredError()
net = NeuralNetTrainer(model, criterion, optimizer, scorer, 32)

x_train, x_test, y_train, y_test = x_tr, x_ts, y_tr, y_ts
score, best_loss, best_trainer = cross_val_score(X=x_train,
y=y_train,
model=net,
epochs=config['epochs'],
folds=config['cv'],
shuffle=True,
type_task=NeuralNetType.REGRESSION)
# Checkpoint - structure of the saved model
checkpoint_data = {
'model_state_dict': best_trainer.model.state_dict(),
'optimizer_state_dict': best_trainer.optimizer.state_dict()
}
# Report loss and score immediate metrics
checkpoint = Checkpoint.from_dict(checkpoint_data)
session.report({'loss': best_loss, 'score': score}, checkpoint=checkpoint)
print('MSE:', score)
print('Finished Training')
print('------------------------------------------------------------------------')


def best_test_score(best_trained_model: NeuralNetTrainer) -> None:
x_train, x_test, y_train, y_test = x_tr, x_ts, y_tr, y_ts

predicted = best_trained_model.predict([torch.Tensor(v) for v in x_test.values])
array = []
label_test = y_test.to_numpy()
for i in range(len(predicted)):
array.append(sum((predicted[i] - label_test[i]) ** 2))
print('Best trial test set RMSE:', (sum(array) / len(array)) ** (1 / 2))


def main():
# Dict represents the search space by model's hyperparameters
config = {
'iters': tune.grid_search([i for i in range(1)]),
'layer_size': tune.qrandint(5, 30),
'layer_count': tune.qrandint(5, 35),
'lr': tune.loguniform(1e-4, 1e-1),
'epochs': tune.grid_search([2]),
'cv': tune.grid_search([2]),
}

scheduler = ASHAScheduler(
metric='loss',
mode='min',
max_t=10,
grace_period=1,
reduction_factor=2
)

reporter = CLIReporter(
metric_columns=['loss', 'score']
)

# Here you can change the number of CPU's you want to use for tuning
result = tune.run(
train,
resources_per_trial={'cpu': 6},
config=config,
num_samples=1,
scheduler=scheduler,
progress_reporter=reporter,
)

# Receive the trial with the best results
best_trial = result.get_best_trial('loss', 'min', 'last')
best_checkpoint = best_trial.checkpoint.to_air_checkpoint()
best_checkpoint_data = None
try:
best_checkpoint_data = best_checkpoint.to_dict()
except Exception as e:
Exception(f'{best_checkpoint} with {e}')

# Construct the best trainer based on the best checkpoint data
best_trained_model = NeuralNet(13, layer_size=best_trial.config['layer_size'],
layer_count=best_trial.config['layer_count'],
out_size=6)
best_trained_model.load_state_dict(best_checkpoint_data['model_state_dict'])
best_trained_optimizer = torch.optim.Adam(best_trained_model.model.parameters(), lr=best_trial.config['lr'])
best_trained_optimizer.load_state_dict(best_checkpoint_data['optimizer_state_dict'])
scorer = torchmetrics.regression.MeanSquaredError()
best_trainer = NeuralNetTrainer(best_trained_model, torch.nn.CrossEntropyLoss(), best_trained_optimizer, scorer, 32)

# Print score of the best trainer on test sample
best_test_score(best_trainer)

best_trainer.save_checkpoint(os.path.join(os.getcwd(), 'checkpoints/'), 'best_model_wg_and_contractor.pth')

print(f'Best trial config: {best_trial.config}')
print(f'Best trial validation loss: {best_trial.last_result["loss"]}')
print(f'Best trial final validation accuracy: {best_trial.last_result["score"]}')


if __name__ == '__main__':
main()
Loading