当前位置:   article > 正文

keras: Build Model_model.build

model.build


keras.layers

inputs = keras.layers.Input(shape=(784,))		# 同 keras.Input()

x = keras.layers.Dense(64)(x)

x = keras.layers.Flatten()(x)

x = keras.layers.average([y1, y2, y3])

x = keras.layers.concatenate([title_features, body_features, tags_input])

x = keras.layers.Embedding(input_dim=10, output_dim=32)(title_input)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • Convolutional layers: Conv1D, Conv2D, Conv3D, Conv2DTranspose
  • Pooling layers: MaxPooling1D, MaxPooling2D, MaxPooling3D, AveragePooling1D, GlobalAveragePooling2D
  • RNN layers: GRU, LSTM, ConvLSTM2D
  • BatchNormalization, Dropout, Embedding

Activation

activation="relu"
activation="softmax"
  • 1
  • 2

utils

keras.utils.to_categorical(y_train, 10)
  • 1

Build Model

Sequential & Model

keras.Sequenctial只适合简单的情况,复杂的情况需要keras.Model。下面是复杂的情况:

  • 您的模型有多个输入或多个输出
    ticket的例子
  • 任何层都有多个输入或多个输出
    ResNet的例子
  • 您需要进行层共享
  • 您想要非线性拓扑(例如,一个残余连接,一个多分支模型)

Code

inputs = keras.layers.Input(shape=(784,))
x = keras.layers.Dense(64, activation="relu")(inputs)
outputs = keras.layers.Dense(10)(x)

model = keras.Model(inputs=inputs, outputs=outputs)
  • 1
  • 2
  • 3
  • 4
  • 5

每层详解

  • 指定shape
inputs = keras.layers.Input(shape=(784,))
  • 1

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.

  • 可变尺寸
body_input = keras.layers.Input(shape=(None,))

# 输入200x200 RGB图像将具有形状(200,200,3),但输入任何大小的RGB图像将具有形状(None, None, 3)。
inputs = keras.Input(shape=(None, None, 3))
  • 1
  • 2
  • 3
  • 4

查看详情

print(inputs.shape)
# TensorShape([None, 784]),表示784 dimensional vector
# 这是inputs 的输出的形状,而不是输入

print(inputs.dtype)
# tf.float32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
model.summary()

'''
Model: "mnist_model"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 784)]             0         
_________________________________________________________________
dense (Dense)                (None, 64)                50240     
_________________________________________________________________
dense_1 (Dense)              (None, 64)                4160      
_________________________________________________________________
dense_2 (Dense)              (None, 10)                650       
=================================================================
Total params: 55,050
Trainable params: 55,050
Non-trainable params: 0
_________________________________________________________________
'''
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

第一个None表示的是batch_size(Here the batch size is None, which indicates our model can process batches of any size),input_1就是名字,[(None, 784)]就是输出的形状。

可视化

keras.utils.plot_model(model, "my_first_model.png")
  • 1

在这里插入图片描述

keras.utils.plot_model(model, "my_first_model_with_shape_info.png", show_shapes=True)
  • 1

在这里插入图片描述

Save & Load Model

This saved file includes the:

  • model architecture
  • model weight values (that were learned during training)
  • model training config, if any (as passed to compile)
  • optimizer and its state, if any (to restart training where you left off)
model.save("path_to_my_model")
  • 1
model = keras.models.load_model("path_to_my_model")
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/293114
推荐阅读
相关标签
  

闽ICP备14008679号