Operators API
神经网络层和激活函数。
基类
Operator
所有算子的抽象基类。
typescript
abstract class Operator {
constructor(protected context: GPUContext);
abstract computeOutputShape(inputShape: TensorShape, params?: OperatorParams): TensorShape;
abstract forward(inputs: Tensor[], params?: OperatorParams): Promise<Tensor>;
protected abstract compileShader(): string;
destroy(): void;
}1
2
3
4
5
6
7
2
3
4
5
6
7
卷积算子
Conv2dOperator
2D 卷积操作(NCHW 布局)。
typescript
import { Conv2dOperator } from 'tiny-dl-inference';
const conv = new Conv2dOperator(context);
const output = await conv.forward([input, weights]);1
2
3
4
2
3
4
参数:
typescript
interface Conv2dParams {
stride: [number, number];
padding: [number, number];
}1
2
3
4
2
3
4
形状:
- 输入:
[batch, inChannels, inHeight, inWidth] - 权重:
[outChannels, inChannels, kernelHeight, kernelWidth] - 输出:
[batch, outChannels, outHeight, outWidth]
Conv2dBiasReLUOperator
融合卷积、偏置和 ReLU 激活。
typescript
import { Conv2dBiasReLUOperator } from 'tiny-dl-inference';
const convBiasRelu = new Conv2dBiasReLUOperator(context);
const output = await convBiasRelu.forward([input, weights, bias]);1
2
3
4
2
3
4
性能: 比分离算子快约 1.8 倍。
池化算子
MaxPoolOperator
2D 最大池化。
typescript
import { MaxPoolOperator } from 'tiny-dl-inference';
const pool = new MaxPoolOperator(context);
const output = await pool.forward([input], {
kernelSize: [2, 2],
stride: [2, 2],
padding: [0, 0]
});1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
参数:
typescript
interface MaxPoolParams {
kernelSize: [number, number];
stride: [number, number];
padding: [number, number];
}1
2
3
4
5
2
3
4
5
激活函数
ReLUOperator
修正线性单元激活。
typescript
import { ReLUOperator } from 'tiny-dl-inference';
const relu = new ReLUOperator(context);
const output = await relu.forward([input]);1
2
3
4
2
3
4
公式: f(x) = max(0, x)
SoftmaxOperator
Softmax 归一化。
typescript
import { SoftmaxOperator } from 'tiny-dl-inference';
const softmax = new SoftmaxOperator(context);
const output = await softmax.forward([input]);1
2
3
4
2
3
4
公式: f(x_i) = exp(x_i) / sum(exp(x_j))
全连接层
DenseOperator
全连接层(线性层)。
typescript
import { DenseOperator } from 'tiny-dl-inference';
const dense = new DenseOperator(context);
const output = await dense.forward([input, weights, bias]);1
2
3
4
2
3
4
参数:
typescript
interface DenseParams {
useBias: boolean;
}1
2
3
2
3
形状操作
FlattenOperator
展平张量为二维。
typescript
import { FlattenOperator } from 'tiny-dl-inference';
const flatten = new FlattenOperator(context);
const output = await flatten.forward([input]);1
2
3
4
2
3
4
示例:
- 输入:
[2, 3, 4, 5] - 输出:
[2, 60]
使用示例
构建简单网络
typescript
// Conv → ReLU → MaxPool → Flatten → Dense
const conv = new Conv2dOperator(context);
const relu = new ReLUOperator(context);
const pool = new MaxPoolOperator(context);
const flatten = new FlattenOperator(context);
const dense = new DenseOperator(context);
let x = input;
x = await conv.forward([x, convWeights], { stride: [1, 1], padding: [0, 0] });
x = await relu.forward([x]);
x = await pool.forward([x], { kernelSize: [2, 2], stride: [2, 2], padding: [0, 0] });
x = await flatten.forward([x]);
x = await dense.forward([x, denseWeights, denseBias]);
const result = await x.download();1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15