> For the complete documentation index, see [llms.txt](https://leonardoaraujosantos.gitbook.io/artificial-inteligence/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://leonardoaraujosantos.gitbook.io/artificial-inteligence/machine_learning/deep_learning/model_solver.md).

# Model Solver

## Introduction

The mission of the model solver is to find the best set of parameters, that minimize the train/accuracy errors. On this chapter we will give a UML description with some piece of python/matlab code that allows you implement it yourself.

![](/files/-LvMRqh4WnQU_SbyrPJ5)

From the UML description we can infer some information about the Solver class:

1. It uses the training set, and has a reference to your model
2. Uses different type of optimizers(ex: SGD, ADAM, SGD with momentum)
3. Keep tracks of all the loss, accuracy during the training phase
4. Keep the set of parameters, that achieved best validation performance

## Usage example

![](/files/-LvMRqh6rUeF7kj0Tihr)

## Train operation

This is the method called when you actually want to start a model training, the methods Step, Check\_Accuracy are called inside the Train method:

1. Calculate number of iterations per epoch, based on number of epochs, train size, and batch size
2. Call step, for each iteration
3. Decay the learning rate
4. Calculate the validation accuracy
5. Cache the best parameters based on validation accuracy

## Step operation

Basically during the step operation the following operations are done:

1. Extract a batch from the training set.
2. Get the model loss and gradients
3. Perform a parameter update with one of the optimizers.

## Check Accuracy

This method basically is called at the end of each epoch. Basically it uses the current set of parameters, and predict the whole validation set. The objective is at the end get the accuracy. $$accuracy=mean(y\_{predicted}==y\_{validation})$$

## Model loss operation

We mentioned during the "Step" operation that we get the model loss and gradients. This operation is implemented by the "getLoss" method. Consider the following basic model.

![](/files/-LvMRqh9pxpOA9MYTZT9) Bellow we have the "getLoss" function for the previous simple model.

![](/files/-LvMRqhBTbKnh2W1qfFp)

Also bellow we have the "softmax*loss" function including "dout", $$\frac{\partial L}{\partial X*{scores}}$$

![](/files/-LvMRqhDV4M5EGQp8geM)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://leonardoaraujosantos.gitbook.io/artificial-inteligence/machine_learning/deep_learning/model_solver.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
