-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.html
41 lines (40 loc) · 1.53 KB
/
test1.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
<!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 x = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
var model = nn.Sequentail(
nn.Linear(10, 5),
nn.ReLU(),
nn.Linear(5, 1),
nn.Sigmoid()
);
var lossfc = nn.MSELoss();
var lr = torch.const(1);
var y = torch.tensor([1]);
for (var i=0; i<10; i++) {
var y_ = model.pred(x);
console.log(y_);
var loss = lossfc(y_, y);
console.log(loss.head.mat[0][0]);
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>