无论输入多少,神经网络都有相同的输出

问题描述 投票:0回答:1

问题:设 G 是一个有 n 个顶点的完全图——所以 G 有 n 个选择 2 = n(n+1)/2 条边。对于 G 中的每条边,我想使用神经网络根据所有先前的边颜色为其分配颜色。有 t 种颜色在使用。

方法:对于这个例子,G 是一个包含 n = 5 个顶点的完整图——因此有 10 条边——并且我们使用 t = 3 种颜色(0,1 和 2)。假设 input1 是一个由零组成的 (10,1) 张量,而 input2 是一个由零组成的 (10,3) 张量。所有边都被索引,边 i 对应于 input1[i,:] = 1,其他位置为零。 input2 告诉我们边缘接收到哪种颜色,如果边缘 i 的颜色为 c,则 input2[i,c] = 1。我们使用 for 循环遍历 G 中的所有边缘,直到 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。每次运行都会发生这种情况;唯一的变化是哪一列全为 1。我已将线性层替换为双线性层,但都不起作用。我尝试添加具有各种概率的 dropout,但没有成功。我已经做了一些打印,看来问题是从双线性层之前或双线性层开始的。无论我使用什么 input1-input2 组合,代码在该层之后都会返回相同的答案。需要注意的一件事是当我在类声明之外运行代码时,如下所示:

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

我选择的颜色是可变的。有谁知道为什么会出现这种情况或如何纠正?我想该模型正在学习输入的稀疏性质。在等待回复时,我将尝试嵌入,因为我的数据非常稀疏。

python neural-network graph-theory binary-data
1个回答
0
投票

我相信问题出现在我的 super() 初始化中。我将初始化更改为

''' BNN 类(nn.Module): '''基础神经网络''' def init(self, number_of_colors): 超级(BNN,自我)。init() self.num_color = number_of_colors

def forward(self,edge,coloring):
    x = nn.Bilinear(in1_features=1,
                              in2_features=self.num_color,
                              out_features=128
                             )(edge,coloring)
    x = nn.ReLU()(x)
    x = nn.Linear(in_features=128,
                            out_features=self.num_color
                           )(x)
    x = nn.Softmax(dim=1)(x)
    return x

'''

这会运行并在我的最终彩色图中产生一些变化。

© www.soinside.com 2019 - 2024. All rights reserved.