Skip to content

Commit

Permalink
feat: docs (#14)
Browse files Browse the repository at this point in the history
* feat: documentation

* feat: documentation

* feat: rename NeuralNetwork to Sequential

* fix: revert rename NeuralNetwork to Sequential
  • Loading branch information
load1n9 authored May 3, 2023
1 parent fcf5303 commit 525c933
Show file tree
Hide file tree
Showing 27 changed files with 463 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ console.log((await net.predict(tensor2D([[1, 1]]))).data);

### Documentation

The full documentation for Netsaur can be found [here](https://deno.land/x/[email protected].0/mod.ts).
The full documentation for Netsaur can be found [here](https://deno.land/x/[email protected].2/mod.ts).

### License

Expand Down
2 changes: 1 addition & 1 deletion deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ export { Core } from "https://raw.githubusercontent.com/denosaurs/neo/e7f602cd21
export { WebGPUData } from "https://raw.githubusercontent.com/denosaurs/neo/e7f602cd218fbdb7418e0fca9ac4aba8d6c22d08/backend/webgpu/data.ts";
export {
CsvStream,
} from "https://deno.land/std@0.159.0/encoding/csv/stream.ts";
} from "https://deno.land/std@0.183.0/encoding/csv/stream.ts";
export { dlopen } from "https://deno.land/x/[email protected]/mod.ts";
export type { FetchOptions } from "https://deno.land/x/[email protected]/mod.ts";
3 changes: 3 additions & 0 deletions src/backend_cpu/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
TrainOptions,
} from "./util.ts";

/**
* CPU Backend.
*/
export class CPUBackend {
config: NetworkConfig;
library: Library;
Expand Down
5 changes: 4 additions & 1 deletion src/backend_cpu/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const options: FetchOptions = {
name: "netsaur",
url: new URL(import.meta.url).protocol !== "file:"
? new URL(
"https://github.com/denosaurs/netsaur/releases/download/0.2.0/",
"https://github.com/denosaurs/netsaur/releases/download/0.2.2/",
import.meta.url,
)
: "./target/release/",
Expand Down Expand Up @@ -68,4 +68,7 @@ export class CPUBackendLoader {
}
}

/**
* CPU Backend Type.
*/
export const CPU = new CPUBackendLoader();
3 changes: 3 additions & 0 deletions src/backend_cpu/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Rank, Shape, Tensor, TensorData } from "../../mod.ts";
import { length } from "../core/tensor/util.ts";
import { BackendType } from "../core/types.ts";

/**
* CPU Tensor Backend.
*/
export class CPUTensorBackend {
zeroes<R extends Rank, B extends BackendType>(shape: Shape[R]): Tensor<R, B> {
return new Tensor(new Float32Array(length(shape)) as TensorData[B], shape);
Expand Down
15 changes: 15 additions & 0 deletions src/backend_cpu/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { DataSet, Rank, Shape } from "../../mod.ts";

/**
* Train Options Interface.
*/
export type TrainOptions = {
datasets: number;
inputShape: Shape[Rank];
Expand All @@ -8,19 +11,31 @@ export type TrainOptions = {
rate: number;
};

/**
* Predict Options Interface.
*/
export type PredictOptions = {
inputShape: Shape[Rank];
outputShape: Shape[Rank];
};

/**
* Encode JSON data.
*/
export function encodeJSON(json: unknown) {
return new TextEncoder().encode(JSON.stringify(json));
}

/**
* Returns the BigInt value of a pointer.
*/
export function pointer(arr: BufferSource) {
return BigInt(Deno.UnsafePointer.value(Deno.UnsafePointer.of(arr)));
}

/**
* Encode datasets.
*/
export function encodeDatasets(datasets: DataSet[]) {
const pointers: bigint[] = [];
for (const dataset of datasets) {
Expand Down
3 changes: 3 additions & 0 deletions src/backend_wasm/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {
} from "./lib/netsaur.generated.js";
import { PredictOptions, TrainOptions } from "./utils.ts";

/**
* Web Assembly Backend.
*/
export class WASMBackend {
config: NetworkConfig;
outputShape?: Shape[Rank];
Expand Down
2 changes: 1 addition & 1 deletion src/backend_wasm/lib/netsaur.generated.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 7077043632c1986dee512bca3299c31e44fd3bcc
// source-hash: 6cc9f6f66326aec7fc3ebedaf05c0a2ac31f17ec
let wasm;
let cachedInt32Memory0;

Expand Down
Binary file modified src/backend_wasm/lib/netsaur_bg.wasm
Binary file not shown.
9 changes: 9 additions & 0 deletions src/backend_wasm/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { WASMTensorBackend } from "./tensor.ts";
import { NetworkJSON } from "../model/types.ts";
import { instantiate } from "./lib/netsaur.generated.js";

/**
* Web Assembly backend instance.
*/
export class WASMInstance {
static initialized = false;

Expand All @@ -19,6 +22,9 @@ export class WASMInstance {
}
}

/**
* Web Assembly Backend Loader.
*/
export class WASMBackendLoader {
async setup(silent = false) {
Tensor.backend = new WASMTensorBackend();
Expand All @@ -38,4 +44,7 @@ export class WASMBackendLoader {
}
}

/**
* Web Assembly Backend Type.
*/
export const WASM = new WASMBackendLoader();
3 changes: 3 additions & 0 deletions src/backend_wasm/tensor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Rank, Shape, Tensor, TensorData } from "../../mod.ts";
import { length } from "../core/tensor/util.ts";
import { BackendType } from "../core/types.ts";

/**
* Web Assembly Tensor Backend.
*/
export class WASMTensorBackend {
zeroes<R extends Rank, B extends BackendType>(shape: Shape[R]): Tensor<R, B> {
return new Tensor(new Float32Array(length(shape)) as TensorData[B], shape);
Expand Down
6 changes: 6 additions & 0 deletions src/backend_wasm/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Rank, Shape } from "../../mod.ts";

/**
* Train Options Interface.
*/
export type TrainOptions = {
datasets: number;
inputShape: Shape[Rank];
Expand All @@ -8,6 +11,9 @@ export type TrainOptions = {
rate: number;
};

/**
* Predict Options Interface.
*/
export type PredictOptions = {
inputShape: Shape[Rank];
outputShape: Shape[Rank];
Expand Down
15 changes: 15 additions & 0 deletions src/core/api/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { BackendType } from "../types.ts";
import { Rank, Shape, Shape2D } from "./shape.ts";

/**
* Incompatible Rank Error is thrown when a tensor is incompatible with a layer.
*/
export class IncompatibleRankError extends Error {
constructor(shape: number, expected: number) {
super(
Expand All @@ -9,12 +12,18 @@ export class IncompatibleRankError extends Error {
}
}

/**
* Invalid Flatten Error is thrown when a tensor cannot be flattened.
*/
export class InvalidFlattenError extends Error {
constructor(input: Shape[Rank], output: Shape[Rank]) {
super(`Cannot flatten tensor of shape ${input} to shape ${output}`);
}
}

/**
* No backend error is thrown when a backend is not initialized.
*/
export class NoBackendError extends Error {
constructor(type: BackendType) {
super(
Expand All @@ -23,12 +32,18 @@ export class NoBackendError extends Error {
}
}

/**
* Invalid Pool Error is thrown when a tensor cannot be pooled.
*/
export class InvalidPoolError extends Error {
constructor(size: Shape[Rank], stride: Shape2D) {
super(`Cannot pool shape ${size} with stride ${stride}`);
}
}

/**
* Invalid Activation Error is thrown when an activation function is invalid.
*/
export class ActivationError extends Error {
constructor(activation: string) {
super(`Unknown activation function: ${activation}.`);
Expand Down
69 changes: 69 additions & 0 deletions src/core/api/layer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Activation, Init, LayerType } from "../types.ts";
import { Rank, Shape, Shape1D, Shape2D, Shape4D } from "./shape.ts";

/**
* Layer is the base type for all layers.
*/
export type Layer =
| { type: LayerType.Dense; config: DenseLayerConfig }
| { type: LayerType.Activation; config: ActivationLayerConfig }
Expand All @@ -9,31 +12,97 @@ export type Layer =
| { type: LayerType.Flatten; config: FlattenLayerConfig }
| { type: LayerType.Softmax };

/**
* The configuration for a dense layer.
*/
export type DenseLayerConfig = {
/**
* The type of initialization to use.
*/
init?: Init;

/**
* The size of the layer.
*/
size: Shape1D;

/**
* The activation function to use.
*/
activation?: Activation;
};

/**
* The configuration for an activation layer.
*/
export type ActivationLayerConfig = {
/**
* The activation function to use.
*/
activation: Activation;
};

/**
* The configuration for a convolutional layer.
*/
export type ConvLayerConfig = {
/**
* The type of initialization to use.
*/
init?: Init;

/**
* The activation function to use.
*/
activation?: Activation;

/**
* The kernel to use.
*/
kernel?: Float32Array;

/**
* The size of the kernel.
*/
kernelSize: Shape4D;

/**
* The optional padding to use.
*/
padding?: number;

/**
* Whether or not to not use bias.
*/
unbiased?: boolean;

/**
* The optional strides to use.
*/
strides?: Shape2D;
};

/**
* The configuration for a pooling layer.
*/
export type PoolLayerConfig = {
/**
* The optional strides to use.
*/
strides?: Shape2D;

/**
* The mode to use for the pool layer.
*/
mode?: "max" | "avg";
};

/**
* The configuration for a flatten layer.
*/
export type FlattenLayerConfig = {
/**
* The size of the layer.
*/
size: Shape[Rank];
};
Loading

0 comments on commit 525c933

Please sign in to comment.