Skip to content

Commit

Permalink
テストデータによる推論を実装した
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaken0515 committed Nov 9, 2019
1 parent f3cad0f commit b5082b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
30 changes: 30 additions & 0 deletions examples/iris/inference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require_relative 'mlp'
require_relative 'dataset'
require 'optparse'

snapshot_filename = nil
opt = OptionParser.new
opt.on('-r', '--resume VALUE', "Resume the training from snapshot") { |v| snapshot_filename = v }
opt.parse!(ARGV)

# 学習させた時と同じモデルを定義
predictor = MLP.new

# スナップショットをモデルに読み込む
Chainer::Serializers::MarshalDeserializer.load_file(snapshot_filename, predictor, path: '/updater/model:main/@predictor/')

# テスト用のデータセットを取得
test_dataset = Dataset.get_iris

pass_count = 0
(0...test_dataset.size).each do |i|
variables, answer = test_dataset[i]

# 変数をモデルに与えて推論結果を取得
prediction = predictor.(variables).data.argmax
pass_count += 1 if prediction == answer

print format("test%03d: prediction = %d, answer = %d\n",i, prediction, answer)
end

print "accuracy: #{pass_count * 100.0 / test_dataset.size}\n"
4 changes: 0 additions & 4 deletions examples/iris/iris.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@

# --------------- 訓練の開始 -----------------
trainer.run

# --------------- testデータを使った推論 -----------------
# todo
# `net` や `net.predictor` を良い感じに使ったら推論できる?

0 comments on commit b5082b3

Please sign in to comment.