In this problem, you will try to build a three-layer fully connected network to approximate the sin function using PyTorch. There are 100 hidden units in each layer and please use ReLU activation after the first two layers and no activation after the last layer. You should implement your network model as a subclass of torch.nn.Module
import torch
from torch import nn
import numpy as np
import random
class Mynetwork(torch.nn.Module):
def __init__(self):
#-- complete the code here
#-- end complete the code here
def forward(self,x):
#-- complete the code here
#-- end complete the code here
model = Mynetwork()
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters())
for epoch in range(10000):
x=10. * torch.rand((200,1)).type(torch.FloatTensor)
y=torch.sin(x).view(-1)
optimizer.zero_grad()
yh = model(x).view(-1)
loss = loss_fn(yh,y)
loss.backward()
optimizer.step()
if epoch % 1000 == 999:
print(loss.item())
0.0034203710965812206 0.00027573294937610626 0.0002785267715808004 9.57260635914281e-05 0.00017568762996234 0.0004204694414511323 0.00021288888819981366 0.00022242884733714163 3.210400973330252e-05 6.517337169498205e-05
from matplotlib import pyplot as plt
xv=np.array(range(100))/50. *np.pi
yv=model(torch.from_numpy(xv).type(torch.FloatTensor).view((100,-1))).view(-1).detach().numpy()
plt.plot(xv,yv,label='network')
plt.plot(xv,np.sin(xv),label='sin(x)')
plt.legend()
<matplotlib.legend.Legend at 0x7f59bd255bb0>
# Hint: model = torch.nn.Sequential(...)
#-- complete the code here
#-- end complete the code here
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters())
for epoch in range(10000):
x=10. * torch.rand((200,1)).type(torch.FloatTensor)
y=torch.sin(x).view(-1)
optimizer.zero_grad()
yh = model(x).view(-1)
loss = loss_fn(yh,y)
loss.backward()
optimizer.step()
if epoch % 1000 == 999:
print(loss.item())
0.027381647378206253 0.0002204020565841347 0.0029236513655632734 7.226680463645607e-05 0.00018450863717589527 0.000196704117115587 0.00015346996951848269 0.0014713072450831532 4.805776916327886e-05 0.0011197581188753247
from matplotlib import pyplot as plt
xv=np.array(range(100))/50. *np.pi
yv=model(torch.from_numpy(xv).type(torch.FloatTensor).view((100,-1))).view(-1).detach().numpy()
plt.plot(xv,yv,label='network')
plt.plot(xv,np.sin(xv),label='sin(x)')
plt.legend()
<matplotlib.legend.Legend at 0x7f59bcb386d0>
In this problem, you will implement a network to classify different types of clothing. We will play with the FachionMNIST dataset, which contains ten different classes.
Your network should contain three conv layers with 6, 16, and 32 filters (all 3x3 and no padding), respectively. You should use ReLU activation after each conv layer followed by 2x2 max pooling. There will be two fully connected layers after the last pooling layer. The first fully connected layer has 64 neurons.
Answer:
Answer:
conv 1:
pooling 1:
conv2:
pooling 2:
conv3:
pooling 3:
fully-connected 1:
fully-connected 2:
import torch
import torchvision
import torchvision.transforms as transforms
use_cuda = False # set it to True if you have GPU and want to use it
transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))])
# Create datasets for training & validation, download if necessary
training_set = torchvision.datasets.FashionMNIST('./data', train=True, transform=transform, download=True)
validation_set = torchvision.datasets.FashionMNIST('./data', train=False, transform=transform, download=True)
# Create data loaders for our datasets; shuffle for training, not for validation
training_loader = torch.utils.data.DataLoader(training_set, batch_size=4, shuffle=True, num_workers=2)
validation_loader = torch.utils.data.DataLoader(validation_set, batch_size=4, shuffle=False, num_workers=2)
# Class labels
classes = ('T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle Boot')
# Report split sizes
print('Training set has {} instances'.format(len(training_set)))
print('Validation set has {} instances'.format(len(validation_set)))
Training set has 60000 instances Validation set has 10000 instances
import matplotlib.pyplot as plt
import numpy as np
# Helper function for inline image display
def matplotlib_imshow(img, one_channel=False):
if one_channel:
img = img.mean(dim=0)
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
if one_channel:
plt.imshow(npimg, cmap="Greys")
else:
plt.imshow(np.transpose(npimg, (1, 2, 0)))
dataiter = iter(training_loader)
images, labels = dataiter.next()
# Create a grid from the images and show them
img_grid = torchvision.utils.make_grid(images)
matplotlib_imshow(img_grid, one_channel=True)
print(' '.join(classes[labels[j]] for j in range(4)))
Sneaker Bag Dress Ankle Boot
import torch.nn as nn
import torch
import torch.nn.functional as F
class GarmentClassifier(nn.Module):
def __init__(self):
#-- complete the code here
#-- end complete the code here
def forward(self, x):
#-- complete the code here
#-- end complete the code here
if torch.cuda.is_available() and use_cuda:
model = GarmentClassifier().cuda()
else:
model = GarmentClassifier()
loss_fn = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
def train_one_epoch(epoch_index):
running_loss = 0.
last_loss = 0.
# Here, we use enumerate(training_loader) instead of
# iter(training_loader) so that we can track the batch
# index and do some intra-epoch reporting
for i, data in enumerate(training_loader):
# Every data instance is an input + label pair
inputs, labels = data
if torch.cuda.is_available() and use_cuda:
inputs=inputs.cuda()
labels=labels.cuda()
# Zero your gradients for every batch!
optimizer.zero_grad()
# Make predictions for this batch
outputs = model(inputs)
# Compute the loss and its gradients
loss = loss_fn(outputs, labels)
loss.backward()
# Adjust learning weights
optimizer.step()
# Gather data and report
running_loss += loss.item()
if i % 1000 == 999:
last_loss = running_loss / 1000 # loss per batch
print(' batch {} loss: {}'.format(i + 1, last_loss))
running_loss = 0.
return last_loss
# Initializing in a separate cell so we can easily add more epochs to the same run
torch.multiprocessing.set_sharing_strategy('file_system')
# timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
# writer = SummaryWriter('runs/fashion_trainer_{}'.format(timestamp))
epoch_number = 0
EPOCHS = 3
best_vloss = 1_000_000.
for epoch in range(EPOCHS):
print('EPOCH {}:'.format(epoch_number + 1))
# Make sure gradient tracking is on, and do a pass over the data
model.train(True)
avg_loss = train_one_epoch(epoch_number)
# We don't need gradients on to do reporting
model.train(False)
running_vloss = 0.0
for i, vdata in enumerate(validation_loader):
vinputs, vlabels = vdata
if torch.cuda.is_available() and use_cuda:
vinputs=vinputs.cuda()
vlabels=vlabels.cuda()
voutputs = model(vinputs)
vloss = loss_fn(voutputs, vlabels)
running_vloss += vloss
avg_vloss = running_vloss / (i + 1)
print('LOSS train {} valid {}'.format(avg_loss, avg_vloss))
# Track best performance, and save the model's state
if avg_vloss < best_vloss:
best_vloss = avg_vloss
epoch_number += 1
EPOCH 1: batch 1000 loss: 0.5016711144386791 batch 2000 loss: 0.4879903859840706 batch 3000 loss: 0.5153748965951381 batch 4000 loss: 0.49523143982025797 batch 5000 loss: 0.4767445433982648 batch 6000 loss: 0.49395503593399187 batch 7000 loss: 0.46382360034110026 batch 8000 loss: 0.46008388720825316 batch 9000 loss: 0.4629328250864637 batch 10000 loss: 0.46100793036620596 batch 11000 loss: 0.45335045937914403 batch 12000 loss: 0.46304751582280734 batch 13000 loss: 0.4592298640280496 batch 14000 loss: 0.4622895815780503 batch 15000 loss: 0.4423966025863192 LOSS train 0.4423966025863192 valid 0.46024414896965027 EPOCH 2: batch 1000 loss: 0.42145762145856863 batch 2000 loss: 0.47108051656538735 batch 3000 loss: 0.44253823141008614 batch 4000 loss: 0.4101803269519005 batch 5000 loss: 0.4296010117421392 batch 6000 loss: 0.41699717939435504 batch 7000 loss: 0.4182659008966293 batch 8000 loss: 0.428519713369722 batch 9000 loss: 0.4146968546194403 batch 10000 loss: 0.4024722200263059 batch 11000 loss: 0.4125637284159893 batch 12000 loss: 0.4207096932434943 batch 13000 loss: 0.4107713693276746 batch 14000 loss: 0.396374596947222 batch 15000 loss: 0.41958106377604415 LOSS train 0.41958106377604415 valid 0.44984331727027893 EPOCH 3: batch 1000 loss: 0.41330638983196694 batch 2000 loss: 0.3985174706102989 batch 3000 loss: 0.37634882318769813 batch 4000 loss: 0.37531123155928797 batch 5000 loss: 0.37615996596391776 batch 6000 loss: 0.4055920859769685 batch 7000 loss: 0.4049173467164219 batch 8000 loss: 0.3926431025690399 batch 9000 loss: 0.39966972182752214 batch 10000 loss: 0.39182805481381366 batch 11000 loss: 0.3917023583765549 batch 12000 loss: 0.38168022965517595 batch 13000 loss: 0.37574650804814885 batch 14000 loss: 0.38726472863345407 batch 15000 loss: 0.38498862162436126 LOSS train 0.38498862162436126 valid 0.41861987113952637
dataiter = iter(validation_loader)
images, labels = dataiter.next()
# Create a grid from the images and show them
img_grid = torchvision.utils.make_grid(images)
matplotlib_imshow(img_grid, one_channel=True)
print('Groundtruth: '+' '.join(classes[labels[j]] for j in range(4)))
if torch.cuda.is_available() and use_cuda:
images=images.cuda()
print('Result: '+' '.join([classes[i] for i in torch.argmax(model(images).cpu(),1)]))
Groundtruth: Ankle Boot Pullover Trouser Trouser Result: Ankle Boot Pullover Trouser Trouser
import cv2
import platform
cap=cv2.VideoCapture(0)
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
while (True):
ret,frame=cap.read()
results = model(frame)
#-- complete the code here
#-- end complete the code here
if cv2.waitKey(1) &0xFF == ord('q'): # press q or ESC to quit. You probably need to hit the screen first
break
cap.release()
cv2.destroyAllWindows()
Using cache found in /home/phsamuel/.cache/torch/hub/ultralytics_yolov5_master YOLOv5 🚀 2022-3-24 torch 1.7.1 CUDA:0 (NVIDIA GeForce GTX 1070, 8120MiB) Fusing layers... YOLOv5s_v6 summary: 213 layers, 7225885 parameters, 0 gradients Adding AutoShape...