简体   繁体   English

层密集的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入形状为 (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)

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?我只是测试不够广泛吗?

  1. Softmax output will return 13 positive values whose sum to 1. Here you seem to want 'independent' probabilities between 0 and 1 for all your values so you should go with activation='sigmoid' that does what you want. Softmax 输出将返回 13 个正值,其总和为 1。在这里,您似乎希望所有值的“独立”概率介于 0 和 1 之间,因此您应该使用activation='sigmoid'来执行您想要的操作。
  2. Your models have 2 InputLayer ( input_layer and input_for_dense_layer ) that probably causes the confusion in shape expectation for the first layer.您的模型有 2 个 InputLayer( input_layerinput_for_dense_layer ),这可能会导致第一层的形状期望混淆。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:密集层的输入 0 与层不兼容:预期轴 -1 的值为 8,但接收到形状为 [None, 1] 的输入 - 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] 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 具有值 8192,但接收到的输入具有形状(无,61608) - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 8192 but received input with shape (None, 61608) 密集层的输入 0 与该层不兼容:输入形状的预期轴 -1 的值为 2700,但接收到的输入形状为 [None, 900] - Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 2700 but received input with shape [None, 900] 层密集_18 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3500,但接收到形状为 [None, 7] 的输入 - Input 0 of layer dense_18 is incompatible with the layer: expected axis -1 of input shape to have value 3500 but received input with shape [None, 7] 层顺序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 8,但收到的输入具有形状(无,71) - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 71) “ValueError:……与图层不兼容:输入形状的预期轴 -1 的值为 8,但接收到的输入形状为(无、7、169)” - “ValueError: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” ValueError: ...与图层不兼容:输入形状的预期轴 -1 的值为 20,但接收到的输入形状为 (None, 20, 637) - ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) conv2d 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但接收到形状为 [None, 64, 64, 3] 的输入 - Input 0 of layer conv2d is incompatible with layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 64, 64, 3] ValueError:dense_24 层的输入 0 不兼容:输入形状的预期轴 -1 具有值 1024,但接收到形状为 [16, 512] 的输入 - ValueError: Input 0 of layer dense_24 is incompatible: expected axis -1 of input shape to have value 1024 but received input with shape [16, 512] 层序的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但收到的输入具有形状 - Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM