[手抄] Keras官方教程 - Sequential

2018-07-03 keras, learn

官方教程,记录以便查阅!

以下代码运行环境为 —— keras[2.2.4], tensorflow[1.11.0]

Model

In [1]:
# The Sequential model is a linear stack of layers
from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])
Using TensorFlow backend.
In [2]:
# You can also simply add layers via the .add() method:
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
In [3]:
# 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 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:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, input_dim=784))

Compile

In [4]:
# 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
# 2. A loss function
# 3. A list of metrics

# 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])

Train

In [9]:
# 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 263us/step - loss: 0.7102 - acc: 0.4970
Epoch 2/10
1000/1000 [==============================] - 0s 45us/step - loss: 0.6995 - acc: 0.5080
Epoch 3/10
1000/1000 [==============================] - 0s 31us/step - loss: 0.6926 - acc: 0.5320
Epoch 4/10
1000/1000 [==============================] - 0s 47us/step - loss: 0.6900 - acc: 0.5280
Epoch 5/10
1000/1000 [==============================] - 0s 46us/step - loss: 0.6833 - acc: 0.5580
Epoch 6/10
1000/1000 [==============================] - 0s 44us/step - loss: 0.6805 - acc: 0.5620
Epoch 7/10
1000/1000 [==============================] - 0s 37us/step - loss: 0.6760 - acc: 0.5750
Epoch 8/10
1000/1000 [==============================] - 0s 37us/step - loss: 0.6737 - acc: 0.5780
Epoch 9/10
1000/1000 [==============================] - 0s 37us/step - loss: 0.6710 - acc: 0.6020
Epoch 10/10
1000/1000 [==============================] - 0s 44us/step - loss: 0.6671 - acc: 0.6010
Out[9]:
<keras.callbacks.History at 0xb2846bc18>
In [7]:
# For a single-input model with 10 classes (categorical classification):

import keras

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 [==============================] - 0s 205us/step - loss: 2.3505 - acc: 0.0980
Epoch 2/10
1000/1000 [==============================] - 0s 62us/step - loss: 2.3144 - acc: 0.1130
Epoch 3/10
1000/1000 [==============================] - 0s 41us/step - loss: 2.3017 - acc: 0.1140
Epoch 4/10
1000/1000 [==============================] - 0s 78us/step - loss: 2.2939 - acc: 0.1290
Epoch 5/10
1000/1000 [==============================] - 0s 48us/step - loss: 2.2845 - acc: 0.1330
Epoch 6/10
1000/1000 [==============================] - 0s 40us/step - loss: 2.2776 - acc: 0.1480
Epoch 7/10
1000/1000 [==============================] - 0s 52us/step - loss: 2.2686 - acc: 0.1570
Epoch 8/10
1000/1000 [==============================] - 0s 41us/step - loss: 2.2617 - acc: 0.1610
Epoch 9/10
1000/1000 [==============================] - 0s 46us/step - loss: 2.2542 - acc: 0.1670
Epoch 10/10
1000/1000 [==============================] - 0s 38us/step - loss: 2.2432 - acc: 0.1760
Out[7]:
<keras.callbacks.History at 0xb2809e278>

Multilayer Perceptron (MLP) for multi-class softmax classification

In [10]:
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 310us/step - loss: 2.3859 - acc: 0.0960
Epoch 2/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.3791 - acc: 0.0980
Epoch 3/20
1000/1000 [==============================] - 0s 21us/step - loss: 2.3372 - acc: 0.1060
Epoch 4/20
1000/1000 [==============================] - 0s 25us/step - loss: 2.3509 - acc: 0.0990
Epoch 5/20
1000/1000 [==============================] - 0s 28us/step - loss: 2.3350 - acc: 0.0910
Epoch 6/20
1000/1000 [==============================] - 0s 25us/step - loss: 2.3248 - acc: 0.1130
Epoch 7/20
1000/1000 [==============================] - 0s 27us/step - loss: 2.3241 - acc: 0.1130
Epoch 8/20
1000/1000 [==============================] - 0s 18us/step - loss: 2.3147 - acc: 0.1050
Epoch 9/20
1000/1000 [==============================] - 0s 25us/step - loss: 2.3142 - acc: 0.0980
Epoch 10/20
1000/1000 [==============================] - 0s 23us/step - loss: 2.3076 - acc: 0.1280
Epoch 11/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.3110 - acc: 0.1020
Epoch 12/20
1000/1000 [==============================] - 0s 21us/step - loss: 2.3159 - acc: 0.1030
Epoch 13/20
1000/1000 [==============================] - 0s 21us/step - loss: 2.3055 - acc: 0.1160
Epoch 14/20
1000/1000 [==============================] - 0s 17us/step - loss: 2.2941 - acc: 0.1140
Epoch 15/20
1000/1000 [==============================] - 0s 22us/step - loss: 2.3014 - acc: 0.1120
Epoch 16/20
1000/1000 [==============================] - 0s 21us/step - loss: 2.3069 - acc: 0.1100
Epoch 17/20
1000/1000 [==============================] - 0s 19us/step - loss: 2.3074 - acc: 0.1230
Epoch 18/20
1000/1000 [==============================] - 0s 23us/step - loss: 2.3065 - acc: 0.0910
Epoch 19/20
1000/1000 [==============================] - 0s 37us/step - loss: 2.2966 - acc: 0.1110
Epoch 20/20
1000/1000 [==============================] - 0s 24us/step - loss: 2.2985 - acc: 0.1110
100/100 [==============================] - 0s 2ms/step

