diff --git a/README.md b/README.md index ef65200..2b35294 100644 --- a/README.md +++ b/README.md @@ -133,12 +133,16 @@ Use the `dataset` command: This command collates all logs of dimension N+1 from the logs directory and saves the data in a compressed `.npz` file in the datasets directory. It will also print out some information about your dataset, in particular the total number of individual interactions. To have a useful dataset, it's good to start with more than 10,000 individual interactions but YMMV. -To train the model, use the `train` command---this can take a while on a normal computer, so be prepared to let your computer sit and think for a few hours! You'll have to decide what _size_ model to try to train: `xxs`, `xs`, `s`, `m`, `l`, `xl`. The size refers to the number of LSTM units in each layer of your model and roughly corresponds to "learning capacity" at a cost of slower training and predictions. +To train the model, use the `train` command. You don't need a GPU: an `s` model trained on a laptop CPU with around 10,000 interactions takes a few minutes. You'll have to decide what _size_ model to try to train: `xxs`, `xs`, `s`, `m`, `l`, `xl`. The size refers to the number of LSTM units in each layer of your model and roughly corresponds to "learning capacity" at a cost of slower training and predictions. It's a good idea to start with an `xs` or `s` model, and the larger models may work better for quite large datasets (e.g., >1M individual interactions). - poetry run ./start_impsy.py train --dimension (N+1) --modelsize s + poetry run ./start_impsy.py train datasets/training-dataset-(N+1)d.npz --modelsize s -Early stopping is enabled by default and will stop training after the model stops improving for 10 epochs (configurable with `--patience`). You can disable it with `--no-earlystopping`. +The model's dimension is read from the dataset. You can also leave out the dataset file and give `--dimension (N+1)` to use `datasets/training-dataset-(N+1)d.npz`. + +Early stopping is enabled by default and will stop training after the model stops improving for 10 epochs (configurable with `--patience`). You can disable it with `--no-earlystopping`. If you press `Ctrl-C` during training, IMPSY stops and saves the model as it is. + +You can also upload logs, make a dataset and train a model from the web UI (`impsy webui`, see the Train page) on whatever device is running IMPSY, e.g., a laptop running the Docker image or a Raspberry Pi. A Raspberry Pi 4 or 5 should be able to train small models, but we wouldn't recommend training on a Pi Zero 2 W. By default, your trained model will be saved in the `models` directory in `.keras` and `.tflite` format. diff --git a/docs/impsypi-workshop.md b/docs/impsypi-workshop.md index 7f6de25..225fd0e 100644 --- a/docs/impsypi-workshop.md +++ b/docs/impsypi-workshop.md @@ -123,7 +123,7 @@ Finally, the SD card is configured to allow ethernet over USB so you can plug yo Connecting to the Raspberry Pi should be straightforward: if your computer knows about it over USB-ethernet, you can just type into a web browser and see the web UI. The Pi gives itself the IP address 169.254.1.107 so you might try as well if the above doesn't work. -Now you can poke around the web UI and see what you can do. Most importantly, you can update the configuration file directly, download log files and generate or download datasets. You can't train new models from the web UI (yet!) and it's possibly a bad idea to train models on the slower Raspberry Pis anyway. +Now you can poke around the web UI and see what you can do. Most importantly, you can update the configuration file directly, upload or download log files and generate or download datasets. The web UI can also train models on the Train page. A Raspberry Pi 4 or 5 should be able to train a small model (try `xs` or `s`), but we wouldn't recommend training on a Pi Zero 2 W; instead, train on your laptop (e.g., with the Docker image) and upload the `.tflite` model to your Pi. > N.B.: I'm new to this whole ethernet over USB and SD card building business. I'm probably doing something wrong so if there are issues here please be patient and if you know a better way to set something up, let me know! @@ -308,7 +308,7 @@ docker run --rm \ --volume ./datasets:/impsy/datasets \ --volume ./models:/impsy/models \ charlepm/impsy \ - poetry run ./start_impsy.py train -D 4 -S datasets/training-dataset-4d.npz + poetry run ./start_impsy.py train datasets/training-dataset-4d.npz ``` to train a 4D model. This command makes sure that the model files that are saved by the train command get saved out of the docker container and in your regular computer filesystem. diff --git a/impsy/dataset.py b/impsy/dataset.py index aef8875..5418135 100755 --- a/impsy/dataset.py +++ b/impsy/dataset.py @@ -70,6 +70,7 @@ def generate_dataset( # Save Performance Data in a compressed numpy file. dataset_name = filename if filename else f"training-dataset-{dimension}d.npz" + Path(destination).mkdir(parents=True, exist_ok=True) dataset_file = Path(destination) / dataset_name # Input format is: diff --git a/impsy/mdrnn.py b/impsy/mdrnn.py index 5b64340..ce95c5d 100644 --- a/impsy/mdrnn.py +++ b/impsy/mdrnn.py @@ -227,8 +227,11 @@ def train( save_location="models", validation_split=0.1, patience=10, + callbacks=None, ): - """Train the network for a number of epochs with a specific dataset.""" + """Train the network for a number of epochs with a specific dataset. + + Extra Keras callbacks can be given in callbacks.""" save_location = Path(save_location) checkpoint_path = save_location / f"{self.model_name}-ckpt.keras" checkpoint_callback = tf.keras.callbacks.ModelCheckpoint( @@ -242,11 +245,13 @@ def train( early_stopping_callback = tf.keras.callbacks.EarlyStopping( monitor="val_loss", mode="min", verbose=1, patience=patience ) + extra_callbacks = list(callbacks or []) callbacks = [terminateOnNaN] if checkpointing: callbacks.append(checkpoint_callback) if early_stopping: callbacks.append(early_stopping_callback) + callbacks += extra_callbacks # Do the data scaling in here. X = np.array(X, dtype=np.float32) * SCALE_FACTOR diff --git a/impsy/templates/base.html b/impsy/templates/base.html index ba3a03c..4e3e8d9 100644 --- a/impsy/templates/base.html +++ b/impsy/templates/base.html @@ -34,6 +34,9 @@ Datasets {% if workflow.datasets %}✓{% endif %} +