-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorchjs_Softmax.html
145 lines (137 loc) · 6.24 KB
/
pytorchjs_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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!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, 3),
nn.Softmax()
);
var lossfc = nn.MSELoss();
var lr = torch.const(0.005);
for (var j=0; j<5; j++) {
for (var i=0; i<1000; 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([1, 0, 0]);
// console.log(y);
var loss = lossfc(y_, y);
// console.log("loss:");
// console.log(loss);
loss.backward();
// console.log('lable:' + labels[i]);
// 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];
}
}
}
if (i%100 == 0) {
console.log("iter:" + j + ' , '+ i +": " + loss.head.mat[0][0]);
option.xAxis.data.push((j*1000)+i)
option.series[0].data.push(loss.head.mat[0][0])
}
}
else if (labels[i] == 1) {
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, 1, 0]);
// console.log(y);
var loss = lossfc(y_, y);
// console.log("loss:");
// console.log(loss);
loss.backward();
// console.log(model.parameters);
// console.log('lable:' + labels[i]);
// 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];
}
}
}
if (i%100 == 0) {
console.log("iter:" + j + ' , '+ i +": " + loss.head.mat[0][1]);
option.xAxis.data.push((j*1000)+i)
option.series[0].data.push(loss.head.mat[0][1])
}
} else if (labels[i] == 2) {
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, 0, 1]);
// console.log(y);
var loss = lossfc(y_, y);
// console.log("loss:");
// console.log(loss);
loss.backward();
// console.log(model.parameters);
// console.log('lable:' + labels[i]);
// 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];
}
}
}
if (i%100 == 0) {
console.log("iter:" + j + ' , '+ i +": " + loss.head.mat[0][2]);
option.xAxis.data.push((j*1000)+i)
option.series[0].data.push(loss.head.mat[0][2])
}
}
}
}
if (option && typeof option === "object") {
myChart.setOption(option, true);
}
</script>
</body>
</html>