Last active
November 30, 2016 12:54
-
-
Save robertleeplummerjr/a55dba74d7fc1ed6ac6794915b614729 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function anonymous(input | |
/**/) { | |
var model = {"type":"RNN","options":{"inputSize":3,"inputRange":2,"hiddenSizes":[3],"outputSize":2,"learningRate":0.01,"decayRate":0.999,"smoothEps":1e-8,"regc":0.000001,"clipval":5,"json":null},"input":{"rows":3,"columns":3,"weights":{"0":0.14504642918678196,"1":0.28865358965723514,"2":-0.3079530522503993,"3":-0.13441059098438157,"4":-0.3943911782285204,"5":0.22545938148786127,"6":-0.19453870088510053,"7":-0.3184347404294077,"8":0.3309666089021073}},"hiddenLayers":[{"weight":{"rows":3,"columns":3,"weights":{"0":0.16208498769941176,"1":0.043981900440411704,"2":0.06736829422601728,"3":0.11339363189835776,"4":-0.028364202776012152,"5":-0.038854912606870705,"6":-0.2981753114004179,"7":0.40875294934195483,"8":0.23295964074505185}},"transition":{"rows":3,"columns":3,"weights":{"0":0.01913355746381583,"1":-0.06787170641122188,"2":0.014116139090044852,"3":0.035298424320335434,"4":0.04757889342466173,"5":0.025314592304064076,"6":0.019473188112568866,"7":0.0009661367633043027,"8":-0.26138070444465134}},"bias":{"rows":3,"columns":1,"weights":{"0":-0.31564288267145085,"1":-0.30846409819710696,"2":-0.11154589087827882}}}],"outputConnector":{"rows":3,"columns":3,"weights":{"0":-0.08734598804793325,"1":-0.037316821193345694,"2":0.05591120064618728,"3":0.23731320186611904,"4":0.1620729203480608,"5":-0.15721187684286653,"6":-0.16763928152739388,"7":-0.13490160129575926,"8":0.14496156926897116}},"output":{"rows":3,"columns":1,"weights":{"0":-0.3762191328693657,"1":-0.030371898985037653,"2":0.09085201947989431}}}; | |
function Matrix(rows, columns) { | |
this.rows = rows; | |
this.columns = columns; | |
this.weights = zeros(rows * columns); | |
this.recurrence = zeros(rows * columns); | |
} | |
function zeros(size) { | |
if (typeof Float64Array !== 'undefined') return new Float64Array(size); | |
var array = new Array(size); | |
for (var i = 0; i < size; i++) { | |
array[i] = 0; | |
} | |
return array; | |
} | |
for (var inputIndex = -1, inputMax = input.length; inputIndex < inputMax; inputIndex++) { | |
var source = (i === -1 ? 0 : input[i] + 1); // first step: start with START token | |
var rowPluckIndex = source; //connect up to rowPluck | |
for (var stateIndex = 0, stateMax = 8; stateIndex < stateMax; stateIndex++) { | |
var states = []; | |
states[0] = { | |
name: 'rowPluck', | |
left: model.input, | |
right: null, | |
product: new Matrix(3, 1) | |
}; | |
states[1] = { | |
name: 'multiply', | |
left: model.hiddenLayers[0].weight, | |
right: states[0].product, | |
product: new Matrix(3, 1) | |
}; | |
states[2] = { | |
name: 'multiply', | |
left: model.hiddenLayers[0].transition, | |
right: new Matrix(3, 1), | |
product: new Matrix(3, 1) | |
}; | |
states[3] = { | |
name: 'add', | |
left: states[1].product, | |
right: states[2].product, | |
product: new Matrix(3, 1) | |
}; | |
states[4] = { | |
name: 'add', | |
left: states[3].product, | |
right: model.hiddenLayers[0].bias, | |
product: new Matrix(3, 1) | |
}; | |
states[5] = { | |
name: 'relu', | |
left: states[4].product, | |
right: null, | |
product: new Matrix(3, 1) | |
}; | |
states[6] = { | |
name: 'multiply', | |
left: model.outputConnector, | |
right: states[5].product, | |
product: new Matrix(3, 1) | |
}; | |
states[7] = { | |
name: 'add', | |
left: states[6].product, | |
right: model.output, | |
product: new Matrix(3, 1) | |
}; | |
var state = states[stateIndex]; | |
var product = state.product; | |
var left = state.left; | |
var right = state.right; | |
switch (state.name) { | |
case 'rowPluck': //compiled from src/recurrent/matrix/row-pluck.js | |
for (var column = 0, columns = left.columns; column < columns; column++) { | |
product.weights[column] = left.weights[columns * rowPluckIndex + column]; | |
} | |
break; | |
case 'multiply': //compiled from src/recurrent/matrix/multiply.js | |
var leftRows = left.rows; | |
var leftColumns = left.columns; | |
var rightColumns = right.columns; | |
// loop over rows of left | |
for (var leftRow = 0; leftRow < leftRows; leftRow++) { | |
// loop over cols of right | |
for (var rightColumn = 0; rightColumn < rightColumns; rightColumn++) { | |
// dot product loop | |
var dot = 0; | |
//loop over columns of left | |
for (var leftColumn = 0; leftColumn < leftColumns; leftColumn++) { | |
dot += left.weights[leftColumns * leftRow + leftColumn] * right.weights[rightColumns * leftColumn + rightColumn]; | |
} | |
var i = rightColumns * leftRow + rightColumn; | |
product.weights[i] = dot; | |
} | |
} | |
break; | |
case 'add': //compiled from src/recurrent/matrix/add.js | |
for (var i = 0, max = left.weights.length; i < max; i++) { | |
product.weights[i] = left.weights[i] + right.weights[i]; | |
} | |
break; | |
case 'relu': //compiled from src/recurrent/matrix/relu.js | |
for (var i = 0, max = left.weights.length; i < max; i++) { | |
product.weights[i] = Math.max(0, left.weights[i]); // relu | |
} | |
break; | |
} | |
} | |
} | |
return state.product; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment