tf.keras Getting Started 1 - Using sequential model to create a model VGGlike

Build a simple model

sequential model

sequential model is a linear model of the stacked layers. You can either initialize method by using the List of adding layers, can also .add adding layers method.

To establish a simple model, here to a fully connected multilayer perceptron layers Example:

import tensorflow as tf
from tensorflow import keras
from keras import layers
model = Sequential([
    layers.Dense(32, activation='relu'),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax'),
])

OR

import tensorflow as tf
from tensorflow import keras
from keras import layers

#instance a Sequential model
model = tf.keras.Sequential()
# Adds a densely-connected layer with 64 units to the model:
model.add(layers.Dense(64, activation='relu'))
# Add another:
model.add(layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(layers.Dense(10, activation='softmax'))

 

Specifying the input shape

Model needs to know that he should enter the desired shape. For this reason, sequential model of the first layer (first layer only, because the back layer may be automatically determined shape) needs to receive information about the shape of its input. Specific methods are as follows:

  • Adding a first layer parameter input_shape = (64,), the input_shape is a tuple, if not fill (or = None) may represent any positive integer excluding the batch dimension in input_shape.
  • In 2D layers, such as Dense parameters can be input_dim support the input shape. In some 3D temporal layers through the parameters input_dim and input_length to support.
  • If you need to enter specify a fixed batch size, you can pass parameters batch_size layer. If you want to pass batch_size = 32, and input_shape = (6,8) pass layers, each layer is a desired input (32,8,6)
  • More relevant parameters on the connection layer may view the whole tf.keras.layers.Dense , i.e. including kernel_regularizer L1L2 regularization, etc. Activation activation function.

For example, the following example is the same effect in two forms:

import tensorflow as tf
from tensorflow import keras
from keras import layers

model = Sequential()
model.add(layers.Dense(32,input_shape=(784,)))

# the same as:
# model = Sequential()
# model.add(layers.Dense(32,input_dim=784))

 

Compilation

Before training model, we need to configure the learning process, which is by compile method (a method tf.keras.Model class) completed. Parameters are as follows:

compile(
    optimizer,
    loss=None,
    metrics=None,
    loss_weights=None,
    sample_weight_mode=None,
    weighted_metrics=None,
    target_tensors=None,
    distribute=None,
    **kwargs
)

One of the most important of the three parameters:

An optimizer: He may be a conventional optimizer (such rmsprop , adagrad ) a string identifier , and may be Optimizer class is instantiated.

A loss function: This model is trying to minimize the objective, he can be a function of the loss of the existing string identifier (such categorical_crossentropy ,  MSE ), or may be a custom objective function. See: LOSSES

A list of metrics: for any classification problem, you need to set metrics = [ 'accuracy']. To specify different output multiple different metrics output model, you can pass the dictionary, for example, metrics = { 'output_a': 'accuracy', 'output_b': [ 'accuracy', 'mse']}. List (len = len (output)) You can also pass index list, such as metrics = [[ 'accuracy'], [ 'accuracy', 'mse']] or metrics = [ 'accuracy', [ 'precision)' , 'mse']]. measurable metrics may be conventional string identifier or custom metric function.

A simple example is as follows:

# 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 tensorflow as tf
from tensorflow,keras import 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 Numpy model training input data and tags the entire column for the training model, usually fit function .

 

fit(
    x=None,
    y=None,
    batch_size=None,
    epochs=1,
    verbose=1,
    callbacks=None,
    validation_split=0.0,
    validation_data=None,
    shuffle=True,
    class_weight=None,
    sample_weight=None,
    initial_epoch=0,
    steps_per_epoch=None,
    validation_steps=None,
    validation_freq=1,
    max_queue_size=10,
    workers=1,
    use_multiprocessing=False,
    **kwargs
)

 

Wherein the input parameters

  x: the input data, he can be a numpy array may be tensorflow of Tensor (or a list of tensor), tf.data may be a class, and the like can also be a generator.

  y: target data, x may be the same or numpy tensor, and he should have the consistency of x.

  batch_size: integer or None. The number of samples representative of a gradient descent. If you did not write, the default is 32.

  epoch: integer.

  callback: a list keras.callbacks.Callback instantiated. see:tf.keras.callbacks

  validation_split: floating point number between 0 and 1.

return:

A history object, his history.history property is a continuous record of the loss of value of training, metrics and measurement and verification value losses value losses.

Raises:

RuntimeError: If the model was never compiled.

ValueError: In case of mismatch between the provided input data and what th

 

Finally, we write about Faster RCNN in front Backbone is an example of VGG:

Import numpy AS NP
 Import tensorflow TF AS
 from tensorflow Import keras
 from tensorflow.keras Import Layers 

# generates false data 
# Note: two rear brackets 1.np.random.random. 
# Learn how to use a random 3-channel image 100 tf * 100 pixels, 100 
x_train np.random.random = ((100,100,100,3 )) 
y_train = keras.utils.to_categorical (np.random.randint (10, size = ( 100,1)), num_classes = 0) 
x_test = np.random.random ((20,100,100,3 )) 
android.permission.FACTOR. = keras.utils.to_categorical (np.random.randint (10, size = (20,1)), num_classes = 10 ) 

#Establish a sequential model. Faster RCNN use of VGG network. 
= Model keras.Sequential ()
 # INPUT:. Images with 100x100 channels. 3 -> (100, 100,. 3) tensors 
# . 64 Applies the this size 3x3 Convolution Filters each of 
# of the first layer to enter input_shape input, but because it is in constructing the model, it is not necessary to enter input, i.e. without the feed placeholder data. 
# Conv2D parameters are: CHANNEL_OUT, kernel_size, a stride of =. 1, Activation, padding = 'Valid', DATA_FORMAT = 'channels_last' (NHWC) 
# MaxPool2D parameters are: pool_size, strides = None, padding = 'valid', data_format = none 
# wherein strides = none i.e., Strides = pool_size 
model.add (layers.Conv2D (64, (3,3), Activation = ' RELU ' , padding = ' Same ',input_shape=(100,100,3),name='conv1_1'))
model.add(layers.Conv2D(64,(3,3),activation='relu',padding='same',name='conv1_2'))
model.add(layers.MaxPool2D(pool_size=(2,2),name='pool1'))
# -----------------------------------
model.add(layers.Conv2D(128,(3,3),activation='relu',padding='same',name='conv2_1'))
model.add(layers.Conv2D(128,(3,3),activation='relu',padding='same',name='conv2_2'))
model.add(layers.MaxPool2D(pool_size=(2,2),name='pool2'))
# -----------------------------------
model.add(layers.Conv2D(256,(3,3),activation='relu',padding='same',name='conv3_1'))
model.add(layers.Conv2D(256,(3,3),activation='relu',padding='same',name='conv3_2'))
model.add(layers.Conv2D(256,(3,3),activation='relu',padding='same',name='conv3_3'))
model.add(layers.MaxPool2D(pool_size=(2,2),name='pool3'))
# -----------------------------------
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv4_1'))
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv4_2'))
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv4_3'))
model.add(layers.MaxPool2D(pool_size=(2,2),name='pool4'))
# -----------------------------------
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv5_1'))
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv5_2'))
model.add(layers.Conv2D(512,(3,3),activation='relu',padding='same',name='conv5_3'))
# -----------------------------------
model.add(layers.Flatten(name='flatten6'))
model.add(layers.Dense(256, activation=''resumption,name='fc6'))
model.add(layers.Dropout(0.5,name='dropout6'))
model.add(layers.Dense(10, activation='softmax',name='fc7'))
# 设置SGD参数lr learing rate, decay 衰减, momentum 动量? nesterov
sgd = keras.optimizers.SGD(lr=0.001,decay=1e-6,momentum=0.9,nesterov=True)

# 编译模型
model.compile(loss='categorical_crossentropy', optimizer=sgd)


model.summary()

model.fit(x_train, y_train, batch_size=32, epochs=10)
score = model.evaluate(x_test, y_test, batch_size=32)  
View Code

 

 

 

 

 

 

 

 

Reference: https: //keras.io/getting-started/sequential-model-guide/

https://tensorflow.google.cn/beta/guide/keras/overview

 

Guess you like

Origin www.cnblogs.com/SsoZhNO-1/p/11269827.html