Skip to content

Commit 0fd1290

Browse files
Lewuathedsmilkov
authored andcommitted
Print bool tensor properly (tensorflow#1612)
BUG Print boolean tensor properly. See: tensorflow/tfjs#1222
1 parent 4beeddf commit 0fd1290

File tree

2 files changed

+77
-6
lines changed

2 files changed

+77
-6
lines changed

src/tensor_format.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,35 @@ function computeMaxSizePerColumn(
5858
const offset = row * numCols;
5959
for (let j = 0; j < numCols; j++) {
6060
padPerCol[j] = Math.max(
61-
padPerCol[j], valToString(valuesOrTuples[offset + j], 0).length);
61+
padPerCol[j], valToString(valuesOrTuples[offset + j], 0,
62+
dtype).length);
6263
}
6364
}
6465
}
6566
return padPerCol;
6667
}
6768

68-
function valToString(val: number|string|[number, number], pad: number) {
69+
function valToString(val: number|string|[number, number], pad: number,
70+
dtype: DataType) {
6971
let valStr: string;
7072
if (Array.isArray(val)) {
7173
valStr = `${parseFloat(val[0].toFixed(FORMAT_NUM_SIG_DIGITS))} + ` +
7274
`${parseFloat(val[1].toFixed(FORMAT_NUM_SIG_DIGITS))}j`;
7375
} else if (isString(val)) {
7476
valStr = `'${val}'`;
77+
} else if (dtype === 'bool') {
78+
valStr = boolNumToString(val);
7579
} else {
7680
valStr = parseFloat(val.toFixed(FORMAT_NUM_SIG_DIGITS)).toString();
7781
}
7882

7983
return rightPad(valStr, pad);
8084
}
8185

86+
function boolNumToString(v: number): string {
87+
return v === 0 ? 'false' : 'true';
88+
}
89+
8290
function subTensorToString(
8391
vals: TypedArray|string[], shape: number[], dtype: DataType,
8492
strides: number[], padPerCol: number[], isLast = true): string[] {
@@ -89,7 +97,10 @@ function subTensorToString(
8997
if (rank === 0) {
9098
if (dtype === 'complex64') {
9199
const complexTuple = createComplexTuples(vals);
92-
return [valToString(complexTuple[0], 0)];
100+
return [valToString(complexTuple[0], 0, dtype)];
101+
}
102+
if (dtype === 'bool') {
103+
return [boolNumToString(vals[0] as number)];
93104
}
94105
return [vals[0].toString()];
95106
}
@@ -107,12 +118,13 @@ function subTensorToString(
107118
lastVals = createComplexTuples(lastVals);
108119
}
109120
return [
110-
'[' + firstVals.map((x, i) => valToString(x, padPerCol[i])).join(', ') +
121+
'[' + firstVals.map((x, i) => valToString(x, padPerCol[i],
122+
dtype)).join(', ') +
111123
', ..., ' +
112124
lastVals
113125
.map(
114126
(x, i) => valToString(
115-
x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i]))
127+
x, padPerCol[size - FORMAT_NUM_FIRST_LAST_VALS + i], dtype))
116128
.join(', ') +
117129
']'
118130
];
@@ -122,7 +134,8 @@ function subTensorToString(
122134
Array.from<number|string>(vals);
123135

124136
return [
125-
'[' + displayVals.map((x, i) => valToString(x, padPerCol[i])).join(', ') +
137+
'[' + displayVals.map((x, i) => valToString(x, padPerCol[i],
138+
dtype)).join(', ') +
126139
']'
127140
];
128141
}

src/tensor_test.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1443,6 +1443,18 @@ describe('tensor.toString', () => {
14431443
' test');
14441444
});
14451445

1446+
it('bool scalar verbose', () => {
1447+
const verbose = true;
1448+
const str = tf.scalar(true).toString(verbose);
1449+
expect(str).toEqual(
1450+
'Tensor\n' +
1451+
' dtype: bool\n' +
1452+
' rank: 0\n' +
1453+
' shape: []\n' +
1454+
' values:\n' +
1455+
' true');
1456+
});
1457+
14461458
it('1d tensor verbose', () => {
14471459
const verbose = true;
14481460
const str = tf.zeros([4]).toString(verbose);
@@ -1467,6 +1479,18 @@ describe('tensor.toString', () => {
14671479
' [\'a\', \'bb\', \'ccc\']');
14681480
});
14691481

1482+
it('1d bool tensor verbose', () => {
1483+
const verbose = true;
1484+
const str = tf.tensor([true, false, true]).toString(verbose);
1485+
expect(str).toEqual(
1486+
'Tensor\n' +
1487+
' dtype: bool\n' +
1488+
' rank: 1\n' +
1489+
' shape: [3]\n' +
1490+
' values:\n' +
1491+
' [true, false, true]');
1492+
});
1493+
14701494
it('2d tensor verbose', () => {
14711495
const verbose = true;
14721496
const str = tf.zeros([3, 3]).toString(verbose);
@@ -1500,6 +1524,20 @@ describe('tensor.toString', () => {
15001524
' [\'g\', \'h\' , \'i\' ]]');
15011525
});
15021526

1527+
it('2d bool tensor verbose', () => {
1528+
const verbose = true;
1529+
const str = tf.zeros([3, 3], 'bool').toString(verbose);
1530+
expect(str).toEqual(
1531+
'Tensor\n' +
1532+
' dtype: bool\n' +
1533+
' rank: 2\n' +
1534+
' shape: [3,3]\n' +
1535+
' values:\n' +
1536+
' [[false, false, false],\n' +
1537+
' [false, false, false],\n' +
1538+
' [false, false, false]]');
1539+
});
1540+
15031541
it('3d tensor verbose', () => {
15041542
const verbose = true;
15051543
const str = tf.zeros([3, 3, 2]).toString(verbose);
@@ -1542,6 +1580,26 @@ describe('tensor.toString', () => {
15421580
' [\'kkk\', \'llll\']]]');
15431581
});
15441582

1583+
it('3d bool tensor verbose', () => {
1584+
const verbose = true;
1585+
const str = tf.ones([3, 3, 2], 'bool').toString(verbose);
1586+
expect(str).toEqual(
1587+
'Tensor\n' +
1588+
' dtype: bool\n' +
1589+
' rank: 3\n' +
1590+
' shape: [3,3,2]\n' +
1591+
' values:\n' +
1592+
' [[[true, true],\n' +
1593+
' [true, true],\n' +
1594+
' [true, true]],\n\n' +
1595+
' [[true, true],\n' +
1596+
' [true, true],\n' +
1597+
' [true, true]],\n\n' +
1598+
' [[true, true],\n' +
1599+
' [true, true],\n' +
1600+
' [true, true]]]');
1601+
});
1602+
15451603
it('1d long tensor verbose', () => {
15461604
const verbose = true;
15471605
const str = tf.zeros([100]).toString(verbose);

0 commit comments

Comments
 (0)