Question: I'm interested in creating a callback while fitting my keras model. More in detail I'd like to receive a message from a bot telegram with val\_acc each time an epoch is over. I know you can add a callback\_list as a parameter in `classifier.fit()` but many callbacks are prebuilt by keras and I don't know how to add a custom one.

Thank you!

Answer: 
Here is an example of how I would add validation accuracy to a callback:

```
class AccuracyHistory(keras.callbacks.Callback):
    def on_train_begin(self, logs={}):
        self.acc = []

    def on_epoch_end(self, batch, logs={}):
        self.acc.append(logs.get('val_acc'))

history = AccuracyHistory()

model.fit(x, y,
          ...
          callbacks=[history])

```
You can perform any action while the model is training. for this purpose keras has provided some methods as following:

```
on_train_begin, on_train_end, on_epoch_begin, on_epoch_end, on_test_begin,
on_test_end, on_predict_begin, on_predict_end, on_train_batch_begin, on_train_batch_end,
on_test_batch_begin, on_test_batch_end, on_predict_batch_begin,on_predict_batch_end

```

**Example:**

If you want to make predictions on the test data at the end of every epoch using the model which is being trained you can do it using the following code

```
class CustomCallback(keras.callbacks.Callback):
    def __init__(self, model, x_test, y_test):
        self.model = model
        self.x_test = x_test
        self.y_test = y_test

    def on_epoch_end(self, epoch, logs={}):
        y_pred = self.model.predict(self.x_test, self.y_test)
        print('y predicted: ', y_pred)

```

You need mention the callback during the `model.fit`

```
model.sequence()
# your model architecture
model.fit(x_train, y_train, epochs=10, 
          callbacks=[CustomCallback(model, x_test, y_test)])

```