In [1]:
import tensorflow as tf

In [2]:
import numpy as np

In [4]:
!pip install keras


Collecting keras
  Downloading Keras-2.0.9-py2.py3-none-any.whl (299kB)
Collecting scipy>=0.14 (from keras)
  Downloading scipy-1.0.0-cp35-none-win_amd64.whl (30.8MB)
Requirement already satisfied: numpy>=1.9.1 in c:\users\raghu\anaconda3\envs\tensorflow\lib\site-packages (from keras)
Collecting pyyaml (from keras)
  Downloading PyYAML-3.12-cp35-cp35m-win_amd64.whl (195kB)
Requirement already satisfied: six>=1.9.0 in c:\users\raghu\anaconda3\envs\tensorflow\lib\site-packages (from keras)
Installing collected packages: scipy, pyyaml, keras
Successfully installed keras-2.0.9 pyyaml-3.12 scipy-1.0.0

In [16]:
import keras

In [6]:
from keras.models import Sequential

model = Sequential()

The Sequential model is a linear stack of layers.

You can create a Sequential model by passing a list of layer instances to the constructor:


In [7]:
from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

You can also simply add layers via the .add() method:


In [8]:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))

Specifying the input shape

The model needs to know what input shape it should expect. For this reason, the first layer in a Sequential model (and only the first, because following layers can do automatic shape inference) needs to receive information about its input shape. There are several possible ways to do this:

Pass an input_shape argument to the first layer. This is a shape tuple (a tuple of integers or None entries, where None indicates that any positive integer may be expected). In input_shape, the batch dimension is not included. Some 2D layers, such as Dense, support the specification of their input shape via the argument input_dim, and some 3D temporal layers support the arguments input_dim and input_length. If you ever need to specify a fixed batch size for your inputs (this is useful for stateful recurrent networks), you can pass a batch_size argument to a layer. If you pass both batch_size=32 and input_shape=(6, 8) to a layer, it will then expect every batch of inputs to have the batch shape (32, 6, 8). As such, the following snippets are strictly equivalent:


In [9]:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, input_dim=784))

Compilation

Before training a model, you need to configure the learning process, which is done via the compile method. It receives three arguments:

  1. An optimizer. This could be the string identifier of an existing optimizer (such as rmsprop or adagrad), or an instance of the Optimizer class. See: optimizers.
  2. A loss function. This is the objective that the model will try to minimize. It can be the string identifier of an existing loss function (such as categorical_crossentropy or mse), or it can be an objective function. See: losses.
  3. A list of metrics. For any classification problem you will want to set this to metrics=['accuracy']. A metric could be the string identifier of an existing metric or a custom metric function.

In [10]:
# For a multi-class classification problem
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# For a binary classification problem
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# For a mean squared error regression problem
model.compile(optimizer='rmsprop',
              loss='mse')

# For custom metrics
import keras.backend as K

def mean_pred(y_true, y_pred):
    return K.mean(y_pred)

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy', mean_pred])

Training

Keras models are trained on Numpy arrays of input data and labels. For training a model, you will typically use the fit function. Read its documentation here.


In [12]:
# For a single-input model with 2 classes (binary classification):

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, epochs=10, batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s 488us/step - loss: 0.7112 - acc: 0.5020
Epoch 2/10
1000/1000 [==============================] - 0s 80us/step - loss: 0.6919 - acc: 0.5380
Epoch 3/10
1000/1000 [==============================] - 0s 84us/step - loss: 0.6867 - acc: 0.5430
Epoch 4/10
1000/1000 [==============================] - 0s 71us/step - loss: 0.6794 - acc: 0.5530
Epoch 5/10
1000/1000 [==============================] - 0s 70us/step - loss: 0.6732 - acc: 0.5690
Epoch 6/10
1000/1000 [==============================] - 0s 81us/step - loss: 0.6705 - acc: 0.5880
Epoch 7/10
1000/1000 [==============================] - 0s 85us/step - loss: 0.6652 - acc: 0.5780
Epoch 8/10
1000/1000 [==============================] - 0s 100us/step - loss: 0.6593 - acc: 0.5980
Epoch 9/10
1000/1000 [==============================] - 0s 93us/step - loss: 0.6549 - acc: 0.6000
Epoch 10/10
1000/1000 [==============================] - 0s 85us/step - loss: 0.6512 - acc: 0.6140
Out[12]:
<keras.callbacks.History at 0x1a7ab4552e8>

