@frozen
public struct Dense<Scalar> : Layer where Scalar : TensorFlowFloatingPoint

A densely-connected neural network layer.

Dense implements the operation activation(matmul(input, weight) + bias), where weight is a weight matrix, bias is a bias vector, and activation is an element-wise activation function.

This layer also supports 3-D weight tensors with 2-D bias matrices. In this case the first dimension of both is treated as the batch size that is aligned with the first dimension of input and the batch variant of the matmul(_:_:) operation is used, thus using a different weight and bias for each element in input batch.

  • The weight matrix.

    Declaration

    public var weight: Tensor<Scalar>
  • The bias vector.

    Declaration

    public var bias: Tensor<Scalar>
  • The element-wise activation function.

    Declaration

    @noDerivative
    public let activation: Activation
  • The element-wise activation function type.

    Declaration

    public typealias Activation = (Tensor<Scalar>) -> Tensor<Scalar>
  • Declaration

    public init(
        weight: Tensor<Scalar>,
        bias: Tensor<Scalar>,
        activation: @escaping Activation
    )
  • Returns the output obtained from applying the layer to the given input.

    Declaration

    @differentiable
    public func callAsFunction(_ input: Tensor<Scalar>) -> Tensor<Scalar>

    Parameters

    input

    The input to the layer.

    Return Value

    The output.

  • Creates a Dense layer with the specified input size, output size, and element-wise activation function. The weight matrix is created with shape [inputSize, outputSize] and the bias vector is created with shape [outputSize].

    Declaration

    init(
        inputSize: Int,
        outputSize: Int,
        activation: @escaping Activation = identity,
        weightInitializer: ParameterInitializer<Scalar> = glorotUniform(),
        biasInitializer: ParameterInitializer<Scalar> = zeros()
    )

    Parameters

    inputSize

    The dimensionality of the input space.

    outputSize

    The dimensionality of the output space.

    activation

    The activation function to use. The default value is identity(_:).

    weightInitializer

    Initializer to use for weight.

    biasInitializer

    Initializer to use for bias.