{"id":15158910,"url":"https://github.com/tensorflow/tfjs-tsne","last_synced_at":"2025-09-30T09:30:37.224Z","repository":{"id":54420769,"uuid":"133990389","full_name":"tensorflow/tfjs-tsne","owner":"tensorflow","description":null,"archived":true,"fork":false,"pushed_at":"2021-02-19T00:58:10.000Z","size":35663,"stargazers_count":314,"open_issues_count":26,"forks_count":58,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-01-02T00:24:12.289Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tensorflow.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-05-18T18:26:53.000Z","updated_at":"2024-10-10T20:33:20.000Z","dependencies_parsed_at":"2022-08-13T15:10:23.305Z","dependency_job_id":null,"html_url":"https://github.com/tensorflow/tfjs-tsne","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Ftfjs-tsne","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Ftfjs-tsne/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Ftfjs-tsne/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Ftfjs-tsne/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tensorflow","download_url":"https://codeload.github.com/tensorflow/tfjs-tsne/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234722054,"owners_count":18876896,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-26T21:01:24.888Z","updated_at":"2025-09-30T09:30:28.808Z","avatar_url":"https://github.com/tensorflow.png","language":"TypeScript","readme":"## tSNE for TensorFlow.js\n\nThis library contains a improved tSNE implementation that runs in the browser.\n\n\n## Installation \u0026 Usage\n\nYou can use tfjs-tsne via a script tag or via NPM\n\n### Script tag\n\nTo use tfjs-tsne via script tag you need to load tfjs first. The following tags\ncan be put into the head section of your html page to load the library.\n\n```html\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.14.1\"\u003e\u003c/script\u003e\n\u003cscript src=\"https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-tsne\"\u003e\u003c/script\u003e\n```\n\nThis library will create a `tsne` variable on the global scope.\nYou can then do the following\n\n```js\n// Create some data\nconst data = tf.randomUniform([2000,10]);\n\n// Get a tsne optimizer\nconst tsneOpt = tsne.tsne(data);\n\n// Compute a T-SNE embedding, returns a promise.\n// Runs for 1000 iterations by default.\ntsneOpt.compute().then(() =\u003e {\n  // tsne.coordinate returns a *tensor* with x, y coordinates of\n  // the embedded data.\n  const coordinates = tsneOpt.coordinates();\n  coordinates.print();\n}) ;\n```\n\n### Via NPM\n\n```\nyarn add @tensorflow/tfjs-tsne\n```\nor\n```\nnpm install @tensorflow/tfjs-tsne\n```\n\nThen\n\n```js\nimport * as tsne from '@tensorflow/tfjs-tsne';\n\n// Create some data\nconst data = tf.randomUniform([2000,10]);\n\n// Initialize the tsne optimizer\nconst tsneOpt = tsne.tsne(data);\n\n// Compute a T-SNE embedding, returns a promise.\n// Runs for 1000 iterations by default.\ntsneOpt.compute().then(() =\u003e {\n  // tsne.coordinate returns a *tensor* with x, y coordinates of\n  // the embedded data.\n  const coordinates = tsneOpt.coordinates();\n  coordinates.print();\n}) ;\n```\n\n## API\n\n### tsne.tsne(data: tf.Tensor2d, config?: TSNEConfiguration)\n\nCreates and returns a TSNE optimizer.\n\n- `data` must be a Rank 2 tensor. Shape is [numPoints, dataPointDimensions]\n- `config` is an optional object with the following params (all are optional):\n  - perplexity: number — defaults to 18. Max value is defined by hardware limitations.\n  - verbose: boolean — defaults to false\n  - exaggeration: number — defaults to 4\n  - exaggerationIter: number — defaults to 300\n  - exaggerationDecayIter: number — defaults to 200\n  - momentum: number — defaults to 0.8\n\n### .compute(iterations: number): Promise\u003cvoid\u003e\n\nThe most direct way to get a tsne projection. Automatically runs the knn preprocessing\nand the tsne optimization. Returns a promise to indicate when it is done.\n\n- iterations the number of iterations to run the tsne optimization for. (The number of knn steps is automatically calculated).\n\n### .iterateKnn(iterations: number): Promise\u003cvoid\u003e\n\nWhen running tsne iteratively (see section below). This runs runs the knn preprocessing\nfor the specified number of iterations.\n\n### .iterate(iterations: number): Promise\u003cvoid\u003e\n\nWhen running tsne iteratively (see section below). This runs the tsne step for the specified number of iterations.\n\n### .coordinates(normalize: boolean): tf.Tensor\n\nGets the current x, y coordinates of the projected data as a tensor. By \ndefault the coordinates are normalized to the range 0-1.\n\n### .coordsArray(normalize: boolean): Promise\u003cnumber[][]\u003e\n\nGets the current x, y coordinates of the projected data as a JavaScript array.\nBy default the coordinates are normalized to the range 0-1. This function is\nasync and returns a promise.\n\n### Computing tSNE iteratively\n\nWhile the `.compute` method provides the most direct way to get an embedding. You can also compute the embedding iteratively and have more control over the process.\n\nThe first step is computing the KNN graph using iterateKNN.\n\nThen you can compute the tSNE iteratively and examine the result as it evolves.\n\nThe code below shows what that would look like\n\n```javascript\nconst data = tf.randomUniform([2000,10]);\nconst tsne = tf_tsne.tsne(data);\n\nasync function iterativeTsne() {\n  // Get the suggested number of iterations to perform.\n  const knnIterations = tsne.knnIterations();\n  // Do the KNN computation. This needs to complete before we run tsne\n  for(let i = 0; i \u003c knnIterations; ++i){\n    await tsne.iterateKnn();\n    // You can update knn progress in your ui here.\n  }\n\n  const tsneIterations = 1000;\n  for(let i = 0; i \u003c tsneIterations; ++i){\n    await tsne.iterate();\n    // Draw the embedding here...\n    const coordinates = tsne.coordinates();\n    coordinates.print();\n  }\n}\n\niterativeTsne();\n```\n\n### Example\n\nWe also have an [example](https://github.com/tensorflow/tfjs-examples/tree/master/tsne-mnist-canvas) of using this library to perform TSNE on the MNIST dataset [here](https://github.com/tensorflow/tfjs-examples/tree/master/tsne-mnist-canvas).\n\n### Limitations\n\nThis library requires WebGL 2 support and thus will not work on certain devices, mobile devices especially. Currently it best works on desktop devices.\n\nFrom our current experiments we suggest limiting the data size passed to this implementation\nto data with a shape of [10000,100], i.e. up to 10000 points with 100 dimensions each. You can do more but it might slow down.\n\nAbove a certain number of data points the computation of the similarities becomes a bottleneck, a problem that we plan to address in the future.\n\n\n### Implementation\nThis work makes use of [linear tSNE optimization](https://arxiv.org/abs/1805.10817) for the optimization of the embedding and an optimized brute force computation of the kNN graph in the GPU.\n\n### Reference\nReference to cite if you use this implementation in a research paper:\n\n```\n@article{TFjs:tSNE,\n  author = {Nicola Pezzotti and Alexander Mordvintsev and Thomas Hollt and Boudewijn P. F. Lelieveldt and Elmar Eisemann and Anna Vilanova},\n  title = {Linear tSNE Optimization for the Web},\n  year = {2018},\n  journal={arXiv preprint arXiv:1805.10817},\n}\n```\n","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftensorflow%2Ftfjs-tsne","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftensorflow%2Ftfjs-tsne","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftensorflow%2Ftfjs-tsne/lists"}