In [11]:
# For a single-input model with 10 classes (categorical classification):

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Generate dummy data
import numpy as np
data = np.random.random((1000, 100))
labels = np.random.randint(10, size=(1000, 1))

# Convert labels to categorical one-hot encoding
one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)

# Train the model, iterating on the data in batches of 32 samples
model.fit(data, one_hot_labels, epochs=10, batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 1s 768us/step - loss: 2.3443 - acc: 0.0850
Epoch 2/10
1000/1000 [==============================] - 0s 82us/step - loss: 2.3188 - acc: 0.1120
Epoch 3/10
1000/1000 [==============================] - 0s 70us/step - loss: 2.3071 - acc: 0.1290
Epoch 4/10
1000/1000 [==============================] - 0s 69us/step - loss: 2.2932 - acc: 0.1290
Epoch 5/10
1000/1000 [==============================] - 0s 64us/step - loss: 2.2866 - acc: 0.1350
Epoch 6/10
1000/1000 [==============================] - 0s 70us/step - loss: 2.2759 - acc: 0.1400
Epoch 7/10
1000/1000 [==============================] - 0s 70us/step - loss: 2.2711 - acc: 0.1340
Epoch 8/10
1000/1000 [==============================] - 0s 88us/step - loss: 2.2615 - acc: 0.1600
Epoch 9/10
1000/1000 [==============================] - 0s 76us/step - loss: 2.2542 - acc: 0.1680
Epoch 10/10
1000/1000 [==============================] - 0s 80us/step - loss: 2.2431 - acc: 0.1750
Out[11]:
<keras.callbacks.History at 0x1a7aa0d8d68>

Examples

Here are a few examples to get you started!

Multilayer Perceptron (MLP) for multi-class softmax classification:


In [14]:
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.optimizers import SGD

# Generate dummy data
import numpy as np
x_train = np.random.random((1000, 20))
y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10)
x_test = np.random.random((100, 20))
y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)

model = Sequential()
# Dense(64) is a fully-connected layer with 64 hidden units.
# in the first layer, you must specify the expected input data shape:
# here, 20-dimensional vectors.
model.add(Dense(64, activation='relu', input_dim=20))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)


Epoch 1/20
1000/1000 [==============================] - 0s 345us/step - loss: 2.3904 - acc: 0.1090
Epoch 2/20
1000/1000 [==============================] - 0s 36us/step - loss: 2.3497 - acc: 0.0990
Epoch 3/20
1000/1000 [==============================] - 0s 33us/step - loss: 2.3221 - acc: 0.0990
Epoch 4/20
1000/1000 [==============================] - 0s 42us/step - loss: 2.3251 - acc: 0.0890
Epoch 5/20
1000/1000 [==============================] - 0s 37us/step - loss: 2.3126 - acc: 0.0970
Epoch 6/20
1000/1000 [==============================] - 0s 26us/step - loss: 2.3123 - acc: 0.1180
Epoch 7/20
1000/1000 [==============================] - 0s 37us/step - loss: 2.3015 - acc: 0.1210
Epoch 8/20
1000/1000 [==============================] - 0s 41us/step - loss: 2.3053 - acc: 0.1070
Epoch 9/20
1000/1000 [==============================] - 0s 30us/step - loss: 2.3066 - acc: 0.1010
Epoch 10/20
1000/1000 [==============================] - 0s 35us/step - loss: 2.3095 - acc: 0.1170
Epoch 11/20
1000/1000 [==============================] - 0s 39us/step - loss: 2.3060 - acc: 0.1030
Epoch 12/20
1000/1000 [==============================] - 0s 38us/step - loss: 2.3000 - acc: 0.1120
Epoch 13/20
1000/1000 [==============================] - 0s 34us/step - loss: 2.3026 - acc: 0.1090
Epoch 14/20
1000/1000 [==============================] - 0s 28us/step - loss: 2.3001 - acc: 0.1070
Epoch 15/20
1000/1000 [==============================] - 0s 42us/step - loss: 2.3046 - acc: 0.1150
Epoch 16/20
1000/1000 [==============================] - 0s 41us/step - loss: 2.3105 - acc: 0.1070
Epoch 17/20
1000/1000 [==============================] - 0s 26us/step - loss: 2.3006 - acc: 0.1080
Epoch 18/20
1000/1000 [==============================] - 0s 36us/step - loss: 2.2919 - acc: 0.1150
Epoch 19/20
1000/1000 [==============================] - 0s 37us/step - loss: 2.2935 - acc: 0.1330
Epoch 20/20
1000/1000 [==============================] - 0s 34us/step - loss: 2.2957 - acc: 0.1170
100/100 [==============================] - 0s 371us/step

