首页 > 解决方案 > keras中函数式API是什么意思?

问题描述

当我阅读 Keras 文档时,我发现了一个称为功能 API 的术语。

Keras 中的函数式 API 是什么意思?

谁能帮助您理解 Keras 中的基本和重要术语?

谢谢

标签: keras

解决方案


这是一种创建模型的方法。可以使用顺序模型(此处为教程):

例如:

from keras.models import Sequential
from keras.layers import Dense, Activation

model = Sequential([
    Dense(32, input_shape=(784,)),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

您可以使用输入功能调用它。第二种方法是功能性的(这里是教程)。您可以使用下一个层调用每一层,这使您在创建模型时具有更大的灵活性,例如:

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
output_1 = Dense(64, activation='relu')(inputs)
# you call layer with another layer to create model
output_2 = Dense(64, activation='relu')(output_1)
predictions = Dense(10, activation='softmax')(output_2)

# 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

您也可以子类Model化,类似于 Chainer 或 PyTorch 提供给用户的内容,但它在 Keras 中被大量使用。


推荐阅读