Skip to content

Commit

Permalink
finish loss utils function, add to package, bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrogleta committed Sep 4, 2023
1 parent d17dd6a commit e562d2b
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export declare const pow: (a: Value, b: Value) => Value;
export declare const neg: (a: Value) => Value;
export declare const div: (a: Value, b: Value) => Value;
export { Neuron, Layer, MLP } from './nn';
export { toValues } from './utils';
export { toValues, getLoss } from './utils';
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toValues = exports.MLP = exports.Layer = exports.Neuron = exports.div = exports.neg = exports.pow = exports.mul = exports.sub = exports.add = exports.Value = void 0;
exports.getLoss = exports.toValues = exports.MLP = exports.Layer = exports.Neuron = exports.div = exports.neg = exports.pow = exports.mul = exports.sub = exports.add = exports.Value = void 0;
class Value {
data;
_children;
Expand Down Expand Up @@ -108,3 +108,4 @@ Object.defineProperty(exports, "Layer", { enumerable: true, get: function () { r
Object.defineProperty(exports, "MLP", { enumerable: true, get: function () { return nn_1.MLP; } });
var utils_1 = require("./utils");
Object.defineProperty(exports, "toValues", { enumerable: true, get: function () { return utils_1.toValues; } });
Object.defineProperty(exports, "getLoss", { enumerable: true, get: function () { return utils_1.getLoss; } });
1 change: 0 additions & 1 deletion lib/nn.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ export declare class MLP {
parameters(): Value[];
run(inputs: Value[]): Value | Value[];
}
export declare const loss: (ygt: Value[], ypred: Value[]) => Value;
10 changes: 1 addition & 9 deletions lib/nn.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loss = exports.MLP = exports.Layer = exports.Neuron = void 0;
exports.MLP = exports.Layer = exports.Neuron = void 0;
const _1 = require(".");
class Neuron {
w;
Expand Down Expand Up @@ -48,11 +48,3 @@ class MLP {
}
}
exports.MLP = MLP;
const loss = (ygt, ypred) => {
let result = new _1.Value(0);
for (let i = 0; i < ygt.length; i++) {
result = (0, _1.add)(result, (0, _1.mul)((0, _1.sub)(ygt[i], ypred[i]), (0, _1.sub)(ygt[i], ypred[i])));
}
return result;
};
exports.loss = loss;
1 change: 1 addition & 0 deletions lib/utils.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import { Value } from '.';
export declare function toValues(arr: number[]): Value[];
export declare const getLoss: (ygt: Value[], ypred: Value[]) => Value;
10 changes: 9 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toValues = void 0;
exports.getLoss = exports.toValues = void 0;
const _1 = require(".");
function toValues(arr) {
return arr.map((x) => new _1.Value(x));
}
exports.toValues = toValues;
const getLoss = (ygt, ypred) => {
let result = new _1.Value(0);
for (let i = 0; i < ygt.length; i++) {
result = (0, _1.add)(result, (0, _1.mul)((0, _1.sub)(ygt[i], ypred[i]), (0, _1.sub)(ygt[i], ypred[i])));
}
return result;
};
exports.getLoss = getLoss;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "microgradts",
"version": "2.3.0",
"version": "2.4.0",
"license": "MIT",
"devDependencies": {
"@types/node": "^20.5.7",
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,4 @@ export const div = (a: Value, b: Value) => {
};

export { Neuron, Layer, MLP } from './nn';
export { toValues } from './utils';
export { toValues, getLoss } from './utils';
8 changes: 0 additions & 8 deletions src/nn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,3 @@ export class MLP {
return outputs.length === 1 ? outputs[0] : outputs;
}
}

export const loss = (ygt: Value[], ypred: Value[]) => {
let result = new Value(0);
for (let i = 0; i < ygt.length; i++) {
result = add(result, mul(sub(ygt[i], ypred[i]), sub(ygt[i], ypred[i])));
}
return result;
};
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Value } from '.';
import { Value, add, mul, sub } from '.';

export function toValues(arr: number[]): Value[] {
return arr.map((x) => new Value(x));
}

export const getLoss = (ygt: Value[], ypred: Value[]) => {
let result = new Value(0);
for (let i = 0; i < ygt.length; i++) {
result = add(result, mul(sub(ygt[i], ypred[i]), sub(ygt[i], ypred[i])));
}
return result;
};

0 comments on commit e562d2b

Please sign in to comment.