Representing and querying data tensors in RDF and SPARQL
Piotr Marciniak, Piotr Sowiński, Maria Ganzha · 2025
Read Representing and querying data tensors in RDF and SPARQL by Piotr Marciniak, Piotr Sowiński, Maria Ganzha (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2504-19224.txt.
Introduction
Tensors from machine learning need a home in knowledge graphs, and Piotr Marciniak, Piotr Sowiński, and Maria Ganzha propose one in a 2025 paper. Embeddings produced by learning methods, including language models, come as tensors. Hybrid uses that join learning with knowledge graphs need a robust, efficient way to store and query those tensors inside a graph database.
The authors introduce a scheme for representing tensors as literals in RDF. They also extend SPARQL with specialized functions for such literals, adding dozens of functions and several aggregates. A tested, open-source implementation based on Apache Jena supports the scheme. An exemplary knowledge graph and query set are provided. Readers get a working bridge between tensor data and semantic queries.
Catalog record
| CREATOR | Piotr Marciniak, Piotr Sowiński, Maria Ganzha |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 11 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2504-19224
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Representing and querying data tensors in RDF and SPARQL Piotr Marciniak, Piotr Sowiński, Maria Ganzha Abstract Embedding tensors in databases has recently gained in significance, due to the rapid proliferation of machine learning methods (including LLMs) which produce embeddings in the form of tensors. To support emerging use cases hybridizing machine learning with knowledge graphs, a robust and efficient tensor representation scheme is needed. We introduce a novel approach for representing data tensors as literals in RDF, along with an extension of SPARQL implementing specialized functionalities for handling such literals. The extension includes 36 SPARQL functions and four aggregates. To support this approach, we provide a thoroughly tested, open-source implementation based on Apache Jena, along with an exemplary knowledge graph and query set. Resources: https://w3id.org/rdf-tensor 1 Introduction The increasing use of machine learning models (especially Large Language Models – LLMs) has created a need for efficient storage and manipulation of tensor data, such as text embeddings. Consequently, a growing demand exists for solutions integrating data storage and querying with data tensor processing. Recently, several database extensions have been developed to address this, such as PostgreSQL with pgvector , MongoDB with Atlas Vector Search , or Redis with Redis VSS . These extensions enable existing architectures to facilitate tensor processing (e.g., finding similar vectors based on cosine similarity). AllegroGraph 1 1 1 https://allegrograph.com/products/neuro-symbolic-ai/ is a solution from the RDF domain that integrates knowledge graphs and vectors. Unlike the earlier solutions mentioned, it uses an additional vector database for this purpose, only exposing semantic-level operations to users. However, a more tightly-integrated approach, with tensor operations happening directly in the RDF database, would be more general (covering more use cases) and on par with modern SQL and NoSQL database architectures [ 5 ] . A data tensor is a multidimensional array used to store and process data. Tensors generalize scalars, vectors, and matrices to higher dimensions. However, currently, there is no direct way to represent such high-dimensional arrays in RDF. RDF 1.1 [ 7 ] does support containers and collections, allowing users to model structures where elements are organized using a dedicated vocabulary. While it is possible to model tensors within these structures, it significantly increases the memory footprint. Each element in a tensor must be explicitly represented by a triple, which is excessively verbose (Fig. 1 ). Furthermore, querying such representations in SPARQL is highly inefficient because it involves filtering elements of the lists using property paths. SPARQL lacks easy-to-use, built-in functions for performing complex operations on collections. This means that even simple tasks such as adding two vectors require manual filtering and joining appropriate elements of the collections. ⬇ : word : hasWordEmbedding : embedding0 . : embedding0 rdf : type rdf : List ; rdf : first 1; rdf : rest : embedding1 . : embedding1 rdf : first 2 ; rdf : rest : embedding2 . : embedding2 rdf : first 3 ; rdf : rest rdf : nil . (a) Representation as rdf:List ⬇ : word : hasWordEmbedding : embedding0 . : embedding0 rdf : type rdf : Seq ; rdf : _1 1 ; rdf : _2 2 ; rdf : _3 3 ; (b) Representation as rdf:Seq Figure 1 : Data tensor representations currently possible with pure RDF 1.1. The recent proposal for RDF datatypes for lists and maps [ 3 ] could be used to represent multidimensional arrays, i.e., data tensors, as lists of lists. However, this proposition lacks specific SPARQL functions and operators, which would be helpful in processing these types of data. Additionally, multidimensional tensors would have to be represented as “zig-zag” nested lists. This approach is much more error-prone than dedicated tensor datatypes, as it does not ensure consistent dimensions of the tensors. In this work, we introduce a framework for representing and processing multidimensional data tensors in RDF and SPARQL. The proposed approach defines new RDF datatypes for tensors and extends SPARQL capabilities with a comprehensive set of functions for tensor manipulation. This work is accompanied by a public specification of the extension, ontology files for datatypes and SPARQL functions, and a complete open-source implementation on top of Apache Jena: https://w3id.org/rdf-tensor 2 Approach In the proposed approach, two new datatypes are introduced: (i) numeric data tensor for storing tensors containing numeric values ( dt:NumericDataTensor ); (ii) boolean data tensor ( dt:BooleanDataTensor ). The lexical form for both datatypes follows the JSON specification [ 2 ] . In other words, a tensor is represented by a JSON string, which was chosen due to its readability and the wide availability of JSON libraries. Each representation consists of two mandatory keys: data and shape . The data key must contain an array of values, where all elements are either numbers or booleans, depending on the tensor type. The shape key must contain an array of integers, and the product of its elements must be equal to the length of the data array. The elements in the data array are assumed to be stored in row-major order (C-order), following the specified shape. An example of a two-dimensional boolean data tensor is presented in the following triple. ⬇ PREFIX dt : < https :// w3id . org / rdf - tensor / datatypes #> : s : p "{\" shape \ ":[1, ␣ 3],\" data \ ":[true, ␣ false, ␣ true]}" ^^ dt : BooleanDataTensor . An additional key, type , is required for numeric data tensors. This key specifies the numeric type to ensure correct interpretation by an RDF processor. It can take one of six string values: three for real numbers ( float16 , float32 , float64 ) and three for integers ( int16 , int32 , int64 ). Example triples including a dt:NumericDataTensor are shown below. ⬇ PREFIX dt : < https :// w3id . org / rdf - tensor / datatypes #> : s : p1 "{\" type \ ":\" int32 \ ",\" shape \ ":[2],\" data \ ":[1, ␣ 2]}" ^^ dt : NumericDataTensor . : s : p2 "{\" type \ ":\" float32 \ ",\" shape \ ":[2],\" data \ ":[1e0, ␣ 2.0]}" ^^ dt : NumericDataTensor . The proposed solution also includes SPARQL functions and aggregates, which facilitate tensor manipulation. A total of 36 functions are defined that enable the transformation, comparison, and concatenation of tensors. These functions are categorized into six groups: • Transformation functions , which apply elementwise operations. For example, dtf:cos computes the cosine of each element in a numeric tensor. • Operators , which perform arithmetic and logical computations with broadcasting. For instance, dtf:add adds two tensors element-wise, while dtf:or performs a logical OR operation on two Boolean tensors. • Indexing functions , which enable tensor slicing. The function dtf:getSubDT extracts a sub-tensor based on a selection tensor. • Concatenation functions , which merge tensors along a specified axis. For example, dtf:concat concatenates two tensors along a given dimension. • Reduction functions , which aggregate tensor values along dimensions. The function dtf:sum computes the sum of tensor elements along a specified axis. • Similarity functions , measuring distances between tensors. For instance, dtf:euclideanDistance calculates the Euclidean distance between tensors. Consider the query below as an example of how the functions can be used. This query