MLP for binary classification

In [12]:
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 397us/step - loss: 0.7619 - acc: 0.4920
Epoch 2/20
1000/1000 [==============================] - 0s 15us/step - loss: 0.7379 - acc: 0.4810
Epoch 3/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.7213 - acc: 0.5030
Epoch 4/20
1000/1000 [==============================] - 0s 22us/step - loss: 0.7156 - acc: 0.4890
Epoch 5/20
1000/1000 [==============================] - 0s 17us/step - loss: 0.7059 - acc: 0.5280
Epoch 6/20
1000/1000 [==============================] - 0s 24us/step - loss: 0.7011 - acc: 0.5140
Epoch 7/20
1000/1000 [==============================] - 0s 23us/step - loss: 0.6979 - acc: 0.4990
Epoch 8/20
1000/1000 [==============================] - 0s 21us/step - loss: 0.7132 - acc: 0.4610
Epoch 9/20
1000/1000 [==============================] - 0s 17us/step - loss: 0.6959 - acc: 0.5260
Epoch 10/20
1000/1000 [==============================] - 0s 23us/step - loss: 0.6925 - acc: 0.5300
Epoch 11/20
1000/1000 [==============================] - 0s 16us/step - loss: 0.6968 - acc: 0.5100
Epoch 12/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6926 - acc: 0.5230
Epoch 13/20
1000/1000 [==============================] - 0s 24us/step - loss: 0.6893 - acc: 0.5360
Epoch 14/20
1000/1000 [==============================] - 0s 23us/step - loss: 0.7002 - acc: 0.5070
Epoch 15/20
1000/1000 [==============================] - 0s 20us/step - loss: 0.6904 - acc: 0.5310
Epoch 16/20
1000/1000 [==============================] - 0s 27us/step - loss: 0.6869 - acc: 0.5580
Epoch 17/20
1000/1000 [==============================] - 0s 18us/step - loss: 0.6915 - acc: 0.5250
Epoch 18/20
1000/1000 [==============================] - 0s 25us/step - loss: 0.6973 - acc: 0.5360
Epoch 19/20
1000/1000 [==============================] - 0s 25us/step - loss: 0.6874 - acc: 0.5420
Epoch 20/20
1000/1000 [==============================] - 0s 35us/step - loss: 0.6903 - acc: 0.5330
100/100 [==============================] - 0s 1ms/step

VGG-like convnet

In [13]:
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD

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

model = Sequential()
# input: 100x100 images with 3 channels -> (100, 100, 3) tensors.
# this applies 32 convolution filters of size 3x3 each.
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(256, 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)

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)
Epoch 1/10
100/100 [==============================] - 5s 46ms/step - loss: 2.3254
Epoch 2/10
100/100 [==============================] - 4s 38ms/step - loss: 2.3368
Epoch 3/10
100/100 [==============================] - 3s 34ms/step - loss: 2.3037
Epoch 4/10
100/100 [==============================] - 3s 35ms/step - loss: 2.3062
Epoch 5/10
100/100 [==============================] - 3s 33ms/step - loss: 2.2929
Epoch 6/10
100/100 [==============================] - 3s 32ms/step - loss: 2.3080
Epoch 7/10
100/100 [==============================] - 3s 32ms/step - loss: 2.2876
Epoch 8/10
100/100 [==============================] - 3s 32ms/step - loss: 2.3008
Epoch 9/10
100/100 [==============================] - 3s 34ms/step - loss: 2.3439
Epoch 10/10
100/100 [==============================] - 3s 32ms/step - loss: 2.3075
20/20 [==============================] - 0s 18ms/step

Sequence classification with LSTM

In [14]:
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import LSTM

max_features = 1024

model = Sequential()
model.add(Embedding(max_features, output_dim=256))
model.add(LSTM(128))
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, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-abe28b1f86cb> in <module>
     16               metrics=['accuracy'])
     17 
---> 18 model.fit(x_train, y_train, batch_size=16, epochs=10)
     19 score = model.evaluate(x_test, y_test, batch_size=16)

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking input: expected embedding_1_input to have 2 dimensions, but got array with shape (100, 100, 100, 3)

Sequence classification with 1D convolutions

In [15]:
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.layers import Embedding
from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1D

seq_length = 64

model = Sequential()
model.add(Conv1D(64, 3, activation='relu', input_shape=(seq_length, 100)))
model.add(Conv1D(64, 3, activation='relu'))
model.add(MaxPooling1D(3))
model.add(Conv1D(128, 3, activation='relu'))
model.add(Conv1D(128, 3, activation='relu'))
model.add(GlobalAveragePooling1D())
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, batch_size=16, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=16)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-15-fc9f7a925290> in <module>
     20               metrics=['accuracy'])
     21 
