# @tensorflow/tfjs-data

> TensorFlow Data API in JavaScript

Latest version **4.22.0** (published 2024-10-21) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @tensorflow/tfjs-data
pnpm add @tensorflow/tfjs-data
yarn add @tensorflow/tfjs-data
bun add @tensorflow/tfjs-data
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score; popular repo.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.22.0 |
| Published | 2024-10-21 |
| First published | 2018-11-20 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 3.6 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 19139 |
| Maintainers | delhibabu, laxma4675, fengwuyao, linchan, pyu10055, caisq, annxingyuan, linazhao128, mattsoulanille, jinjingforever |

## Links

- npm: https://www.npmjs.com/package/@tensorflow/tfjs-data
- Repository: https://github.com/tensorflow/tfjs
- Homepage: https://github.com/tensorflow/tfjs#readme
- Issues: https://github.com/tensorflow/tfjs/issues
- npm.io page: https://npm.io/package/@tensorflow/tfjs-data

## Dependencies (3)

- [node-fetch](https://npm.io/package/node-fetch.md) ~2.6.1
- [string_decoder](https://npm.io/package/string_decoder.md) ^1.3.0
- [@types/node-fetch](https://npm.io/package/@types/node-fetch.md) ^2.1.2

## Recent versions

- 4.22.0 (latest) — 2024-10-21
- 4.23.0-rc.0 (next) — 2025-01-08
- 4.22.0-rc.0 — 2024-10-14
- 4.21.0 — 2024-09-03
- 4.21.0-rc.0 — 2024-08-26
- 4.20.0 — 2024-06-03
- 4.20.0-rc.0 — 2024-05-28
- 4.19.0 — 2024-04-30
- 4.19.0-rc.0 — 2024-04-24
- 4.18.0 — 2024-04-17
- 4.18.0-rc.0 — 2024-04-11
- 4.17.0 — 2024-01-30
- 4.17.0-rc.0 — 2024-01-23
- 4.16.0 — 2024-01-11
- 4.16.0-rc.0 — 2024-01-09
- … 112 more at https://npm.io/package/@tensorflow/tfjs-data/versions

## README

# 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](https://www.tensorflow.org/guide/data) on the
Python/C++ side.  TF.js Data will match the tf.data API to the extent possible.

To keep track of issues we use the [tensorflow/tfjs](https://github.com/tensorflow/tfjs/issues?q=is%3Aissue+is%3Aopen+label%3Acomp%3Adata) Github repo with `comp:data` tag.

## Importing

There are two ways to import TensorFlow.js Data

1. You can access TensorFlow.js Data through the union package: [@tensorflow/tfjs](https://www.npmjs.com/package/@tensorflow/tfjs)
2. You can get TensorFlow.js Data as a module:
   [@tensorflow/tfjs-data](https://www.npmjs.com/package/@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

```js
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() {
  // 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);
      }
    }
  });
}

run().then(() => console.log('Done'));
```

## For more information

- [TensorFlow.js API documentation](https://js.tensorflow.org/api/latest/)
- [TensorFlow.js Tutorials](https://js.tensorflow.org/tutorials/)

---
_Source: https://npm.io/package/@tensorflow/tfjs-data · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
