![](/img/trans.png)
[英]ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 to have value 8 but received input with shape [None, 1]
[英]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)
我正在尝试使用遗传算法来自动化神经网络的设计。 我对神经网络和 tensorflow 很陌生,所以如果我不能提供信息或正确解释事情,请原谅我。 我有多个问题需要解决。
我的输入是一个浮点值数组:
self.data_inputs = np.array([self.car_location, self.car_velocity, self.ball_location]).astype(np.float)
我想要的输出是这样的:
self.desired_output = np.asarray([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0])
也就是说,我试图使使用 softmax 的神经网络输出层在此特定实例中生成接近 1 的分数。
第一个问题,我应该如何定义输出(对于神经网络)? 目前它被定义为:
output_layer = tensorflow.keras.layers.Dense(13, activation="softmax", name="output")
第二个问题,我将我的网络定义为这样生成:
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)
但这会产生一个错误:
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)
谁能向我解释我如何没有正确地将这些层连接在一起? 据我所知(和测试)它似乎可以正常工作,但是当我在此 API 的上下文中启动它时,我尝试使用它会引发此错误。 我只是测试不够广泛吗?
activation='sigmoid'
来执行您想要的操作。input_layer
和input_for_dense_layer
),这可能会导致第一层的形状期望混淆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.