torch学习笔记(1)

    xiaoxiao2021-03-25  73

    参考博客地址:

    http://blog.csdn.net/u010946556/article/details/51395648

    https://github.com/soumith/cvpr2015/blob/master/Deep Learning with Torch.ipynb

    在CIFAR-10数据集上,构建一个卷积神经网络,用于分类

    require 'paths'; require 'nn'; --Load TrainSet paths.filep("/home/ljz/torch/data/cifar10torchsmall.zip") trainset = torch.load('/home/ljz/torch/data/cifar10-train.t7') testset = torch.load('/home/ljz/torch/data/cifar10-test.t7') classes = {'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'} --Add size() function and Tensor index operator setmetatable(trainset, {__index = function(t, i) return {t.data[i], t.label[i]} end} ); trainset.data = trainset.data:double() function trainset:size() return self.data:size(1) end --Normalize data mean = {} stdv = {} for i=1,3 do mean[i] = trainset.data[{ {}, {i}, {}, {} }]:mean() print('Channel ' .. i .. ', Mean: ' .. mean[i]) trainset.data[{ {}, {i}, {}, {} }]:add(-mean[i]) stdv[i] = trainset.data[{ {}, {i}, {}, {} }]:std() print('Channel ' .. i .. ', Standard Deviation:' .. stdv[i]) trainset.data[{ {}, {i}, {}, {} }]:div(stdv[i]) end --Define Network net = nn.Sequential() --change 1 channel to 3 channels --net:add(nn.SpatialConvolution(1, 6, 5, 5)) net:add(nn.SpatialConvolution(3, 6, 5, 5)) net:add(nn.ReLU()) net:add(nn.SpatialMaxPooling(2,2,2,2)) net:add(nn.SpatialConvolution(6, 16, 5, 5)) net:add(nn.ReLU()) net:add(nn.SpatialMaxPooling(2,2,2,2)) net:add(nn.View(16*5*5)) net:add(nn.Linear(16*5*5, 120)) net:add(nn.ReLU()) net:add(nn.Linear(120, 84)) net:add(nn.ReLU()) net:add(nn.Linear(84, 10)) net:add(nn.LogSoftMax()) --Define criterion criterion = nn.ClassNLLCriterion(); --StochasticGradient with Criterion trainer = nn.StochasticGradient(net, criterion) trainer.learningRate = 0.001 trainer.maxIteration = 5 trainer:train(trainset) --test --normalize test data testset.data=testset.data:double(); for i=1,3 do testset.data[{ {},{i},{},{} }]:add(-mean[i]) testset.data[{ {},{i},{},{} }]:div(stdv[i]) end --predict test and print confidences print(classes[testset.label[100]]) --itorch.image(testset.data[100]) predicted=net:forward(testset.data[100]) print(predicted:exp()) --sort confidence and print predicted result confidences, indices = torch.sort(predicted, true) print(confidences[1]) print(indices[1]) print(classes[indices[1]]) --correct rate in total correct = 0 for i=1,10000 do local groundtruth = testset.label[i] local prediction = net:forward(testset.data[i]) local confidences, indices = torch.sort(prediction, true) -- true means sort in descending order if groundtruth == indices[1] then correct = correct + 1 end end print(correct, 100*correct/10000 .. ' % ') --correct rate every class class_performance = {0,0,0,0,0,0,0,0,0,0} for i = 1,10000 do local groundtruth = testset.label[i] local prediction = net:forward(testset.data[i]) local confidences, indices = torch.sort(prediction, true) if groundtruth == indices[1] then class_performance[groundtruth] = class_performance[groundtruth] + 1 end end for i = 1, #classes do print(classes[i], 100*class_performance[i]/1000 .. '%') end

    出现的错误:

    在运行原博代码时报错:cannot open <cifar10-train.t7> in mode r

    解决方法:在torch.load中加入全路径解决。

    itorch.image(testset.data[100])显示图片出错

    不知道怎么解决,最后只能把这句注释掉了

    转载请注明原文地址: https://ju.6miu.com/read-18407.html

    最新回复(0)