# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="g4_g5ckyOrle" colab_type="text"
# ###Imports

# + id="b-yt8dgYNysn" colab_type="code" outputId="c5a49f2b-e832-46a7-cf34-726cbaa69ae5" executionInfo={"status": "ok", "timestamp": 1591440942619, "user_tz": -180, "elapsed": 2064, "user": {"displayName": "\u01<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
import keras
import numpy as np
import matplotlib.pyplot as plt
import math
import random

# + id="rpg_5clqN_Mz" colab_type="code" colab={}
from tensorflow.keras.datasets.cifar10 import load_data

# + [markdown] id="_bVuGgZbOuKG" colab_type="text"
# ### Data Examination

# + id="euEAn3pBOGQE" colab_type="code" outputId="44aaa620-d463-4096-a9e3-b2934d85d7cd" executionInfo={"status": "ok", "timestamp": 1591440960125, "user_tz": -180, "elapsed": 19536, "user": {"displayName": "\u01<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 51}
# CIFAR has 10 classes like horses, boats, cats ...
dataset_size = 10000
(inps,labs), (tinps,tlabs) = load_data()
inps = inps[:dataset_size]
labs = labs[:dataset_size]

# + id="VsHV2QJnRy2H" colab_type="code" colab={}
labs = keras.utils.to_categorical(labs,10)
tlabs = keras.utils.to_categorical(tlabs,10)

# + id="YoErbMvYOSeH" colab_type="code" outputId="cf50f316-dedf-4069-f627-aca494aafcb4" executionInfo={"status": "ok", "timestamp": 1591440960127, "user_tz": -180, "elapsed": 19523, "user": {"displayName": "\u015<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
print(np.shape(inps),np.shape(labs))

# + id="Z3X9KuDyOXQw" colab_type="code" outputId="fce1529c-56d8-4dd1-9d17-a97ff2ff5b2a" executionInfo={"status": "ok", "timestamp": 1591440960128, "user_tz": -180, "elapsed": 19511, "user": {"displayName": "\u015<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 283}
indx = int(random.random()*dataset_size)
plt.imshow(inps[indx])
print(labs[indx])

# + id="JsOhZ7Y3OhMh" colab_type="code" outputId="c9edf484-939e-4f1e-b0e5-48cc18c6ce4f" executionInfo={"status": "ok", "timestamp": 1591440960129, "user_tz": -180, "elapsed": 19496, "user": {"displayName": "\u0<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 34}
print(np.amax(inps[0]),np.amin(inps[0]))

# + [markdown] id="zWN8wHsPOxy8" colab_type="text"
# ### Network

# + id="lvAD7dv3Opx8" colab_type="code" colab={}
# We use keras sequential API here
# It provides easy development, but we cannot use/manipulate the latent variables manually, the outputs between layers.
# To do that, either use keras functional API or pytorch

from keras.layers import Conv2D, BatchNormalization, Dropout, Dense, Flatten, MaxPool2D
from keras.models import Sequential

inzNet = Sequential()

inzNet.add(BatchNormalization())
inzNet.add(Conv2D(64,(5,5),activation = 'tanh', padding = 'same'))
inzNet.add(MaxPool2D())
inzNet.add(Dropout(0.2))

inzNet.add(BatchNormalization())
inzNet.add(Conv2D(128,(5,5),activation = 'tanh', padding = 'same'))
inzNet.add(MaxPool2D())
inzNet.add(Dropout(0.2))

inzNet.add(BatchNormalization())
inzNet.add(Conv2D(256,(5,5),activation = 'tanh', padding = 'same'))
inzNet.add(MaxPool2D())
inzNet.add(Dropout(0.2))

inzNet.add(Flatten())
inzNet.add(Dense(10,activation = 'softmax'))

# + id="jjdg17PNPi8I" colab_type="code" colab={}
# just like functional API, optimizer and compile
from keras.optimizers import Adam
opt = Adam(lr = 0.001)
inzNet.compile(optimizer = opt,
               loss = 'categorical_crossentropy',
               metrics = ['accuracy'])

# + id="-a1juTaDRKEv" colab_type="code" outputId="e76a2d80-5d00-444a-cd19-3506ac1fd5d6" executionInfo={"status": "ok", "timestamp": 1591441109396, "user_tz": -180, "elapsed": 30011, "user": {"displayName": "\u015<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 393}
# Remember, if training loss goes down, it only means we are FITTING to the training set
# It does not tell if we are fitting correctly, if we are underfitting or if we are overfitting
# for that, check val loss

# When you run this several times, it might take validation sets differently (check this from internet)
inzNet.fit(inps,labs,batch_size=100, epochs = 10 , validation_split = 0.1)

# + id="V998dnPwRi7e" colab_type="code" outputId="c9273997-55d4-49a2-801c-17db6b741634" executionInfo={"status": "ok", "timestamp": 1591441161922, "user_tz": -180, "elapsed": 2935, "user": {"displayName": "\u015<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 51}
inzNet.evaluate(tinps,tlabs)

# + [markdown] id="lNm-njYJTArG" colab_type="text"
#

# + id="a9bJTfddSPLz" colab_type="code" outputId="eefee04c-b343-4a44-9115-777133ecf252" executionInfo={"status": "ok", "timestamp": 1591441408989, "user_tz": -180, "elapsed": 2284, "user": {"displayName": "\u<NAME>\u015ftan", "photoUrl": "", "userId": "02958926420055160792"}} colab={"base_uri": "https://localhost:8080/", "height": 300}
#checking some predictions of our model
randind = int(random.random()*1000)
plt.imshow(tinps[randind])
print(np.argmax(inzNet.predict(tinps[[randind]])))
print(np.argmax(tlabs[randind]))

# + id="dIisMO0LSqve" colab_type="code" colab={}


# + id="XroDJTPCV2Mk" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered