Giter Site home page Giter Site logo

Comments (1)

griegler avatar griegler commented on August 23, 2024

The output avg_f is not the accuracy of the model, but the average loss (cross entropy).
The following code should compute the accuracy

function common.test_epoch(opt, data_loader)
  local net = opt.net or error('no net in test_epoch')
  local criterion = opt.criterion or error('no criterion in test_epoch')
  local n_batches = data_loader:n_batches()

  net:evaluate()

  local avg_f = 0
  local accuracy = 0
  local n_samples = 0
  for batch_idx = 1, n_batches do
    print(string.format('[INFO] test batch %d/%d', batch_idx, n_batches))

    local timer = torch.Timer()
    local input, target = data_loader:getBatch()
    print(string.format('[INFO] loading data took %f[s] - n_batches %d', timer:time().real, target:size(1)))

    local timer = torch.Timer()
    local output = net:forward(input)
    output = output[{{1,target:size(1)}, {}}]
    local f = criterion:forward(output, target)
    print(string.format('[INFO] net/crtrn fwd took %f[s]', timer:time().real))
    avg_f = avg_f + f
    
    local maxs, indices = torch.max(output, 2)
    for bidx = 1, target:size(1) do
      if indices[bidx][1] == target[bidx] then
        accuracy = accuracy + 1
      end
      n_samples = n_samples + 1
    end
  end 
  avg_f = avg_f / n_batches
  accuracy = accuracy / n_samples

  print(string.format('test_epoch=%d, avg_f=%f, accuracy=%f', opt.epoch, avg_f, accuracy))
end

from octnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.