Skip to content

Commit 6c93cc6

Browse files
Merge pull request #1404 from NiklasGustafsson/bugs
Fixed npstr() of empty tensor()
2 parents 829aae2 + 619c62d commit 6c93cc6

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/TorchSharp/Tensor/Tensor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6506,16 +6506,18 @@ private static string ToNumpyString(Tensor t, long mdim, bool isFCreate, string
65066506

65076507
var dim = t.dim();
65086508

6509-
if (t.size().Length == 0) return "";
65106509
var sb = new StringBuilder(isFCreate ? string.Join("", Enumerable.Repeat(' ', (int)(mdim - dim))) : "");
6510+
6511+
if (dim == 0) {
6512+
PrintValue(sb, t.dtype, t.ToScalar(), fltFormat, actualCulturInfo);
6513+
return sb.ToString(); ;
6514+
}
6515+
65116516
sb.Append('[');
65126517
var currentSize = t.size()[0];
65136518
if (currentSize == 0) {
65146519
// print nothing
65156520
}
6516-
else if (dim == 0) {
6517-
PrintValue(sb, t.dtype, t.ToScalar(), fltFormat, actualCulturInfo);
6518-
}
65196521
else if (dim == 1) {
65206522
if (currentSize <= torch.maxColumns) {
65216523
for (var i = 0; i < currentSize - 1; i++) {

test/TorchSharpTest/TestTorchTensorBugs.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,15 @@ public void Validate_1250()
16601660
Assert.Equal("[1], type = Float32, device = cpu, value = float [] {0f}", torch.zeros(1).cstr());
16611661
}
16621662

1663+
[Fact]
1664+
public void Validate_1250a()
1665+
{
1666+
var scalar = torch.zeros(Array.Empty<long>());
1667+
Assert.Equal("0", scalar.npstr());
1668+
Assert.Equal("[], type = Float32, device = cpu, value = 0", scalar.cstr());
1669+
Assert.Equal("[], type = Float32, device = cpu, value = 0", scalar.jlstr());
1670+
}
1671+
16631672
[Fact]
16641673
public void ValidateLoadWithDeflateStream()
16651674
{

0 commit comments

Comments
 (0)