-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorchjs_FCN.html
114 lines (109 loc) · 4.66 KB
/
pytorchjs_FCN.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!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 dom = document.getElementById("container");
var myChart = echarts.init(dom);
var app = {};
option = null;
option = {
xAxis: {
type: 'category',
data: []
},
yAxis: {
type: 'value'
},
series: [{
data: [],
type: 'line'
}]
};
;
// for (var c=0; c<images.length; c++) {
// for (var n=0; n<images[c].length; n++) {
// images[c][n] = images[c][n] / 255;
// }
// }
var model = nn.Sequentail(
nn.Linear(784, 128),
// nn.ReLU(),
nn.Linear(128, 1),
nn.Sigmoid()
);
var lossfc = nn.MSELoss();
var lr = torch.const(0.0001);
for (var j=0; j<5; j++) {
// var j = 0;
for (var i=0; i<100; i++) {
if (labels[i] == 0) {
var x = torch.const(images[i]);
// console.log(x);
var y_ = model.pred(x);
// console.log(labels[i]);
// console.log(y_);
var y = torch.const(0);
// console.log(y);
var loss = lossfc(y_, y);
// console.log("loss:");
// console.log(loss);
loss.backward();
// console.log(model.parameters);
// console.log(lr.mul(model.parameters[0].grad).transpose());
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];
}
}
}
if (i%10 == 0) {
console.log("iter:" + j + ' , '+ i +": " + loss.head.mat);
option.xAxis.data.push((j*1000)+i)
option.series[0].data.push(loss.head.mat)
}
}
else if (labels[i] == 1) {
var x = torch.tensor(images[i]);
// console.log(x);
var y_ = model.pred(x);
// console.log(labels[i]);
// console.log(y_);
var y = torch.tensor(1);
var loss = lossfc(y_, y);
// if (loss.head.mat[0][0] < 0) {
// loss.head.mat[0][0] = -loss.head.mat[0][0];
// }
// console.log(loss);
loss.backward();
for (var z=0; z<model.parameters.length; z++) {
var newTensor = model.parameters[z].add(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];
}
}
}
// console.log(model.parameters);
if (i%10 == 0) {
console.log("iter:" + j + ' , '+ i +": " + loss.head.mat);
option.xAxis.data.push((j*1000)+i)
option.series[0].data.push(loss.head.mat)
}
}
}
}
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>