keras-gcn拟合模型ValueError

时间:2019-11-29 05:54:27

标签: python keras keras-layer model-fitting

我正在使用this library创建一个模型来学习图形。这是代码(来自存储库):

import numpy as np

from keras_gcn.backend import keras
from keras_gcn import GraphConv

# feature matrix
input_data = np.array([[[0, 1, 2],
                        [2, 3, 4],
                        [4, 5, 6],
                        [7, 7, 8]]])

# adjacency matrix
input_edge = np.array([[[1, 1, 1, 0],
                        [1, 1, 0, 0],
                        [1, 0, 1, 0],
                        [0, 0, 0, 1]]])

labels = np.array([[[1],
                    [0],
                    [1],
                    [0]]])

data_layer = keras.layers.Input(shape=(None, 3), name='Input-Data')
edge_layer = keras.layers.Input(shape=(None, None), dtype='int32', name='Input-Edge')
conv_layer = GraphConv(units=4, step_num=1, kernel_initializer='ones', 
                       bias_initializer='ones', name='GraphConv')([data_layer, edge_layer])
model = keras.models.Model(inputs=[data_layer, edge_layer], outputs=conv_layer)
model.compile(optimizer='adam', loss='mae', metrics=['mae'])

model.fit([input_data, input_edge], labels)

但是,当我运行代码时,出现以下错误:

ValueError: Error when checking target: expected GraphConv to have 3 dimensions, but got array with shape (4, 1)

标签的形状为(1, 4, 1)

2 个答案:

答案 0 :(得分:1)

您应该使用onehot-encoder对标签进行编码,如下所示:

lables = np.array([[[0, 1],
                    [1, 0],
                    [0, 1],
                    [1, 0]]])

GraphConv层中的单位数量也应等于唯一标签的数量(在您的情况下为2)。

答案 1 :(得分:0)

我认为问题在于edge_layer和data_layer的形状不匹配。

使用函数keras.layers.Input时,给data_layer形状为shape=(None, 3),然后为edge_layer赋予形状shape=(None, None)

匹配形状,让我知道它的运行方式。