[英]Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1)
I'm trying to use genetic algorithms to automate the design of a neural network.我正在尝试使用遗传算法来自动化神经网络的设计。 I'm very new to neural networks and tensorflow so excuse me if I fail to provide information or explain things correctly.
我对神经网络和 tensorflow 很陌生,所以如果我不能提供信息或正确解释事情,请原谅我。 I have multiple issues which I'm trying to address.
我有多个问题需要解决。
My input is an array of float values:我的输入是一个浮点值数组:
self.data_inputs = np.array([self.car_location, self.car_velocity, self.ball_location]).astype(np.float)
My desired output is this:我想要的输出是这样的:
self.desired_output = np.asarray([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
That is, I am trying to make the neural network's output layer, that uses softmax, generate scores close to 1 in this specific instance.也就是说,我试图使使用 softmax 的神经网络输出层在此特定实例中生成接近 1 的分数。
First question, what should I define the output to be (for the neural network)?第一个问题,我应该如何定义输出(对于神经网络)? Currently it is defined as:
目前它被定义为:
output_layer = tensorflow.keras.layers.Dense(13, activation="softmax", name="output")
Second question, I defined my network to be generated as such:第二个问题,我将我的网络定义为这样生成:
input_layer = tensorflow.keras.layers.InputLayer(3, name="input")
dense_layers = []
output_layer = tensorflow.keras.layers.Dense(13, activation="softmax", name="output")
self.text = f"Creating new generation"
if (len(model_array) == 0): # generate random population
for individual in range(self.population_size):
chosen_input = random.randint(3, 60)
input_for_dense_layer = tensorflow.keras.layers.InputLayer(chosen_input)
dense_layers.append(input_for_dense_layer)
index = 1
for i in range(random.randint(1,5)):
dense_layer = tensorflow.keras.layers.Dense(chosen_input, activation = "relu")
dense_layers.append(dense_layer)
chosen_input = random.randint(3, 60)
index += 1
model = tensorflow.keras.Sequential()
model.add(input_layer)
for dense_layer in dense_layers:
model.add(dense_layer)
model.add(output_layer)
model.compile(optimizer=random.choice(self.optimizer_array), loss=random.choice(self.loss_array), metrics=['accuracy'])
model_array.append(model)
But this generates an error:但这会产生一个错误:
ValueError: Input 0 of layer dense_1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 1)
Can anyone explain to me how I'm not connecting these layers together properly?谁能向我解释我如何没有正确地将这些层连接在一起? From what I can tell (and test) it seems to be working, but when I launch it within the context of this API I'm trying to use it throws this error.
据我所知(和测试)它似乎可以正常工作,但是当我在此 API 的上下文中启动它时,我尝试使用它会引发此错误。 Did I just not test expansively enough?
我只是测试不够广泛吗?
activation='sigmoid'
that does what you want. activation='sigmoid'
来执行您想要的操作。input_layer
and input_for_dense_layer
) that probably causes the confusion in shape expectation for the first layer.input_layer
和input_for_dense_layer
),这可能会导致第一层的形状期望混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.