-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (146 loc) · 4.02 KB
/
Copy pathscript.js
File metadata and controls
169 lines (146 loc) · 4.02 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
let model;
let stopTraining;
async function getData() {
const dataHouseR = await fetch("data.json");
const dataHouse = await dataHouseR.json();
let cleanedData = dataHouse.map((house) => ({
precio: house.Precio,
cuartos: house.NumeroDeCuartosPromedio,
}));
cleanedData = cleanedData.filter(
(house) => house.precio != null && house.cuartos != null
);
return cleanedData;
}
async function lookAtInferenceCurve() {
let data = await getData();
let tensorData = await convertDataTensor(data);
const { inputsMax, inputsMin, labelMin, labelMax } = tensorData;
const [xs, preds] = tf.tidy(() => {
const xs = tf.linspace(0, 1, 100);
const preds = model.predict(xs.reshape([100, 1]));
const desnormX = xs.mul(labelMax.sub(labelMin)).add(labelMin);
const desnormY = preds.mul(labelMax.sub(labelMin)).add(labelMin);
return [desnormX.dataSync(), desnormY.dataSync(), desnormY.dataSync()];
});
const predictionPoints = Array.from(xs).map((val, i) => {
return { x: val, y: preds[i] };
});
const originalPoints = data.map((d) => ({
x: d.cuartos,
y: d.precio,
}));
tfvis.render.scatterplot(
{ name: "predictions vs originals" },
{ values: [originalPoints, predictionPoints], series: [] },
{
xLabel: "Cuartos",
yLabel: "Precio",
height: 300,
}
);
}
async function uploadModel() {
const uploadJSONInput = document.getElementById("upload-json");
const uploadWeightsInput = document.getElementById("upload-weights");
model = await tf.loadLayersModel(
tf.io.browserFiles([uploadJSONInput.files[0], uploadWeightsInput.files[0]])
);
console.log("modelo cargado");
}
function visualizeData(data) {
const values = data.map((d) => ({
x: d.cuartos,
y: d.precio,
}));
tfvis.render.scatterplot(
{ name: "Cuartos vs Precio" },
{ values: values },
{ xLabel: "Cuartos", yLabel: "Precio", height: 300 }
);
}
function createModel() {
const model = tf.sequential();
model.add(
tf.layers.dense({
inputShape: [1],
units: 1,
useBias: true,
})
);
model.add(
tf.layers.dense({
units: 1,
useBias: true,
})
);
return model;
}
const optimizer = tf.train.adam();
const loss_function = tf.losses.meanSquaredError;
const metric = ["mse"];
async function trainModel(model, inputs, labels) {
model.compile({
optimizer: optimizer,
loss: loss_function,
metrics: metric,
});
const surface = { name: "show.history live", tab: "Training" };
const sizeBatch = 28;
const epochs = 50;
const history = [];
return await model.fit(inputs, labels, {
sizeBatch,
epochs,
shuffle: true,
callbacks: {
onEpochEnd: (epoch, log) => {
history.push(log);
tfvis.show.history(surface, history, ["loss", "mse"]);
if (stopTraining) {
model.stopTraining = true;
}
},
},
});
}
async function saveModel() {
const saveResult = await model.save("downloads://model-regresion");
return saveResult;
}
function convertDataTensor(data) {
return tf.tidy(() => {
tf.util.shuffle(data);
const inputs = data.map((d) => d.cuartos);
const label = data.map((d) => d.precio);
const tensorInput = tf.tensor2d(inputs, [inputs.length, 1]);
const tensorLabel = tf.tensor2d(label, [label.length, 1]);
const inputsMax = tensorInput.max();
const inputsMin = tensorInput.min();
const labelMax = tensorLabel.max();
const labelMin = tensorLabel.min();
const inputsNormalized = tensorInput
.sub(inputsMin)
.div(inputsMax.sub(inputsMin));
const labelNormalized = tensorLabel
.sub(labelMin)
.div(labelMax.sub(inputsMax));
return {
inputs: inputsNormalized,
label: labelNormalized,
inputsMax,
inputsMin,
labelMax,
labelMin,
};
});
}
async function run() {
const data = await getData();
visualizeData(data);
model = createModel();
const tensorData = convertDataTensor(data);
const { inputs, label } = tensorData;
trainModel(model, inputs, label);
}
run();