← 开源
eduardoleao052

js-pytorch

A JavaScript library like PyTorch, with GPU acceleration.

Model DevelopmentDeep Learning FrameworksJavaScript
在 GitHub 打开
增长势头
+024 小时新增 Star0.0%
1.22k
Star
55
Fork
+0
本周
5
贡献者
创建于 2024-02-27 · 更新于 2026-10-12 · 今日第 11090 名
主要开发者
eduardoleao052pparkemedic-code

eduardoleao052/js-pytorch 排名第 11,090,有 1,221 个星标。

同类别比较

eduardoleao052/js-pytorch 排名第 11,090,此处有 8 个同类条目;google-research/fixmatch 排名第 11,087。

同类别比较
名称排名指标
Bolin97/GongBU11,077
语言:
Svelte
星标数:
1,224
分叉数:
89
简介:
Paper accepted by CIKM 2024. Codes of GongBU, a LLM fine-tuning platform for domain-specific adaptation.
dee1024/pytorch-captcha-recognition11,084
语言:
Python
星标数:
1,223
分叉数:
326
简介:
基于CNN训练的一套 "端到端" 的验证码识别模型,使用深度学习+训练数据+大量计算力,纯数字识别率高达 99.99%,数字+字母识别率 96%
wagenaartje/neataptic11,086
语言:
JavaScript
星标数:
1,223
分叉数:
286
简介:
:rocket: Blazing fast neuro-evolution & backpropagation for the browser and Node.js
google-research/fixmatch11,087
语言:
Python
星标数:
1,222
分叉数:
191
简介:
A simple method to perform semi-supervised learning with limited data.
lucidrains/perceiver-pytorch11,093
语言:
Python
星标数:
1,221
分叉数:
141
简介:
Implementation of Perceiver, General Perception with Iterative Attention, in Pytorch
getnamo/TensorFlow-Unreal11,095
语言:
C++
星标数:
1,220
分叉数:
207
简介:
TensorFlow plugin for the Unreal Engine.
xvjiarui/GCNet11,098
语言:
Python
星标数:
1,220
分叉数:
165
简介:
GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
Jhy1993/HAN11,100
语言:
Python
星标数:
1,219
分叉数:
224
简介:
Heterogeneous Graph Neural Network
关键参数与规格
关键参数与规格
参数值
语言JavaScript
类别Model Development
子类别Deep Learning Frameworks
星标数1,221
分叉数55
贡献者5
创建日期2024-02-27
更新日期2026-10-12
1 天星标数0
7 天星标数0
排名11,090
上次排名11,042
排名变化-48
7 天星标排名12,882
上次 7 天星标排名12,897
7 天星标排名变化15
总星标排名9,568
上次总星标排名9,560
总星标排名变化-8
国家Brazil
README 摘录

PyTorch in JavaScript JS-PyTorch is a Deep Learning **JavaScript library** built from scratch, to closely follow PyTorch's syntax. This library has **GPU support**, using GPU. If you want to run it yourself, check out the Documentation . **Note:** You can install the package locally with: Implemented Tensor Operations : Add Subtract Multiply Divide Matrix Multiply Power Square Root Exponentiate Log Sum Mean Variance Transpose At MaskedFill Reshape Implemented Deep Learning Layers : nn. Table of Contents Installation Running it Yourself Simple Autograd

Example Complex Autograd Example (Transformer) Saving and Loading models Distribution & Devtools Future Work 2. Installation On **MacOS**, **Windows**, and **Ubuntu**, you can install the library with . On **Windows**, if you run into an error, you might need to install the latest version of Visual Studio, including the "Desktop development with C++" workload. To run in the **Browser**, paste the following tag in the of your HTML file: After that, you can use JS-PyTorch freely in any 3. Running it

Yourself Simple Autograd Example: Complex Autograd Example (Transformer): Saving and Loading models: 4. Distribution & Devtools **Build for Distribution** by running . CJS and ESM modules and will be output in the folder. **Check the Code** with ESLint at any time, running . **Improve Code Formatting** with prettier, running . **Performance Benchmarks** are also included in the directory. Run all benchmarks with and save new benchmarks with . Future Work This package is not as optimized as PyTorch yet, but I tried making it more

interpretable. Create a merge request to the branch, and also feel free to reach out.

常见问题

记录了多少星标和分叉?

eduardoleao052/js-pytorch 有 1,221 个星标和 55 个分叉。

仓库排名是多少?

eduardoleao052/js-pytorch 排名第 11,090。

记录的语言和类别是什么?

eduardoleao052/js-pytorch 有 1,221 个星标,语言为 JavaScript,类别为 Model Development。

哪个仓库排名最接近?

eduardoleao052/js-pytorch 与 google-research/fixmatch 比较,后者排名第 11,087,有 1,222 个星标。

README

