I had the opportunity to write Keras, so I translated it into Japanese.
Getting started with the Keras Sequential model
The Sequential model is made by stacking layers.
You can create a Sequential by giving an instance of the layer to the constructor of the Sequential. The layer given the Dense
, ʻActivation` below (By the way, Dense is a so-called Full connected Neural Network).
from Keras.models import Sequential
model = Sequential([
Dense(32, input_dim=784),
Activation('relu'),
Dense(10),
Activation('softmax'),
])
You can also add layers using the .add ()
method.
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
Specifying the input shape The model must know what comes in the number of dimensions of the input. Therefore, it is necessary to give the information of the input dimension to the first layer of the Sequential model (the other layers can automatically infer the number of dimensions). There are three ways to do this.
Here's what I put these into my code:
model = Sequential()
model.add(Dense(32, input_shape=(784,)))
model = Sequential()
model.add(Dense(32, batch_input_shape=(None, 784)))
# note that batch dimension is "None" here,(Although the batch size is None)
# so the model will be able to process batches of any size(Now you can learn any batch size)
model = Sequential()
model.add(Dense(32, input_dim=784))
And the following three codes have the same meaning.
model = Sequential()
model.add(LSTM(32, input_shape=(10, 64)))
model = Sequential()
model.add(LSTM(32, batch_input_shape=(None, 10, 64)))
model = Sequential()
model.add(LSTM(32, input_length=10, input_dim=64))
The Merge layer It is possible to integrate several instances of Sequential so that one output is output. The output Sequential can be layered on a new Sequential. The following is an example and a conceptual diagram of layers.
from keras.layers import Merge
left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))
right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))
merged = Merge([left_branch, right_branch], mode='concat')
final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(10, activation='softmax'))
Merge has several modes, and you can describe how to merge two tensors.
sum
(default): Add each element.concat
: Concatenate tensors. Specify the concatenated dimensions with the concat_axis
argument.mul
: Multiply each element.dot
: Inner product. Specify which dimension to take the inner product with the dot_axes
argument.cos
: Take cosine proximity to the 2D tensor.Arbitrary calculation formula can be described in mode argument using lambda expression etc.
merged = Merge([left_branch, right_branch], mode=lambda x, y: x - y)
If you want to build a complex model that cannot be represented by Sequential and Merge, you need to refer to the function API.
Compilation
It is necessary to set the learning process before training the model.
It is done by the compile
method. The compile
method takes the following three arguments.
argument. Specify an instance of ʻOptimizer
class such as rmsprop
and ʻadagrad`. See optimizers.metrics = ['accuracy']
in the classification task. Currently only ʻaccuracy` is implemented.# 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')
Training
In Keras, training is done with Numpy array inputs and labels.
The fit
function is often used in the learning phase.
# for a single-input model with 2 classes(binary):(Two-class classification)
model = Sequential()
model.add(Dense(1, input_dim=784, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
# generate dummy data(Create appropriate input data)
import numpy as np
data = np.random.random((1000, 784))
labels = np.random.randint(2, size=(1000, 1))
# train the model, interating on the data in batches(Learn with batch size 32)
# of 32 samples
model.fit(data, labels, nb_epoch=10, batch_size=32)
# for a multi-input model with 10 classes:(10 classification)
left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))
right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))
merged = Merge([left_branch, right_branch], mode='concat')
model = Sequential()
model.add(merged)
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
# generate dummy data(Creating appropriate input data)
import numpy as np
from keras.utils.np_utils import to_categorical
data_1 = np.random.random((1000, 784))
data_2 = np.random.random((1000, 784))
# these are integers between 0 and 9(Generate integer values from 0 to 9)
labels = np.random.randint(10, size=(1000, 1))
# we convert the labels to a binary matrix of size (1000, 10)(For use in multi-class classification tasks 1-Convert to hot vector)
# for use with categorical_crossentropy
labels = to_categorical(labels, 10)
# train the model
#note that we are passing a list of Numpy arrays as training data
# since the model has 2 inputs(Take two inputs as arguments)
model.fit([data_1, data_2], labels, nb_epoch=10, batch_size=32)
Examples There are some implementation examples. Check out the original Keras.
Recommended Posts