アダム

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

アダムオプティマイザー。

Adam 最適化アルゴリズムを実装します。 Adam は、勾配の一次モーメントと二次モーメントの推定値からさまざまなパラメーターに対する個々の適応学習率を計算する確率的勾配降下法です。

参考文献: 「Adam: 確率的最適化の手法」 (Kingma および Ba、2014)。

例:

  • 単純な強化学習エージェントをトレーニングします。
...
// 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)
    ...
    }
}
  • 敵対的生成ネットワーク (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)
        }
}       
  • 宣言

    public typealias Model = Model
  • 学習率。

    宣言

    public var learningRate: Float
  • 勾配の最初のモーメントを計算するために使用される係数。

    宣言

    public var beta1: Float
  • 勾配の二次モーメントを計算するために使用される係数。

    宣言

    public var beta2: Float
  • 数値の安定性を向上させるために、分母に小さなスカラーが追加されました。

    宣言

    public var epsilon: Float
  • 学習率が低下します。

    宣言

    public var decay: Float
  • 現在のステップ。

    宣言

    public var step: Int
  • 重みの最初の瞬間。

    宣言

    public var firstMoments: Model.TangentVector
  • 重みの第二の瞬間。

    宣言

    public var secondMoments: Model.TangentVector
  • 宣言

    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
    )

    パラメーター

    learningRate

    学習率。デフォルト値は1e-3です。

    beta1

    最初の瞬間の推定値の指数関数的減衰率。デフォルト値は0.9です。

    beta2

    2 次モーメント推定の指数関数的減衰率。デフォルト値は0.999です。

    epsilon

    数値の安定性を向上させるために、分母に小さなスカラーが追加されました。デフォルト値は1e-8です。

    decay

    学習率が低下します。デフォルト値は0です。

  • 宣言

    public func update(_ model: inout Model, along direction: Model.TangentVector)
  • 宣言

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