Class: Torchrb::Torch
Direct Known Subclasses
Instance Attribute Summary collapse
-
#error_rate ⇒ Object
Returns the value of attribute error_rate.
-
#network_loaded ⇒ Object
Returns the value of attribute network_loaded.
-
#network_timestamp ⇒ Object
Returns the value of attribute network_timestamp.
Attributes inherited from Lua
Instance Method Summary collapse
- #cudify ⇒ Object
-
#initialize(options = {}) ⇒ Torch
constructor
A new instance of Torch.
- #iteration_callback=(callback) ⇒ Object
- #load_network(network_storage_path) ⇒ Object
- #predict(sample, network_storage_path = nil) ⇒ Object
- #print_results ⇒ Object
- #store_network(network_storage_path) ⇒ Object
- #train ⇒ Object
Methods inherited from Lua
Constructor Details
#initialize(options = {}) ⇒ Torch
Returns a new instance of Torch.
7 8 9 10 11 12 |
# File 'lib/torchrb/torch.rb', line 7 def initialize ={} super @network_loaded = false @error_rate = Float::NAN load_network [:network_storage_path] unless network_loaded rescue nil end |
Instance Attribute Details
#error_rate ⇒ Object
Returns the value of attribute error_rate.
5 6 7 |
# File 'lib/torchrb/torch.rb', line 5 def error_rate @error_rate end |
#network_loaded ⇒ Object
Returns the value of attribute network_loaded.
3 4 5 |
# File 'lib/torchrb/torch.rb', line 3 def network_loaded @network_loaded end |
#network_timestamp ⇒ Object
Returns the value of attribute network_timestamp.
4 5 6 |
# File 'lib/torchrb/torch.rb', line 4 def end |
Instance Method Details
#cudify ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/torchrb/torch.rb', line 102 def cudify eval " -- print(sys.COLORS.red .. '==> using CUDA GPU #' .. cutorch.getDevice() .. sys.COLORS.black)\n train_set.input = train_set.input:cuda()\n train_set.label = train_set.label:cuda()\n test_set.input = test_set.input:cuda()\n test_set.label = test_set.label:cuda()\n validation_set.input = validation_set.input:cuda()\n validation_set.label = validation_set.label:cuda()\n\n criterion = nn.ClassNLLCriterion():cuda()\n net = cudnn.convert(net:cuda(), cudnn)\n EOF\nend\n", __FILE__, __LINE__ |
#iteration_callback=(callback) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/torchrb/torch.rb', line 14 def iteration_callback= callback state.function "iteration_callback" do |trainer, iteration, currentError| progress = iteration / state['number_of_iterations'] self.error_rate = currentError/100.0 callback.call progress: progress, error_rate: error_rate end end |
#load_network(network_storage_path) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/torchrb/torch.rb', line 51 def load_network network_storage_path raise "Neuronal net not trained yet. Call 'Torch#update_training_data'." unless File.exist?(network_storage_path) = eval(" net = torch.load('\#{network_storage_path}')\n metadata = torch.load('\#{network_storage_path}.meta')\n classes = metadata[1]\n timestamp = metadata[3]\n return metadata[2]\n EOF\n self.error_rate = metadata\n self.network_timestamp = @state['timestamp']\n puts \"Network with metadata [\#{@state['classes'].to_h}, \#{error_rate}] loaded from \#{network_storage_path} @ \#{network_timestamp}\" if debug\n self.network_loaded = true\nend\n", __FILE__, __LINE__).to_ruby |
#predict(sample, network_storage_path = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/torchrb/torch.rb', line 36 def predict sample, network_storage_path=nil load_network network_storage_path unless network_loaded classes = eval " \#{sample.to_tensor(\"sample_data\").strip}\n local prediction = \#{enable_cuda ? \"net:forward(sample_data:cuda()):float()\" : \"net:forward(sample_data)\"}\n prediction = prediction:exp()\n confidences = prediction:totable()\n return classes\n EOF\n puts \"predicted \#{@state['confidences'].to_h} based on network @ \#{network_timestamp}\" if debug\n classes = classes.to_h\n @state['confidences'].to_h.map { |k, v| {classes[k] => v} }.reduce({}, :merge)\nend\n", __FILE__, __LINE__ |
#print_results ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/torchrb/torch.rb', line 74 def print_results result = eval " class_performance = torch.LongTensor(#classes):fill(0):totable()\n test_set_size = test_set:size()\n for i=1,test_set_size do\n local groundtruth = test_set.label[i]\n local prediction = net:forward(test_set.input[i])\n local confidences, indices = torch.sort(prediction, true) -- true means sort in descending order\n\n class_performance[groundtruth] = class_performance[groundtruth] + 1\n\n end\n\n local result = {}\n for i=1,#classes do\n local confidence = 100*class_performance[i]/test_set_size\n table.insert(result, { classes[i], confidence } )\n end\n return result\n EOF\n result = result.to_ruby.map(&:to_ruby)\n if defined?(DEBUG)\n puts \"#\" * 80\n puts \"Results: \#{result.to_h}\"\n puts \"#\" * 80\n end\nend\n", __FILE__, __LINE__ |
#store_network(network_storage_path) ⇒ Object
66 67 68 69 70 71 72 |
# File 'lib/torchrb/torch.rb', line 66 def store_network network_storage_path eval " torch.save('\#{network_storage_path}', net)\n torch.save('\#{network_storage_path}.meta', {classes, \#{error_rate}, '\#{network_timestamp}}'} )\n EOF\n puts \"Network with metadata [\#{@state['classes'].to_h}, \#{error_rate}] stored in \#{network_storage_path} @ \#{network_timestamp}\" if debug\nend\n", __FILE__, __LINE__ |
#train ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/torchrb/torch.rb', line 22 def train eval " local oldprint = print\n print = function(...)\n end\n\n trainer:train(train_set)\n\n print = oldprint\n EOF\n self.network_loaded = true\n self.network_timestamp = Time.now\nend\n", __FILE__, __LINE__ |