You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为完全图边着色时神经网络输出恒同的问题求助

问题背景

给定n个顶点的完全图G,其边数为组合数C(n,2)=n(n-1)/2。需使用神经网络,根据所有已着色边的结果为每条边分配t种颜色之一。

实现方案

以n=5的完全图(共10条边)、t=3种颜色(0、1、2)为例:

  • input1为(10,1)零张量,第i条边对应input1[i,:]=1,其余为0;
  • input2为(10,3)零张量,若第i条边被着色为c,则input2[i,c]=1;
  • 通过循环遍历所有边,直到input2每行都有一个1。
代码实现

类定义代码

class BNN(nn.Module):
    def __init__(self, number_of_colors):
        super(BNN,self).__init__()
        self.layer1 = nn.Bilinear(in1_features=1,
                                  in2_features=number_of_colors,
                                  out_features=128
                                 )
        self.relu1 = nn.ReLU()
        self.layer2 = nn.Linear(in_features=128,
                                out_features=number_of_colors
                               )
        self.softmax = nn.Softmax(dim=1)

    def forward(self,edge,coloring):
        
        x = self.layer1(edge,coloring)
        x = self.relu1(x)
        x = self.layer2(x)
        x = self.softmax(x)

        return x

运行代码

r,s,t = (3,3,3)
size = 10
model = BNN(t)
model.eval()
coloring = torch.zeros(*(size,t), dtype = torch.float)

for i in range(10):
    blank_edge = torch.zeros(*(size,1), dtype = torch.float)
    blank_edge[i] = 1.0

    c = model(blank_edge,coloring)
        
    color = c[i,:].argmax().item()

    coloring[i,color] = 1.0
问题现象

运行上述代码时,所有边始终被染成同一种颜色(input2某一列全为1),仅每次运行的颜色列不同。尝试替换线性层为双线性层、添加不同概率的Dropout均无效,排查发现问题出在双线性层前后,不同输入组合输出结果相同。

特殊情况

当采用函数形式实现时,颜色选择存在随机性:

def test_word(size,number_of_colors):
        
    empty_coloring = torch.tensor(np.zeros((size,number_of_colors)), dtype = torch.float)
    
    for i in range(size):
        edge = torch.tensor(np.zeros((size,1)), dtype = torch.float)
        edge[i] = 1.0

        x = nn.Bilinear(in1_features=1,
                        in2_features=number_of_colors,
                        out_features=64)(edge,empty_coloring)
        x = nn.ReLU()(x)
        x = nn.Linear(in_features=64,
                      out_features=number_of_colors)(x)
        x = nn.Softmax(dim=1)(x)

        color = x[i,:].argmax().item()

        empty_coloring[i,color] = 1.0
        
    return empty_coloring
求助内容

现需明确该问题的原因及解决办法,目前计划尝试用Embedding处理稀疏输入。

内容的提问来源于stack exchange,提问作者Christian McRoberts

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 12:16:08