The total loss and the inference result will be the same value every time.
gpu cpu is working normally
detaset CIFAR100
Please tell me if there is not enough information. I will add it.
import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data import Dataset
import time
class Firttan(nn.Module):
def init(self):
super().init()
def forward(self, inputs):
inputs=inputs.view(inputs.size(0),-1)
return inputs
class MYoptimizer():
def init(self):
self.tortal_losses=0
self.optimizer = torch.optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
self.scheduler = optim.lr_scheduler.StepLR(self.optimizer, step_size=10, gamma=0.9)
self.criterion = torch.nn.CrossEntropyLoss()
def loss(self,inputs, labels):
    self.optimizer.zero_grad()
    outputs=net(inputs)
    loss=self.criterion(outputs, labels)
    loss.backward()
    torch.nn.utils.clip_grad_norm_(net.parameters(),1,2)
    self.optimizer.step()
    self.tortal_losses+=loss.to('cpu').item()
def Done(self,epoc,timee):
    print("Epoch",epoc,"total_loss",self.tortal_losses,timee)
    self.tortal_losses=0
    self.scheduler.step()

class swish(nn.Module):
def init(self, beta = 1.25):
super().init()
self.beta = beta
def forward(self, inputs):
return inputs * torch.sigmoid(self.beta * inputs)
class DataSet:
def init(self,detas,labels):
self.X = detas # 入力
self.t = labels # 出力
def __len__(self):
    return len(self.X) # データ数(10)を返す

def __getitem__(self, index):
    # index番目の入出力ペアを返す
    return self.X[index], self.t[index]

class Net(torch.nn.Module):
def __init__(self):
    super(Net, self).__init__()
    
    self.conv1 = nn.Sequential(nn.Conv2d(3, 16, 3, padding=1),swish(),
                               nn.Conv2d(16, 16, 3, padding=1),swish(),
                               nn.MaxPool2d(2, 2),swish(),
                               nn.Conv2d(16, 32, 3, padding=1),swish(),
                               nn.Conv2d(32, 32, 3, padding=1),swish(),
                               nn.MaxPool2d(2, 2),swish(),
                               Firttan(),
                               nn.Linear(32 * 8* 8, 512),
                               swish(),
                               nn.Linear(512, 100),
                               nn.Softmax(dim=1))

def forward(self, x):
    x = self.conv1(x)
    return x

def eval_(dataloader):
correct = 0
total = 0
with torch.no_grad():
for (images, labels) in dataloader:
images, labels = images, labels
        outputs = net(images.to('cuda:0'))
        _, predicted = torch.max(outputs.to('cpu').data, 1)
        correct += (predicted == labels).sum().item()
        total += labels.size(0)

print("Val Acc",(correct/total))

cifar100_data = torchvision.datasets.CIFAR100(
‘./cifar-100’, train=True, download=True,
transform=torchvision.transforms.ToTensor())
さっき作ったDataSetクラスのインスタンスを作成
datasetをDataLoaderの引数とすることでミニバッチを作成．
batch_size=128
n_epochs = 50
dataloader = torch.utils.data.DataLoader(cifar100_data, batch_size=batch_size,shuffle=True,drop_last=True)
net = Net().to(‘cuda:0’)
net.train()
MYoptimizer=MYoptimizer()
print(“start”)
for _ in range(n_epochs):
timee=time.time()
net.train()
for inputs, labels in dataloader:
MYoptimizer.loss(inputs.to(‘cuda:0’),labels.to(‘cuda:0’))
MYoptimizer.Done(,time.time()-timee)
#テスト
net.eval()
eval(dataloader)
print(“a”)
result
Files already downloaded and verified
start
Epoch 0 total_loss 1796.0163736343384 8.27594780921936
Val Acc 0.009995993589743589
Epoch 1 total_loss 1796.0164184570312 7.108090877532959
Val Acc 0.009935897435897435
Epoch 2 total_loss 1796.0164275169373 7.067081689834595
Val Acc 0.009995993589743589
Epoch 3 total_loss 1796.0164308547974 7.0995776653289795
Val Acc 0.009995993589743589
I can’t display the code well
How can my site recognize it as a code?