我想知道 Alexnet 中某一层的推理时间。此代码测量随着批量大小变化,Alexnet 第一个全连接层的推理时间。我对此有几个问题。
给出以下代码:
import torch
import torch.optim as optim
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
import time
from tqdm import tqdm
class AlexNet(nn.Module):
def __init__(self):
super(AlexNet, self).__init__()
self.relu = nn.ReLU(inplace=True)
self.maxpool2D = nn.MaxPool2d(kernel_size=3, stride=2, padding=0)
self.adaptive_avg_polling = nn.AdaptiveAvgPool2d((6, 6))
self.dropout = nn.Dropout(p=0.5)
self.conv1 = nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2)
self.conv2 = nn.Conv2d(64, 192, kernel_size=5, padding=2)
self.conv3 = nn.Conv2d(192, 384, kernel_size=3, padding=1)
self.conv4 = nn.Conv2d(384, 256, kernel_size=3, padding=1)
self.conv5 …Run Code Online (Sandbox Code Playgroud) 我的服务器有两个GPU,如何同时使用两个GPU进行训练,以最大限度地发挥其计算能力?我下面的代码正确吗?它能让我的模型得到正确的训练吗?
class MyModel(nn.Module):
def __init__(self):
super(MyModel, self).__init__()
self.bert = pretrained_model
# for param in self.bert.parameters():
# param.requires_grad = True
self.linear = nn.Linear(2048, 4)
#def forward(self, input_ids, token_type_ids, attention_mask):
def forward(self, input_ids, attention_mask):
batch = input_ids.size(0)
#output = self.bert(input_ids, token_type_ids, attention_mask).pooler_output
output = self.bert(input_ids, attention_mask).last_hidden_state
print('last_hidden_state',output.shape) # torch.Size([1, 768])
#output = output.view(batch, -1) #
output = output[:,-1,:]#(batch_size, hidden_size*2)(batch_size,1024)
output = self.linear(output)
return output
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.device_count() > 1:
print("Use", torch.cuda.device_count(), 'gpus')
model = MyModel()
model …Run Code Online (Sandbox Code Playgroud) 如何在 python 二维元组中对具有相邻索引的元组进行分组?
我还不熟悉 zip 功能。我已经写了这样的代码,但是效果不是很好。任何帮助,将不胜感激。谢谢你!!
coords = ((1, 2), (3, 4), (5, 6), (7, 8))
coords = tuple(zip(coords[0::2], coords[1::2]))
print(coords)
Run Code Online (Sandbox Code Playgroud)
实际输出:
(((1, 2), (3, 4)), ((5, 6), (7, 8)))
Run Code Online (Sandbox Code Playgroud)
预期输出:
((1, 2, 3, 4), (5, 6, 7, 8))
Run Code Online (Sandbox Code Playgroud)