报错内容

Node: 'sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits'
logits and labels must have the same first dimension, got logits shape [32,2] and labels shape [64]
[[{{node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits}}]] [Op:__inference_train_function_12674]
2023-05-27 17:19:53.743415: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
[[{{node PyFunc}}]]

实际上是损失函数的问题
model.compile(optimizer= opt, loss= ‘sparse_categorical_crossentropy’, metrics=[‘accuracy’]) # adam优化器,loss function,训练过程中计算准确率
解决错误方法:将loss= ‘sparse_categorical_crossentropy’改为’categorical_crossentropy’
model.compile(optimizer= opt, loss= ‘categorical_crossentropy’, metrics=[‘accuracy’])