#!/usr/bin/env python
# coding: utf-8

# In[1]:


import torch


# In[2]:


x = torch.empty(5, 3)
print(x)
print(x.type())


# In[3]:


x = torch.rand(5, 3)
print(x)
print(x.type())
y = torch.randn(5, 3)
print(y)
print(y.type())


# In[4]:


x = torch.zeros(5, 3, dtype=torch.long)
print(x.type())


# In[5]:


x = torch.tensor([5.5, 3])
print(x)
print(x.type())


# In[6]:


x = x.new_ones(5, 3, dtype=torch.double)
print(x)
print(x.type())
x = torch.randn_like(x, dtype=torch.float)
print(x)
print(x.type())
x = torch.rand_like(x, dtype=torch.float)
print(x)
print(x.size())


# In[7]:


y = torch.rand(5, 3)
print(y)
print(x)
print(x + y)


# In[8]:


print(torch.add(x, y))


# In[9]:


result = torch.empty(5, 3)
print(result)
torch.add(x, y, out=result)
print(result)


# In[10]:


y.add_(x)
print(x)
print(y)


# In[11]:


print(x)
print(x[:, 1])


# In[12]:


x = torch.randn(4, 4)
print(x)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())


# In[13]:


x = torch.randn(1)
print(x)
print(x.item())


# In[14]:


a = torch.ones(5)
print(a)
print(a.dtype)
b = a.numpy()
print(b)
print(b.dtype)
a.add_(1)
print(a)
print(b)


# In[15]:


import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
# np.add(b, 1, out=a)
# np.add(a, 1, out=a)
torch.add(b, 1,out=b)
print(a)
print(b)


# In[16]:


# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!


# In[17]:


import torch
print(torch.__version__)
print(torch.version.cuda)
print(torch.backends.cudnn.version())
print(torch.cuda.is_available())


# In[18]:


device = torch.device("cuda")          # a CUDA device object
y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
x = x.to(device)                       # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!


# In[19]:


x = torch.rand(5, 2)
x = x.to(device)
print(x)
q = torch.ones_like(x)
print(q)


# In[20]:


x = torch.ones(2, 2, requires_grad=True)
print(x)


# In[21]:


y = x + 2
print(y)


# In[22]:


print(y.grad_fn)


# In[23]:


z = y * y * 3
out = z.mean()

print(z)
print(out)


# In[24]:


# a = torch.randn(2, 2, requires_grad=False)
a = torch.randn(2, 2)
a = ((a * 3) / (a - 1))
print(a.requires_grad)
a.requires_grad_(True)
print(a.requires_grad)
b = (a * a).sum()
print(b.grad_fn)


# In[25]:


out.backward()
print(x.grad)


# In[26]:


x = torch.randn(3, requires_grad=True)
print(x)
print(x.norm())

y = x * 2
while y.data.norm() < 1:
    y = y * 2
print(y)


# In[27]:


v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)#指定梯度的倍率
y.backward(v)
#y 是 x的2^n次倍
print(x.grad)


# In[28]:


print(x.requires_grad)
print((x ** 2).requires_grad)

with torch.no_grad():
    print((x ** 2).requires_grad)


# In[29]:


import torch
import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
print(net)


# In[30]:


params = list(net.parameters())
print(len(params))
print(params[0].size())# conv1's .weight
print(params[1].size())
print(params[2].size())
print(params[3].size())
print(params[4].size())
print(params[5].size())
print(params[6].size())
print(params[7].size())
print(params[8].size())
print(params[9].size())


# In[31]:


input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)


# In[32]:


net.zero_grad()
out.backward(torch.randn(1, 10))


# In[33]:


output = net(input)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1) # make it the same shape as output
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)


# In[34]:


print(loss.grad_fn)  # MSELoss
print(loss.grad_fn.next_functions)  # Linear
print(loss.grad_fn.next_functions[0][0].next_functions)  # ReLU


# In[35]:


net.zero_grad()

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)


# In[36]:


learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)


# In[37]:


import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()


# In[38]:


import torch
import torchvision
import torchvision.transforms as transforms


# In[39]:


transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')


# In[40]:


import matplotlib.pyplot as plt
import numpy as np

# functions to show an image

def imshow(img):
    img = img / 2 + 0.5 # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()
    
# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))


# In[41]:


import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
        
    def forward(self, x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16 * 5 * 5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()


# In[42]:


import torch.optim as optim

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)


# In[43]:


for epoch in range(2):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')


# In[44]:


dataiter = iter(testloader)
images, labels = dataiter.next()

# print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(4)))


# In[45]:


outputs = net(images)


# In[46]:


_, predicted = torch.max(outputs, 1)

print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
                              for j in range(4)))


# In[47]:


correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))


# In[48]:


class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1
            
            
for i in range(10):
    print('Accuracy of %5s : %2d %%' %(
            classes[i], 100 * class_correct[i] / class_total[i]))


# In[49]:


device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')

# Assuming that we are on a CUDA machine, this should print a CUDA device:

print(device)

net.to(device)

 

https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