mirror of
https://github.com/wassname/keras-js.git
synced 2026-09-10 12:15:12 +08:00
start implementation of activations
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Keras.js
|
||||
|
||||
Run trained Keras models in JavaScript. Works in the browser or Node.js.
|
||||
Run trained [Keras](https://github.com/fchollet/keras) models in your browser, GPU-powered using WebGL.
|
||||
|
||||
##### Why?
|
||||
|
||||
@@ -12,4 +12,4 @@ No. Why would you even want to?
|
||||
|
||||
### License
|
||||
|
||||
MIT
|
||||
MIT
|
||||
|
||||
Vendored
+44
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+26
-28
@@ -1,29 +1,28 @@
|
||||
{
|
||||
"name": "keras-js",
|
||||
"version": "0.0.1",
|
||||
"description": "Run trained Keras models in JavaScript. Works in the browser or Node.js.",
|
||||
"main": "lib/index.js",
|
||||
"description": "Run trained Keras models in your browser, GPU-powered using WebGL.",
|
||||
"scripts": {
|
||||
"lint": "standard src test",
|
||||
"test": "ava --verbose",
|
||||
"check": "npm run lint && npm run test",
|
||||
"compile": "rimraf lib && babel src -d lib",
|
||||
"build": "echo \"No build step currently.\"",
|
||||
"prepublish": "npm run check && npm run compile && npm run build"
|
||||
"watch": "webpack src/index.js dist/keras.js --watch --config webpack.dev.config.js",
|
||||
"build": "npm run lint && webpack --config webpack.prod.config.js",
|
||||
"server": "http-server . -p 9001"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/transcranial/keras-js.git"
|
||||
},
|
||||
"keywords": [
|
||||
"Keras",
|
||||
"keras",
|
||||
"deep",
|
||||
"learning",
|
||||
"machine",
|
||||
"learning",
|
||||
"neural",
|
||||
"networks",
|
||||
"javascript"
|
||||
"javascript",
|
||||
"webgl",
|
||||
"gpu"
|
||||
],
|
||||
"author": "Leon Chen",
|
||||
"license": "MIT",
|
||||
@@ -32,39 +31,38 @@
|
||||
},
|
||||
"homepage": "https://github.com/transcranial/keras-js#readme",
|
||||
"dependencies": {
|
||||
"vectorious": "^4.3.2"
|
||||
"ndarray": "^1.0.18",
|
||||
"ndarray-ops": "^1.2.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^0.16.0",
|
||||
"babel-cli": "^6.11.4",
|
||||
"babel-core": "^6.13.2",
|
||||
"babel-eslint": "^6.1.2",
|
||||
"babel-loader": "^6.2.5",
|
||||
"babel-plugin-transform-class-properties": "^6.11.5",
|
||||
"babel-plugin-transform-object-rest-spread": "^6.8.0",
|
||||
"babel-preset-es2015": "^6.13.2",
|
||||
"babel-register": "^6.11.6",
|
||||
"rimraf": "^2.5.4",
|
||||
"standard": "^8.0.0-beta.5"
|
||||
"http-server": "^0.9.0",
|
||||
"standard": "^8.0.0-beta.5",
|
||||
"webpack": "^1.13.2"
|
||||
},
|
||||
"standard": {
|
||||
"parser": "babel-eslint"
|
||||
"parser": "babel-eslint",
|
||||
"globals": [
|
||||
"KerasJS",
|
||||
"chai",
|
||||
"testUtils",
|
||||
"weblas",
|
||||
"GPU",
|
||||
"performance"
|
||||
]
|
||||
},
|
||||
"babel": {
|
||||
"presets": [
|
||||
"es2015"
|
||||
],
|
||||
"plugins": [
|
||||
"transform-class-properties"
|
||||
"transform-class-properties",
|
||||
"transform-object-rest-spread"
|
||||
]
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"tests/**/*.js"
|
||||
],
|
||||
"concurrency": 4,
|
||||
"failFast": true,
|
||||
"require": [
|
||||
"babel-register"
|
||||
],
|
||||
"babel": "inherit"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
import ndarray from 'ndarray'
|
||||
import ops from 'ndarray-ops'
|
||||
|
||||
/**
|
||||
* Softmax activation function. In-place operation.
|
||||
* @param {Tensor} x
|
||||
* @returns {Tensor} `this`
|
||||
*/
|
||||
export function softmax (x) {
|
||||
if (x.tensor.shape.length === 1) {
|
||||
ops.expeq(x.tensor)
|
||||
const sum = ops.sum(x.tensor)
|
||||
ops.divseq(x.tensor, sum)
|
||||
} else if (x.tensor.shape.length === 2) {
|
||||
for (let i = 0; i < x.tensor.shape[0]; i++) {
|
||||
ops.expeq(x.tensor.pick(i, null))
|
||||
const sum = ops.sum(x.tensor.pick(i, null))
|
||||
ops.divseq(x.tensor.pick(i, null), sum)
|
||||
}
|
||||
} else {
|
||||
throw new Error(`[activations.softmax] tensor shape ${x.tensor.shape} not supported.`)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
export function softplus (x) {
|
||||
|
||||
}
|
||||
|
||||
export function softsign (x) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* ReLU activation function. In-place operation.
|
||||
* @param {Tensor} x
|
||||
* @param {Number} alpha
|
||||
* @param {Number} maxValue
|
||||
* @returns {Tensor} `this`
|
||||
*/
|
||||
export function relu (x, opts = {}) {
|
||||
const { alpha = 0, maxValue = null } = opts
|
||||
let neg
|
||||
if (alpha !== 0) {
|
||||
neg = ndarray(new x._type(x.tensor.data.length), x.tensor.shape)
|
||||
ops.mins(neg, x.tensor, 0.0)
|
||||
ops.mulseq(neg, alpha)
|
||||
}
|
||||
ops.maxseq(x.tensor, 0.0)
|
||||
if (maxValue) {
|
||||
ops.minseq(x.tensor, maxValue)
|
||||
}
|
||||
if (neg) {
|
||||
ops.addeq(x.tensor, neg)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
export function tanh (x) {
|
||||
|
||||
}
|
||||
|
||||
export function sigmoid (x) {
|
||||
|
||||
}
|
||||
|
||||
export function hardSigmoid (x) {
|
||||
|
||||
}
|
||||
|
||||
export function linear (x) {
|
||||
return x
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
import Tensor from './tensor'
|
||||
import * as activations from './activations'
|
||||
|
||||
export {
|
||||
Tensor,
|
||||
activations
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import ndarray from 'ndarray'
|
||||
|
||||
export default class Tensor {
|
||||
constructor (data, shape, options = {}) {
|
||||
this._type = options.type || Float32Array
|
||||
const TypedArray = this._type
|
||||
|
||||
if (shape.length && data.length !== shape.reduce((a, b) => a * b, 1)) {
|
||||
throw new Error('Specified shape incompatible with data.')
|
||||
}
|
||||
|
||||
if (data && data.length && data instanceof TypedArray) {
|
||||
this.tensor = ndarray(data, shape)
|
||||
} else if (data && data.length && data instanceof Array) {
|
||||
this.tensor = ndarray(new TypedArray(data), shape)
|
||||
} else {
|
||||
this.tensor = ndarray(new TypedArray([]), [])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user