Skip to content

Commit cf56803

Browse files
authored
Add tensor transpose and flatten, update reshape (#17)
1 parent 842aee4 commit cf56803

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

docs/index.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ The result of the function is a tensor of the same shape as the input tensor, wh
346346

347347
The result of the function is a tensor with the specified new shape. The total number of elements will remain the same; thus, the product of the dimensions in the new shape must equal the product of the dimensions in the original shape.
348348

349+
If one of the dimensions in the new shape is specified as -1, its size will be inferred such that the total number of elements remains unchanged.
350+
349351
!!! example "Example 1"
350352

351353
Evaluating the SPARQL expression
@@ -374,6 +376,96 @@ The result of the function is a tensor with the specified new shape. The total n
374376
"{\"type\": \"bool\", \"shape\": [1, 4], \"data\": [true, false, true, false]}"^^tensor:BooleanDataTensor
375377
```
376378

379+
!!! example "Example 3"
380+
381+
Evaluating the SPARQL expression
382+
383+
```sparql
384+
tensor:reshape(2, -1, "{\"type\":\"int32\",\"shape\":[6],\"data\":[1, 2, 3, 4, 5, 6]}"^^tensor:NumericDataTensor)
385+
```
386+
387+
returns
388+
389+
```turtle
390+
"{\"type\": \"int32\", \"shape\": [2, 3], \"data\": [1, 2, 3, 4, 5, 6]}"^^tensor:NumericDataTensor
391+
```
392+
393+
---
394+
395+
#### `tensor:transpose`
396+
397+
[tensor:NumericDataTensor](https://w3id.org/rdf-tensor/vocab#NumericDataTensor) **tensor:transpose** ([tensor:NumericDataTensor](https://w3id.org/rdf-tensor/vocab#NumericDataTensor) *term_1*)
398+
399+
[tensor:BooleanDataTensor](https://w3id.org/rdf-tensor/vocab#BooleanDataTensor) **tensor:transpose** ([tensor:BooleanDataTensor](https://w3id.org/rdf-tensor/vocab#BooleanDataTensor) *term_1*)
400+
401+
The result of the function is a tensor where the dimensions are reversed. For example, a tensor with shape `[2, 3, 4]` will become a tensor with shape `[4, 3, 2]`.
402+
403+
!!! example "Example 1"
404+
405+
Evaluating the SPARQL expression
406+
407+
```sparql
408+
tensor:transpose("{\"type\":\"int32\",\"shape\":[2, 3],\"data\":[1, 2, 3, 4, 5, 6]}"^^tensor:NumericDataTensor)
409+
```
410+
411+
returns
412+
413+
```turtle
414+
"{\"type\": \"int32\", \"shape\": [3, 2], \"data\": [1, 4, 2, 5, 3, 6]}"^^tensor:NumericDataTensor
415+
```
416+
417+
!!! example "Example 2"
418+
419+
Evaluating the SPARQL expression
420+
421+
```sparql
422+
tensor:transpose("{\"shape\":[2, 2],\"data\":[true, false, false, true]}"^^tensor:BooleanDataTensor)
423+
```
424+
425+
returns
426+
427+
```turtle
428+
"{\"shape\": [2, 2], \"data\": [true, false, false, true]}"^^tensor:BooleanDataTensor
429+
```
430+
431+
---
432+
433+
#### `tensor:flatten`
434+
435+
[tensor:NumericDataTensor](https://w3id.org/rdf-tensor/vocab#NumericDataTensor) **tensor:flatten** ([tensor:NumericDataTensor](https://w3id.org/rdf-tensor/vocab#NumericDataTensor) *term_1*)
436+
437+
[tensor:BooleanDataTensor](https://w3id.org/rdf-tensor/vocab#BooleanDataTensor) **tensor:flatten** ([tensor:BooleanDataTensor](https://w3id.org/rdf-tensor/vocab#BooleanDataTensor) *term_1*)
438+
439+
The result of the function is a one-dimensional tensor containing all the elements of the input tensor in row-major (C-style) order.
440+
441+
!!! example "Example 1"
442+
443+
Evaluating the SPARQL expression
444+
445+
```sparql
446+
tensor:flatten("{\"type\":\"int32\",\"shape\":[2, 3],\"data\":[1, 2, 3, 4, 5, 6]}"^^tensor:NumericDataTensor)
447+
```
448+
449+
returns
450+
451+
```turtle
452+
"{\"type\": \"int32\", \"shape\": [6], \"data\": [1, 2, 3, 4, 5, 6]}"^^tensor:NumericDataTensor
453+
```
454+
455+
!!! example "Example 2"
456+
457+
Evaluating the SPARQL expression
458+
459+
```sparql
460+
tensor:flatten("{\"shape\":[2, 2],\"data\":[true, false, false, true]}"^^tensor:BooleanDataTensor)
461+
```
462+
463+
returns
464+
465+
```turtle
466+
"{\"shape\": [4], \"data\": [true, false, false, true]}"^^tensor:BooleanDataTensor
467+
```
468+
377469
### 4.2 Operators
378470

379471
When using the binary operators, the input tensors are broadcasted to a common shape. The broadcasting rules are the same as in NumPy**[[NumPy 8259](#numpy)]**. In the case of numeric tensors, the result of the mathematical operation is a tensor with the more precise type of the two input tensors. For example, if one tensor is `float32` and the other is `int32`, the result will be `float32`.

ontology/functions.ttl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,13 @@ tensor:create a sd:Function ;
219219
rdfs:label "Create Tensor" ;
220220
rdfs:comment "Creates a tensor from raw data and type."@en ;
221221
dc:creator "Nikita Kozlov" .
222+
223+
tensor:transpose a sd:Function ;
224+
rdfs:label "Transpose Tensor" ;
225+
rdfs:comment "Transposes a tensor by permuting its dimensions."@en ;
226+
dc:creator "Nikita Kozlov" .
227+
228+
tensor:flatten a sd:Function ;
229+
rdfs:label "Flatten Tensor" ;
230+
rdfs:comment "Flattens a tensor into a one-dimensional array."@en ;
231+
dc:creator "Nikita Kozlov" .

0 commit comments

Comments
 (0)