网页
综合
更新时间 2026-11-15 00:14:34
The base Layer class
自动蜘蛛
keras.io
正文约 2615 字
This is the class from which all layers inherit.
A layer is a callable object that takes as input one or more tensors and that outputs one or more tensors. It involves computation, defined in the call() method, and a state (weight variables). State can be created:
Layers are recursively composable: If you assign a Layer instance as an attribute of another Layer, the outer layer will start tracking the weights created by the inner layer. Nested layers should be instantiated in the __init__() method or build() method.
Users will just instantiate a layer and then treat it as a callable.
We recommend that descendants of Layer implement the following methods:
Here's a basic example: a layer with two variables, w and b, that returns y = w . x + b. It shows how to implement build() and call(). Variables set as attributes of a layer are tracked as weights of the layers (in layer.weights).
Besides trainable weights, updated via backpropagation during training, layers can also have non-trainable weights. These weights are meant to be updated manually during call(). Here's a example layer that computes the running sum of its inputs:
Guides and examples using Layer
List of all weight variables of the layer.
Unlike, layer.variables this excludes metric state and random seeds.
List of all trainable weight variables of the layer.
These are the weights that get updated by the optimizer during training.
List of all non-trainable weight variables of the layer.
These are the weights that should not be updated by the optimizer during training. Unlike, layer.non_trainable_variables this excludes metric state and random seeds.
Add a weight variable to the layer.
Settable boolean, whether this layer should be trainable or not.
Return the values of layer.weights as a list of NumPy arrays.
Sets the values of layer.weights from a list of NumPy arrays.
Returns the config of the object.
An object config is a Python dictionary (serializable) containing the information needed to re-instantiate it.
Can be called inside of the call() method to add a scalar loss.
List of scalar losses from add_loss, regularizers and sublayers.
Represents a backend-agnostic variable in Keras.
A Variable acts as a container for state. It holds a tensor value and can be updated. With the JAX backend, variables are used to implement "functionalization", the pattern of lifting stateful operations out of a piece of computation to turn it into a stateless function.
Initializing a Variable with a NumPy array:
Using a Keras initializer to create a Variable:
Updating the value of a Variable:
Marking a Variable as non-trainable:
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。