Adam

public class Adam<Model: Differentiable>: Optimizer
where
  Model.TangentVector: VectorProtocol & PointwiseMultiplicative
    & ElementaryFunctions & KeyPathIterable,
  Model.TangentVector.VectorSpaceScalar == Float

Trình tối ưu hóa Adam.

Triển khai thuật toán tối ưu hóa Adam. Adam là một phương pháp giảm độ dốc ngẫu nhiên để tính toán tốc độ học thích ứng riêng lẻ cho các tham số khác nhau từ ước tính mô men bậc một và bậc hai của độ dốc.

Tham khảo: “Adam: Phương pháp tối ưu hóa ngẫu nhiên” (Kingma và Ba, 2014).

Ví dụ:

  • Đào tạo một tác nhân học tăng cường đơn giản:
...
// Instantiate an agent's policy - approximated by the neural network (`net`) after defining it 
in advance.
var net = Net(observationSize: Int(observationSize), hiddenSize: hiddenSize, actionCount: actionCount)
// Define the Adam optimizer for the network with a learning rate set to 0.01.
let optimizer = Adam(for: net, learningRate: 0.01)
...
// Begin training the agent (over a certain number of episodes).
while true {
...
    // Implementing the gradient descent with the Adam optimizer:
    // Define the gradients (use withLearningPhase to call a closure under a learning phase).
    let gradients = withLearningPhase(.training) {
        TensorFlow.gradient(at: net) { net -> Tensor<Float> in
            // Return a softmax (loss) function
            return loss = softmaxCrossEntropy(logits: net(input), probabilities: target)
        }
    }
    // Update the differentiable variables of the network (`net`) along the gradients with the Adam 
optimizer.
    optimizer.update(&net, along: gradients)
    ...
    }
}
  • Huấn luyện một mạng lưới đối thủ tổng quát (GAN):
...
// Instantiate the generator and the discriminator networks after defining them.
var generator = Generator()
var discriminator = Discriminator()
// Define the Adam optimizers for each network with a learning rate set to 2e-4 and beta1 - to 0.5.
let adamOptimizerG = Adam(for: generator, learningRate: 2e-4, beta1: 0.5)
let adamOptimizerD = Adam(for: discriminator, learningRate: 2e-4, beta1: 0.5)
...
Start the training loop over a certain number of epochs (`epochCount`).
for epoch in 1...epochCount {
    // Start the training phase.
    ...
    for batch in trainingShuffled.batched(batchSize) {
        // Implementing the gradient descent with the Adam optimizer:
        // 1) Update the generator.
        ...
        let 𝛁generator = TensorFlow.gradient(at: generator) { generator -> Tensor<Float> in
            ...
            return loss
            }
        // Update the differentiable variables of the generator along the gradients (`𝛁generator`) 
        // with the Adam optimizer.
        adamOptimizerG.update(&generator, along: 𝛁generator)

        // 2) Update the discriminator.
        ...
        let 𝛁discriminator = TensorFlow.gradient(at: discriminator) { discriminator -> Tensor<Float> in
            ...
            return loss
        }
        // Update the differentiable variables of the discriminator along the gradients (`𝛁discriminator`) 
        // with the Adam optimizer.
        adamOptimizerD.update(&discriminator, along: 𝛁discriminator)
        }
}       
  • Tuyên ngôn

    public typealias Model = Model
  • Tỷ lệ học tập.

    Tuyên ngôn

    public var learningRate: Float
  • Một hệ số được sử dụng để tính toán những khoảnh khắc đầu tiên của độ dốc.

    Tuyên ngôn

    public var beta1: Float
  • Một hệ số được sử dụng để tính toán mômen thứ hai của độ dốc.

    Tuyên ngôn

    public var beta2: Float
  • Một đại lượng vô hướng nhỏ được thêm vào mẫu số để cải thiện độ ổn định về số.

    Tuyên ngôn

    public var epsilon: Float
  • Tỷ lệ học tập suy giảm.

    Tuyên ngôn

    public var decay: Float
  • Bước hiện tại.

    Tuyên ngôn

    public var step: Int
  • Những khoảnh khắc đầu tiên của tạ.

    Tuyên ngôn

    public var firstMoments: Model.TangentVector
  • Khoảnh khắc thứ hai của tạ.

    Tuyên ngôn

    public var secondMoments: Model.TangentVector
  • Tuyên ngôn

    public init(
      for model: __shared Model,
      learningRate: Float = 1e-3,
      beta1: Float = 0.9,
      beta2: Float = 0.999,
      epsilon: Float = 1e-8,
      decay: Float = 0
    )

    Thông số

    learningRate

    Tỷ lệ học tập. Giá trị mặc định là 1e-3 .

    beta1

    Tốc độ phân rã theo cấp số nhân cho ước tính thời điểm đầu tiên. Giá trị mặc định là 0.9 .

    beta2

    Tốc độ phân rã theo cấp số nhân cho ước tính thời điểm thứ 2. Giá trị mặc định là 0.999 .

    epsilon

    Một đại lượng vô hướng nhỏ được thêm vào mẫu số để cải thiện độ ổn định về số. Giá trị mặc định là 1e-8 .

    decay

    Tỷ lệ học tập suy giảm. Giá trị mặc định là 0 .

  • Tuyên ngôn

    public func update(_ model: inout Model, along direction: Model.TangentVector)
  • Tuyên ngôn

    public required init(copying other: Adam, to device: Device)