python - 为 Keras 顺序模型格式化 networkx 数据
问题描述
我正在尝试根据为它们着色所需的最少颜色为图形构建一个分类器。我正在使用 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_train 是一个由 800 个数组组成的数组,其中填充了 100 个 0 和 1。然后我认为输入形状应该是 x_train 的形状,因此是 input_shape(800,0)。但是,当我尝试编译和拟合模型时:
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(x_train,y_train, epochs=200, batch_size=15)
我收到以下错误:“ValueError:检查输入时出错:预期 input_input 的形状为 (800,) 但数组的形状为 (1,)”
我还尝试重塑 x_train: x_train = np.reshape(x_train, (1, x_train.shape[0]))
但目标值出现错误:“ValueError:检查目标时出错:预期输出具有形状 (10,) 但得到的数组形状为 (1,)”
解决方案
你在维度上犯了一个错误。如果您的输入是 800 个 100s 0 和 1 的数组,那么您输入的维度应该是 800,100 。你应该让你的模型输入形状等于 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,就像现在的模型一样。
推荐阅读
- ios - LaunchScreen.storyboard 中用于 iPad 横向和纵向模式的初始屏幕的不同背景图像
- javascript - 为 jQuery 集合运行映射后获取合并数组
- java - Java中Hashset的这种奇怪行为的原因是什么?
- python - 来自 div 的 python xpath 选择器
- r - 如何打印具有 2 个有效数字的 p 值?
- terraform - 尝试使用 terraform 将 EBS 卷附加/挂载到 ECS 实例
- arrays - 在 Swift 5 中将字节数组转换为 Int64
- java - 如何在运行时为嵌套属性创建 CriteriaBuilder 表达式?
- c# - 无法使用适用于 .NET 的 AWS 开发工具包获取预签名对象 URL
- sparql - WDQS - 查询格式错误