我需要创建一个包含 1000 个图形的数据集。我使用了以下代码:
data_list = []
ngraphs = 1000
for i in range(ngraphs):
num_nodes = randint(10,500)
num_edges = randint(10,num_nodes*(num_nodes - 1))
f1 = np.random.randint(10, size=(num_nodes))
f2 = np.random.randint(10,20, size=(num_nodes))
f3 = np.random.randint(20,30, size=(num_nodes))
f_final = np.stack((f1,f2,f3), axis=1)
capital = 2*f1 + f2 - f3
f1_t = torch.from_numpy(f1)
f2_t = torch.from_numpy(f2)
f3_t = torch.from_numpy(f3)
capital_t = torch.from_numpy(capital)
capital_t = capital_t.type(torch.LongTensor)
x = torch.from_numpy(f_final)
x = x.type(torch.LongTensor)
edge_index = torch.randint(low=0, high=num_nodes, size=(num_edges,2), dtype=torch.long)
edge_attr = torch.randint(low=0, high=50, size=(num_edges,1), dtype=torch.long)
data = Data(x = x, edge_index = edge_index.t().contiguous(), y = capital_t, edge_attr=edge_attr )
data_list.append(data)
这有效。但是当我按如下方式运行我的训练功能时:
for epoch in range(1, 500):
loss = train()
print(f'Loss: {loss:.4f}')
我不断收到以下错误:
RuntimeError Traceback (most recent call last) in () 1 for epoch in range(1, 500): ----> 2 loss = train() 3 print(f'Loss: {loss:.4f}')
5 帧 /usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in linear(input, weight, bias) 1845 if has_torch_function_variadic(input, weight): 1846 return handle_torch_function(linear, (input) ,重量),输入,重量,偏差=偏差)-> 1847 返回 torch._C._nn.linear(输入,重量,偏差)1848 1849
RuntimeError: 预期标量类型 Float 但发现 Long
有人可以帮我解决这个问题。或者制作一个不会引发此错误的 1000 个图形数据集。
将 x 和 y 张量更改为 FloatTensor,因为 Python 中的线性层仅接受 FloatTensor 输入
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句