-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmax2.html
40 lines (38 loc) · 1.31 KB
/
softmax2.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html style="height: 100%">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="torch.js"></script>
<script type="text/javascript" src="mnist.js"></script>
</head>
<body style="height: 100%; margin: 0">
<div id="container" style="height: 100%"></div>
<script type="text/javascript">
function softmax(x) {
let exp = torch.const(Math.E).pow(torch.tensor(x));
let sum = exp.reduce_sum();
let func = torch.function(exp.div(sum));
func.name = "softmax";
return func;
}
var lr = torch.tensor(0.001);
var x = torch.const(1, 20);
var model = nn.Sequentail(
nn.Linear(20, 10),
nn.Linear(10, 3),
nn.Softmax(),
);
var y_ = model.pred(x);
var y = torch.const([1, 0, 0]);
// console.log("y_");
// console.log(y_);
// console.log(y);
var loss = torch.function(torch.tensor(y_).sub(y), "loss");
// console.log('loss_tensor');
// console.log(loss.head.transpose());
loss.backward();
console.log('grad');
console.log(model.parameters);
</script>
</body>
</html>