Skip to content

Commit

Permalink
Merge a three-part tutorial into one markdown file (#523)
Browse files Browse the repository at this point in the history
* Merge a three-part tutorial into one markdown file

* Edit tutorial text
  • Loading branch information
juliabeliaeva committed Feb 27, 2023
1 parent 5031f5d commit a0eacbc
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 118 deletions.
126 changes: 122 additions & 4 deletions docs/create_your_first_nn.md
@@ -1,5 +1,5 @@
In this tutorial, we'll use Kotlin DL to create a neural network, and you'll learn about the basic building blocks and concepts needed to get started with Deep Learning.
In subsequent tutorials, you will learn how to [train this neural network](training_a_model.md) and [use the resulting model to generate predictions](loading_trained_model_for_inference.md).
In this tutorial you'll learn about the basic building blocks and concepts needed to get started with Deep Learning.
We'll use Kotlin DL to create a neural network, train it and use the resulting model to generate predictions.
No previous deep learning experience is required to get started with these tutorials.

## Image Classification
Expand Down Expand Up @@ -165,6 +165,124 @@ Total params: 266610
==============================================================================
```
Great! We have now defined the structure of our neural network, specified the optimization algorithm that will be used during its training, and identified accuracy as the metric we will use to gauge its success.

In the [following tutorial](training_a_model.md), you'll learn how to train this model on the [Fashion-MNIST dataset](https://github.com/zalandoresearch/fashion-mnist).
## Training a model

In the previous sections, we defined our neural network's structure,
specified the optimization algorithm that will be used during its training, and identified accuracy as the metric we will use to gauge its success.

In this section you'll learn how to train this model on the [Fashion-MNIST dataset](https://github.com/zalandoresearch/fashion-mnist).

Before you can use data, typically, some preprocessing is required.
In this case, it's minimal – all the images are already the same size and are grayscale.
With the built-in functionality, we can convert the [Fashion MNIST image archives](https://github.com/zalandoresearch/fashion-mnist#get-the-data) into a dataset object used for model training.

```kotlin
val (train, test) = fashionMnist()
```

You may also notice that we are splitting the data into two sets.
We have the test set, which we won't be touching until we are satisfied with the model and want to confirm its performance on unseen data.
And we have the train set which we'll use during the training process.

Note that it's a slightly simplified approach.
In practice, there is also a third subset of data.
It's called a validation set, and it's needed to monitor the model target metrics during training and tuning the model hyperparameters.
Another common technique for robust model evaluation is [cross-validation](https://en.wikipedia.org/wiki/Cross-validation_(statistics)).
Splitting a dataset for model evaluation is essential in deep learning and machine learning, but it's enough for our tutorial to use a simplified approach.

Now everything is ready to train the model. Use the `fit()` method for this:

```kotlin
model.use {
it.compile(
optimizer = Adam(),
loss = Losses.SOFT_MAX_CROSS_ENTROPY_WITH_LOGITS,
metric = Metrics.ACCURACY
)

it.printSummary()

// You can think of the training process as "fitting" the model to describe the given data :)
it.fit(
dataset = train,
epochs = 10,
batchSize = 100
)

val accuracy = it.evaluate(dataset = test, batchSize = 100).metrics[Metrics.ACCURACY]

println("Accuracy: $accuracy")
it.save(File("model/my_model"), writingMode = WritingMode.OVERRIDE)
}
```

Here are some important parameters that we need to pass to the `fit()` method:
* `epochs` - Number of iterations over the data you want the training process to perform. Epoch = iteration.
* `batchSize` - How many examples will be used for updating the model's parameters (aka weights and biases) at a time.

After the model has been trained, it's important to evaluate its performance on the test set, so that we can check how it generalizes to the new data.

```kotlin
val accuracy = it.evaluate(dataset = test,
batchSize = 100).metrics[Metrics.ACCURACY]

println("Accuracy: $accuracy")
```

```
Accuracy: 0.8821001648902893
```

---
**NOTE**

The results are nondeterministic, and you may have a slightly different Accuracy value.

---

When we are happy with the model's evaluation metric, we can save the model for future use in the production environment.

```kotlin
it.save(File("model/my_model"), writingMode = WritingMode.OVERRIDE)
```

And just like that, we have trained, evaluated, and saved a deep learning model that we can now use to generate predictions (aka inference).

## Loading trained model for inference

In this section let's look at how to load and use a trained model to generate predictions on new, previously unseen data.

For illustration purposes, and to simplify this tutorial,
we'll use the test data subset to generate a prediction example that the model has not been trained on.

The example images in the test data have the same size and format as the ones the model has been trained on,
so we do not need to do any additional preprocessing.
However, if you train the model on your data,
use the same image preprocessing before using an image for inference as you did while training your model.
Every model expects to get the same input as it was trained on.

To load the model simply use the path to it, tell it how incoming images should be reshaped (if needed), and call the
`predict` method on them.

```kotlin
fun main() {
val (train, test) = fashionMnist()

TensorFlowInferenceModel.load(File("model/my_model")).use {
it.reshape(28, 28, 1)
val prediction = it.predict(test.getX(0))
val actualLabel = test.getY(0)

println("Predicted label is: $prediction. This corresponds to class ${stringLabels[prediction]}.")
println("Actual label is: $actualLabel.")
}
}
```

```
Predicted label is: 9. This corresponds to class Ankle boots.
Actual label is: 9.0.
```

Congratulations! You have learned to create, train, and use your first neural network model!
36 changes: 0 additions & 36 deletions docs/loading_trained_model_for_inference.md

This file was deleted.

78 changes: 0 additions & 78 deletions docs/training_a_model.md

This file was deleted.

0 comments on commit a0eacbc

Please sign in to comment.