Class: Datasets::MNIST

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

Defined Under Namespace

Classes: Record

Instance Attribute Summary

Attributes inherited from Dataset

#metadata

Instance Method Summary collapse

Methods inherited from Dataset

#to_table

Constructor Details

#initialize(type: :train) ⇒ MNIST

Returns a new instance of MNIST.



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

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

  super()

  .id = "mnist-#{type}"
  .name = "MNIST: #{type}"
  .url = "http://yann.lecun.com/exdb/mnist/"
  @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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/datasets/mnist.rb', line 42

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)
  base_url = "http://yann.lecun.com/exdb/mnist/"

  unless image_path.exist?
    download(image_path, base_url + target_file(:image))
  end

  unless label_path.exist?
    download(label_path, base_url + target_file(:label))
  end

  open_data(image_path, label_path, &block)
end