Skip to content
Snippets Groups Projects
Unverified Commit 6f31fcb7 authored by Jakub Beránek's avatar Jakub Beránek
Browse files

Add exercise 2

parent 944e09e4
No related branches found
No related tags found
No related merge requests found
Showing
with 194 additions and 0 deletions
...@@ -8,5 +8,7 @@ This repository contains a few simple tasks for practicing work with HyperQueue. ...@@ -8,5 +8,7 @@ This repository contains a few simple tasks for practicing work with HyperQueue.
## Exercises ## Exercises
First, download and extract [HyperQueue](https://github.com/It4innovations/hyperqueue/releases/download/v0.17.0/hq-v0.17.0-linux-x64.tar.gz). First, download and extract [HyperQueue](https://github.com/It4innovations/hyperqueue/releases/download/v0.17.0/hq-v0.17.0-linux-x64.tar.gz).
Then you can try to implement the following exercises:
- [Exercise 1](exercise-1) - [Exercise 1](exercise-1)
- [Exercise 1b](exercise-1b) - [Exercise 1b](exercise-1b)
- [Exercise 2](exercise-2)
# Exercise 2
The goal of this exercise is to execute a computational graph containing dependencies between
individual tasks, using the HyperQueue [Python API](https://it4innovations.github.io/hyperqueue/stable/python/).
For this task, you will need to have Python 3.6+ and the `hyperqueue` Python package available. You
can install the package from [PyPi](https://pypi.org/project/hyperqueue/):
```bash
# Create a virtual environment
$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python3 -m pip install -U setuptools wheel pip
# Install the HyperQueue Python package
(venv) $ python3 -m pip install hyperqueue[all]
```
The `configs` directory contains several configurations for training a machine learning model.
Your goal is to train a model for each configuration, and then postprocess the results of all the
trainings into a single result file, which will print the results of the individual trainings,
sorted by accuracy.
Perform the following tasks:
1) Implement the `train_model_task` function in [`tasks.py`](tasks.py).
2) Create HQ tasks for training the models and for performing the final postprocsesing in
[`main.py`](main.py)
## Checking the results
After the job is completed, check the contents of the `output/result.txt` file. It should contain
the training results of all model configurations.
{
"learning_rate": 0.01,
"batch_size": 1
}
\ No newline at end of file
{
"learning_rate": 0.01,
"batch_size": 2
}
\ No newline at end of file
{
"learning_rate": 0.0001,
"batch_size": 1
}
\ No newline at end of file
{
"learning_rate": 0.0001,
"batch_size": 2
}
\ No newline at end of file
{
"learning_rate": 0.0001,
"batch_size": 4
}
\ No newline at end of file
{
"learning_rate": 0.0001,
"batch_size": 8
}
\ No newline at end of file
{
"learning_rate": 0.0001,
"batch_size": 16
}
\ No newline at end of file
{
"learning_rate": 0.01,
"batch_size": 4
}
\ No newline at end of file
{
"learning_rate": 0.01,
"batch_size": 8
}
\ No newline at end of file
{
"learning_rate": 0.01,
"batch_size": 16
}
\ No newline at end of file
{
"learning_rate": 0.001,
"batch_size": 1
}
\ No newline at end of file
{
"learning_rate": 0.001,
"batch_size": 2
}
\ No newline at end of file
{
"learning_rate": 0.001,
"batch_size": 4
}
\ No newline at end of file
{
"learning_rate": 0.001,
"batch_size": 8
}
\ No newline at end of file
{
"learning_rate": 0.001,
"batch_size": 16
}
\ No newline at end of file
"""
You don't need to modify this file.
"""
import json
import random
import time
from pathlib import Path
def train_model(learning_rate: float, batch_size: float, output_path: Path):
"""
"Trains" a machine learning model using the given parameters (`learning_rate` and `batch_size`)
and stores the result into `output_path`.
"""
print(f"Training model using learning_rate={learning_rate} and batch_size={batch_size}")
time.sleep(30 / batch_size)
accuracy = 0.9 + (random.random() / 10)
print(f"Model training finished, resulting accuracy: {accuracy}")
with open(output_path, "w") as f:
json.dump({
"parameters": dict(learning_rate=learning_rate, batch_size=batch_size),
"accuracy": accuracy
}, f, indent=4)
import glob
import shutil
from pathlib import Path
from hyperqueue import Job, LocalCluster
from hyperqueue.visualization import visualize_job
from tasks import postprocess_task, train_model_task
if __name__ == "__main__":
# Spawn a HQ server
with LocalCluster() as cluster:
# Add a single HyperQueue worker to the server
cluster.start_worker()
# Create a client and a job
client = cluster.client()
job = Job()
# Directory where output will be stored
output_dir = Path("output")
shutil.rmtree(output_dir, ignore_errors=True)
output_dir.mkdir(parents=True, exist_ok=True)
train_tasks = []
result_files = []
for (index, config_file) in enumerate(sorted(glob.glob("configs/*.json"))):
# TODO: add a Python function task to the job. The task should compute the
# `train_model_task` function. The path to the config file, and the path to a result
# file should be passed as arguments to the task. Create a unique result path for each
# task (e.g. output/<index>-result.json).
# TODO: create the final postprocessing task, which should execute the `postprocessing_task`
# function. The task should receive a list of results from the model training and a path to
# the final result file (`postprocessing_result_path`) as arguments.
# Submit the job
submitted = client.submit(job)
# Visualize the created job using the DOT format.
# You can render the graph using `$ xdot job.dot`.
visualize_job(job, "job.dot")
# Wait until the job completes
client.wait_for_jobs([submitted])
import json
from pathlib import Path
from typing import List
from functions import train_model
def train_model_task(config: Path, output_path: Path):
"""
TODO: read the JSON config from `config` and pass its parameters (along with `output_path`)
to the `train_model` function.
"""
def postprocess_task(result_files: List[Path], output_file: Path):
results = []
for result in result_files:
with open(result) as f:
result = json.load(f)
results.append(result)
results = sorted(results, key=lambda r: r["accuracy"], reverse=True)
with open(output_file, "w") as f:
print("Training results sorted by accuracy:", file=f)
for result in results:
learning_rate = result["parameters"]["learning_rate"]
batch_size = result["parameters"]["batch_size"]
accuracy = result["accuracy"]
print(
f"Learning rate={learning_rate}, batch_size={batch_size}: {accuracy * 100.0:.2f}%",
file=f
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment