伊人99re_av日韩成人_91高潮精品免费porn_色狠狠色婷婷丁香五月_免费看的av_91亚色网站

中培偉業IT資訊頻道
您現在的位置:首頁 > IT資訊 > 精選文章 > 如何利用PyTorch API構建CNN?

如何利用PyTorch API構建CNN?

2020-07-16 16:15:01 | 來源:中培企業IT培訓網

很多人對于卷積神經網絡(CNN)并不了解,卷積神經網絡是一種前饋神經網絡,它包括卷積計算并具有很深的結構,卷積神經網絡是深度學習的代表性算法之一。那么如何利用PyTorch API構建CNN?方式有哪些?今天本文將以一個簡單的指南,將幫助您構建和了解構建簡單的CNN的方式。通過閱讀本文之后,將能夠基于PyTorch API構建一個簡單的CNN,并使用FashionMNIST日期集對服裝進行分類。但前提是您已具備人工神經網絡知識。

  如何利用PyTorch API構建CNN?

CNN或卷積神經網絡的工作原理與人眼的工作原理非常相似。CNN背后的核心運算是矩陣加法和乘法,因此無需擔心它們。

但是要了解CNN的工作原理,我們需要了解如何將圖像存儲在計算機中。

  CNN架構

CNN的核心功能是卷積運算。將圖像矩陣與濾波器矩陣相乘以從圖像矩陣中提取一些重要特征。

通過使濾波器矩陣移動通過圖像矩陣來填充卷積矩陣。

CNN的另一個重要組成部分稱為最大池層。這有助于我們減少功能部件的數量,即使功能銳化以使我們的CNN性能更好。

對于所有卷積層,我們都應用RELU激活函數。

在將卷積層映射到輸出時,我們需要使用線性層。因此,我們使用稱為全連接層(簡稱為fc)的層。最終fc的激活大部分是S型激活函數。

我們可以清楚地看到所有輸入值在0和1之間的輸出映射。

現在,您已經知道我們將要使用的圖層。這些知識足以構建一個簡單的CNN,但是一個可選的調用dropout的層將有助于CNN發揮良好的作用。輟學層位于fc層之間,這會以設定的概率隨機丟棄連接,這將有助于我們更好地訓練CNN。

我們的CNN體系結構,但最后,我們將在fc層之間添加一個dropout。

不再浪費時間,我們將開始編寫代碼。

import torchimport torchvision# data loading and transformingfrom torchvision.datasets import FashionMNISTfrom torch.utils.data import DataLoaderfrom torchvision import transforms# The output of torchvision datasets are PILImage images of range [0, 1]. # We transform them to Tensors for input into a CNN## Define a transform to read the data in as a tensor

data_transform = transforms.ToTensor()# choose the training and test datasets

train_data = FashionMNIST(root='./data', train=True,

download=True, transform=data_transform)

test_data = FashionMNIST(root='./data', train=False,

download=True, transform=data_transform)# Print out some stats about the training and test data

print('Train data, number of images: ', len(train_data))

print('Test data, number of images: ', len(test_data))# prepare data loaders, set the batch_size

batch_size = 20

train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)

test_loader = DataLoader(test_data, batch_size=batch_size, shuffle=True)# specify the image classes

classes = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',

'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

For visualizing the Data import numpy as npimport matplotlib.pyplot as plt

%matplotlib inline

# obtain one batch of training images

dataiter = iter(train_loader)

images, labels = dataiter.next()

images = images.numpy()# plot the images in the batch, along with the corresponding labels

fig = plt.figure(figsize=(25, 4))for idx in np.arange(batch_size):

ax = fig.add_subplot(2, batch_size/2, idx+1, xticks=[], yticks=[])

ax.imshow(np.squeeze(images[idx]), cmap='gray')

ax.set_title(classes[labels[idx]])# Defining the CNNimport torch.nn as nnimport torch.nn.functional as Fclass Net(nn.Module):

def __init__(self):

super(Net, self).__init__()

# 1 input image channel (grayscale), 10 output channels/feature maps

# 3x3 square convolution kernel

## output size = (W-F)/S +1 = (28-3)/1 +1 = 26

# the output Tensor for one image, will have the dimensions: (10, 26, 26)

# after one pool layer, this becomes (10, 13, 13)

self.conv1 = nn.Conv2d(1, 10, 3)

# maxpool layer

# pool with kernel_size=2, stride=2

self.pool = nn.MaxPool2d(2, 2)

# second conv layer: 10 inputs, 20 outputs, 3x3 conv

## output size = (W-F)/S +1 = (13-3)/1 +1 = 11

# the output tensor will have dimensions: (20, 11, 11)

# after another pool layer this becomes (20, 5, 5); 5.5 is rounded down

self.conv2 = nn.Conv2d(10, 20, 3)

