|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2017 Google Inc. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {NDArrayMath} from '../../math/math'; |
| 19 | +import {NDArray, Scalar} from '../../math/ndarray'; |
| 20 | +import {Node} from '../graph'; |
| 21 | +import {SessionRuntime} from '../session'; |
| 22 | +import {SummedTensorArrayMap, TensorArrayMap} from '../tensor_array_map'; |
| 23 | + |
| 24 | +import {Optimizer} from './optimizer'; |
| 25 | + |
| 26 | +export class AdamOptimizer extends Optimizer { |
| 27 | + constructor( |
| 28 | + protected learningRate: number, |
| 29 | + private beta1: number, private beta2: number, |
| 30 | + specifiedVariableList?: Node[]) { |
| 31 | + super(learningRate, specifiedVariableList); |
| 32 | + this.eps = Scalar.new(1e-8); |
| 33 | + // b1, b2 keep initial value of beta* hyperparameters. |
| 34 | + this.b1 = Scalar.new(this.beta1); |
| 35 | + this.b2 = Scalar.new(this.beta2); |
| 36 | + // accB* will be updated by batch. |
| 37 | + this.accB1 = Scalar.new(this.beta1); |
| 38 | + this.accB2 = Scalar.new(this.beta2); |
| 39 | + } |
| 40 | + |
| 41 | + beforeBatch( |
| 42 | + math: NDArrayMath, batchSize: number, runtime: SessionRuntime, |
| 43 | + activationArrayMap: TensorArrayMap, |
| 44 | + gradientArrayMap: SummedTensorArrayMap) { |
| 45 | + super.beforeBatch( |
| 46 | + math, batchSize, runtime, activationArrayMap, gradientArrayMap); |
| 47 | + |
| 48 | + if (this.firstMoment.size() === 0) { |
| 49 | + this.variableNodes.forEach(node => { |
| 50 | + this.firstMoment.set(node.output, NDArray.zeros(node.output.shape)); |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + if (this.secondMoment.size() === 0) { |
| 55 | + this.variableNodes.forEach(node => { |
| 56 | + this.secondMoment.set(node.output, NDArray.zeros(node.output.shape)); |
| 57 | + }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + afterBatch( |
| 62 | + math: NDArrayMath, batchSize: number, runtime: SessionRuntime, |
| 63 | + activationArrayMap: TensorArrayMap, |
| 64 | + gradientArrayMap: SummedTensorArrayMap) { |
| 65 | + math.scope((keep) => { |
| 66 | + this.variableNodes.forEach(node => { |
| 67 | + const oldVariable = activationArrayMap.get(node.output); |
| 68 | + const gradient = this.variableGradients.get(node.output); |
| 69 | + |
| 70 | + const oldFirstMoment = this.firstMoment.get(node.output); |
| 71 | + const oldSecondMoment = this.secondMoment.get(node.output); |
| 72 | + |
| 73 | + const newFirstMoment = math.scaledArrayAdd( |
| 74 | + this.b1, oldFirstMoment, math.sub(this.one, this.b1), gradient); |
| 75 | + const gradientSquare = math.multiply(gradient, gradient); |
| 76 | + const newSecondMoment = math.scaledArrayAdd( |
| 77 | + this.b2, oldSecondMoment, math.sub(this.one, this.b2), |
| 78 | + gradientSquare); |
| 79 | + |
| 80 | + const biasCorrectedFirstMoment = math.divide( |
| 81 | + newFirstMoment, math.sub(this.one, this.accB1)); |
| 82 | + const biasCorrectedSecondMoment = math.divide( |
| 83 | + newSecondMoment, math.sub(this.one, this.accB2)); |
| 84 | + |
| 85 | + const variable = math.scaledArrayAdd( |
| 86 | + this.c, math.divide(biasCorrectedFirstMoment, |
| 87 | + math.add(math.sqrt(biasCorrectedSecondMoment), this.eps)), |
| 88 | + this.one, oldVariable); |
| 89 | + activationArrayMap.set(node.output, keep(variable)); |
| 90 | + node.data = variable; |
| 91 | + |
| 92 | + this.firstMoment.set(node.output, keep(newFirstMoment)); |
| 93 | + this.secondMoment.set(node.output, keep(newSecondMoment)); |
| 94 | + |
| 95 | + oldVariable.dispose(); |
| 96 | + gradient.dispose(); |
| 97 | + oldFirstMoment.dispose(); |
| 98 | + oldSecondMoment.dispose(); |
| 99 | + }); |
| 100 | + // accB* represents beta1 and beta2 to |
| 101 | + // the power t (the number of iteration). |
| 102 | + this.accB1 = keep(math.multiply(this.accB1, this.b1)); |
| 103 | + this.accB2 = keep(math.multiply(this.accB2, this.b2)); |
| 104 | + }); |
| 105 | + |
| 106 | + this.variableGradients.dispose(); |
| 107 | + this.variableGradients = new TensorArrayMap(); |
| 108 | + } |
| 109 | + |
| 110 | + dispose() { |
| 111 | + super.dispose(); |
| 112 | + this.firstMoment.dispose(); |
| 113 | + this.secondMoment.dispose(); |
| 114 | + this.eps.dispose(); |
| 115 | + this.b1.dispose(); |
| 116 | + this.b2.dispose(); |
| 117 | + this.accB1.dispose(); |
| 118 | + this.accB2.dispose(); |
| 119 | + } |
| 120 | + |
| 121 | + // Average of gradient |
| 122 | + private firstMoment = new TensorArrayMap(); |
| 123 | + // Average of squared gradient |
| 124 | + private secondMoment = new TensorArrayMap(); |
| 125 | + private eps: Scalar; |
| 126 | + private b1: Scalar; |
| 127 | + private b2: Scalar; |
| 128 | + private accB1: Scalar; |
| 129 | + private accB2: Scalar; |
| 130 | +} |
0 commit comments