Module: Torch::Inspector

Included in:
Tensor
Defined in:
lib/torch/inspector.rb

Instance Method Summary collapse

Instance Method Details

#inspectObject

TODO make more performant, especially when summarizing how? only read data that will be displayed



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/torch/inspector.rb', line 5

def inspect
  data =
    if numel == 0
      "[]"
    elsif dim == 0
      item
    else
      summarize = numel > 1000

      if dtype == :bool
        fmt = "%s"
      else
        values = _flat_data
        abs = values.select { |v| v != 0 }.map(&:abs)
        max = abs.max || 1
        min = abs.min || 1

        total = 0
        if values.any? { |v| v < 0 }
          total += 1
        end

        if floating_point?
          sci = max > 1e8 || max < 1e-4

          all_int = values.all? { |v| v.finite? && v == v.to_i }
          decimal = all_int ? 1 : 4

          total += sci ? 10 : decimal + 1 + max.to_i.to_s.size

          if sci
            fmt = "%#{total}.4e"
          else
            fmt = "%#{total}.#{decimal}f"
          end
        else
          total += max.to_s.size
          fmt = "%#{total}d"
        end
      end

      inspect_level(to_a, fmt, dim - 1, 0, summarize)
    end

  attributes = []
  if requires_grad
    attributes << "requires_grad: true"
  end
  if ![:float32, :int64, :bool].include?(dtype)
    attributes << "dtype: #{dtype.inspect}"
  end

  "tensor(#{data}#{attributes.map { |a| ", #{a}" }.join("")})"
end