为什么python模型中的fit命令会出错?

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

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

ValueError Traceback(最后一次调用) 在 () 1 model.compile(优化器='adam',损失='categorical_crossentropy',指标=['accuracy']) ----> 2 history=model.fit(train_X,one_hot_train,batch_size=7,epochs=10,validation_split=0.2)

1 帧 /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py 在 autograph_handler(*args, **kwargs) 1127 除了异常为 e: # pylint:disable=broad-except 第1128章 -> 1129 提高 e.ag_error_metadata.to_exception(e) 1130 其他: 1131 提高

ValueError:在用户代码中:

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

尝试重塑张量。

例如:

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)

此 model.fit 不适用于 X 和 y,直到它们以这种方式重塑。