[![](https://github.com/eduardoleao052/js-torch/actions/workflows/test.yml/badge.svg)](https://github.com/eduardoleao052/js-torch/actions/workflows/test.yml/badge.svg)
[![](https://img.shields.io/github/commit-activity/m/eduardoleao052/js-torch)](https://github.com/eduardoleao052/js-torch/pulse)
[![](https://img.shields.io/github/contributors/eduardoleao052/js-torch)](https://github.com/eduardoleao052/js-torch/graphs/contributors)
[![](https://img.shields.io/badge/language-JavaScript-yellow)](https://github.com/eduardoleao052/js-torch)
[![](https://img.shields.io/badge/-Email-red?style=flat-square&logo=gmail&logoColor=white)](mailto:eduardoleao052@usp.br)
[![](https://img.shields.io/badge/-Linkedin-blue?style=flat-square&logo=linkedin)](https://www.linkedin.com/in/eduardoleao052/)

PyTorch in JavaScript

  • JS-PyTorch is a Deep Learning JavaScript library built from scratch, to closely follow PyTorch's syntax.
  • This library has GPU support, using GPU.js.
  • If you want to run it yourself, check out the Documentation.
  • Try out the Web Demo!

Note: You can install the package locally with: npm install js-pytorch

Implemented Tensor Operations:

Implemented Deep Learning Layers:

1.Table of Contents

2. Installation

  • On MacOS, Windows, and Ubuntu, you can install the library with npm install js-pytorch.
  • On Windows, if you run into an error, you might need to install the latest version of Visual Studio, including the "Desktop development with C++" workload.
  • To run in the Browser, paste the following tag in the `` of your HTML file:

  • After that, you can use JS-PyTorch freely in any `

## 3. Running it Yourself

### Simple Autograd Example:

```typescript
// Require the Library if running in node (not necessary in the browser):
const { torch } = require("js-pytorch");

// Pass device as an argument to a Tensor or nn.Module (same as PyTorch):
const device = 'gpu';

// Instantiate Tensors:
let x = torch.randn([8, 4, 5]);
let w = torch.randn([8, 5, 4], true, device);
let b = torch.tensor([0.2, 0.5, 0.1, 0.0], true);

// Make calculations:
let out = torch.matmul(x, w);
out = torch.add(out, b);

// Compute gradients on whole graph:
out.backward();

// Get gradients from specific Tensors:
console.log(w.grad);
console.log(b.grad);

Complex Autograd Example (Transformer):

// Require the Library if running in node (not necessary in the browser):
const { torch } = require("js-pytorch");
const nn = torch.nn;
const optim = torch.optim;

const device = 'gpu';

// Define training hyperparameters:
const vocab_size = 52;
const hidden_size = 32;
const n_timesteps = 16;
const n_heads = 4;
const dropout_p = 0;
const batch_size = 8;

// Create Transformer decoder Module:
class Transformer extends nn.Module {
  constructor(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device) {
    super();
    // Instantiate Transformer's Layers:
    this.embed = new nn.Embedding(vocab_size, hidden_size);
    this.pos_embed = new nn.PositionalEmbedding(n_timesteps, hidden_size);
    this.b1 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);
    this.b2 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p, device);
    this.ln = new nn.LayerNorm(hidden_size);
    this.linear = new nn.Linear(hidden_size, vocab_size, device);
  }

  forward(x) {
    let z;
    z = torch.add(this.embed.forward(x), this.pos_embed.forward(x));
    z = this.b1.forward(z);
    z = this.b2.forward(z);
    z = this.ln.forward(z);
    z = this.linear.forward(z);
    return z;
  }
}

// Instantiate your custom nn.Module:
const model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p, device);

// Define loss function and optimizer:
const loss_func = new nn.CrossEntropyLoss();
const optimizer = new optim.Adam(model.parameters(), (lr = 5e-3), (reg = 0));

// Instantiate sample input and output:
let x = torch.randint(0, vocab_size, [batch_size, n_timesteps, 1]);
let y = torch.randint(0, vocab_size, [batch_size, n_timesteps]);
let loss;

// Training Loop:
for (let i = 0; i < 40; i++) {
  // Forward pass through the Transformer:
  let z = model.forward(x);

  // Get loss:
  loss = loss_func.forward(z, y);

  // Backpropagate the loss using torch.tensor's backward() method:
  loss.backward();

  // Update the weights:
  optimizer.step();

  // Reset the gradients to zero after each training step:
  optimizer.zero_grad();

  // Print loss at every iteration:
  console.log(`Iter ${i} - Loss ${loss.data[0].toFixed(4)}`)
}

Saving and Loading models:

// Instantiate your model:
const model = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);

// Train the model:
trainModel(model);

// Save model to JSON file:
torch.save(model, 'model.json')

// To load, instantiate placeHolder using the original model's architecture:
const placeHolder = new Transformer(vocab_size, hidden_size, n_timesteps, n_heads, dropout_p);

// Load weights into placeHolder:
const newModel = torch.load(placeHolder, 'model.json')

4. Distribution & Devtools

  • Build for Distribution by running npm run build. CJS and ESM modules and index.d.ts will be output in the dist/ folder.
  • Check the Code with ESLint at any time, running npm run lint.
  • Run tests run npm test.
  • Improve Code Formatting with prettier, running npm run prettier.
  • Performance Benchmarks are also included in the tests/benchmarks/ directory. Run all benchmarks with npm run bench and save new benchmarks with npm run bench:update.

5. Future Work

  • This package is not as optimized as PyTorch yet, but I tried making it more interpretable. Efficiency improvements are incoming!
  • Feel free to contribute! Create a merge request to the develop branch, and also feel free to reach out. I'll try to answer as soon as possible.
  • Hope you enjoy!
排名相邻
同类项目