Class: DataLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/toy/train/training.rb

Overview


DataLoader  manual batch iteration over an Array of IntArrays

Spinel doesn’t let us hand a class instance to the each_slice / each_with_index style, and Spinel compiles every class method (with poly fallback for uncalled-typed params) — uncalled ‘at(i)` would force @sequences’s element type to PolyArray and poison IntArray inference everywhere. So we keep DataLoader to batch-bounds only, and the training loop reads sequences directly: ‘seq = sequences`.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequences, batch_size) ⇒ DataLoader

Returns a new instance of DataLoader.



98
99
100
101
# File 'lib/toy/train/training.rb', line 98

def initialize(sequences, batch_size)
  @sequences  = sequences
  @batch_size = batch_size
end

Instance Attribute Details

#batch_sizeObject

Returns the value of attribute batch_size.



96
97
98
# File 'lib/toy/train/training.rb', line 96

def batch_size
  @batch_size
end

#sequencesObject

Returns the value of attribute sequences.



96
97
98
# File 'lib/toy/train/training.rb', line 96

def sequences
  @sequences
end

Instance Method Details

#batch_countObject



107
108
109
110
111
112
113
114
# File 'lib/toy/train/training.rb', line 107

def batch_count
  n  = @sequences.length
  bc = n / @batch_size
  if n % @batch_size != 0
    bc += 1
  end
  bc
end

#batch_end(bi) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/toy/train/training.rb', line 120

def batch_end(bi)
  e = (bi + 1) * @batch_size
  if e > @sequences.length
    e = @sequences.length
  end
  e
end

#batch_start(bi) ⇒ Object



116
117
118
# File 'lib/toy/train/training.rb', line 116

def batch_start(bi)
  bi * @batch_size
end

#lengthObject



103
104
105
# File 'lib/toy/train/training.rb', line 103

def length
  @sequences.length
end