functional API
Setup
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
Introduction
The Keras functional API is a way to create models that is more flexible than the tf.keras.Sequential API. The functional API can handle models with non-linear topology, models with shared layers, and models with multiple inputs or outputs.
The main idea that a deep learning model is usually a directed acyclic graph (DAG) of layers. So the functional API is a way to build graphs of layers.
Consider the following model:
(input: 784-dimensional vectors)
↧
[Dense (64 units, relu activation)]
↧
[Dense (64 units, relu activation)]
↧
[Dense (10 units, softmax activation)]
↧
(output: logits of a probability distribution over 10 classes)
(1) Input Layer
This is a basic graph with three layers. To build this model using the functional API, start by creating an input node:
inputs = keras.Input(shape=(784,))
The shape of the data is set as a 784-dimensional vector. The batch size is always omitted since only the shape of each sample is specified.
If, for example, you have an image input with a shape of (32, 32, 3), you would use:
# Just for demonstration purposes.
img_inputs = keras.Input(shape=(32, 32, 3))
(2)Dense Layer
创建一个Dense层,并把img_inputs层作为输入:
dense = layers.Dense(64, activation='relu')
x = dense(inputs)
就像从 “inputs”层画一个箭头,指向这个我们建立的Dense层。把inputs传递到Dense Layer, 然后输出给 x。
(3)加几个新层
x = layers.Dense(64, activation='relu')(x)
outputs = layers.Dense(10)(x)
(4)建立一个Model
这时,可以建立一个model,通过指明层的inputs和outputs :
model = keras.Model(inputs=inputs, outputs=outputs, name='mnist_model')
然后,可以检查模型总结(summary):
model.summary()
Sequential API
Setup
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
Example
model = tf.keras.Sequential()
model.add(layers.Dense(32, input_shape=(500,), activation='relu'))
model.add(layers.Dense(1))
model.summary()
Other API
tf.keras
这是一个很好用的API系列,可以参考Tensorflow 官网:Tensorflow API
(1)Model
官网上说,有两种建立Model的方法
1- With the “functional API”, where you start from Input, you chain layer calls to specify the model’s forward pass, and finally you create your model from inputs and outputs:
import tensorflow as tf
inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation='relu')(inputs)
outputs = tf.keras.layers.Dense(5, activation='softmax')(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)
其实就是上面介绍的Functional Input 简洁版。
2- 第二种,类似于自己写一个subclass Model
in that case, you should define your layers in init and you should implement the model’s forward pass in call.(可参考官网)
(2)optimizers
tf.keras.optimizers
其中比较常见的Adam 算法:
tf.keras.optimizers.Adam(
learning_rate=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, amsgrad=False,
name='Adam', **kwargs
)
(3)layers
layers 里面有各种层的构建:Con2D,Dense,Flatten,MaxPool2D…
这里面介绍一个Lambda: tf.keras.layers.Lambda
tf.keras.layers.Lambda(
function, output_shape=None, mask=None, arguments=None, **kwargs
)
Example(举个栗子):
1-
# add a x -> x^2 layer
model.add(Lambda(lambda x: x ** 2))
2-
把上一层乘以scale然后输出:
new_layer = tf.keras.layers.Lambda(lambda x : x * scale) (previous_layer)
如有想要参考的,可以参考官网上面的内容,很全面完整。
参考:
Tensorflow官网:TensorFlow Core v2.1.0(API_docs)