-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoftmax.html
44 lines (41 loc) · 1.54 KB
/
softmax.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
41
42
43
44
<!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" src="echarts.min.js"></script>
<script type="text/javascript">
var lr = torch.const(0.001);
var x = torch.const(1, 20);
var model = nn.Sequentail(
nn.Linear(20, 10),
nn.ReLU(),
nn.Linear(10, 3),
nn.Softmax()
);
var lossfunc = nn.MSELoss();
for (var i=0; i<10; i++) {
var y_ = model.pred(x);
console.log(y_);
var y = torch.const([0, 1, 0]);
console.log(y);
var loss = lossfunc(y_, y);
console.log(loss);
loss.backward();
console.log(model.parameters);
for (var z=0; z<model.parameters.length; z++) {
var newTensor = model.parameters[z].sub(lr.mul(model.parameters[z].grad).transpose());
for (var r=0; r<model.parameters[z].rows; r++) {
for (var c=0; c<model.parameters[z].cols; c++) {
model.parameters[z].mat[r][c] = newTensor.mat[r][c];
}
}
}
}
</script>
</body>
</html>