TensorFlow.js Data
This repo is under active development and is not production-ready. We are
actively developing as an open source project.
TensorFlow.js Data provides simple APIs to load and parse data from disk or over
the web in a variety of formats, and to prepare that data for use in machine
learning models (e.g. via operations like filter, map, shuffle, and batch).
This project is the JavaScript analogue of
tf.data on the
Python/C++ side. TF.js Data will match the tf.data API to the extent possible.
Importing
There are two ways to import TensorFlow.js Data
- You can access TensorFlow.js Data through the union package: @tensorflow/tfjs
- You can get TensorFlow.js Data as a module:
@tensorflow/tfjs-data.
Note that
tfjs-data
has peer dependency on tfjs-core, so if you import
@tensorflow/tfjs-data
, you also need to import
@tensorflow/tfjs-core
.
Sample Usage
Reading a CSV file
import * as tf from '@tensorflow/tfjs';
const csvUrl = 'https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv';
async function run() {
const csvDataset = tf.data.csv(
csvUrl, {
columnConfigs: {
medv: {
isLabel: true
}
}
});
const numOfFeatures = (await csvDataset.columnNames()).length - 1;
const flattenedDataset =
csvDataset
.map(([rawFeatures, rawLabel]) =>
[Object.values(rawFeatures), Object.values(rawLabel)])
.batch(10);
const model = tf.sequential();
model.add(tf.layers.dense({
inputShape: [numOfFeatures],
units: 1
}));
model.compile({
optimizer: tf.train.sgd(0.000001),
loss: 'meanSquaredError'
});
return model.fitDataset(flattenedDataset, {
epochs: 10,
callbacks: {
onEpochEnd: async (epoch, logs) => {
console.log(epoch, logs.loss);
}
}
});
}
run().then(() => console.log('Done'));
For more information