Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow on-the-fly regularization changes #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class Activations {
};
}

/** Build-in regularization functions */
/** Built-in regularization functions */
export class RegularizationFunction {
public static L1: RegularizationFunction = {
output: w => Math.abs(w),
Expand Down Expand Up @@ -176,12 +176,10 @@ export class Link {
* @param regularization The regularization function that computes the
* penalty for this weight. If null, there will be no regularization.
*/
constructor(source: Node, dest: Node,
regularization: RegularizationFunction, initZero?: boolean) {
constructor(source: Node, dest: Node, initZero?: boolean) {
this.id = source.id + "-" + dest.id;
this.source = source;
this.dest = dest;
this.regularization = regularization;
if (initZero) {
this.weight = 0;
}
Expand All @@ -204,7 +202,6 @@ export class Link {
export function buildNetwork(
networkShape: number[], activation: ActivationFunction,
outputActivation: ActivationFunction,
regularization: RegularizationFunction,
inputIds: string[], initZero?: boolean): Node[][] {
let numLayers = networkShape.length;
let id = 1;
Expand All @@ -230,7 +227,7 @@ export function buildNetwork(
// Add links from nodes in the previous layer to this node.
for (let j = 0; j < network[layerIdx - 1].length; j++) {
let prevNode = network[layerIdx - 1][j];
let link = new Link(prevNode, node, regularization, initZero);
let link = new Link(prevNode, node, initZero);
prevNode.outputs.push(link);
node.inputLinks.push(link);
}
Expand Down Expand Up @@ -333,7 +330,7 @@ export function backProp(network: Node[][], target: number,
* derivatives.
*/
export function updateWeights(network: Node[][], learningRate: number,
regularizationRate: number) {
regularization: RegularizationFunction, regularizationRate: number) {
for (let layerIdx = 1; layerIdx < network.length; layerIdx++) {
let currentLayer = network[layerIdx];
for (let i = 0; i < currentLayer.length; i++) {
Expand All @@ -350,16 +347,16 @@ export function updateWeights(network: Node[][], learningRate: number,
if (link.isDead) {
continue;
}
let regulDer = link.regularization ?
link.regularization.der(link.weight) : 0;
let regulDer = regularization ?
regularization.der(link.weight) : 0;
if (link.numAccumulatedDers > 0) {
// Update the weight based on dE/dw.
link.weight = link.weight -
(learningRate / link.numAccumulatedDers) * link.accErrorDer;
// Further update the weight based on regularization.
let newLinkWeight = link.weight -
(learningRate * regularizationRate) * regulDer;
if (link.regularization === RegularizationFunction.L1 &&
if (regularization === RegularizationFunction.L1 &&
link.weight * newLinkWeight < 0) {
// The weight crossed 0 due to the regularization term. Set it to 0.
link.weight = 0;
Expand Down
10 changes: 6 additions & 4 deletions src/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,17 @@ function makeGUI() {
function() {
state.regularization = regularizations[this.value];
parametersChanged = true;
reset();
state.serialize();
userHasInteracted();
});
regularDropdown.property("value",
getKeyFromValue(regularizations, state.regularization));

let regularRate = d3.select("#regularRate").on("change", function() {
state.regularizationRate = +this.value;
parametersChanged = true;
reset();
state.serialize();
userHasInteracted();
});
regularRate.property("value", state.regularizationRate);

Expand Down Expand Up @@ -913,7 +915,7 @@ function oneStep(): void {
nn.forwardProp(network, input);
nn.backProp(network, point.label, nn.Errors.SQUARE);
if ((i + 1) % state.batchSize === 0) {
nn.updateWeights(network, state.learningRate, state.regularizationRate);
nn.updateWeights(network, state.learningRate, state.regularization, state.regularizationRate);
}
});
// Compute the loss.
Expand Down Expand Up @@ -956,7 +958,7 @@ function reset(onStartup=false) {
let outputActivation = (state.problem === Problem.REGRESSION) ?
nn.Activations.LINEAR : nn.Activations.TANH;
network = nn.buildNetwork(shape, state.activation, outputActivation,
state.regularization, constructInputIds(), state.initZero);
constructInputIds(), state.initZero);
lossTrain = getLoss(network, trainData);
lossTest = getLoss(network, testData);
drawNetwork(network);
Expand Down