线性堆叠的图层。
继承自:Model
用法:
tf.keras.Sequential
(
layers=None,
name=None
)
参数说明:
- layers - 要添加到model的层列表。
首先创建一个模型结构:
model = keras.Sequential()
建立一个最终输出维度为10的分类结果的全连接神经网络。使用.add()函数进行各个层的堆叠:
# Adds a densely-connected layer with 64 units to the model:
model.add(keras.layers.Dense(64, activation='relu'))
# Add another:
model.add(keras.layers.Dense(64, activation='relu'))
# Add a softmax layer with 10 output units:
model.add(keras.layers.Dense(10, activation='softmax'))
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense
# 第一层可以接收一个“ input_shape”参数:
model = Sequential()
model.add(Dense(32, input_shape=(500,)))
# 然后,我们进行自动shape推断:
model.add(Dense(32))
import tensorflow as tf
x = tf.random.normal([2, 3])
model = tf.keras.Sequential([
tf.keras.layers.Dense(2, activation = 'relu'),
tf.keras.layers.Dense(2, activation = 'relu'),
tf.keras.layers.Dense(2)
])
model.build(input_shape = [None, 3])
model.summary() # 相当于一个print
# Model: "sequential_3"
# _________________________________________________________________
# Layer (type) Output Shape Param #
# =================================================================
# dense_5 (Dense) multiple 8 ## 注:(keras为3*2) + (bias为2)
# _________________________________________________________________
# dense_6 (Dense) multiple 6
# _________________________________________________________________
# dense_7 (Dense) multiple 6
# =================================================================
# Total params: 20
# Trainable params: 20
# Non-trainable params: 0
# _________________________________________________________________
for p in model.trainable_variables:
print(p.name, p.shape)
# dense_5/kernel:0 (3, 2)
# dense_5/bias:0 (2,)
# dense_6/kernel:0 (2, 2)
# dense_6/bias:0 (2,)
# dense_7/kernel:0 (2, 2)
# dense_7/bias:0 (2,)