简体   繁体   中英

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. 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.

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. 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.
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question 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 8192 but received input with shape (None, 61608) 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] 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] 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: …incompatible with the layer: expected axis -1 of input shape to have value 8 but received input with shape (None, 7, 169)” ValueError: ...incompatible with the layer: expected axis -1 of input shape to have value 20 but received input with shape (None, 20, 637) 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: 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] 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