Class: Rumale::Torch::NeuralNetClassifier

Inherits:
Base::Estimator
  • Object
show all
Includes:
Base::Classifier
Defined in:
lib/rumale/torch/neural_net_classifier.rb

Overview

NeuralNetClassifier is a class that provides learning and inference by the neural network defined in torch.rb with an interface similar to classifier of Rumale.

Examples:

require 'rumale/torch'

class MyNet < Torch::NN::Module
  def initialize
    super
    @dropout = Torch::NN::Dropout.new(p: 0.5)
    @fc1 = Torch::NN::Linear.new(2, 8)
    @fc2 = Torch::NN::Linear.new(8, 2)
  end

  def forward(x)
    x = @fc1.call(x)
    x = Torch::NN::F.relu(x)
    x = @dropout.call(x)
    x = @fc2.call(x)
    Torch::NN::F.softmax(x)
  end
end

device = Torch.device('gpu')
net = MyNet.new.to(device)

classifier = Rumale::Torch::NeuralNetClassifier.new(model: net, device: device, batch_size: 50, max_epoch: 10)
classifier.fit(x, y)

classifier.predict(x)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model:, device: nil, optimizer: nil, loss: nil, batch_size: 128, max_epoch: 10, shuffle: true, validation_split: 0, verbose: false, random_seed: nil) ⇒ NeuralNetClassifier

Create a new classifier with neural nets defined by torch.rb.

Parameters:

  • model (Torch::NN::Module)

    The neural nets defined with torch.rb.

  • device (Torch::Device/Nil) (defaults to: nil)

    The compute device to be used. If nil is given, it to be set to Torch.device(‘cpu’).

  • optimizer (Torch::Optim/Nil) (defaults to: nil)

    The optimizer to be used to optimize the model. If nil is given, it to be set to Torch::Optim::Adam.

  • loss (Torch:NN) (defaults to: nil)

    The loss function to be used to optimize the model. If nil is given, it to be set to Torch::NN::CrossEntropyLoss.

  • batch_size (Integer) (defaults to: 128)

    The number of samples per batch to load.

  • max_epoch (Integer) (defaults to: 10)

    The number of epochs to train the model.

  • shuffle (Boolean) (defaults to: true)

    The flag indicating whether to shuffle the data at every epoch.

  • validation_split (Float) (defaults to: 0)

    The fraction of the training data to be used as validation data.

  • verbose (Boolean) (defaults to: false)

    The flag indicating whether to output loss during epoch.

  • random_seed (Integer/Nil) (defaults to: nil)

    The seed value using to initialize the random generator for data splitting.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rumale/torch/neural_net_classifier.rb', line 79

def initialize(model:, device: nil, optimizer: nil, loss: nil,
               batch_size: 128, max_epoch: 10, shuffle: true, validation_split: 0,
               verbose: false, random_seed: nil)
  super()
  @model = model
  @device = device || ::Torch.device('cpu')
  @optimizer = optimizer || ::Torch::Optim::Adam.new(model.parameters)
  @loss = loss || ::Torch::NN::CrossEntropyLoss.new
  @params = {}
  @params[:batch_size] = batch_size
  @params[:max_epoch] = max_epoch
  @params[:shuffle] = shuffle
  @params[:validation_split] = validation_split
  @params[:verbose] = verbose
  @params[:random_seed] = random_seed || srand
  define_parameter_accessors
end

Instance Attribute Details

#classesNumo::Int32 (readonly)

Return the class labels.

Returns:

  • (Numo::Int32)

    (size: n_classes)



46
47
48
# File 'lib/rumale/torch/neural_net_classifier.rb', line 46

def classes
  @classes
end

#deviceTorch::Device

Return the compute device.

Returns:

  • (Torch::Device)


54
55
56
# File 'lib/rumale/torch/neural_net_classifier.rb', line 54

def device
  @device
end

#lossTorch::NN

Return the loss function.

Returns:

  • (Torch::NN)


62
63
64
# File 'lib/rumale/torch/neural_net_classifier.rb', line 62

def loss
  @loss
end

#modelTorch::NN::Module

Return the neural nets defined with torch.rb.

Returns:

  • (Torch::NN::Module)


50
51
52
# File 'lib/rumale/torch/neural_net_classifier.rb', line 50

def model
  @model
end

#optimizerTorch::Optim

Return the optimizer.

Returns:

  • (Torch::Optim)


58
59
60
# File 'lib/rumale/torch/neural_net_classifier.rb', line 58

def optimizer
  @optimizer
end

Instance Method Details

#decision_function(x) ⇒ Numo::DFloat

Calculate confidence scores for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to compute the scores.

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_classes]) The confidence score per sample.



135
136
137
# File 'lib/rumale/torch/neural_net_classifier.rb', line 135

def decision_function(x)
  Numo::DFloat.cast(::Torch.no_grad { model.call(::Torch.from_numo(x).to(:float32)) }.numo)
end

#fit(x, y) ⇒ NeuralNetClassifier

Fit the model with given training data.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The training data to be used for fitting the model.

  • y (Numo::Int32)

    (shape: [n_samples]) The labels to be used for fitting the model.

Returns:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rumale/torch/neural_net_classifier.rb', line 102

def fit(x, y)
  encoder = Rumale::Preprocessing::LabelEncoder.new
  y_encoded = encoder.fit_transform(y)
  @classes = Numo::NArray[*encoder.classes]

  train_loader, test_loader = prepare_dataset(x, y_encoded)

  model.children.each do |layer|
    layer.reset_parameters if layer.class.method_defined?(:reset_parameters)
  end

  1.upto(max_epoch) do |epoch|
    train(train_loader)
    display_epoch(train_loader, test_loader, epoch) if verbose
  end

  self
end

#predict(x) ⇒ Numo::Int32

Predict class labels for samples.

Parameters:

  • x (Numo::DFloat)

    (shape: [n_samples, n_features]) The samples to predict the labels.

Returns:

  • (Numo::Int32)

    (shape: [n_samples]) Predicted class label per sample.



125
126
127
128
129
# File 'lib/rumale/torch/neural_net_classifier.rb', line 125

def predict(x)
  output = ::Torch.no_grad { model.call(::Torch.from_numo(x).to(:float32)) }
  _, indices = ::Torch.max(output, 1)
  @classes[indices.numo].dup
end