# 20 outputs * the 5*5 filtered/pooled map size

self.fc1 = nn.Linear(20*5*5, 50)

# dropout with p=0.4

self.fc1_drop = nn.Dropout(p=0.4)

# finally, create 10 output channels (for the 10 classes)

self.fc2 = nn.Linear(50, 10)

# define the feedforward behavior

def forward(self, x):

# two conv/relu + pool layers

x = self.pool(F.relu(self.conv1(x)))

x = self.pool(F.relu(self.conv2(x)))

# prep for linear layer

# this line of code is the equivalent of Flatten in Keras

x = x.view(x.size(0), -1)

# two linear layers with dropout in between

x = F.relu(self.fc1(x))

x = self.fc1_drop(x)

x = self.fc2(x)

# final output

return x# instantiate and print your Net

net = Net()

print(net)import torch.optim as optim# using cross entropy whcih combines softmax and NLL loss

criterion = nn.CrossEntropyLoss()# stochastic gradient descent with a small learning rate and some momentum

optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)# Training the CNNdef train(n_epochs):

loss_over_time = [] # to track the loss as the network trains

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

running_loss = 0.0

for batch_i, data in enumerate(train_loader):

# get the input images and their corresponding labels

inputs, labels = data

# zero the parameter (weight) gradients

optimizer.zero_grad()

# forward pass to get outputs

outputs = net(inputs)

# calculate the loss

loss = criterion(outputs, labels)

# backward pass to calculate the parameter gradients

loss.backward()

# update the parameters

optimizer.step()

# print loss statistics

# to convert loss into a scalar and add it to running_loss, we use .item()

running_loss += loss.item()

if batch_i % 1000 == 999: # print every 1000 batches

avg_loss = running_loss/1000

# record and print the avg loss over the 1000 batches

loss_over_time.append(avg_loss)

print('Epoch: {}, Batch: {}, Avg. Loss: {}'.format(epoch + 1, batch_i+1, avg_loss))

running_loss = 0.0

print('Finished Training')

return loss_over_time# define the number of epochs to train for

n_epochs = 30 # start small to see if your model works, initially# call train

training_loss = train(n_epochs)# visualize the loss as the network trained

plt.plot(training_loss)

plt.xlabel('1000's of batches')

plt.ylabel('loss')

plt.ylim(0, 2.5) # consistent scale

plt.show()# obtain one batch of test images

dataiter = iter(test_loader)

images, labels = dataiter.next()# get predictions

preds = np.squeeze(net(images).data.max(1, keepdim=True)[1].numpy())

images = images.numpy()# plot the images in the batch, along with predicted and true labels

fig = plt.figure(figsize=(25, 4))for idx in np.arange(batch_size):

ax = fig.add_subplot(2, batch_size/2, idx+1, xticks=[], yticks=[])

ax.imshow(np.squeeze(images[idx]), cmap='gray')

ax.set_title("{} ({})".format(classes[preds[idx]], classes[labels[idx]]),

color=("green" if preds[idx]==labels[idx] else "red"))

以上就是關于如何利用PyTorch API構建CNN的全部內容介紹,想了解更多關于卷積神經網絡的信息,請繼續關注中培偉業。

標簽: CNN 人工智能
主站蜘蛛池模板: 免费人成视频在线观看播放网站 | 少妇口述玌伦 | 精品区一区二区三 | 国产又粗又猛又黄又爽无遮 | 一区二区三区视频在线播放 | 看黄色特级片 | 黄片免费看wwwwwww | 欧美va亚洲va日韩∨a综合色 | 亚洲精品视频网 | 日韩精品资源二区在线 | 九九热精| 青久久久 | 一区二区的视频 | 在线免费中文字幕 | 国产精品拍天天在线 | 久久久www成人免费精品张筱雨 | 无码午夜人妻一区二区三区不卡视频 | 鲁鲁狠狠狠7777一区二区 | 啦啦啦www在线观看免费视频 | 国产一区二区三区乱码在线观看 | 国产精品美脚玉足脚交欧美 | 97久久久国产精品 | 亚洲成人999 | 中文字幕制服丝袜一区二区 | 国产精品一码二码三码在线 | 成人午夜精品久久久久久久3d | 亚洲自拍小视频 | 亚洲欧美国产精品无码中文字 | 亚洲Ⅴ国产V天堂A无码二区 | 亚洲国产一区二区三区在线播 | 麻豆最新国产剧情AV原创 | 成人在线视频成人 | 日产精品卡2卡三卡乱码网站 | 一级毛片成人免费看免费不卡 | 奇米第四影视 | av免费网页 | 新普新京亚洲欧美日韩国产 | 神马视频在线观看 | av二区在线观看 | 在线免费中文字幕 | 精品国产乱码aaa一区二区 |