Method: Chainer::Dataset::Convert._concat_arrays_with_padding

Defined in:
lib/chainer/dataset/convert.rb

._concat_arrays_with_padding(arrays, padding, device) ⇒ Object



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
79
80
81
82
83
84
# File 'lib/chainer/dataset/convert.rb', line 53

def self._concat_arrays_with_padding(arrays, padding, device)
  xm = device.xm
  if Chainer.array?(arrays[0]) and arrays[0].ndim > 0
    xm = Chainer.get_array_module(arrays[0])
    shape = xm::Int32.cast(arrays[0].shape)
    arrays[1..-1].each do |array|
      if xm::Bit.[](shape != array.shape).any?
        shape = xm::Int32.maximum(shape, array.shape)
      end
    end
  else # Integer
    shape = []
  end

  shape = shape.insert(0, arrays.size).to_a
  if Chainer.array?(arrays[0]) and arrays[0].ndim > 0
    result = arrays[0].class.new(shape).fill(padding)
  else # Integer
    result = xm::Int32.new(shape).fill(padding)
  end

  arrays.size.times do |i|
    src = arrays[i]
    if Chainer.array?(src) and src.ndim > 0
      result[i, 0...src.shape[0], 0...src.shape[1]] = src
    else # Integer
      result[i] = src
    end
  end

  result
end