如何在 python 中创建包含 1000 个图形的数​​据集

How to create a DataSet of 1000 graphs in python

我需要创建一个包含 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 frames /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, weight), input, weight, bias=bias) -> 1847 return torch._C._nn.linear(input, weight, bias) 1848 1849

RuntimeError: expected scalar type Float but found Long

谁能帮我解决这个问题。或者制作一个不会抛出此错误的 1000 图数据集。

将您的 x 和 y 张量更改为 FloatTensor,因为 python 中的线性层仅接受 FloatTensor 输入