Class: Rumale::Torch::NeuralNetRegressor

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

Overview

NeuralNetRegressor is a class that provides learning and inference by the neural network defined in torch.rb with an interface similar to regressor 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, 64)
    @fc2 = Torch::NN::Linear.new(64, 1)
  end

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

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

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

regressor.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) ⇒ NeuralNetRegressor

Create a new regressor 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::MSELoss.

  • 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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rumale/torch/neural_net_regressor.rb', line 73

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::MSELoss.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

#deviceTorch::Device

Return the compute device.

Returns:

  • (Torch::Device)


48
49
50
# File 'lib/rumale/torch/neural_net_regressor.rb', line 48

def device
  @device
end

#lossTorch::NN

Return the loss function.

Returns:

  • (Torch::NN)


56
57
58
# File 'lib/rumale/torch/neural_net_regressor.rb', line 56

def loss
  @loss
end

#modelTorch::NN::Module

Return the neural nets defined with torch.rb.

Returns:

  • (Torch::NN::Module)


44
45
46
# File 'lib/rumale/torch/neural_net_regressor.rb', line 44

def model
  @model
end

#optimizerTorch::Optim

Return the optimizer.

Returns:

  • (Torch::Optim)


52
53
54
# File 'lib/rumale/torch/neural_net_regressor.rb', line 52

def optimizer
  @optimizer
end

Instance Method Details

#fit(x, y) ⇒ NeuralNetRegressor

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::DFloat)

    (shape: [n_samples, n_outputs]) The target values to be used for fitting the model.

Returns:



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/rumale/torch/neural_net_regressor.rb', line 96

def fit(x, y)
  y = y.expand_dims(1) if y.ndim == 1

  train_loader, test_loader = prepare_dataset(x, y)

  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::DFloat

Predict values for samples.

Parameters:

  • x (Numo::DFloat)

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

Returns:

  • (Numo::DFloat)

    (shape: [n_samples, n_outputs]) The predicted values per sample.



117
118
119
120
# File 'lib/rumale/torch/neural_net_regressor.rb', line 117

def predict(x)
  output = Numo::DFloat.cast(::Torch.no_grad { model.call(::Torch.from_numo(x).to(:float32)) }.numo)
  output.shape[1] == 1 ? output[true, 0].dup : output
end