When training a model using pytorch etc., it may take several hours to train, or even days if anything. In such a case, the effect of a constant multiple of the execution time becomes large. For example, even if the constant multiple of the amount of calculation changes from 2 to 1, the learning that took 2 days can be shortened to 1 day. Competitive programming is mainly about the order of computational complexity, so the atmosphere is different in that respect. This time, I will introduce a simple example that can be used often.
You'll often see the code that converts a list of np.arrays to a Tensor type. This time, it will be faster to use in such situations.
import time
import numpy as np
import torch
a = [np.ones([100]) for _ in range(100)]
# 8.44s
start = time.time()
for _ in range(10000):
b = torch.tensor(a)
print(time.time()-start)
# 0.44s
start = time.time()
for _ in range(10000):
b = torch.tensor(np.array(a))
print(time.time()-start)
# 0,32s
start = time.time()
for _ in range(10000):
b = torch.from_numpy(np.array(a))
print(time.time()-start)
There is almost no difference between the above three codes, but there is a big difference in execution time. I don't know why, but when converting the list of np.array to Tensor type, rather than directly using torch.tensor (), use np.array () once to convert the whole np. The execution time converted to array will be shorter.
I think that you can notice the difference relatively visibly when learning about images, which often converts a large list to Tensor type. By the way, even if you use torch.as_tensor, you can do the same processing as if you use torch.tensor, and it is a little slow if you use it alone.
I personally use it with the following code
def convert2tensor(x, device):
return torch.tensor(np.array(x, dtype=np.float32), device=device)
Life is finite, so don't spend too much time learning the model.
Recommended Posts