a = 'hello' print(a) b = {} b[1] = a print(b) b[2] = 30 for i=1,#b do -- '#'는 루아에서 요소의 개수를 리턴하는 연산자임. print(b[i]) end a = torch.Tensor(5,3) -- 5x3 행렬 생성, 초기화되지 않음. a = torch.rand(5,3) print(a) b=torch.rand(3,4) -- 행렬-행렬 곱: 문법 1 a*b -- 행렬-행렬 곱: 문법 2 torch.mm(a,b) -- 행렬-행렬 곱: 문법 3 c=torch.Tensor(5,4) c:mm(a,b) -- a*b 결과를 c에 저장 require 'cutorch'; a = a:cuda() b = b:cuda() c = c:cuda() c:mm(a,b) -- GPU에서 계산됨 function addTensors(a,b) return a -- a로 고정 end a = torch.ones(5,2) b = torch.Tensor(3,4):fill(4) print(addTensors(a,b)) require 'nn'; net = nn.Sequential() net:add(nn.SpatialConvolution(1, 6, 5, 5)) -- 1 입력 영상 채널, 6 출력 채널, 5x5 컨볼루션 커널 net:add(nn.SpatialMaxPooling(2,2,2,2)) -- 2x2 윈도우들을 통해 보는 맥스 풀링(max-pooling) 연산 그리고 그 각 윈도우에서 최대값을 찾음. net:add(nn.SpatialConvolution(6, 16, 5, 5)) net:add(nn.SpatialMaxPooling(2,2,2,2)) net:add(nn.View(16*5*5)) -- 16x5x5의 3 차원 텐서를 16*5*5의 1 차원 텐서로 변환 net:add(nn.Linear(16*5*5, 120)) -- 완전 연결된(fully connected) 층 (입력과 가중치들 사이의 행렬 곱) net:add(nn.Linear(120, 84)) net:add(nn.Linear(84, 10)) -- 10 은 출력 층의 노드 수. net:add(nn.LogSoftMax()) -- 그 출력을 로그 확률로 변환. 분류 문제들에 유용함. print('Lenet5\n' .. net:__tostring()); input = torch.rand(1,32,32) -- 한 랜덤 텐서를 네트워크 입력으로 전달 output = net:forward(input) print(output) net:zeroGradParameters() -- 네트워크의 기울기(gradient) 버퍼들을 0으로 초기화 (나중에 다시 나옴) gradInput = net:backward(input, torch.rand(10)) print(#gradInput) criterion = nn.ClassNLLCriterion() -- 다부류 분류를 위한 negative log-likelihood criterion criterion:forward(output, 3) -- 정답이 부류 넘버 3이라고 합시다. gradients = criterion:backward(output, 3) gradInput = net:backward(input, gradients) m = nn.SpatialConvolution(1,3,2,2) -- 새 개의 2x2 커널들 학습 print(m.weight) -- 초기에, 가중치들은 랜덤하게 초기화됨 print(m.bias) -- 한 컨볼루션 층에서 이 연산은: output = convolution(input,weight) + bias os.execute('wget -c https://s3.amazonaws.com/torch7/data/cifar10torchsmall.zip') os.execute('unzip cifar10torchsmall.zip') trainset = torch.load('cifar10-train.t7') testset = torch.load('cifar10-test.t7') classes = {'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'} print(trainset) print(#trainset.data) itorch.image(trainset.data[100]) -- 데이터세트 안의 100번 째 영상을 디스플레이 print(classes[trainset.label[100]]) -- 지금은 setmetatable을 무시하십시오, 이 튜토리얼의 범위를 벗어나는 특성이기 때문입니다. setmetatable은 인덱스 연산자를 셋팅합니다. setmetatable(trainset, {__index = function(t, i) return {t.data[i], t.label[i]} end} ); trainset.data = trainset.data:double() -- 데이터를 ByteTensor에서 DoubleTensor로 변환합니다. function trainset:size() return self.data:size(1) end print(trainset:size()) -- 단지 테스트 목적 print(trainset[33]) -- 33번 샘플 로드. itorch.image(trainset[33][1]) redChannel = trainset.data[{ {}, {1}, {}, {} }] -- 이것은 {모든 영상들, 첫 번째 채널, 모든 수직 픽셀들, 모든 수평 픽셀들}을 선택합니다. print(#redChannel) -- 여기에 코드를 적고 실행해 보세요. 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 net = nn.Sequential() net:add(nn.SpatialConvolution(3, 6, 5, 5)) -- 3 입력 영상 채널, 6 출력 채널, 5x5 컨볼루션 커널 net:add(nn.SpatialMaxPooling(2,2,2,2)) -- 2x2 윈도우들을 통해 보면서 최댓값을 찾는 맥스풀링 연산 net:add(nn.SpatialConvolution(6, 16, 5, 5)) net:add(nn.SpatialMaxPooling(2,2,2,2)) net:add(nn.View(16*5*5)) -- 16x5x5 3 차원 텐서를 16*5*5 1차원 텐서로 변환 net:add(nn.Linear(16*5*5, 120)) -- 완전 연결 층(fully connected layer) (입력과 가중치들 사이의 행렬 곱) net:add(nn.Linear(120, 84)) net:add(nn.Linear(84, 10)) -- 10은 네트워크 출력의 개수 net:add(nn.LogSoftMax()) -- 출력을 로그 확률로 변환. 분류 문제에 유용함. criterion = nn.ClassNLLCriterion() trainer = nn.StochasticGradient(net, criterion) trainer.learningRate = 0.001 trainer.maxIteration = 5 -- 훈련을 위해 5 에포크(epoch)만 반복. trainer:train(trainset) print(classes[testset.label[100]]) itorch.image(testset.data[100]) testset.data = testset.data:double() -- Byte tensor를 Double tensor로 변환 for i=1,3 do -- 각 영상 채널에 대해 testset.data[{ {}, {i}, {}, {} }]:add(-mean[i]) -- 평균 빼기 testset.data[{ {}, {i}, {}, {} }]:div(stdv[i]) -- 표준 편차 스케일링 end -- 재미를 위해, 100번째 예제의 평균과 표준 편차 출력 horse = testset.data[100] print(horse:mean(), horse:std()) print(classes[testset.label[100]]) itorch.image(testset.data[100]) predicted = net:forward(testset.data[100]) -- 네트워크 출력은 로그 확률들 입니다. 그것들을 확률들로 바꾸기 위해 exp(x)를 취합니다. print(predicted:exp()) for i=1,predicted:size(1) do print(classes[i], predicted[i]) end 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) -- 실제 평균들, 내림 차순으로 정렬 if groundtruth == indices[1] then correct = correct + 1 end end print(correct, 100*correct/10000 .. ' % ') 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 require 'cunn' net = net:cuda() criterion = criterion:cuda() trainset.data = trainset.data:cuda() trainer = nn.StochasticGradient(net, criterion) trainer.learningRate = 0.001 trainer.maxIteration = 5 -- 훈련을 위해 5 에포크(epoch)만 반복합니다. trainer:train(trainset)