---> 22 model.fit(x_train, y_train, batch_size=16, epochs=10)
     23 score = model.evaluate(x_test, y_test, batch_size=16)

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, **kwargs)
    950             sample_weight=sample_weight,
    951             class_weight=class_weight,
--> 952             batch_size=batch_size)
    953         # Prepare validation data.
    954         do_validation = False

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size)
    749             feed_input_shapes,
    750             check_batch_axis=False,  # Don't enforce the batch size.
--> 751             exception_prefix='input')
    752 
    753         if y is not None:

~/anaconda3/envs/tf_models/lib/python3.6/site-packages/keras/engine/training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
    126                         ': expected ' + names[i] + ' to have ' +
    127                         str(len(shape)) + ' dimensions, but got array '
--> 128                         'with shape ' + str(data_shape))
    129                 if not check_batch_axis:
    130                     data_shape = data_shape[1:]

ValueError: Error when checking input: expected conv1d_1_input to have 3 dimensions, but got array with shape (100, 100, 100, 3)

Stacked LSTM for sequence classification

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

data_dim = 16
timesteps = 8
num_classes = 10

# expected input data shape: (batch_size, timesteps, data_dim)
model = Sequential()
model.add(LSTM(32, return_sequences=True,
               input_shape=(timesteps, data_dim)))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32, return_sequences=True))  # returns a sequence of vectors of dimension 32
model.add(LSTM(32))  # return a single vector of dimension 32
model.add(Dense(10, activation='softmax'))

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

# Generate dummy training data
x_train = np.random.random((1000, timesteps, data_dim))
y_train = np.random.random((1000, num_classes))

# Generate dummy validation data
x_val = np.random.random((100, timesteps, data_dim))
y_val = np.random.random((100, num_classes))

model.fit(x_train, y_train,
          batch_size=64, epochs=5,
          validation_data=(x_val, y_val))
Train on 1000 samples, validate on 100 samples
Epoch 1/5
1000/1000 [==============================] - 2s 2ms/step - loss: 11.5941 - acc: 0.0800 - val_loss: 11.3713 - val_acc: 0.1500
Epoch 2/5
1000/1000 [==============================] - 0s 316us/step - loss: 11.5916 - acc: 0.1190 - val_loss: 11.3700 - val_acc: 0.1500
Epoch 3/5
1000/1000 [==============================] - 0s 319us/step - loss: 11.5916 - acc: 0.0910 - val_loss: 11.3693 - val_acc: 0.1300
Epoch 4/5
1000/1000 [==============================] - 0s 332us/step - loss: 11.5905 - acc: 0.0980 - val_loss: 11.3705 - val_acc: 0.0700
Epoch 5/5
1000/1000 [==============================] - 0s 310us/step - loss: 11.5903 - acc: 0.0990 - val_loss: 11.3727 - val_acc: 0.0700
Out[17]:
<keras.callbacks.History at 0xb31bdbba8>

Same stacked LSTM model, rendered "stateful"

In [18]:
# https://www.zydarchen.top/20180925/21_stateful_LSTM/

from keras.models import Sequential
from keras.layers import LSTM, Dense
import numpy as np

data_dim = 16
timesteps = 8
num_classes = 10
batch_size = 32

# Expected input batch shape: (batch_size, timesteps, data_dim)
# Note that we have to provide the full batch_input_shape since the network is stateful.
# the sample of index i in batch k is the follow-up for the sample i in batch k-1.
model = Sequential()
model.add(LSTM(32, return_sequences=True, stateful=True,
               batch_input_shape=(batch_size, timesteps, data_dim)))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(10, activation='softmax'))

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

# Generate dummy training data
x_train = np.random.random((batch_size * 10, timesteps, data_dim))
y_train = np.random.random((batch_size * 10, num_classes))

# Generate dummy validation data
x_val = np.random.random((batch_size * 3, timesteps, data_dim))
y_val = np.random.random((batch_size * 3, num_classes))

model.fit(x_train, y_train,
          batch_size=batch_size, epochs=5, shuffle=False,
          validation_data=(x_val, y_val))
Train on 320 samples, validate on 96 samples
Epoch 1/5
320/320 [==============================] - 2s 7ms/step - loss: 11.7019 - acc: 0.0844 - val_loss: 11.1811 - val_acc: 0.0833
Epoch 2/5
320/320 [==============================] - 0s 457us/step - loss: 11.6964 - acc: 0.0875 - val_loss: 11.1820 - val_acc: 0.0833
Epoch 3/5
320/320 [==============================] - 0s 483us/step - loss: 11.6954 - acc: 0.1000 - val_loss: 11.1824 - val_acc: 0.0833
Epoch 4/5
320/320 [==============================] - 0s 554us/step - loss: 11.6947 - acc: 0.1000 - val_loss: 11.1826 - val_acc: 0.0833
Epoch 5/5
320/320 [==============================] - 0s 479us/step - loss: 11.6940 - acc: 0.1062 - val_loss: 11.1829 - val_acc: 0.0938
Out[18]:
<keras.callbacks.History at 0xb3384a518>
In [ ]: