Class: Rumale::Torch::NeuralNetRegressor
- Inherits:
-
Base::Estimator
- Object
- Base::Estimator
- Rumale::Torch::NeuralNetRegressor
- 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.
Instance Attribute Summary collapse
-
#device ⇒ Torch::Device
Return the compute device.
-
#loss ⇒ Torch::NN
Return the loss function.
-
#model ⇒ Torch::NN::Module
Return the neural nets defined with torch.rb.
-
#optimizer ⇒ Torch::Optim
Return the optimizer.
Instance Method Summary collapse
-
#fit(x, y) ⇒ NeuralNetRegressor
Fit the model with given training data.
-
#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
constructor
Create a new regressor with neural nets defined by torch.rb.
-
#predict(x) ⇒ Numo::DFloat
Predict values for samples.
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.
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
#device ⇒ Torch::Device
Return the compute device.
48 49 50 |
# File 'lib/rumale/torch/neural_net_regressor.rb', line 48 def device @device end |
#loss ⇒ Torch::NN
Return the loss function.
56 57 58 |
# File 'lib/rumale/torch/neural_net_regressor.rb', line 56 def loss @loss end |
#model ⇒ Torch::NN::Module
Return the neural nets defined with torch.rb.
44 45 46 |
# File 'lib/rumale/torch/neural_net_regressor.rb', line 44 def model @model end |
#optimizer ⇒ Torch::Optim
Return the optimizer.
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.
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.(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.
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 |