MLP for binary classification:


In [15]:
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout

# Generate dummy data
x_train = np.random.random((1000, 20))
y_train = np.random.randint(2, size=(1000, 1))
x_test = np.random.random((100, 20))
y_test = np.random.randint(2, size=(100, 1))

model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x_train, y_train,
          epochs=20,
          batch_size=128)
score = model.evaluate(x_test, y_test, batch_size=128)


Epoch 1/20
1000/1000 [==============================] - 0s 453us/step - loss: 0.7288 - acc: 0.4870
Epoch 2/20
1000/1000 [==============================] - 0s 26us/step - loss: 0.7118 - acc: 0.4990
Epoch 3/20
1000/1000 [==============================] - 0s 35us/step - loss: 0.7021 - acc: 0.4970
Epoch 4/20
1000/1000 [==============================] - 0s 33us/step - loss: 0.7006 - acc: 0.5300
Epoch 5/20
1000/1000 [==============================] - 0s 38us/step - loss: 0.7159 - acc: 0.4870
Epoch 6/20
1000/1000 [==============================] - 0s 32us/step - loss: 0.7054 - acc: 0.5020
Epoch 7/20
1000/1000 [==============================] - 0s 36us/step - loss: 0.7080 - acc: 0.5080
Epoch 8/20
1000/1000 [==============================] - 0s 33us/step - loss: 0.7024 - acc: 0.5120
Epoch 9/20
1000/1000 [==============================] - 0s 28us/step - loss: 0.6989 - acc: 0.5200
Epoch 10/20
1000/1000 [==============================] - 0s 39us/step - loss: 0.7015 - acc: 0.5030
Epoch 11/20
1000/1000 [==============================] - 0s 31us/step - loss: 0.6973 - acc: 0.5160
Epoch 12/20
1000/1000 [==============================] - 0s 37us/step - loss: 0.6905 - acc: 0.5250
Epoch 13/20
1000/1000 [==============================] - 0s 36us/step - loss: 0.6943 - acc: 0.5290
Epoch 14/20
1000/1000 [==============================] - 0s 40us/step - loss: 0.6951 - acc: 0.5010
Epoch 15/20
1000/1000 [==============================] - 0s 34us/step - loss: 0.6947 - acc: 0.5180
Epoch 16/20
1000/1000 [==============================] - 0s 34us/step - loss: 0.6911 - acc: 0.5310
Epoch 17/20
1000/1000 [==============================] - 0s 36us/step - loss: 0.6910 - acc: 0.5260
Epoch 18/20
1000/1000 [==============================] - 0s 31us/step - loss: 0.6908 - acc: 0.5300
Epoch 19/20
1000/1000 [==============================] - 0s 33us/step - loss: 0.6908 - acc: 0.5390
Epoch 20/20
1000/1000 [==============================] - 0s 33us/step - loss: 0.6936 - acc: 0.5280
100/100 [==============================] - 0s 651us/step

In [ ]:


In [ ]:


In [ ]: