Keras basic use of (1) - to create, compile, training model

Reference Links: https://blog.csdn.net/yuezhilanyi/article/details/79201705

Keras is written in Python, advanced neural network API, use TensorFlow, Theano, etc. as the back end. Fast, easy to use, easy to verify its advantages.
The official document portal: http://keras.io/
Chinese Document Portal: http://keras.io/zh
Chinese third-party documentation: http://keras-cn.readthedocs.io

1. build a model

Method a: use the Sequential () construction model

Sequential is the best way to achieve a fully connected network.

. 1) Sequential model is a linear stack of a plurality of network layer , can be introduced from the model Sequential keras model library:

from keras.models import Sequential
import tensorflow as tf

# create a sequential model 
model = Sequential()
Using TensorFlow backend.

2) The number of layers via the network layer add()superimposed to form a network:

from keras.layers import Dense,Activation

model.add(Dense(units=64,input_dim=100))
model.add(Activation('relu'))
model.add(Dense(units=10))
model.add(Activation('softmax'))

You can also enter directly a complete list Sequential model building:

model = Sequential([
                  (Dense(units=64,input_dim=100)),
                  (Activation('relu')),
                  (Dense(units=10)),
                  (Activation('softmax'))
                  ])

The simplicity is that, in addition to the first layer shape data to specify the external input, the frame shape data of other layers will be automatically derived.

  • Can use input_shapethe input keyword to specify the shape of the first layer input_shapeis a tuple type of data (which may be an integer None filled, if the inserting None indicates that this position may be any positive integer) but it should be noted that, batch size of data which should not be included in the
  • Some 2D layer, a Densespecified dimension of the first input layer input_dimto the hidden shape specified input data, which is a data type Int. There are a number of time-domain 3D support layer parameters input_dimand the input_lengthinput to the specified shape.
model = Sequential()
model.add(Dense(32, input_shape=(784,)))



model = Sequential()
model.add(Dense(32, input_dim=784))

3) create a good model can be used model.summary()to view the final model of the structure

Method two: using the Model () construction model

One method of using Sequential () (translated Chinese document is: sequential model) to build the model used here Model () (ie: functional model) to build the model.

Description Chinese document: Keras user interface is a functional model defined multi-output model, acyclic or with a pathway model to model complex models shared layer and the like. In short, as long as your model is not similar VGG a road go black model or models you need more than one output, you should always choose functional model. Functional model is the most widely used class of models, the sequential model (the Sequential) it is only a special case.

First to a simple example:

from keras.layers import Input, Dense
from keras.models import Model

# This returns a tensor
inputs = Input(shape=(784,))

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# This creates a model that includes
# the Input layer and three Dense layers
model = Model(inputs=inputs, outputs=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels)  # starts training

Functional model provides an interface for us to use, you can use it conveniently call interface model has been trained, like VGG, Inception these powerful networks. But note that the call model, but also to call its weight data. Sequential model can be as fit as compile and after functional model is created, the same method.

See more: http://keras-cn.readthedocs.io/en/latest/getting_started/functional_API/#functional

2. Compile the created model

After building a network model, we need to configure the network learning process, or when you call the fit or evaluate throws an exception. You may be used compile(self, optimizer, loss, metrics=None, sample_weight_mode=None, weighted_metrics=None, target_tensors=None)to complete the configuration of
compile()the primary receiver first three parameters:

  • loss: String type, to specify the loss function, such as: categorical_crossentropy, binary_crossentropy
  • optimizer: String type, to specify optimization methods, such as: rmsprop, adam, sgd
  • metrics: List the type of model used to specify the measure indicators, such as: accuracy
model.compile(loss='categorical_crossentropy', 
                                optimizer='sgd', metrics=['accuracy'])

3. Training Model

Training model commonly used fit()functions:

fit(self, x, y, batch_size=32, epochs=10, verbose=1, 
                callbacks=None, validation_split=0.0,
                validation_data=None, shuffle=True,
                class_weight=None, sample_weight=None, 
                initial_epoch=0)
  • x: training data array. If the input frame is a local tensor (e.g. Tensorflow data tensors), x may be None (default).
  • y: target (tag) data array. If the input frame is a local tensor (e.g. Tensorflow data tensors), y may be None (default).
  • batch_size: Specifies the size of the batch, or a is an integer None. If not specified, the default is 32.
  • epochs: the number of iterations of all samples is specified training, is an integer.

Here is an example of a training model (from official documentation):

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



Author: caoqi95
link: https: //www.jianshu.com/p/6aa40d99fc9e
Source: Jane book
Jane book copyright reserved by the authors, are reproduced in any form, please contact the author to obtain authorization and indicate the source.

Guess you like

Origin blog.csdn.net/yimixgg/article/details/90694172