Home > database >  Why is there an error in the fit command in the python model?
Why is there an error in the fit command in the python model?

Time:12-03

I want to train models. but i am getting this error. I have 7 elements in my dataset. I can give details.

ValueError Traceback (most recent call last) in () 1 model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy']) ----> 2 history=model.fit(train_X,one_hot_train,batch_size=7,epochs=10,validation_split=0.2)

1 frames /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs) 1127 except Exception as e: # pylint:disable=broad-except 1128 if hasattr(e, "ag_error_metadata"): -> 1129 raise e.ag_error_metadata.to_exception(e) 1130 else: 1131 raise

ValueError: in user code:

File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 867, in step_function  **
    outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in run_step  **
    outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 810, in train_step
    y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
    loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
    losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call  **
    return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1665, in categorical_crossentropy
    y_true, y_pred, from_logits=from_logits, axis=axis)
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 4994, in categorical_crossentropy
    target.shape.assert_is_compatible_with(output.shape)

ValueError: Shapes (None, 2) and (None, 7) are incompatible

CodePudding user response:

try reshaping the tensors.

for example:

X = tf.constant(np.array([-7, -4, -1, 2,5,8,11,14,17,20]))  
y =    tf.constant(np.array([3, 6, 9, 12,15,18,21,24,27,30])) 
X=    tf.reshape(X,(10,1)) 
y = tf.reshape(y,(10,1))    
model.fit(X,y,epochs=10)

this model.fit would not work on X and y until they are reshaped this way.

  • Related