Class: NeuralNetworkRb::MNIST

Inherits:
Object
  • Object
show all
Defined in:
lib/neural_network_rb/mnist.rb

Constant Summary collapse

N_FEATURES =
28 * 28
N_CLASSES =
10
TRAIN_FILE_NAMES =
%w(train-images-idx3-ubyte.gz train-labels-idx1-ubyte.gz)
TEST_FILE_NAMES =
%w(t10k-images-idx3-ubyte.gz t10k-labels-idx1-ubyte.gz)
ROOT_URL =
'http://yann.lecun.com/exdb/mnist/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_file = nil, label_file = nil) ⇒ MNIST

Returns a new instance of MNIST.



55
56
57
58
# File 'lib/neural_network_rb/mnist.rb', line 55

def initialize(data_file = nil, label_file = nil)
  @labels = get_labels(label_file) if label_file
  @data   = get_images(data_file)  if data_file
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



53
54
55
# File 'lib/neural_network_rb/mnist.rb', line 53

def data
  @data
end

#labelsObject

Returns the value of attribute labels.



53
54
55
# File 'lib/neural_network_rb/mnist.rb', line 53

def labels
  @labels
end

#validation_dataObject

Returns the value of attribute validation_data.



53
54
55
# File 'lib/neural_network_rb/mnist.rb', line 53

def validation_data
  @validation_data
end

#validation_labelsObject

Returns the value of attribute validation_labels.



53
54
55
# File 'lib/neural_network_rb/mnist.rb', line 53

def validation_labels
  @validation_labels
end

Class Method Details

.display_image(narray) ⇒ Object



24
25
26
27
28
# File 'lib/neural_network_rb/mnist.rb', line 24

def display_image(narray)
  narray.shape[0].times do |r|
    puts narray[r*28..((r+1)*28-1)].to_a.inspect
  end
end

.embed_labels(labels, algorithm, class_count) ⇒ Object



30
31
32
# File 'lib/neural_network_rb/mnist.rb', line 30

def embed_labels(labels, algorithm, class_count)
  NeuralNetworkRb::Embeddings.send(algorithm, labels, class_count)
end

.test_setObject



20
21
22
# File 'lib/neural_network_rb/mnist.rb', line 20

def test_set
  download(TEST_FILE_NAMES)        
end

.training_setObject



16
17
18
# File 'lib/neural_network_rb/mnist.rb', line 16

def training_set
  download(TRAIN_FILE_NAMES)        
end

Instance Method Details

#batches(batches_count) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/neural_network_rb/mnist.rb', line 81

def batches(batches_count)
  total_size = self.data.shape[0]
  batch_size = (total_size.to_f/batches_count).ceil
  Array.new(batches_count).tap do |result|
    batches_count.times do |i|
      range = batch_size*i..batch_size*(i+1)-1
      batch_data   = NeuralNetworkRb.rows(@data,   range) # self.data[batch_size*i..batch_size*(i+1)-1, true]
      batch_labels = NeuralNetworkRb.rows(@labels, range) # self.labels[batch_size*i..batch_size*(i+1)-1]
      result[i] = [batch_data, batch_labels]
    end
  end
end

#cloneObject



60
61
62
63
64
65
66
67
# File 'lib/neural_network_rb/mnist.rb', line 60

def clone
  self.class.new.tap do |m|
    m.data = self.data.copy
    m.labels = self.labels.copy
    m.validation_data = self.validation_data.copy if self.validation_data
    m.validation_labels = self.validation_labels.copy if self.validation_labels
  end
end

#partition!(train_ratio) ⇒ Object



74
75
76
77
78
# File 'lib/neural_network_rb/mnist.rb', line 74

def partition!(train_ratio)
  @data, @validation_data = NeuralNetworkRb.split(@data, train_ratio)
  @labels, @validation_labels = NeuralNetworkRb.split(@labels, train_ratio)
  self
end

#shuffle!(seed) ⇒ Object



69
70
71
72
# File 'lib/neural_network_rb/mnist.rb', line 69

def shuffle!(seed)
  @data, @labels = NeuralNetworkRb.shuffle(@data, @labels, seed)
  self
end