I want to test pytroch on my GPU, but I am doing something very wrong as it takes 5 times longer then CPU. Actually even if
device = “cpu”
the “.to(device)” commands slow it down from 9 seconds to 12. On GPU it takes 50 seconds.
There might be a problem with my installation, but I do have Cuda installed and torch.cuda.is_available() returns True.
Below are the most important code snippets, it is hard to provide a short working example.
I think it there is something wrong with my net definition.
class Net(nn.Module):
    def __init__(self, d=1):
        super(Net, self).__init__()
        self.fc1 = nn.Linear(d, internal_neurons).to(device)  # TODO: THIS
        self.fc3 = nn.Linear(internal_neurons, 1).to(device)

        self.fc = []

        for k in range(hidden_layer_count):
            self.fc.append(nn.Linear(internal_neurons, internal_neurons).to(device))

    def forward(self, y):
        y = activation1(self.fc1(y))

        for k in range(hidden_layer_count):
            y = activation1(self.fc[k](y))

        y = activation2(self.fc3(y))
        return y


net = Net()
net.to(device)

def train():
    last_epoch = 10e10
    net.train()
    losses = []
    for epoch in range(1, epochs):
        x_train = Variable(torch.from_numpy(x_values)).float()
        y_train = np.zeros_like(x_train)

        llist = []

        for k in range(x_train.shape[0]):
            h = x_train[k] * torch.ones(1)
            y_train[k] = target_function.f(x_train[k])
            h = h.to(device)  # TODO: This
            llist.append(loss_func(net(h), y_train[k]))

        loss = sum(llist)
        losses.append(loss.item())
        opt.zero_grad()
        loss.backward()
        opt.step()