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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/torch/inspector.rb', line 13
def initialize(tensor)
@floating_dtype = tensor.floating_point?
@complex_dtype = tensor.complex?
@int_mode = true
@sci_mode = false
@max_width = 1
tensor_view = Torch.no_grad { tensor.reshape(-1) }
if !@floating_dtype
tensor_view.each do |value|
value_str = value.item.to_s
@max_width = [@max_width, value_str.length].max
end
else
nonzero_finite_vals = Torch.masked_select(tensor_view, Torch.isfinite(tensor_view) & tensor_view.ne(0))
return if nonzero_finite_vals.numel == 0
nonzero_finite_abs = nonzero_finite_vals.abs.double
nonzero_finite_min = nonzero_finite_abs.min.double
nonzero_finite_max = nonzero_finite_abs.max.double
nonzero_finite_vals.each do |value|
if value.item != value.item.ceil
@int_mode = false
break
end
end
if @int_mode
if nonzero_finite_max / nonzero_finite_min > 1000.0 || nonzero_finite_max > 1.0e8
@sci_mode = true
nonzero_finite_vals.each do |value|
value_str = "%.#{PRINT_OPTS[:precision]}e" % value.item
@max_width = [@max_width, value_str.length].max
end
else
nonzero_finite_vals.each do |value|
value_str = "%.0f" % value.item
@max_width = [@max_width, value_str.length + 1].max
end
end
else
if nonzero_finite_max / nonzero_finite_min > 1000.0 || nonzero_finite_max > 1.0e8 || nonzero_finite_min < 1.0e-4
@sci_mode = true
nonzero_finite_vals.each do |value|
value_str = "%.#{PRINT_OPTS[:precision]}e" % value.item
@max_width = [@max_width, value_str.length].max
end
else
nonzero_finite_vals.each do |value|
value_str = "%.#{PRINT_OPTS[:precision]}f" % value.item
@max_width = [@max_width, value_str.length].max
end
end
end
end
@sci_mode = PRINT_OPTS[:sci_mode] unless PRINT_OPTS[:sci_mode].nil?
end
|