예제

실행 가능한 코드에서 배우기

예제는 프로젝트 소스 저장소로 연결됩니다. 측정하지 않은 성능이나 학습 결과를 사이트에서 꾸며내지 않습니다.

MNIST MLP

28×28 이미지를 펼쳐 784 → 128 Dense 계층과 ReLU를 적용한 뒤 10개 클래스의 logits를 계산합니다. 저장소 예제에는 배치 처리, Adam, 정수 레이블 cross entropy와 평가 코드도 포함되어 있습니다.

모델 정의examples/mnist/model.ts
export class SimpleMLP extends nn.Module {
  private readonly flatten: nn.Flatten;
  private readonly fc1: nn.Linear;
  private readonly relu: nn.ReLU;
  private readonly dropout: nn.Dropout;
  private readonly fc2: nn.Linear;

  constructor() {
    super();
    this.flatten = this.register("flatten", new nn.Flatten());
    this.fc1 = this.register("fc1", new nn.Linear(784, 128));
    this.relu = this.register("relu", new nn.ReLU());
    this.dropout = this.register("dropout", new nn.Dropout(0.2));
    this.fc2 = this.register("fc2", new nn.Linear(128, 10));
  }

  forward(input: nn.Tensor): nn.Tensor {
    return this.fc2.forward(this.dropout.forward(
      this.relu.forward(this.fc1.forward(this.flatten.forward(input))),
    ));
  }
}

MNIST 소스 탐색 ↗

Tensor 연산

시작하기와 Playground에서 rank-2 MatMul, elementwise 연산, activation, 명시적 결과 읽기를 다룹니다. 동일한 입력으로 CPU와 브라우저에서 사용할 수 있는 백엔드를 확인할 수 있습니다.

Playground 열기

추가 소스 예제