{"id":15158758,"url":"https://github.com/tensorflow/java-ndarray","last_synced_at":"2025-07-15T17:38:38.238Z","repository":{"id":43123958,"uuid":"349488998","full_name":"tensorflow/java-ndarray","owner":"tensorflow","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-10T19:14:10.000Z","size":687,"stargazers_count":79,"open_issues_count":3,"forks_count":18,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-07-12T00:44:53.488Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","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":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-19T16:34:02.000Z","updated_at":"2025-06-25T03:46:33.000Z","dependencies_parsed_at":"2024-05-10T20:28:06.143Z","dependency_job_id":null,"html_url":"https://github.com/tensorflow/java-ndarray","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tensorflow/java-ndarray","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Fjava-ndarray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Fjava-ndarray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Fjava-ndarray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Fjava-ndarray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tensorflow","download_url":"https://codeload.github.com/tensorflow/java-ndarray/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tensorflow%2Fjava-ndarray/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265450682,"owners_count":23767671,"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:02.542Z","updated_at":"2025-07-15T17:38:38.192Z","avatar_url":"https://github.com/tensorflow.png","language":"Java","readme":"# NdArray Java Library\n\n## Introduction\n\nNdArray is a library exposing utilities for manipulating data in a n-dimensional space in Java. \nUnlike other Java artifacts distributed by TensorFlow, this library does not depend on the TensorFlow\nruntime, therefore is very lightweight and can be used by any kind of Java project.\n\nTo import the NdArray library in your project, simply add the following dependency:\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eorg.tensorflow\u003c/groupId\u003e\n  \u003cartifactId\u003endarray\u003c/artifactId\u003e\n  \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n### Data Buffers\n\nInstances of `DataBuffer` map contiguous segments of memory with 64-bits indexing and supports \ngeneric parametrization while still allowing direct access to primitive types. Such segments \ncould be standard Java arrays, JDK NIO buffers or native memory. In addition, it can serialize and \ndeserialize data of any type (and not only primitive types, as with `java.util.nio`).\n\n```java\n// Allocate a buffer of 4K int values\nIntDataBuffer bufferA = DataBuffers.ofInts(4096L);\nassertEquals(4096L, bufferA.size());\n\n// Write an int array at the beginning of the buffer\nbufferA.write(new int[] { 1, 2, 3 });\nassertEquals(3, bufferA.getInt(2));\n\n// Slice buffer after its first value\nIntDataBuffer bufferB = bufferA.offset(1);\nassertEquals(4095L, bufferB.size());\nassertEquals(2, bufferB.getInt(0));\n\n// Resize a buffer to 10 elements\nIntDataBuffer bufferC = bufferB.narrow(10);\nassertEquals(10L, bufferB.size());\nassertEquals(2, bufferB.getInt(0));\n```\n\n### ND Arrays\n\nInstances of `NdArray` are used to view memory segments stored in a `DataBuffer` as a \nmultidimensional arrays and to provide an API for traversing, reading, writing and slicing\ntheir data. The goal of these tools is to replace the usage of standard multidimensional Java arrays \n(e.g. `new int[][][]`) since those results in slow performances, from the non-contiguous \nstorage of their data and the multiple dereferences required to access their values. \n\n```java\n// Allocating a 3D matrix of 2x3x2\nIntNdArray matrix3d = NdArrays.ofInts(Shape.of(2, 3, 2));\nassertEquals(3, matrix3d.rank());\n\n// Initializing 3D matrix data with vectors from the first dimension (index 0)\nmatrix3d.elements(0).forEach(matrix -\u003e {\n    assertEquals(2, matrix.rank());\n    assertEquals(Shape.of(3, 2), matrix.shape());\n    matrix\n      .set(NdArrays.vectorOf(1, 2), 0)\n      .set(NdArrays.vectorOf(3, 4), 1)\n      .set(NdArrays.vectorOf(5, 6), 2);\n});\n\n// Visit all scalars of 3D matrix, printing their coordinates and value\nmatrix3d.scalars().forEachIdx((coords, scalar) -\u003e\n    System.out.println(\"Scalar at \" + Arrays.toString(coords) + \" has value \" + scalar.getInt())\n);\n\n// Retrieving the second vector of the first matrix\nIntNdArray vector = matrix3d.get(0, 1);\nassertEquals(1, vector.rank());\n\n// Rewriting the values of the vector using a primitive array\nvector.copyFrom(DataBuffers.of(new int[] { 7, 8 }));\nassertEquals(7, matrix3d.getInt(0, 1, 0));\nassertEquals(8, matrix3d.getInt(0, 1, 1));\n\n// Slicing the 3D matrix so we only keep the second element of the second dimension\nIntNdArray slice = matrix3d.slice(all(), at(1));\nassertEquals(2, slice.rank());\nassertEquals(Shape.of(2, 2), slice.shape());\nassertEquals(7, slice.getInt(0, 0));  // (0, 1, 0) in the original matrix\nassertEquals(3, slice.getInt(1, 0));  // (1, 1, 0) in the original matrix\n```\n\n## Integration with TensorFlow\n\nThe NdArray library is independent of the TensorFlow runtime library, making it a good choice for\nmanipulating multi-dimensional data structures from anywhere. But as an example, here\nis how it is actually being used by the [TensorFlow Java API](https://github.com/tensorflow/java/):\n\n```java\n// Allocate a tensor of 32-bits integer of the shape (2, 3, 2)\nTInt32 tensor = TInt32.ofShape(2, 3, 2);\n\n// Access tensor memory directly\nassertEquals(3, tensor.rank());\nassertEquals(12, tensor.size());\n\ntry (EagerSession session = EagerSession.create()) {\n  Ops tf = Ops.create(session);\n\n  // Initialize tensor memory with zeros and take a snapshot\n  tensor.scalars().forEach(scalar -\u003e scalar.setInt(0));\n  Constant\u003cT\u003e x = tf.constant(tensor);\n\n  // Initialize the same tensor memory with ones and take a snapshot\n  tensor.scalars().forEach(scalar -\u003e scalar.setInt(1));\n  Constant\u003cT\u003e y = tf.constant(tensor);\n\n  // Subtract y from x and validate the result\n  Sub\u003cT\u003e sub = tf.math.sub(x, y);\n  sub.asTensor().scalars().forEach(scalar -\u003e\n      assertEquals(-1, scalar.getInt())\n  );\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftensorflow%2Fjava-ndarray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftensorflow%2Fjava-ndarray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftensorflow%2Fjava-ndarray/lists"}