Edit: Great work! I'd love to have a nice alternative to PyTorch in JavaScript :)
Edit: formatting
Making JavaScript look like Python in this case could potentially bite you in the ass.
From the example:
const torch = require("js-pytorch");
// Instantiate Tensors:
x = torch.randn([8,4,5]);
w = torch.randn([8,5,4], requires_grad = true);
b = torch.tensor([0.2, 0.5, 0.1, 0.0], requires_grad = true);
And:
class Transformer extends nn.Module {
constructor(vocab_size, hidden_size, n_timesteps, n_heads, p) {
//.....
this.b1 = new nn.Block(hidden_size, hidden_size, n_heads, n_timesteps, dropout_p=p);
For both `requires_grad` and `dropout_p` you wouldn't be able to change the ordering + you're creating global variables.
/**
* All of the arguments for this function are positional
* and cannot be provided in a different order than defined
*/
function performOperation(values, arg1 = false, arg2 = -1) {
//....
}
/**
* This works, but only because of the order
*/
const result = performOperation([1, 2, 3], arg1 = true, arg2 = 10);
/**
* This does not work
*/
const result = performOperation([1, 2, 3], arg2 = 10, arg1 = true);
/**
\* What is actually happening
\*/
arg1 = true; // global variable
arg2 = 10; // global variable
const result = performOperation([1, 2, 3], 10, true);
Thats true, it’s a limitation of working between these languages. I tried to mitigate it by using clear JSDoc, so that each variable pops up alongside an explanation when calling a function.
I feel you - Python has much better (more flexible) argument support than JavaScript in this case. Converting the entire set of arguments into a keyed object is usually what happens, but then it wouldn't look like PyTorch anymore.
Edit: formatting
Making JavaScript look like Python in this case could potentially bite you in the ass.
From the example:
And: For both `requires_grad` and `dropout_p` you wouldn't be able to change the ordering + you're creating global variables.