# Magic

Wherever you are, whatever strange function you are building up, `Magic Object` allows you to easily develop and interact with:skull:[Innocuous Book experiment](/innocuous-book/experiment-1/experiment.md).

### Let's start:soon:

Instantiate `Magic Object` before use.

```
mj = MagicObj()
```

:arrow\_down: Then, enjoy Innocuous Book Magic power:sparkles:

### Methods:notebook:

#### get\_path

```
get_path(filename, path)
```

Search the data by `filename` from `path` , if exist, return the data path.

| Arguments |                                                                 |
| --------- | --------------------------------------------------------------- |
| filename  | String (name of data with extension which you want to download) |
| path      | String (directory where data put)                               |

Example:&#x20;

```
data_path = mj.get_path(
                filename = "boston_housing.csv",
                path = "/home/project/workspace/dataset")
data = pd.read_csv(data_path)
```

##

#### log

```
log(**kwargs)
```

Log metrics then you can choose one as optimal standard.

{% hint style="warning" %}
&#x20;Do not use `log` within a `Trainable` class.
{% endhint %}

| Arguments  |                                          |
| ---------- | ---------------------------------------- |
| \*\*kwargs | Arguments to log which must have **key** |

Example:

```
mj.log(loss=running_loss, eval_loss=eval_loss)
```

##

so u can read the data with that save ur checkpoints no matter what framework api for every single framework log you metrics

##

#### torch\_get\_checkpoint\_path

```
torch_get_checkpoint_path(path, epoch)
```

Return the path of checkpoints during each epoch or whenever you want.

| Arguments |                                                                                                                                |
| --------- | ------------------------------------------------------------------------------------------------------------------------------ |
| path      | String (directory where you wnat to save checkpoints)                                                                          |
| epoch     | integer (number of epochs to save the model as checkpoint. An epoch is an iteration over the entire `x` and `y` data provided) |

Example:

```
mj.torch_get_checkpoint_path(path='/home/project/workspace/results', epoch=epoch)
```

##

#### torch\_save

```
torch_save(checkpoint, path, epoch)
```

Save checkpoint during each epoch.

| Arguments  |                                                                                                                             |
| ---------- | --------------------------------------------------------------------------------------------------------------------------- |
| checkpoint | Dictionary or OrderDictionary (model, weights or anything to save as checkpoint)                                            |
| path       | String (directory where you wnat to save checkpoints)                                                                       |
| epoch      | Integer (number of epochs to save the model as checkpoint. `epoch` would iterate over the entire `x` and `y` data provided) |

Example:

```
mj.torch_save(
    checkpoint = model.state_dict(),
    path = '/home/project/workspace/results',
    epoch = epoch)
```

##

#### tensor\_save

```
tensor_save(checkpoint, path)
```

Save checkpoint whenever you want.

| Arguments  |                                                       |
| ---------- | ----------------------------------------------------- |
| checkpoint | Sequential object (model to save as checkpoint)       |
| path       | String (directory where you wnat to save checkpoints) |

Example:

```
mj.tensor_save(checkpoint=model, path='/home/project/workspace/results')
```

##

#### callback

```
callback(metrics, path, frequency, on, filename=None)
```

Save checkpoint with the opportunity by `on` during each epoch and s

| Arguments |                                                                                                                                                                                                                                                            |
| --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| metrics   | Metrics in dictionary to be evaluated by the model during training and testing. Each can be a string (name of a built-in function), function or a [`tf.keras.metrics.Metric`](https://www.tensorflow.org/api_docs/python/tf/keras/metrics/Metric) instance |
| path      | String (directory where you wnat to save checkpoints)                                                                                                                                                                                                      |
| frequency | Integer or List (if an integer `n`, checkpoints are saved every `n` times of each hook. if a list, it specifies the checkpoint frequencies for each hook individually.)                                                                                    |
| on        | Integer or List (when to trigger checkpoint creations. must be one of the Keras event hooks (less the `on_`), e.g. "train\_start", or "predict\_end". defaults to "epoch\_end".)                                                                           |
| filename  | String (name of checkpoint you want to save)                                                                                                                                                                                                               |

Example:

```
model.fit(
	X_train, Y_train,
	initial_epoch=1, epochs=epochs, batch_size=16, verbose=0,
	callbacks=[m.callback(
		metrics={"accuracy":"accuracy"},
		filename="modelcheckpoint01",
		path="/home/pj/ray_results",
		frequency=1,
		on="epoch_end")]
		)
```


---

# Agent Instructions: 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:

```
GET https://innocuous-book.gitbook.io/innocuous-book/experiment-1/experiment/magic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
