Input 0 of layer "hidden1" is incompatible with the layer: expected min_ndim=2, found ndim=1

Viewed 27

I train my model to have input of an n*n size vector.

I train the model and load it. Then I try to call it on a n*n 1 dim array and I get the error in the title.

def gen_models(m, n, r, vnf_fail_prob, server_fail_prob, num_rounds):                                                                          
      training_data = gen_training_data(m, n, r, vnf_fail_prob, server_fail_prob, num_rounds)                                                    
      inputs = [x[0] for x in training_data]                                                                                                     
      input_arr = np.array(inputs)                                                                                                               
      for i in range(n):                                                                                                                         
          model = keras.Sequential()                                                                                                             
          model.add(keras.layers.Dense(32, input_shape=(n*n,), activation='relu', name='hidden1'))                                               
          model.add(keras.layers.Dense(64, activation='relu', name='hidden2'))                                                                   
          model.add(keras.layers.Dense(256, activation='relu', name='hidden3'))                                                                  
          model.add(keras.layers.Dense(n, activation='softmax', name='output'))                                                                  
          model.build((m,n))                                                          
          opt = keras.optimizers.Adam(learning_rate=0.001)                            
          model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])    
          outputs = [x[1][i] for x in training_data]                                  
          output_arr = np.array(outputs)                                              
          model.fit(x=input_arr, y=output_arr,batch_size=1024, epochs=1000, shuffle=True)    
          model.save(f'../models/model{i}.h5')              

I then load the model and try to call it on some data.

Model0 = keras.models.load_model('./models/model0.h5')                                                                                   
cost_vec  = gen_training_instance(G)[0]                                                                                                    
print(cost_vec)                                                                                                                            
Model0(cost_vec) 

My cost vec if a 3x3 array that is flattened and looks like this.

[0.04569468 0.17683705 0.18402121 0.02979581 0.11263915 0.11706112
 0.0414878  0.15954582 0.16596771]

What do I need to change to resolve this error?

1 Answers

Per the comments, my input was in the form of np.array((9,)).

To fix the error just required reshaping the input. np.reshape(arr, (1,9)).

Related