Commit 394d4921 authored by Jon Krohn's avatar Jon Krohn
Browse files

add deep net in keras

parent fdbd75a0
Loading
Loading
Loading
Loading
+112 −0
Changes for notebooks/deep_net_in_keras_LT.ipynb: 112 added lines, 0 removed lines.
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# Deep Neural Network in Keras

%% Cell type:markdown id: tags:

In this notebook, we build a Deep Neural Network while discussing the theory we've learnt thus far.

%% Cell type:markdown id: tags:

#### Set seed for reproducibility

%% Cell type:code id: tags:

``` python
import numpy as np
np.random.seed(42)
```

%% Cell type:markdown id: tags:

#### Load dependencies

%% Cell type:code id: tags:

``` python
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout # new!
from keras.optimizers import SGD
```

%% Cell type:markdown id: tags:

#### Load data

%% Cell type:code id: tags:

``` python
(X_train, y_train), (X_test, y_test) = mnist.load_data()
```

%% Cell type:markdown id: tags:

#### Preprocess data

%% Cell type:code id: tags:

``` python
X_train = X_train.reshape(60000, 784).astype('float32')
X_test = X_test.reshape(10000, 784).astype('float32')
```

%% Cell type:code id: tags:

``` python
X_train /= 255
X_test /= 255
```

%% Cell type:code id: tags:

``` python
n_classes = 10
y_train = keras.utils.to_categorical(y_train, n_classes)
y_test = keras.utils.to_categorical(y_test, n_classes)
```

%% Cell type:markdown id: tags:

#### Design neural network architecture

%% Cell type:code id: tags:

``` python
model = Sequential()
model.add(Dense(64, activation='sigmoid', input_shape=(784,)))
model.add(Dense(10, activation='softmax'))
```

%% Cell type:code id: tags:

``` python
model.summary()
```

%% Cell type:markdown id: tags:

#### Configure model

%% Cell type:code id: tags:

``` python
model.compile(loss='mean_squared_error', optimizer=SGD(lr=0.01), metrics=['accuracy'])
```

%% Cell type:markdown id: tags:

#### Train!

%% Cell type:code id: tags:

``` python
model.fit(X_train, y_train, batch_size=128, epochs=200, verbose=1, validation_data=(X_test, y_test))
```

%% Cell type:code id: tags:

``` python
```