Skip to content

Commit

Permalink
feat: structure
Browse files Browse the repository at this point in the history
  • Loading branch information
load1n9 committed Oct 6, 2022
1 parent 864aaf6 commit 6e9ddaf
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 21 deletions.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,46 @@ console.log(await net.predict(new Float32Array([1, 0])));
console.log(await net.predict(new Float32Array([0, 1])));
console.log(await net.predict(new Float32Array([1, 1])));
```

#### Use the Native Backend

```typescript
import { DenseLayer, NeuralNetwork } from "https://deno.land/x/netsaur/mod.ts";
import { Matrix, Native } from "https://deno.land/x/netsaur/backends/native.ts";

const network = await new NeuralNetwork({
input: 2,
layers: [
new DenseLayer({ size: 3, activation: "sigmoid" }),
new DenseLayer({ size: 1, activation: "sigmoid" }),
],
cost: "crossentropy",
}).setupBackend(Native);

network.train(
[
{
inputs: Matrix.of([
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]),
outputs: Matrix.column([0, 1, 1, 0]),
},
],
5000,
0.1,
);

console.log(
await network.predict(
Matrix.of([
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]),
),
);
```
1 change: 1 addition & 0 deletions backends/cpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { CPU } from "../src/cpu/mod.ts";
1 change: 1 addition & 0 deletions backends/gpu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { GPU } from "../src/gpu/mod.ts";
4 changes: 4 additions & 0 deletions backends/native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export { Native } from "../src/native/mod.ts";
export { Matrix } from "../src/native/matrix.ts";
export type { DataType } from "../src/native/matrix.ts";
export type { Dataset } from "../src/native/backend.ts";
3 changes: 1 addition & 2 deletions bench/netsaur/xor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NeuralNetwork, DenseLayer } from "../../mod.ts";
import { Native } from "../../src/native/mod.ts";
import { Matrix } from "../../src/native/matrix.ts";
import { Native, Matrix } from "../../backends/native.ts";

const start = Date.now();

Expand Down
4 changes: 2 additions & 2 deletions examples/mnist_digits/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Dataset } from "../../src/native/backend.ts";
import { Matrix } from "../../src/native/matrix.ts";
import type { Dataset } from "../../backends/native.ts";
import { Matrix } from "../../backends/native.ts";


export function assert(condition: boolean, message?: string) {
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist_digits/predict.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeBackend } from "../../src/native/backend.ts";
import { DataType, Matrix } from "../../src/native/matrix.ts";
import { DataType, Matrix } from "../../backends/native.ts";
import { loadDataset } from "./common.ts";

const network = NativeBackend.load("digit_model.bin");
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist_digits/train.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DenseLayer, NeuralNetwork } from "../../mod.ts";
import { Native } from "../../src/native/mod.ts";
import { Native } from "../../backends/native.ts";
import { loadDataset } from "./common.ts";

const network = await new NeuralNetwork({
Expand Down
2 changes: 1 addition & 1 deletion examples/train_conv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConvCPULayer } from "../src/cpu/layers/conv.ts";
import { PoolCPULayer } from "../src/cpu/layers/pool.ts";
import { CPUMatrix } from "../src/cpu/matrix.ts";
import { CPUBackend } from "../src/cpu/backend.ts";
import { CPU } from "../src/cpu/mod.ts";
import { CPU } from "../backends/cpu.ts";

const kernel = new Float32Array([
1,
Expand Down
2 changes: 1 addition & 1 deletion examples/train_emotion.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataType, DataTypeArray } from "../deps.ts";
import { DenseLayer, NeuralNetwork } from "../mod.ts";
import { CPU } from "../src/cpu/mod.ts";
import { CPU } from "../backends/cpu.ts";

const character = (string: string): Float32Array =>
Float32Array.from(string.trim().split("").map(integer));
Expand Down
2 changes: 1 addition & 1 deletion examples/train_letter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DataType, DataTypeArray } from "../deps.ts";
import { DenseLayer, NeuralNetwork } from "../mod.ts";
import { CPU } from "../src/cpu/mod.ts";
import { CPU } from "../backends/cpu.ts";

// https://github.com/BrainJS/brain.js/blob/master/examples/typescript/which-letter-simple.ts
const character = (string: string): Float32Array =>
Expand Down
2 changes: 1 addition & 1 deletion examples/train_xor_cpu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DenseLayer, NeuralNetwork } from "../mod.ts";
import { CPU } from "../src/cpu/mod.ts";
import { CPU } from "../backends/cpu.ts";

const net = await new NeuralNetwork({
silent: true,
Expand Down
2 changes: 1 addition & 1 deletion examples/train_xor_gpu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DenseLayer, NeuralNetwork } from "../mod.ts";
import { GPU } from "../src/gpu/mod.ts";
import { GPU } from "../backends/gpu.ts";

const net = await new NeuralNetwork({
silent: true,
Expand Down
3 changes: 1 addition & 2 deletions examples/train_xor_native.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NeuralNetwork, DenseLayer } from "../mod.ts";
import { Native } from "../src/native/mod.ts";
import { Matrix } from "../src/native/matrix.ts";
import { Native, Matrix } from "../backends/native.ts";

const start = Date.now();

Expand Down
14 changes: 8 additions & 6 deletions tests/conv.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ConvLayer, DenseLayer, NeuralNetwork } from "../mod.ts";
import { ConvCPULayer } from "../src/cpu/layers/conv.ts";
import { CPUMatrix } from "../src/cpu/matrix.ts";
import { CPUNetwork } from "../src/cpu/network.ts";
import { CPUBackend } from "../src/cpu/backend.ts";
import { CPU } from "../backends/cpu.ts";
import { PoolCPULayer } from "../src/cpu/layers/pool.ts";
import { PoolLayer } from "../src/mod.ts";

import { decode } from "https://deno.land/x/[email protected]/mod.ts";
import { DataTypeArray } from "../deps.ts";
import { PoolCPULayer } from "../src/cpu/layers/pool.ts";
import { PoolLayer } from "../src/mod.ts";

import { Canvas } from "https://deno.land/x/[email protected]/canvas/mod.ts";

import { Canvas } from "https://deno.land/x/[email protected]/canvas/mod.ts";

const canvas = new Canvas({
title: "Netsaur Convolutions",
Expand Down Expand Up @@ -58,10 +60,10 @@ const net = await new NeuralNetwork({
],
cost: "crossentropy",
input: 2,
}).setupBackend("cpu");
}).setupBackend(CPU);

const input = new CPUMatrix(buf, dim, dim);
const network = net.network as CPUNetwork;
const network = net.backend as CPUBackend;
const conv = network.layers[0] as ConvCPULayer;
const pool = network.layers[1] as PoolCPULayer;
network.initialize({ x: dim, y: dim }, 1);
Expand Down
3 changes: 1 addition & 2 deletions tests/linear.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NeuralNetwork, DenseLayer } from "../mod.ts";
import { Native } from "../src/native/mod.ts";
import { Matrix } from "../src/native/matrix.ts";
import { Native, Matrix } from "../backends/native.ts";

const start = performance.now();

Expand Down

0 comments on commit 6e9ddaf

Please sign in to comment.