{"id":19466138,"url":"https://github.com/yoshoku/rumale-torch","last_synced_at":"2025-04-25T09:32:31.494Z","repository":{"id":56893800,"uuid":"322865997","full_name":"yoshoku/rumale-torch","owner":"yoshoku","description":"Rumale::Torch provides the learning and inference by the neural network defined in torch.rb with the same interface as Rumale.","archived":false,"fork":false,"pushed_at":"2025-01-02T12:06:40.000Z","size":77,"stargazers_count":16,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-03T19:51:10.151Z","etag":null,"topics":["deep-learning","gem","machine-learning","ruby","rumale"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yoshoku.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-19T14:28:53.000Z","updated_at":"2025-03-29T21:16:03.000Z","dependencies_parsed_at":"2024-06-19T02:52:51.261Z","dependency_job_id":"505a247f-3a05-4b36-85b8-4a3fe0c841ee","html_url":"https://github.com/yoshoku/rumale-torch","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Frumale-torch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Frumale-torch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Frumale-torch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yoshoku%2Frumale-torch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yoshoku","download_url":"https://codeload.github.com/yoshoku/rumale-torch/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250790244,"owners_count":21487772,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deep-learning","gem","machine-learning","ruby","rumale"],"created_at":"2024-11-10T18:25:49.632Z","updated_at":"2025-04-25T09:32:31.199Z","avatar_url":"https://github.com/yoshoku.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rumale::Torch\n\n[![Build Status](https://github.com/yoshoku/rumale-torch/workflows/build/badge.svg)](https://github.com/yoshoku/rumale-torch/actions?query=workflow%3Abuild)\n[![Gem Version](https://badge.fury.io/rb/rumale-torch.svg)](https://badge.fury.io/rb/rumale-torch)\n[![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/rumale-torch/blob/main/LICENSE.txt)\n[![Documentation](https://img.shields.io/badge/api-reference-blue.svg)](https://yoshoku.github.io/rumale-torch/doc/)\n\nRumale::Torch provides the learning and inference by the neural network defined in [torch.rb](https://github.com/ankane/torch.rb)\nwith the same interface as [Rumale](https://github.com/yoshoku/rumale).\n\n## Installation\ntorch.rb is a runtime dependent gem of Rumale::Torch. It requires to install [LibTorch](https://github.com/ankane/torch.rb#libtorch-installation):\n\n    $ brew install automake libtorch\n\nHere, automake is needed to install [rice](https://github.com/jasonroelofs/rice) gem, which torch.rb depends on.\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'rumale-torch'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install rumale-torch\n\n## Usage\n\n### Example 1. Pendigits dataset classification\n\nWe start by downloading the pendigits dataset from [LIBSVM Data](https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/) web site.\n\n```bash\n$ wget https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits\n$ wget https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/multiclass/pendigits.t\n```\n\nTraining phase:\n\n```ruby\nrequire 'rumale'\nrequire 'rumale/torch'\n\nTorch.manual_seed(1)\ndevice = Torch.device('cpu')\n\n# Loading pendigits dataset consisting of\n# 16-dimensional data divided into 10 classes.\nx, y = Rumale::Dataset.load_libsvm_file('pendigits')\n\n# Define a neural network in torch.rb framework.\nclass MyNet \u003c Torch::NN::Module\n  def initialize\n    super\n    @dropout = Torch::NN::Dropout.new(p: 0.5)\n    @fc1 = Torch::NN::Linear.new(16, 128)\n    @fc2 = Torch::NN::Linear.new(128, 10)\n  end\n\n  def forward(x)\n    x = @fc1.call(x)\n    x = Torch::NN::F.relu(x)\n    x = @dropout.call(x)\n    x = @fc2.call(x)\n    Torch::NN::F.softmax(x)\n  end\nend\n\nnet = MyNet.new.to(device)\n\n# Create a classifier with neural network model.\nclassifier = Rumale::Torch::NeuralNetClassifier.new(\n  model: net, device: device,\n  batch_size: 10, max_epoch: 50, validation_split: 0.1,\n  verbose: true\n)\n\n# Learning classifier.\nclassifier.fit(x, y)\n\n# Saving model.\nTorch.save(net.state_dict, 'pendigits.pth')\nFile.binwrite('pendigits.dat', Marshal.dump(classifier))\n```\n\nTesting phase:\n\n```ruby\nrequire 'rumale'\nrequire 'rumale/torch'\n\n# Loading neural network model.\nclass MyNet \u003c Torch::NN::Module\n  def initialize\n    super\n    @dropout = Torch::NN::Dropout.new(p: 0.5)\n    @fc1 = Torch::NN::Linear.new(16, 128)\n    @fc2 = Torch::NN::Linear.new(128, 10)\n  end\n\n  def forward(x)\n    x = @fc1.call(x)\n    x = Torch::NN::F.relu(x)\n    # x = @dropout.call(x)\n    x = @fc2.call(x)\n    Torch::NN::F.softmax(x)\n  end\nend\n\nnet = MyNet.new\nnet.load_state_dict(Torch.load('pendigits.pth'))\n\n# Loading classifier.\nclassifier = Marshal.load(File.binread('pendigits.dat'))\nclassifier.model = net\n\n# Loading test dataset.\nx_test, y_test = Rumale::Dataset.load_libsvm_file('pendigits.t')\n\n# Predict labels of test data.\np_test = classifier.predict(x_test)\n\n# Evaluate predicted result.\naccuracy = Rumale::EvaluationMeasure::Accuracy.new.score(y_test, p_test)\nputs(format(\"Accuracy: %2.1f%%\", accuracy * 100))\n```\n\nThe result of executing the above scripts is as follows:\n\n```sh\n$ ruby train.rb\nepoch:  1/50 - loss: 0.2073 - accuracy: 0.3885 - val_loss: 0.2074 - val_accuracy: 0.3853\nepoch:  2/50 - loss: 0.1973 - accuracy: 0.4883 - val_loss: 0.1970 - val_accuracy: 0.4893\nepoch:  3/50 - loss: 0.1962 - accuracy: 0.4997 - val_loss: 0.1959 - val_accuracy: 0.5013\n\n...\n\nepoch: 50/50 - loss: 0.1542 - accuracy: 0.9199 - val_loss: 0.1531 - val_accuracy: 0.9293\n\n$ ruby test.rb\nAccuracy: 91.2%\n```\n\n### Example 2. Cross-validation with Rumale\n\nPerform 5-fold cross-validation for regression problem using the housing dataset.\n\n```sh\n$ wget https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression/housing\n```\n\nThe example script:\n\n```ruby\nrequire 'rumale'\nrequire 'rumale/torch'\n\nTorch.manual_seed(1)\ndevice = Torch.device('cpu')\n\n# Loading pendigits dataset consisting of\n# 13-dimensional data with single target variable.\nx, y = Rumale::Dataset.load_libsvm_file('housing')\n\n# Define a neural network in torch.rb framework.\nclass MyNet \u003c Torch::NN::Module\n  def initialize\n    super\n    @fc1 = Torch::NN::Linear.new(13, 128)\n    @fc2 = Torch::NN::Linear.new(128, 1)\n  end\n\n  def forward(x)\n    x = @fc1.call(x)\n    x = Torch::NN::F.relu(x)\n    x = @fc2.call(x)\n  end\nend\n\nnet = MyNet.new.to(device)\n\n# Create a regressor with neural network model.\nregressor = Rumale::Torch::NeuralNetRegressor.new(\n  model: net, device: device, batch_size: 10, max_epoch: 100\n)\n\n# Create evaluation measure, splitting strategy, and cross validation.\nev = Rumale::EvaluationMeasure::R2Score.new\nkf = Rumale::ModelSelection::ShuffleSplit.new(n_splits: 5, test_size: 0.1, random_seed: 1)\ncv = Rumale::ModelSelection::CrossValidation.new(estimator: regressor, splitter: kf, evaluator: ev)\n\n# Perform 5-cross validation.\nreport = cv.perform(x, y)\n\n# Output result.\nmean_score = report[:test_score].sum / kf.n_splits\nputs(format(\"5-CV R2-score: %.3f\", mean_score))\n```\n\nThe execution result is as follows:\n\n```ruby\n$ ruby cv.rb\n5-CV R2-score: 0.755\n```\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/rumale-torch.\nThis project is intended to be a safe, welcoming space for collaboration,\nand contributors are expected to adhere to the [Contributor Covenant](https://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshoku%2Frumale-torch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyoshoku%2Frumale-torch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyoshoku%2Frumale-torch/lists"}