• Create a CSVDataset by reading and decoding CSV file(s) from provided URL or local path if it's in Node environment.

    Note: If isLabel in columnConfigs is true for at least one column, the element in returned CSVDataset will be an object of {xs:features, ys:labels}: xs is a dict of features key/value pairs, ys is a dict of labels key/value pairs. If no column is marked as label, returns a dict of features only.

    const csvUrl =
    'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';

    async function run() {
    // We want to predict the column "medv", which represents a median value of
    // a home (in $1000s), so we mark it as a label.
    const csvDataset = tf.data.csv(
    csvUrl, {
    columnConfigs: {
    medv: {
    isLabel: true
    }
    }
    });

    // Number of features is the number of column names minus one for the label
    // column.
    const numOfFeatures = (await csvDataset.columnNames()).length - 1;

    // Prepare the Dataset for training.
    const flattenedDataset =
    csvDataset
    .map(({xs, ys}) =>
    {
    // Convert xs(features) and ys(labels) from object form (keyed by
    // column name) to array form.
    return {xs:Object.values(xs), ys:Object.values(ys)};
    })
    .batch(10);

    // Define the model.
    const model = tf.sequential();
    model.add(tf.layers.dense({
    inputShape: [numOfFeatures],
    units: 1
    }));
    model.compile({
    optimizer: tf.train.sgd(0.000001),
    loss: 'meanSquaredError'
    });

    // Fit the model using the prepared Dataset
    return model.fitDataset(flattenedDataset, {
    epochs: 10,
    callbacks: {
    onEpochEnd: async (epoch, logs) => {
    console.log(epoch + ':' + logs.loss);
    }
    }
    });
    }

    await run();

    Parameters

    • source: RequestInfo

      URL or local path to get CSV file. If it's a local path, it must have prefix file:// and it only works in node environment.

    • Optional csvConfig: CSVConfig

      (Optional) A CSVConfig object that contains configurations of reading and decoding from CSV file(s).

    Returns CSVDataset

    Doc

Generated using TypeDoc