mratsim / Arraymancer

A fast, ergonomic and portable tensor library in Nim with a deep learning focus for CPU, GPU and embedded devices via OpenMP, Cuda and OpenCL backends
https://mratsim.github.io/Arraymancer/
Apache License 2.0
1.33k stars 96 forks source link

Two (small) issues with display of tensor #500

Closed pietroppeter closed 2 years ago

pietroppeter commented 3 years ago

example (playground):

import arraymancer, sequtils
echo toSeq(1..24).toTensor().reshape(2,3,4)

output:

Tensor[system.int] of shape [2, 3, 4]" on backend "Cpu"
| |     1   2   3   4 |     13  14  15  16|
| |     5   6   7   8 |     17  18  19  20|
| |     9   10  11  12 |    21  22  23  24|

The two issues are:

  1. an additional " after tensor shape
  2. and additional set of | in first columns

First one is a trivial fix (source); the second one might be a little be more involved (source), but I could try to produce a PR with a fix for both.

Also, not sure what should be the expected display of a 4d tensor, an example:

import arraymancer, sequtils
echo toSeq(1..16).toTensor().reshape(2,2,2,2)

output:

Tensor[system.int] of shape [2, 2, 2, 2]" on backend "Cpu"
|                       |
|-  -   -   -   -   -   -|
| |     1   2 |     5   6|
| |     3   4 |     7   8|
|-  -   -   -   -   -   -|
| |     9   10 |    13  14|
| |     11  12 |    15  16|

I noticed these two issues while writing a remake of the Tutorial using a library I am developing: nimib.

I also noticed this very interesting issue #488 which is relevant to documentation and for which maybe nimib in the future (I am close to first release of 0.1) might be able to help with.

mratsim commented 3 years ago

Unfortunately 4D display on a 2D screen is a bit hard. Ideally 4D display looks like that https://github.com/mratsim/Arraymancer/issues/5#issuecomment-751852128

image

I try to reverse engineer the Haskell code of that library but got lost in Monads :/.

Regarding nimib, you might want to have a chat with @haxscramper as he also wanted to help tackle that part (and many others tooling issues like nimble dependencies resolution), https://github.com/haxscramper/haxdoc

Vindaar commented 2 years ago

Since #509 (and some fixes of it after) the first example now gives:

import arraymancer, sequtils
echo toSeq(1..24).toTensor().reshape(2,3,4)
Tensor[system.int] of shape "[2, 3, 4]" on backend "Cpu"                                                           
          0                      1                                                                                 
|1      2     3     4| |13    14    15    16|                                                                      
|5      6     7     8| |17    18    19    20|
|9     10    11    12| |21    22    23    24| 

and the second:

import arraymancer, sequtils
echo toSeq(1..16).toTensor().reshape(2,2,2,2)
Tensor[system.int] of shape "[2, 2, 2, 2]" on backend "Cpu"
      0          1     
0 |1      2| |5      6|
  |3      4| |7      8|
  ---------------------
      0          1     
1 |9     10| |13    14|
  |11    12| |15    16|
  ---------------------

I'll close the issue for now. If I'm missing something that is still broken, feel free to reopen.