Importing and Exporting Tensor Data

You can import and export tensor data in ASCII format for the following data types:

Contents

Tensor/Matrix Data

In the case of a tensor, the first three lines give details about the tensor. The format for a 4 x 3 x 2 tensor is as follows...

  tensor
  3
  4 3 2
  <value of A(1,1,1)>
  <value of A(2,1,1)>
  <value of A(3,1,1)>
  <value of A(4,1,1)>
  <value of A(1,2,1)>
  <value of A(2,2,1)>
  <value of A(3,2,1)>
  <value of A(4,2,1)>
  ...
  <value of A(2,3,2)>
  <value of A(3,3,2)>
  <value of A(4,3,2)>

A matrix is formatted the same as a 2-way tensor except that the first line says matrix rather than tensor.

Y1 = tenrand([4 3 2]);
export_data(Y1,'Y.tensor');
Y2 = import_data('Y.tensor');
assert(isequal(Y1,Y2)) %<-- test if equal when reading tensor from file
type 'Y.tensor'
tensor
3 
4 3 2 
7.8410984944456918e-01
2.6761755162945355e-01
9.7039153429968650e-01
9.4510452140896994e-01
9.1857177911762622e-01
7.6670088559754168e-01
1.5661279895469682e-01
7.5410547015482998e-01
1.4114456638613404e-01
7.1495769667190401e-02
5.9315889926690779e-01
5.5243694756677753e-01
7.7619043346318617e-01
4.7640496891668416e-01
7.2436234142735267e-01
5.7227121419711569e-01
3.5316198205832472e-01
5.8119606757561004e-02
6.4105967325759805e-01
3.1387368807787064e-01
2.5976459387313477e-01
3.4807513253975986e-01
1.2389652883493230e-01
9.2594519156509847e-02

Sptensor Data

In the case of an sptensor, the first four lines give details about the sptensor. The format for a 4 x 3 x 2 sptensor with 10 nonzeros is as follows...

  sptensor
  3
  4 3 2
  3
  i1 j1 k1
  i2 j2 k2 <value of A(i2,j2,k2)>
  i3 j3 k3 <value of A(i3,j3,k3)>
X1 = sptensor([1 1 1;2 2 2; 4 3 2],rand(3,1),[4 3 2]);
export_data(X1,'X.sptensor');
X2 = import_data('X.sptensor');
assert(isequal(X1,X2)) %<-- test if equal when reading tensor from file
type 'X.sptensor'
sptensor
3 
4 3 2 
3 
1 1 1 8.9733153495338303e-01
2 2 2 4.9896060922967300e-01
4 3 2 6.5262946333672744e-01

Ktensor Data

In the case of an ktensor, the first four lines give details about the ktensor. The format for a 4 x 3 x 2 ktensor with rank=2 is as follows, which contains the lambda values and factor matrices, U, (one per tensor dimension), ...

  ktensor
  3
  4 3 2
  2
  <value of lambda1> <value of lambda2>
  matrix
  2
  4 2
  <value of U{1}(1,1)> <value of U{1}(1,2)
  ...
  <value of U{1}(4,1)> <value of U{1}(4,2)
  matrix
  2
  3 2
  <value of U{2}(1,1)> <value of U{2}(1,2)
  ...
  <value of U{2}(3,1)> <value of U{2}(3,2)
  matrix
  2
  2 2
  <value of U{3}(1,1)> <value of U{3}(1,2)
  <value of U{3}(2,1)> <value of U{3}(2,2)
K1 = ktensor(rand(2,1), rand(4,2), rand(3,2), rand(2,2));
export_data(K1,'K.ktensor');
K2 = import_data('K.ktensor');
assert(isequal(K1,K2)) %<-- test if equal when reading tensor from file
type 'K.ktensor'
ktensor
3 
4 3 2 
2 
5.5321794848598371e-01 2.8539029379907055e-01 
matrix
2 
4 2 
9.7373498999922170e-01 5.0705186097791988e-01 
7.2298481642071333e-01 9.2451864889949120e-01 
5.3540633990533504e-01 9.6085221326105419e-03 
9.1921869350005314e-01 2.6354160551053118e-01 
matrix
2 
3 2 
4.1401269139957864e-01 7.8129482939465800e-01 
8.9449474691739461e-01 6.2150938203639761e-01 
4.3032399708548463e-01 3.4315258530567716e-01 
matrix
2 
2 2 
2.0310353424746941e-01 6.4395364377355735e-01 
9.2082402018958531e-01 5.7706691115717912e-02 

Formatting Output - Data Values

When exporting tensor data your can specify the format of the data values using character vector that follows the convention used in fprintf. For example, this can be useful when exporting tensors that contain only integers, resulting in a file that is smaller in size. The optional fmt_data parameter to export_data specifies the format string, with '%.16' being the default value.

X3 = sptensor([1 1 1;2 2 2; 4 3 2],[1;2;3],[4 3 2]);
export_data(X3,'X_fmt_data.sptensor','fmt_data','%d');
X4 = import_data('X_fmt_data.sptensor');
assert(isequal(X3,X4)) %<-- test if equal when reading tensor from file
type 'X_fmt_data.sptensor'
sptensor
3 
4 3 2 
3 
1 1 1 1
2 2 2 2
4 3 2 3

Formatting Output - Ktensor Lambda Values

When exporting ktensor data your can specify the format of the lambda values using character vector that follows the convention used in fprint. For example, this can be useful when exporting tensors that contain only integers, resulting in a file that is smaller in size. The optional fmt_lambda parameter to export_data specifies the format string, with '%.16' being the default value.

K3 = ktensor([1; 2], 4*ones(4,2), 3*ones(3,2), 2*ones(2,2));
export_data(K3,'K_fmt_data_lambda.ktensor','fmt_data','%d','fmt_lambda','%d');
K4 = import_data('K_fmt_data_lambda.ktensor');
assert(isequal(K3,K4)) %<-- test if equal when reading tensor from file
type 'K_fmt_data_lambda.ktensor'
ktensor
3 
4 3 2 
2 
1 2 
matrix
2 
4 2 
4 4 
4 4 
4 4 
4 4 
matrix
2 
3 2 
3 3 
3 3 
3 3 
matrix
2 
2 2 
2 2 
2 2