This repository has been archived by the owner on Aug 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ============================================================================= | ||
*/ | ||
|
||
import {NDArrayMath} from '../../math/math'; | ||
import {NDArray, Scalar} from '../../math/ndarray'; | ||
import {Node} from '../graph'; | ||
import {SessionRuntime} from '../session'; | ||
import {SummedTensorArrayMap, TensorArrayMap} from '../tensor_array_map'; | ||
|
||
import {Optimizer} from './optimizer'; | ||
|
||
export class AdamOptimizer extends Optimizer { | ||
constructor( | ||
protected learningRate: number, | ||
private beta1: number, private beta2: number, | ||
specifiedVariableList?: Node[]) { | ||
super(learningRate, specifiedVariableList); | ||
this.eps = Scalar.new(1e-8); | ||
// b1, b2 keep initial value of beta* hyperparameters. | ||
this.b1 = Scalar.new(this.beta1); | ||
this.b2 = Scalar.new(this.beta2); | ||
// accB* will be updated by batch. | ||
this.accB1 = Scalar.new(this.beta1); | ||
this.accB2 = Scalar.new(this.beta2); | ||
} | ||
|
||
beforeBatch( | ||
math: NDArrayMath, batchSize: number, runtime: SessionRuntime, | ||
activationArrayMap: TensorArrayMap, | ||
gradientArrayMap: SummedTensorArrayMap) { | ||
super.beforeBatch( | ||
math, batchSize, runtime, activationArrayMap, gradientArrayMap); | ||
|
||
if (this.firstMoment.size() === 0) { | ||
this.variableNodes.forEach(node => { | ||
this.firstMoment.set(node.output, NDArray.zeros(node.output.shape)); | ||
}); | ||
} | ||
|
||
if (this.secondMoment.size() === 0) { | ||
this.variableNodes.forEach(node => { | ||
this.secondMoment.set(node.output, NDArray.zeros(node.output.shape)); | ||
}); | ||
} | ||
} | ||
|
||
afterBatch( | ||
math: NDArrayMath, batchSize: number, runtime: SessionRuntime, | ||
activationArrayMap: TensorArrayMap, | ||
gradientArrayMap: SummedTensorArrayMap) { | ||
math.scope((keep) => { | ||
this.variableNodes.forEach(node => { | ||
const oldVariable = activationArrayMap.get(node.output); | ||
const gradient = this.variableGradients.get(node.output); | ||
|
||
const oldFirstMoment = this.firstMoment.get(node.output); | ||
const oldSecondMoment = this.secondMoment.get(node.output); | ||
|
||
const newFirstMoment = math.scaledArrayAdd( | ||
this.b1, oldFirstMoment, math.sub(this.one, this.b1), gradient); | ||
const gradientSquare = math.multiply(gradient, gradient); | ||
const newSecondMoment = math.scaledArrayAdd( | ||
this.b2, oldSecondMoment, math.sub(this.one, this.b2), | ||
gradientSquare); | ||
|
||
const biasCorrectedFirstMoment = math.divide( | ||
newFirstMoment, math.sub(this.one, this.accB1)); | ||
const biasCorrectedSecondMoment = math.divide( | ||
newSecondMoment, math.sub(this.one, this.accB2)); | ||
|
||
const variable = math.scaledArrayAdd( | ||
this.c, math.divide(biasCorrectedFirstMoment, | ||
math.add(math.sqrt(biasCorrectedSecondMoment), this.eps)), | ||
this.one, oldVariable); | ||
activationArrayMap.set(node.output, keep(variable)); | ||
node.data = variable; | ||
|
||
this.firstMoment.set(node.output, keep(newFirstMoment)); | ||
this.secondMoment.set(node.output, keep(newSecondMoment)); | ||
|
||
oldVariable.dispose(); | ||
gradient.dispose(); | ||
oldFirstMoment.dispose(); | ||
oldSecondMoment.dispose(); | ||
}); | ||
// accB* represents beta1 and beta2 to | ||
// the power t (the number of iteration). | ||
this.accB1 = keep(math.multiply(this.accB1, this.b1)); | ||
this.accB2 = keep(math.multiply(this.accB2, this.b2)); | ||
}); | ||
|
||
this.variableGradients.dispose(); | ||
this.variableGradients = new TensorArrayMap(); | ||
} | ||
|
||
dispose() { | ||
super.dispose(); | ||
this.firstMoment.dispose(); | ||
this.secondMoment.dispose(); | ||
this.eps.dispose(); | ||
this.b1.dispose(); | ||
this.b2.dispose(); | ||
this.accB1.dispose(); | ||
this.accB2.dispose(); | ||
} | ||
|
||
// Average of gradient | ||
private firstMoment = new TensorArrayMap(); | ||
// Average of squared gradient | ||
private secondMoment = new TensorArrayMap(); | ||
private eps: Scalar; | ||
private b1: Scalar; | ||
private b2: Scalar; | ||
private accB1: Scalar; | ||
private accB2: Scalar; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters