我想问为什么前向传递通过 MLP 模型的时间取决于输入数据的大小(在采样和创建较小批次之前)。
例如,如果我从大小为 100(整个数据大小)的原始数据集中选择 10(批量大小)数据,则前向传递所需的时间不应取决于 100,而是取决于 10。
我发现,当我增加批次大小时,前向传递时间也会增加。然而,当我增加总数据集的数量时,也会发生同样的事情。
我有一个模型,可以训练一定批量大小的输入(在本例中为 100),这些输入是从连续整数数组中采样的,其形式为,
# Generate input
time0 = time.time()
input_data = np.zeros([n,1]).astype(np.float64)
for i in range(len(input_array)):
input_data[i, 0] = -1.5 + 3 / (n-1) * i
time1 = time.time()
# Train
batchSize = 100
for train_step in range(1000):
indexTotal = [num for num in range(n)]
batchIndices = random.sample(indexTotal, batchSize)
input_data_0 = input_data[batchIndices, :] # Sample data
input_data_combined = np.concatenate([input_data_0] * 3, axis=0) # Combine data
input_data_combined = input_data_combined + shiftMat # Add bias
input_data_combined = torch.tensor(input_data_combined, requires_grad=True, device=device) # Convert to tensor
time2 = time.time()
output_combined = model(input_data_combined) # Forward pass
time3 = time.time()
// Back propagation
基本上,它将多个 numpy 数组组合成一个数组,为其添加偏差(也是一个 numpy 数组),然后转发到模型。该模型是一个简单的 MLP 模型,其条件是如果它接收到的张量在特定边界内,则返回零。
# Define neural network
class myModel(nn.Module):
def __init__(self):
super(myModel, self).__init__()
self.fc1 = nn.Linear(1, 31) # (x, y) input
self.fc2 = nn.Linear(31, 31)
self.fc3 = nn.Linear(31, 1)
def forward(self, x):
# Check if each row is close to [1.1, 0] within epsilon (=0.01)
epsilon = 0.01
isClose2Target = torch.all(torch.abs(x - torch.tensor([1.1, 0], device=device)) < epsilon, dim=1)
x = self.fc1(x)
x = torch.tanh(x)
x = self.fc2(x)
x = torch.tanh(x)
x = self.fc3(x)
# Set the output to 0 for rows that are close to [1.1, 0]
x[isClose2Target] = torch.tensor([0])
return x # Single Eb output
如果增加batchSize,前向传播所需的时间就会增加。当前的问题是,如果我增加
n
(完整数据集的大小),前向传递时间也会增加,这是没有意义的(因为模型接收到的输入大小为 batchSize
,而不是 n
)。
我认为这可能是
random.sample()
函数的问题,它可能存储有关 n 的信息,但我不确定......
您能否建议哪部分代码可能会导致此问题?
谢谢你。
我尝试了 GPU 和 CPU,但它们都给了我相同的结果,其中前向传递速度取决于
n
和 batchSize
。
我目前正在使用时间模块测量时间,
time.time()
如上面代码中所写。
我还发现了另一个例子,展示了使用
random.sample()
函数的效果。以下是代码:
timeForward = 0
for i in range(100):
# Sample batch indices
indexTotal = [num for num in range(n**dim)]
indexTotal = indexTotal[0:(n**1)*end[0]+(n**0)*end[1]] + indexTotal[(n**1)*end[0]+(n**0)*end[1]+1:n**dim]
batchIndices = random.sample(indexTotal, batchSize) # Never sample i=N (end state)
input_data_0 = np.ones([batchSize, 2])
input_data_combined = torch.tensor(input_data_0, requires_grad=True, device=device)
# Model output
time0 = time.time()
output_combined = model(input_data_combined)
time1 = time.time()
# Update time
timeForward = timeForward + time1 - time0
输入
input_data_0
与变量n
和采样过程无关。但是,如果我们将 n
从 10 增加到 61,我们可以看到时间(仅适用于前向传播)timeForward
也会增加。
有趣的是,如果我们删除随机采样的行(修改后的代码如下):
timeForward = 0
for i in range(100):
input_data_0 = np.ones([batchSize, 2])
input_data_combined = torch.tensor(input_data_0, requires_grad=True, device=device)
# Model output
time0 = time.time()
output_combined = model(input_data_combined)
time1 = time.time()
# Update time
timeForward = timeForward + time1 - time0
它显示出与时间无关的
n
:
这是python模块的问题吗
time
?谢谢你。