Hello. I’m trying to run my model with some data and am getting the following error:
TypeError: expected Variable[CPUType] (got torch.cuda.FloatTensor)
I’ve checked some of the answers here and it seemed that I hadn’t pushed my model onto the device yet.
However, I checked the code and I have in fact done that, and I even explicitly pushed it onto the device in the Python Debugger interactive shell and am still getting the same error.
The code is as following:
### Module: main.py
def main():
config = get_args()
dataset = Data(config)
model = GCN(config, dataset.num_features, dataset.num_classes)
trainer = Trainer(config, model, dataset)
if torch.cuda.is_available():
model = model.to('cuda') # I've double checked that torch.cuda.is_available() returns True.
trainer.train()
### Module solver.py
class Trainer():
def __init__(self, config, model, dataset):
self.config = config
self.num_epochs = self.config.num_epochs
self.model = model
self.dataset = dataset
self.features = self.dataset.features
self.adj_hat = self.dataset.adj_hat
def train(self):
self.model.train()
optimizer = get_optimizer(self.config, self.model)
loss_train = nn.NLLLoss()
for epoch in range(self.num_epochs):
optimizer.zero_grad()
output = self.model(self.features, self.adj_hat)
### Module: models.py
class GraphConv(nn.Module):
def __init__(self, in_features, out_features):
super().__init__()
self.weight_mat = nn.Linear(in_features=in_features, out_features=out_features)
def forward(self, x, adj_mat):
weight_prod = torch.DoubleTensor(self.weight_mat(x))
output = torch.matmul(adj_mat, weight_prod)
return output
class GCN(nn.Module):
def __init__(self, config, num_features, num_classes):
super().__init__()
self.config = config
self.num_hidden = self.config.num_hidden
self.num_classes = num_classes
self.num_features = num_features
self.p = self.config.dropout_rate
self.graphconv1 = GraphConv(in_features=self.num_features, out_features=self.num_hidden)
self.graphconv2 = GraphConv(in_features=self.num_hidden, out_features=self.num_clases)
def forward(self, x, adj_hat):
x = F.relu(self.graphconv1(x, adj_hat))
x = F.dropout(input=x, p=self.p, training=self.training)
output = F.softmax(self.graphconv2(x, adj_hat), dim=1)
return output
The specific line of code that’s triggering the error is the x = F.relu(self.graphconv1(x, adj_hat))
inside the GCN model. I don’t understand because if I put self.model
on the device, shouldn’t that take care of this issue?
Thanks in advance!