|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 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 {ENV} from '../environment'; |
| 19 | +import {keep, tidy} from '../globals'; |
| 20 | +import {Node} from '../graph/graph'; |
| 21 | +import {SessionRuntime} from '../graph/session'; |
| 22 | +// tslint:disable-next-line:max-line-length |
| 23 | +import {SummedTensorArrayMap, TensorArrayMap} from '../graph/tensor_array_map'; |
| 24 | +import {NDArrayMath} from '../math'; |
| 25 | +import {scalar, zerosLike} from '../ops/ops'; |
| 26 | +import {Scalar, Tensor, Variable} from '../tensor'; |
| 27 | +import {variable} from '../tensor'; |
| 28 | +import {NamedVariableMap} from '../types'; |
| 29 | + |
| 30 | +import {Optimizer} from './optimizer'; |
| 31 | + |
| 32 | +export class AdamOptimizer extends Optimizer { |
| 33 | + private c: Scalar; |
| 34 | + private eps: Scalar; |
| 35 | + private beta1: Scalar; |
| 36 | + private beta2: Scalar; |
| 37 | + private accBeta1: Variable; |
| 38 | + private accBeta2: Variable; |
| 39 | + private oneMinusBeta1: Scalar; |
| 40 | + private oneMinusBeta2: Scalar; |
| 41 | + private one: Scalar; |
| 42 | + |
| 43 | + private accumulatedFirstMoment: NamedVariableMap = {}; |
| 44 | + private accumulatedSecondMoment: NamedVariableMap = {}; |
| 45 | + |
| 46 | + constructor( |
| 47 | + protected learningRate: number, beta1: number, beta2: number, |
| 48 | + epsilon = 1e-8, specifiedVariableList?: Node[]) { |
| 49 | + super(learningRate, specifiedVariableList); |
| 50 | + this.c = keep(scalar(-learningRate)); |
| 51 | + this.eps = keep(scalar(epsilon)); |
| 52 | + // b1, b2 keep initial value of beta* hyperparameters. |
| 53 | + this.beta1 = keep(scalar(beta1)); |
| 54 | + this.beta2 = keep(scalar(beta2)); |
| 55 | + // accB* will be updated by batch. |
| 56 | + this.accBeta1 = variable(scalar(beta1)); |
| 57 | + this.accBeta2 = variable(scalar(beta2)); |
| 58 | + this.oneMinusBeta1 = keep(scalar(1 - beta1)); |
| 59 | + this.oneMinusBeta2 = keep(scalar(1 - beta2)); |
| 60 | + this.one = keep(scalar(1)); |
| 61 | + } |
| 62 | + |
| 63 | + applyGradients(variableGradients: NamedVariableMap) { |
| 64 | + tidy(() => { |
| 65 | + const oneMinusAccBeta1 = this.one.sub(this.accBeta1); |
| 66 | + const oneMinusAccBeta2 = this.one.sub(this.accBeta2); |
| 67 | + |
| 68 | + for (const variableName in variableGradients) { |
| 69 | + const value = ENV.engine.registeredVariables[variableName]; |
| 70 | + if (this.accumulatedFirstMoment[variableName] == null) { |
| 71 | + const trainable = false; |
| 72 | + this.accumulatedFirstMoment[variableName] = |
| 73 | + variable(zerosLike(value), trainable); |
| 74 | + } |
| 75 | + if (this.accumulatedSecondMoment[variableName] == null) { |
| 76 | + const trainable = false; |
| 77 | + this.accumulatedSecondMoment[variableName] = |
| 78 | + variable(zerosLike(value), trainable); |
| 79 | + } |
| 80 | + |
| 81 | + const gradient = variableGradients[variableName]; |
| 82 | + const firstMoment = this.accumulatedFirstMoment[variableName]; |
| 83 | + const secondMoment = this.accumulatedSecondMoment[variableName]; |
| 84 | + |
| 85 | + const newFirstMoment = |
| 86 | + this.beta1.mul(firstMoment).add(this.oneMinusBeta1.mul(gradient)); |
| 87 | + const newSecondMoment = |
| 88 | + this.beta2.mul(secondMoment) |
| 89 | + .add(this.oneMinusBeta2.mul(gradient.square())); |
| 90 | + |
| 91 | + const biasCorrectedFirstMoment = newFirstMoment.div(oneMinusAccBeta1); |
| 92 | + const biasCorrectedSecondMoment = newSecondMoment.div(oneMinusAccBeta2); |
| 93 | + |
| 94 | + this.accumulatedFirstMoment[variableName].assign(newFirstMoment); |
| 95 | + this.accumulatedSecondMoment[variableName].assign(newSecondMoment); |
| 96 | + |
| 97 | + const newValue = this.c |
| 98 | + .mul(biasCorrectedFirstMoment.div(this.eps.add( |
| 99 | + biasCorrectedSecondMoment.sqrt()))) |
| 100 | + .add(value); |
| 101 | + value.assign(newValue); |
| 102 | + } |
| 103 | + |
| 104 | + this.accBeta1.assign(this.accBeta1.mul(this.beta1)); |
| 105 | + this.accBeta2.assign(this.accBeta2.mul(this.beta2)); |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + beforeBatch( |
| 110 | + math: NDArrayMath, batchSize: number, runtime: SessionRuntime, |
| 111 | + activationArrayMap: TensorArrayMap, |
| 112 | + gradientArrayMap: SummedTensorArrayMap) { |
| 113 | + super.beforeBatch( |
| 114 | + math, batchSize, runtime, activationArrayMap, gradientArrayMap); |
| 115 | + |
| 116 | + if (this.firstMomentGraph.size() === 0) { |
| 117 | + this.variableNodes.forEach(node => { |
| 118 | + this.firstMomentGraph.set(node.output, Tensor.zeros(node.output.shape)); |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + if (this.secondMomentGraph.size() === 0) { |
| 123 | + this.variableNodes.forEach(node => { |
| 124 | + this.secondMomentGraph.set( |
| 125 | + node.output, Tensor.zeros(node.output.shape)); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + afterBatch( |
| 131 | + math: NDArrayMath, batchSize: number, runtime: SessionRuntime, |
| 132 | + activationArrayMap: TensorArrayMap, |
| 133 | + gradientArrayMap: SummedTensorArrayMap) { |
| 134 | + tidy(() => { |
| 135 | + const oneMinusAccBeta1 = this.one.sub(this.accBeta1); |
| 136 | + const oneMinusAccBeta2 = this.one.sub(this.accBeta2); |
| 137 | + |
| 138 | + this.variableNodes.forEach(node => { |
| 139 | + const oldVariable = activationArrayMap.get(node.output); |
| 140 | + const gradient = this.variableGradients.get(node.output); |
| 141 | + |
| 142 | + const oldFirstMoment = this.firstMomentGraph.get(node.output); |
| 143 | + const oldSecondMoment = this.secondMomentGraph.get(node.output); |
| 144 | + |
| 145 | + const newFirstMoment = math.scaledArrayAdd( |
| 146 | + this.beta1, oldFirstMoment, this.oneMinusBeta1, gradient); |
| 147 | + const newSecondMoment = math.scaledArrayAdd( |
| 148 | + this.beta2, oldSecondMoment, this.oneMinusBeta2, gradient.square()); |
| 149 | + |
| 150 | + const biasCorrectedFirstMoment = newFirstMoment.div(oneMinusAccBeta1); |
| 151 | + const biasCorrectedSecondMoment = newSecondMoment.div(oneMinusAccBeta2); |
| 152 | + const variable = math.scaledArrayAdd( |
| 153 | + this.cGraph, |
| 154 | + biasCorrectedFirstMoment.div( |
| 155 | + this.eps.add(biasCorrectedSecondMoment.sqrt())), |
| 156 | + this.one, oldVariable); |
| 157 | + activationArrayMap.set(node.output, keep(variable)); |
| 158 | + node.data = variable; |
| 159 | + |
| 160 | + this.firstMomentGraph.set(node.output, keep(newFirstMoment)); |
| 161 | + this.secondMomentGraph.set(node.output, keep(newSecondMoment)); |
| 162 | + |
| 163 | + oldVariable.dispose(); |
| 164 | + gradient.dispose(); |
| 165 | + oldFirstMoment.dispose(); |
| 166 | + oldSecondMoment.dispose(); |
| 167 | + }); |
| 168 | + this.accBeta1.assign(this.accBeta1.mul(this.beta1)); |
| 169 | + this.accBeta2.assign(this.accBeta2.mul(this.beta2)); |
| 170 | + }); |
| 171 | + |
| 172 | + this.variableGradients.dispose(); |
| 173 | + this.variableGradients = new TensorArrayMap(); |
| 174 | + } |
| 175 | + |
| 176 | + dispose() { |
| 177 | + super.dispose(); |
| 178 | + this.c.dispose(); |
| 179 | + this.eps.dispose(); |
| 180 | + this.beta1.dispose(); |
| 181 | + this.beta2.dispose(); |
| 182 | + this.accBeta1.dispose(); |
| 183 | + this.accBeta2.dispose(); |
| 184 | + this.oneMinusBeta1.dispose(); |
| 185 | + this.oneMinusBeta2.dispose(); |
| 186 | + this.one.dispose(); |
| 187 | + |
| 188 | + if (this.firstMomentGraph != null) { |
| 189 | + this.firstMomentGraph.dispose(); |
| 190 | + } |
| 191 | + |
| 192 | + if (this.secondMomentGraph != null) { |
| 193 | + this.secondMomentGraph.dispose(); |
| 194 | + } |
| 195 | + |
| 196 | + if (this.accumulatedFirstMoment != null) { |
| 197 | + Object.keys(this.accumulatedFirstMoment) |
| 198 | + .forEach(name => this.accumulatedFirstMoment[name].dispose()); |
| 199 | + } |
| 200 | + |
| 201 | + if (this.accumulatedSecondMoment != null) { |
| 202 | + Object.keys(this.accumulatedSecondMoment) |
| 203 | + .forEach(name => this.accumulatedSecondMoment[name].dispose()); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + // Average of gradient |
| 208 | + private firstMomentGraph = new TensorArrayMap(); |
| 209 | + // Average of squared gradient |
| 210 | + private secondMomentGraph = new TensorArrayMap(); |
| 211 | +} |
0 commit comments