Skip to content

Commit 07fc7e3

Browse files
Fix parsing for one dimensional tensors into an npy file (#94)
1 parent d3139b4 commit 07fc7e3

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

spec/io/data/single_rank_tensor.npy

84 Bytes
Binary file not shown.

spec/io/io_spec.cr

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ describe Tensor do
2727
expected = Tensor.new([5, 5]) { |i| i }
2828
Num::Testing.tensor_equal(result, expected).should be_true
2929
end
30+
31+
it "reads a rank 1 tensor from a file" do
32+
result = Tensor(Int32, CPU(Int32)).from_npy("#{__DIR__}/data/single_rank_tensor.npy")
33+
expected = [1, 2, 3, 4].to_tensor
34+
Num::Testing.tensor_equal(result, expected).should be_true
35+
end
3036
end

src/io/npy.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class Tensor(T, S)
4848
file.read_fully(header)
4949
size = IO::Memory.new(header).read_bytes(Int16, IO::ByteFormat::LittleEndian)
5050

51-
header = file.read_string(size).downcase.tr("()'", "[]\"").gsub(/,]|],/, "]")
51+
header = file.read_string(size).downcase.tr("()'", "[]\"").gsub(/,],|],/, "]")
5252
json_header = JSON.parse(header)
5353

5454
dtype = json_header["descr"].as_s

0 commit comments

Comments
 (0)