Salve a tutti,
sto cercando di migliorare nel campo ML, ho svolto un esercizio consigliato tra i tutorial TensorFlow.js.
Ho provato a fare una semplice regressione tra 2 dati che ad occhio dimostrano avere una relazione piuttosto curvilinea.
Il risultato e' abbastanza soddisfacente.

Ma quella coda arancione a sinistra prima che abbia inizio la serie di dati originale (puntini blu) e' orrenda.
Quindi mi chiedo se e' possibile riuscire a far estrapolare la parte iniziale della curva delle predizioni alla rete neurale senza allargare effettivamente il dataset.
Grazie a chiunque sia interessato alla mia domanda e saluti.
Di seguito lo script (molto procedurale, mi serve solo per imparare questi meccanismi).
sto cercando di migliorare nel campo ML, ho svolto un esercizio consigliato tra i tutorial TensorFlow.js.
Ho provato a fare una semplice regressione tra 2 dati che ad occhio dimostrano avere una relazione piuttosto curvilinea.
Il risultato e' abbastanza soddisfacente.

Ma quella coda arancione a sinistra prima che abbia inizio la serie di dati originale (puntini blu) e' orrenda.
Quindi mi chiedo se e' possibile riuscire a far estrapolare la parte iniziale della curva delle predizioni alla rete neurale senza allargare effettivamente il dataset.
Grazie a chiunque sia interessato alla mia domanda e saluti.
Di seguito lo script (molto procedurale, mi serve solo per imparare questi meccanismi).
HTML:
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/axios@latest/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-vis@latest/dist/tfjs-vis.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
</head>
<body>
<h4>Tiny TFJS example<hr/></h4>
<div id="micro-out-div">Training...</div>
<script>
const data = axios.get('https://storage.googleapis.com/tfjs-tutorials/carsData.json').then(response => {
const tensors = mapData(response.data);
runModel(tensors);
});
// Create a simple model.
const model = tf.sequential();
model.add(tf.layers.dense({
units: 50,
inputShape: [1]
}));
model.add(tf.layers.dense({
units: 50,
activation: 'sigmoid'
}));
model.add(tf.layers.dense({
units: 50,
activation: 'tanh'
}));
model.add(tf.layers.dense({
units: 50,
activation: 'sigmoid'
}));
model.add(tf.layers.dense({
units: 50,
activation: 'sigmoid'
}));
model.add(tf.layers.dense({
units: 1
}));
// Prepare the model for training: Specify the loss and the optimizer.
model.compile({
optimizer: tf.train.adam(),
loss: tf.losses.meanSquaredError,
metrics: ['mse'],
});
const mapData = (data) => {
const xy = data.map(car => {
return {
x: car.Miles_per_Gallon,
y: car.Weight_in_lbs
}
});
tfvis.render.scatterplot(
{name: 'Weight_in_lbs = f(Miles_per_Gallon)'},
{values: xy},
{
xLabel: 'Miles_per_Gallon',
yLabel: 'Weight_in_lbs',
height: 300
}
);
tfvis.show.modelSummary(
{name: 'Model info'},
model
);
const Miles_per_Gallon = data.map(car => {
return car.Miles_per_Gallon
});
const Weight_in_lbs = data.map(car => {
return car.Weight_in_lbs
});
const xs = tf.tensor2d(Miles_per_Gallon, [Miles_per_Gallon.length, 1]);
const ys = tf.tensor2d(Weight_in_lbs, [Weight_in_lbs.length, 1]);
const xsmax = xs.max();
const xsmin = xs.min();
const ysmax = ys.max();
const ysmin = ys.min();
const nxs = xs.sub(xsmin).div(xsmax.sub(xsmin));
const nys = ys.sub(ysmin).div(ysmax.sub(ysmin));
const tpt = tf.linspace(0, 50, 200);
const tptmin = tpt.min();
const tptmax = tpt.max();
const ntpt = tpt.sub(tptmin).div(tptmax.sub(tptmin));
const tptds = tpt.dataSync();
return {
xy,
xs,
ys,
nxs,
nys,
xsmax,
xsmin,
ysmax,
ysmin,
Miles_per_Gallon,
Weight_in_lbs,
tpt,
tptmin,
tptmax,
ntpt,
tptds
};
}
const runModel = ({
xy,
xs,
ys,
nxs,
nys,
xsmax,
xsmin,
ysmax,
ysmin,
Miles_per_Gallon,
Weight_in_lbs,
tpt,
tptmin,
tptmax,
ntpt,
tptds
}) => {
// Train the model using the data.
model.fit(nxs, nys, {
batchSize: 32,
epochs: 150,
callbacks: tfvis.show.fitCallbacks(
{name: 'Train Performance'},
['loss','mse'],
{height: 200, callbacks: ['onEpochEnd']}
)
}).then(() => {
// Use the model to do inference on a data point the model hasn't seen.
// Should print approximately 39.
const nprediction = model.predict(ntpt);
const prediction = nprediction.mul(ysmax.sub(ysmin)).add(ysmin);
const predictionCollection = prediction.dataSync();
const predictionSeries = Array.from(tptds).map((val, i) => {
return {
x: val,
y: predictionCollection[i]
};
});
tfvis.render.scatterplot(
{name: 'Prediction quality'},
{values: [xy, predictionSeries], series: ['original','prediction']},
{
xLabel: 'Miles_per_Gallon',
yLabel: 'Weight_in_lbs',
height: 300
}
);
});
}
</script>
</body>
</html>
Allegati
Ultima modifica: