为Keras顺序模型格式化networkx数据

2024-09-29 22:31:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试建立一个基于最小颜色的图分类器,以给它们着色。我正在使用networkx库生成图形,并计算正确着色所需的最小颜色数。以下是数据生成:

nodes=10
numGraphs=1000
probability= .25
graphs=np.empty(numGraphs, dtype=dict)
colorings=np.empty(numGraphs, dtype=dict)
numColors=np.zeros(numGraphs)

#Producing array of random graphs 
for i in range(numGraphs):
    graphs[i]=nx.erdos_renyi_graph(nodes,p)


#Coloring the array of graphs
for i in range(numGraphs):
    colorings[i]=nx.coloring.greedy_color(graphs[i], strategy="largest_first")



#Storing the minimum colors needed for each graph and the largest color needed from all graphs
#The numColors is the desired output of the network for each graph.
for i in range(len(colorings)):
    numColors[i]=max(colorings[i].values())+1
    
maxColor=max(numColors)

#Converting graphs from dicts to flattened arrays which are nodes^2 long, This is the input data
for i in range(numGraphs):
    graphs[i]=nx.to_numpy_array(graphs[i])
    graphs[i]=graphs[i].flatten()

拆分数据和模型:

x_train, x_test, y_train, y_test = train_test_split (graphs, numColors, test_size = 0.2)

model = Sequential()
model.add(Dense(nodes, input_shape=(800,), activation='relu', name="input"))
model.add(Dense(100,activation='sigmoid',name="layer2"))
model.add(Dense(100, activation='relu', name="layer3"))
model.add(Dense(int(maxColor), activation='softmax', name="ouput"))
model.summary()
Model: "sequential_17"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (Dense)                (None, 10)                8010      
_________________________________________________________________
layer2 (Dense)               (None, 100)               1100      
_________________________________________________________________
layer3 (Dense)               (None, 100)               10100     
_________________________________________________________________
ouput (Dense)                (None, 10)                1010      
=================================================================
Total params: 20,220
Trainable params: 20,220
Non-trainable params: 0
_________________________________________________________________

模型应该接受一个数组,数组的节点长度为^2,并将其分类为“maxColor”数量的类之一。在这种情况下,maxColor=10,x_列是一个由800个数组组成的数组,其中填充了100个0和1。然后我计算出输入形状应该是x_列的形状,因此输入_形状(800,0)。但是,当我尝试编译并拟合模型时:

model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(x_train,y_train, epochs=200, batch_size=15)

我得到以下错误: ValueError:检查输入时出错:预期输入\u输入具有形状(800),但获得具有形状(1,)的数组

我还尝试了重塑x_-train:x_-train=np.重塑(x_-train,(1,x_-train.shape[0]))

但目标值出现错误:“ValueError:检查目标时出错:预期输出具有形状(10),但获得具有形状(1,)的数组”


Tags: theinformodelnprangetrain数组
1条回答
网友
1楼 · 发布于 2024-09-29 22:31:29

您在尺寸方面犯了一个错误。如果您的输入是100s 0和1的800数组,那么您输入的维度应该是800100。您应该使模型输入形状等于100,如下所示model.add(Dense(nodes, input_shape=(100), activation='relu', name="input"))

也不要改变这个x_train = np.reshape(x_train, (1, x_train.shape[0])) 你需要像这样重塑x_train = np.reshape(x_train, (-1, x_train.shape[1]))

确保最后一个尺寸是100,就像现在的模型一样

相关问题 更多 >

    热门问题