训练过程中未显示损失和均方误差值。没有得到预测的线

时间:2019-07-15 18:52:17

标签: regression tensorflow.js

当前,我们正在尝试输入包括一天中的时间在内的超级数据,并将票价添加到我们的TensorFlow.js模型中。我们注意到,当我们在浏览器上运行模型时,这些点会显示在散点图上,但是在训练过程中,损失和均方误差值都不会显示出来,最重要的是我们的模型没有显示预测线。

var userData = [
    {
        "City": "San Francisco",
        "Product_Type": "UberEATS Marketplace",
        "Trip_or_Order_Status": "COMPLETED",
        "Request_Time": "2019-06-16 04:10:44 +0000 UTC",
        "Begin_Trip_Time": "2019-06-16 04:44:40 +0000 UTC",
        "Begin_Trip_Lat": "37.7352602",
        "Begin_Trip_Lng": "-122.4203465",
        "Begin_Trip_Address": "",
        "Dropoff_Time": "2019-06-16 04:44:40 +0000 UTC",
        "Dropoff_Lat": "37.7352602",
        "Dropoff_Lng": "-122.4203465",
        "Dropoff_Address": "",
        "Distance_miles": "2.04",
        "Fare_Amount": "32.34",
        "Fare_Currency": "USD"
    }...]

async function getData() {

    const carsData = await userData;

    // Here we map out the values for each car and filter out the list item that do not have an day or a pay value
    const cleaned = carsData.map(car => ({

        timeInMinutes: calculateMins(car.Request_Time),
        pay_rate: normalizeUberPrice(car.Distance_miles, car.Fare_Amount),
    }))
        .filter(car => (car.day != null && car.pay != null));
    return cleaned;
}

async function run() {

    const data = await getData();


    const values = data.map(d => ({
        x: d.day,
        y: d.pay,
    }));


    tfvis.render.scatterplot(
        { name: 'Horsepower v MPG' },
        { values },
        {
            xAxisDomain: [0, 1600],
            yAxisDomain: [0,10],
            xLabel: 'Day',
            yLabel: 'Pay',
            height: 300
        }
    );

    const model = createModel();
    tfvis.show.modelSummary({ name: 'Model Summary' }, model);

    // Convert the data to a form we can use for training.
    const tensorData = convertToTensor(data);
    console.log(tensorData)
    const { inputs, labels } = tensorData;

    // Train the model  
    await trainModel(model, inputs, labels);
    console.log('Done Training');

    testModel(model, data, tensorData);
}


function createModel() {

    const model = tf.sequential();


    model.add(tf.layers.dense({ inputShape: [1], units: 25, useBias: true }));


    model.add(tf.layers.dense({ units: 50, activation: "sigmoid" }));




    model.add(tf.layers.dense({ units: 1, useBias: true }));

    return model;
}

function convertToTensor(data) {

    return tf.tidy(() => {


        tf.util.shuffle(data);


        const inputs = data.map(d => d.pay)
        const labels = data.map(d => d.day);

        const inputTensor = tf.tensor2d(inputs, [inputs.length, 1]);
        const labelTensor = tf.tensor2d(labels, [labels.length, 1]);

        //Step 3. Normalize the data to the range 0 - 1 using min-max scaling
        const inputMax = inputTensor.max();
        const inputMin = inputTensor.min();
        const labelMax = labelTensor.max();
        const labelMin = labelTensor.min();

        const normalizedInputs = inputTensor.sub(inputMin).div(inputMax.sub(inputMin));
        const normalizedLabels = labelTensor.sub(labelMin).div(labelMax.sub(labelMin));

        return {

            inputs: normalizedInputs,
            labels: normalizedLabels,
            // Return the min/max bounds so we can use them later.
            inputMax,
            inputMin,
            labelMax,
            labelMin,
        }
    });
}

async function trainModel(model, inputs, labels) {

    model.compile({
        optimizer: tf.train.adam(),
        loss: tf.losses.meanSquaredError,
        metrics: ['mse'],
    });

    const batchSize = 32;
    const epochs = 30;



        callbacks: tfvis.show.fitCallbacks(
            { name: 'Training Performance' },
            ['loss', 'mse'],
            { 
                xAxisDomain: [0, 100],
                yAxisDomain: [0,1],
                height: 200, 
                callbacks: ['onEpochEnd'] }
                // ',onBatchEnd'
        ),
        history: tfvis.show.history({
            name: 'History'},
            history,
            ["loss","mse"])

    });
}

function testModel(model, inputData, normalizationData) {
    const { inputMax, inputMin, labelMin, labelMax } = normalizationData;


    const [xs, preds] = tf.tidy(() => {


        const xs = tf.linspace(0, 1, 100);


        const preds = model.predict(xs.reshape([100, 1]));


        const unNormXs = xs
            .mul(inputMax.sub(inputMin))
            .add(inputMin);

        const unNormPreds = preds
            .mul(labelMax.sub(labelMin))
            .add(labelMin);

        return [unNormXs.dataSync(), unNormPreds.dataSync()];
    });


    const predictedPoints = Array.from(xs).map((val, i) => {
        return { x: val, y: preds[i] }
    });

    const originalPoints = inputData.map(d => ({
        x: d.pay, y: d.day,
    }));
    console.log("ORIGINAL POINTS:")
    console.log(originalPoints)


    tfvis.render.scatterplot(
        { name: 'Model Predictions vs Original Data' },
        { values: [originalPoints, predictedPoints], series: ['original', 'predicted'] },
        {
            xAxisDomain: [0,10],
            yAxisDomain: [0,1600],
            xLabel: 'Horsepower',
            yLabel: 'MPG',
            height: 1000
        }
    );
}


document.addEventListener('DOMContentLoaded', run);

基本上,我们希望看到数据的预测线,但什么也得不到。

当我们使用这样的数据时,它会起作用:

var userData = [{
          day: 1
          pay: 20
       },...]

1 个答案:

答案 0 :(得分:0)

数据处理不正确。因此,用于预测的值包含NaNInfinity。结果,由model.fit计算出的误差为NaN,因此无法显示在tfjs-vis的图表上。

过滤

.filter(car => (car.day != null && car.pay != null)); 

没有删除NaN和Infinity。而是可以使用以下条件:

.filter(car => isFinite(car.pay + car.day) && !isNaN(car.pay + car.day)); 

尽管在NaN的值中找到了Infinitycar.day,但是这里对car.paycar.day进行了常规过滤-因此其他操作-确保这些值不会出现在清除的数据中的任何地方。

here,您可以查看如何显示损失。