Class: Datasets::MNIST

Inherits:
Dataset show all
Defined in:
lib/datasets/mnist.rb

Direct Known Subclasses

FashionMNIST, KuzushijiMNIST

Defined Under Namespace

Classes: Record

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#clear_cache!, #to_table

Constructor Details

#initialize(type: :train) ⇒ MNIST

Returns a new instance of MNIST.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/datasets/mnist.rb', line 19

def initialize(type: :train)
  unless [:train, :test].include?(type)
    raise ArgumentError, "Please set type :train or :test: #{type.inspect}"
  end

  super()

  .id = "#{dataset_name.downcase}-#{type}"
  .name = "#{dataset_name}: #{type}"
  .url = base_urls.first
  .licenses = licenses
  @type = type

  case type
  when :train
    .description = "a training set of 60,000 examples"
  when :test
    .description = "a test set of 10,000 examples"
  end
end

Instance Method Details

#each(&block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datasets/mnist.rb', line 40

def each(&block)
  return to_enum(__method__) unless block_given?

  image_path = cache_dir_path + target_file(:image)
  label_path = cache_dir_path + target_file(:label)

  download(image_path,
           *base_urls.collect { |base_url| base_url + target_file(:image) })
  download(label_path,
           *base_urls.collect { |base_url| base_url + target_file(:label) })

  open_data(image_path, label_path, &block)
end