简体   繁体   中英

Input incompatible with layers - Tensorflow

I'm currently facing a problem with Tensorflow library that I couldn't resolve. All solutions I've already found on stackoverflow didn't help me understanding the real problem.

def build_model(dim_data, n_neurons):

model = tf.keras.Sequential(name="Reseau_de_neurones")

model.add(layers.Dense(units = n_neurons, input_shape=(3, 1), bias_initializer="glorot_uniform")) 
model.add(layers.ReLU())
model.add(layers.Dense(units = n_neurons, input_shape=(dim_data,), bias_initializer="glorot_uniform")) 
model.add(layers.ReLU())
model.add(layers.Dense(units = n_neurons, input_shape=(dim_data,),  bias_initializer="glorot_uniform")) 
model.add(layers.ReLU())
model.add(layers.Dense(units = n_neurons, input_shape=(dim_data,), bias_initializer="glorot_uniform")) 
model.add(layers.ReLU())
model.add(layers.Dense(units = dim_data - 1, bias_initializer="glorot_uniform")) 

return model

Then I build my NN:

model = build_model(dim_data=3, n_neurons=10)

I then define a training step:

def train_step(model):

   with tf.GradientTape() as tape:
    pos = [0,0]
    controle_actuel = model(np.array([0,pos[0],pos[1]]))
    loss_value = loss
   gradients = tape.gradient(loss_value, model.trainable_variables)
   return loss_value, gradients

When I try this function, I get this error

input 0 of layer "dense_37" is incompatible with the layer: expected min_ndim=2, found ndim=1. Full shape received: (3,)

Could you please help me?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM