This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add ELU activation function * Update ops-resolve.ts
- Loading branch information
1 parent
7429f07
commit 3439e56
Showing
6 changed files
with
92 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import {Elu} from '../../../ops/elu'; | ||
import {Tensor} from '../../../tensor'; | ||
import {WebGLInferenceHandler} from '../inference-handler'; | ||
import {ProgramInfo} from '../program-info'; | ||
import {RunData} from '../program-manager'; | ||
import {WebGLOperator} from '../webgl-operator'; | ||
import {WebGLOperatorHelper} from '../webgl-operator-utils'; | ||
|
||
export class WebGLElu extends Elu implements WebGLOperator { | ||
run(inferenceHandler: WebGLInferenceHandler, inputs: Tensor[]): Tensor[] { | ||
return WebGLOperatorHelper.run(this, inferenceHandler, inputs); | ||
} | ||
createProgramInfo(handler: WebGLInferenceHandler, inputs: Tensor[]): ProgramInfo { | ||
const outputShape = inputs[0].dims.slice(); | ||
const shaderSource = ` | ||
uniform sampler2D A; | ||
void main() { | ||
float v = texture2D(A, TexCoords).r; | ||
gl_FragColor = vec4(v >= 0.0 ? v: (exp(v) - 1.0) * ${this.alpha.toExponential()}); /* float number format */ | ||
} | ||
`; | ||
return { | ||
hasMain: true, | ||
inputLayouts: [handler.getOrCreateTextureLayout(inputs[0])], | ||
outputLayout: handler.createBasicTextureLayout(outputShape), | ||
shaderSource, | ||
}; | ||
} | ||
createRunData(handler: WebGLInferenceHandler, programInfo: ProgramInfo, inputs: Tensor[]): RunData { | ||
const inputTDs = [handler.getOrCreate(inputs[0], programInfo.inputLayouts[0])]; | ||
return { | ||
inputTextureDatas: inputTDs, | ||
outputTextureData: handler.createTextureDataFromLayout(programInfo.outputLayout, inputTDs[0].dataType), | ||
uniformData: {} | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. | ||
|
||
import {Attribute} from '../attribute'; | ||
import {InferenceHandler} from '../backend'; | ||
import {Operator} from '../operators'; | ||
import {Tensor} from '../tensor'; | ||
|
||
export abstract class Elu implements Operator { | ||
abstract run(inferenceHandler: InferenceHandler, inputs: Tensor[]): Tensor[]|Promise<Tensor[]>; | ||
|
||
initialize(attributes: Attribute): void { | ||
this.alpha = attributes.getFloat('alpha', 1.0); | ||
} | ||
|
||
checkInputs(inputs: Tensor[]): boolean { | ||
if (!inputs || inputs.length !== 1) { | ||
return false; | ||
} | ||
|
||
return this.checkInputTypes(inputs); | ||
} | ||
|
||
protected checkInputTypes(inputs: Tensor[]): boolean { | ||
if (inputs[0].type !== 'float32' && inputs[0].type !== 'float64') { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected alpha: number; | ||
} |
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