> For the complete documentation index, see [llms.txt](https://stephanosterburg.gitbook.io/scrapbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stephanosterburg.gitbook.io/scrapbook/coding/intro-to-tensorflow-for-ai-ml-and-dl/notes.md).

# Notes

Earlier when you trained for extra epochs you had an issue where your loss might change. It might have taken a bit of time for you to wait for the training to do that, and you might have thought 'wouldn't it be nice if I could stop the training when I reach a desired value?' -- i.e. 95% accuracy might be enough for you, and if you reach that after 3 epochs, why sit around waiting for it to finish a lot more epochs....So how would you fix that? Like any other program...you have callbacks! Let's see them in action...

```python
import tensorflow as tf
print(tf.__version__)

class myCallback(tf.keras.callbacks.Callback):
  def on_epoch_end(self, epoch, logs={}):
    if(logs.get('loss')<0.4):
      print("\nReached 60% accuracy so cancelling training!")
      self.model.stop_training = True

callbacks = myCallback()

mnist = tf.keras.datasets.fashion_mnist
(train_images, training_labels), (test_images, test_labels) = mnist.load_data()

train_images=train_images/255.0
test_images=test_images/255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(512, activation=tf.nn.relu),
  tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy')
model.fit(train_images, training_labels, epochs=5, callbacks=[callbacks])

```

```
1.13.1
Epoch 1/5
60000/60000 [==============================] - 19s 316us/sample - loss: 0.4767
Epoch 2/5
59872/60000 [============================>.] - ETA: 0s - loss: 0.3590
Reached 60% accuracy so cancelling training!
60000/60000 [==============================] - 17s 279us/sample - loss: 0.3588
<tensorflow.python.keras.callbacks.History at 0x7faf5dc59110>
```

### convolutional 2d layer

{% embed url="<https://bit.ly/2UGa7uH>" %}

{% embed url="<https://lodev.org/cgtutor/filtering.html>" %}

{% embed url="<https://gombru.github.io/2018/05/23/cross_entropy_loss/>" %}

{% embed url="<http://www.cs.toronto.edu/~tijmen/csc321/slides/lecture_slides_lec6.pdf>" %}

\----

<https://stackoverflow.com/questions/54854797/valueerror-validation-data-should-be-a-tuple-val-x-val-y-val-sample-weigh/56490365#56490365>

```
import keras

class Generator(keras.utils.Sequence):
    def __init__(self, data_file):
        self.data_file = data_file
        self.length = -1

    def __iter__(self):
        while True:
            with open(self.data_file, 'r') as f:
                reader = csv.reader(f)
                for row in reader:
                    yield row[0], row[1]

    def __len__(self):
        if self.length ==  -1:
            n_rows = 0
            with open(self.data_file, 'r') as f:
                reader = csv.reader(f)
                for row in reader:
                    n_rows += 1
            self.length = n_rows
        return self.length
```
