
Research
Malicious npm Packages Impersonate Flashbots SDKs, Targeting Ethereum Wallet Credentials
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
This is a javascript implementation of autodiff translated from micrograd by Adrej Karpathy. Then built upon this to build a torch like neural network composition
This translates Andrej Karpathy's micrograd into javascript and modifies the neural network composition to look more like pytorch. Thank you to Andrej!
First install from npm
npm install js-grad
Then import from package in javascript
import { autodiff, nn } from "js-grad";
or
const { autodiff, nn } = require("js-grad");
in node.
This is a simple example of linear regression. You can see this live in the console at code sandbox
import { autodiff, nn } from "js-grad";
class LinearRegression extends nn.layers.FeedForward {
constructor() {
super();
this.model = new nn.layers.Linear(1, 1);
}
forward(X) {
return this.model.forward(X);
}
parameters() {
return this.model.parameters();
}
}
function createLinearVector(length) {
return {
X: new Array(length).fill(0).map((_, i) => [new autodiff.Value(i)]),
y: new Array(length).fill(0).map((_, i) => [new autodiff.Value(i)])
};
}
function main() {
const numDataPoints = 10;
const { X, y } = createLinearVector(numDataPoints);
const learningRate = 0.01,
epochs = 15; // hyper params
// create model, how to evaluate(loss) and optimize model
const model = new LinearRegression();
const loss = new nn.losses.MSE();
const optimizer = new nn.optimizers.SGD(model.parameters(), learningRate);
for (let epoch = 1; epoch <= epochs; epoch++) {
// forward pass
const outputs = model.forward(X);
const totalLoss = loss.forward(outputs, y);
// backward pass and optimize
optimizer.zeroGrad();
totalLoss.backward();
optimizer.step();
// log metrics
console.log(`EPOCH=${epoch} \t LOSS=${totalLoss.data}`);
}
}
main(); // run the main program for linear regression
import { autodiff, nn } from "js-grad";
// destructure necessary components
const { Value } = autodiff;
const {
layers: { Sequential, Linear, ReLU, FeedForward },
losses: { MSE },
optimizers: { SGD }
} = nn;
class NeuralNetwork extends FeedForward {
constructor() {
super();
this.model = new Sequential(
new Linear(1, 8),
new ReLU(),
new Linear(8, 8),
new ReLU(),
new Linear(8, 1)
);
}
forward(X) {
return this.model.forward(X);
}
parameters() {
return this.model.parameters();
}
}
function generate2D(start, stop, interval, func) {
let X = [],
y = [];
for (let i = start; i < stop; i += interval) {
X.push([new Value(i)]);
y.push([new Value(func(i))]);
}
return { X, y };
}
function main() {
const { X, y } = generate2D(0, 2 * Math.PI, Math.PI / 16, Math.sin);
console.log(X.length, y.length);
const model = new NeuralNetwork();
const epochs = 10000;
const learningRate = 0.01;
const loss = new MSE();
const optimizer = new SGD(model.parameters(), learningRate);
// training
for (let epoch = 0; epoch < epochs; epoch++) {
//forward
const outputs = model.forward(X);
const totalLoss = loss.forward(outputs, y);
//backward
optimizer.zeroGrad(); // clear gradient
totalLoss.backward();
// gradient descent
optimizer.step();
console.log(`Epoch=${epoch + 1} \t Loss=${totalLoss.data}`);
}
}
main();
FAQs
This is a javascript implementation of autodiff translated from micrograd by Adrej Karpathy. Then built upon this to build a torch like neural network composition
The npm package js-grad receives a total of 0 weekly downloads. As such, js-grad popularity was classified as not popular.
We found that js-grad demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Four npm packages disguised as cryptographic tools steal developer credentials and send them to attacker-controlled Telegram infrastructure.
Security News
Ruby maintainers from Bundler and rbenv teams are building rv to bring Python uv's speed and unified tooling approach to Ruby development.
Security News
Following last week’s supply chain attack, Nx published findings on the GitHub Actions exploit and moved npm publishing to Trusted Publishers.