Class: Daimond::Data::DataLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/daimond/data/data_loader.rb

Instance Method Summary collapse

Constructor Details

#initialize(images, labels, batch_size: 32, shuffle: true) ⇒ DataLoader

Returns a new instance of DataLoader.



4
5
6
7
8
9
10
11
# File 'lib/daimond/data/data_loader.rb', line 4

def initialize(images, labels, batch_size: 32, shuffle: true)
  @images = images
  @labels = labels
  @batch_size = batch_size
  @shuffle = shuffle
  @n_samples = images.length
  reset
end

Instance Method Details

#batches_countObject



36
37
38
# File 'lib/daimond/data/data_loader.rb', line 36

def batches_count
  (@n_samples.to_f / @batch_size).ceil
end

#each_batchObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/daimond/data/data_loader.rb', line 19

def each_batch
  reset
  while @position < @n_samples
    batch_indices = @indices[@position, @batch_size]
    @position += @batch_size

    batch_images = batch_indices.map { |i| @images[i] }
    batch_labels = batch_indices.map { |i| @labels[i] }

    # Конвертируем в Tensor [batch_size, 784]
    x = Tensor.new(Numo::DFloat[*batch_images])
    y = Tensor.new(Numo::Int32[*batch_labels])

    yield x, y
  end
end

#resetObject



13
14
15
16
17
# File 'lib/daimond/data/data_loader.rb', line 13

def reset
  @indices = (0...@n_samples).to_a
  @indices.shuffle! if @shuffle
  @position = 0
end