使用百度AI studio创建pytorch tensorflow gpu环境

保姆级教程,持久性安装,无需反复操作,一次安装,永远可用

1. 配合视频服用更佳:

https://www.bilibili.com/video/BV16i4y1x7Yn/

2. 视频中使用到的命令:

mkdir /home/aistudio/cuda10
mkdir /home/aistudio/cuda92
mkdir /home/aistudio/cudnn
mkdir /home/aistudio/tf
cp /home/aistudio/data/data32924/cudnn-9.2.tgz /home/aistudio/cudnn
cp /home/aistudio/data/data32924/cudnn-10.1.tgz /home/aistudio/cudnn
pip install torch==1.5.0+cu101 torchvision==0.6.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html -t /home/aistudio/cuda10
pip install torch==1.5.0+cu92 torchvision==0.6.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html -t /home/aistudio/cuda92
pip install tensorflow-gpu==1.15 -t /home/aistudio/tf
tar -zxvf cudnn-9.2.tgz
tar -zxvf cudnn-10.1.tgz

import sys
sys.path.append('/home/aistudio/cuda92')
sys.path.append('/home/aistudio/tf')

import torch
import tensorflow as tf
print(torch.cuda.is_available())
print(tf.test.is_built_with_cuda())

Pytorch的Helloworld

import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms

BATCH_SIZE=512 #大概需要2G的显存
EPOCHS=20 # 总共训练批次
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu") # 让torch判断是否使用GPU,建议使用GPU环境,因为会快很多
MODEL_PATH="model/helloworld.pth"
os.environ['CUDA_VISIBLE_DEVICES'] = '3'

train_loader = torch.utils.data.DataLoader(
        datasets.MNIST('data', train=True, download=True,
                       transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=BATCH_SIZE, shuffle=True)

test_loader = torch.utils.data.DataLoader(
        datasets.MNIST('data', train=False, transform=transforms.Compose([
                           transforms.ToTensor(),
                           transforms.Normalize((0.1307,), (0.3081,))
                       ])),
        batch_size=BATCH_SIZE, shuffle=True)

# 下面我们定义一个网络,网络包含两个卷积层,conv1和conv2,然后紧接着两个线性层作为输出,
# 最后输出10个维度,这10个维度我们作为0-9的标识来确定识别出的是那个数字
class ConvNet(nn.Module):
    def __init__(self):
        super().__init__()
        # batch*1*28*28(每次会送入batch个样本,输入通道数1(黑白图像),图像分辨率是28x28)
        # 下面的卷积层Conv2d的第一个参数指输入通道数,第二个参数指输出通道数,第三个参数指卷积核的大小
        self.conv1 = nn.Conv2d(1, 10, 5) # 输入通道数1,输出通道数10,核的大小5
        self.conv2 = nn.Conv2d(10, 20, 3) # 输入通道数10,输出通道数20,核的大小3
        # 下面的全连接层Linear的第一个参数指输入通道数,第二个参数指输出通道数
        self.fc1 = nn.Linear(20*10*10, 500) # 输入通道数是2000,输出通道数是500
        self.fc2 = nn.Linear(500, 10) # 输入通道数是500,输出通道数是10,即10分类
    def forward(self,x):
        in_size = x.size(0) # 在本例中in_size=512,也就是BATCH_SIZE的值。输入的x可以看成是512*1*28*28的张量。
        out = self.conv1(x) # batch*1*28*28 -> batch*10*24*24(28x28的图像经过一次核为5x5的卷积,输出变为24x24)
        out = F.relu(out) # batch*10*24*24(激活函数ReLU不改变形状))
        out = F.max_pool2d(out, 2, 2) # batch*10*24*24 -> batch*10*12*12(2*2的池化层会减半)
        out = self.conv2(out) # batch*10*12*12 -> batch*20*10*10(再卷积一次,核的大小是3)
        out = F.relu(out) # batch*20*10*10
        out = out.view(in_size, -1) # batch*20*10*10 -> batch*2000(out的第二维是-1,说明是自动推算,本例中第二维是20*10*10)
        out = self.fc1(out) # batch*2000 -> batch*500
        out = F.relu(out) # batch*500
        out = self.fc2(out) # batch*500 -> batch*10
        out = F.log_softmax(out, dim=1) # 计算log(softmax(x))
        return out

model = ConvNet().to(DEVICE)
optimizer = optim.Adam(model.parameters())

def train(model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        if(batch_idx+1)%30 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))
    torch.save(model, MODEL_PATH)


def test(model, device, test_loader):
    # model.eval()  # 直接导入也可
    # 以下为通过路径读
    model=None
    model=torch.load(MODEL_PATH)
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item() # 将一批的损失相加
            pred = output.max(1, keepdim=True)[1] # 找到概率最大的下标
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

for epoch in range(1, EPOCHS + 1):
    train(model, DEVICE, train_loader, optimizer, epoch)
    test(model, DEVICE, test_loader)

Tensorflow Hello World程序

import tensorflow as tf

# 创建常量
hello = tf.constant('Hello,world!')

# 创建会话
sess = tf.Session()

# 执行
result = sess.run(hello)

# 关闭会话
sess.close()

# 输出结果
print(result)

发表评论

电子邮件地址不会被公开。 必填项已用*标注