class SimpleCNN(torch.nn.Module):
#Our batch shape for input x is (3, 64, 64)
def __init__(self):
super().__init__()
#Input channels = 3, output channels = 32
self.conv1 = torch.nn.Conv2d(3, 32, kernel_size=3, stride=1, padding=1)
self.pool = torch.nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
#4608 input features, 64 output features (see sizing flow below)
self.fc1 = torch.nn.Linear(32*32*32, 64)
#64 input features, 2 output features for our 2 defined classes
self.fc2 = torch.nn.Linear(64, 2)
def forward(self, x):
#Computes the activation of the first convolution
#Size changes from (3, 64, 64) to (32, 64, 64)
x = F.relu(self.conv1(x))
#Size changes from (32, 64, 64) to (32, 32, 32)
x = self.pool(x)
#Reshape data to input to the input layer of the neural net
#Size changes from (18, 16, 16) to (1, 4608)
#Recall that the -1 infers this dimension from the other given dimension
x = x.view(-1, 32*32*32)
#Computes the activation of the first fully connected layer
#Size changes from (1, 32*32*32) to (1, 64)
x = F.relu(self.fc1(x))
#Computes the second fully connected layer (activation applied later)
#Size changes from (1, 64) to (1, 2)
x = self.fc2(x)
return x
model=SimpleCNN()
model.double()
criterion=nn.CrossEntropyLoss().cuda()
optimizer=torch.optim.Adam(model.parameters(),lr=0.1)
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
epochs=3
model.to(device)
for e in range(epochs):
train_loss=0
valid_loss=0
model.train()
for inputs,labels in train_loader:
model.zero_grad()
print('before ')
p=next(model.parameters())
print(p[0,0,0,0])
inputs.requires_grad=True
inputs=inputs.to(device)
labels=labels.to(device)
outputs=model(inputs)
print#(outputs.shape,labels.shape)
loss=criterion(outputs,labels)
print(loss)
#train_loss+=loss.item()
loss.backward()
# print(optimizer)
optimizer.step()
print('after ')
p=next(model.parameters())
print(p[0,0,0,0])
model.eval()
for inputs,labels in valid_loader:
inputs=inputs.to(device)
labels=labels.to(device)
outputs=model(inputs)
loss=criterion(outputs,labels)
valid_loss+=loss.item()
#train_loss/=len(train_loader)
#valid_loss/=len(valid_loader)
print(f'Epoch {e}: train loss= {train_loss} valid loss={valid_loss}')
my weights are not updating.Can someone please help me.
Using a random dataset, your weights seem to upgrade fine:
train_loader = DataLoader(
torch.utils.data.TensorDataset(
torch.randn(10, 3, 64, 64),
torch.randint(0, 2, (10,))
)
)