如何解决数字识别器 kaggle 版本
在使用 MNIST 数据集解决数字识别任务后,我尝试在 kaggle 上解决相同的精确竞争:
https://www.kaggle.com/c/digit-recognizer
尝试拟合模型后,出现以下错误:
InvalidArgumentError: logits and labels must have the same first dimension,got logits shape [32,10] and labels shape [640]
[[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-122-653fb886dd19>:3) ]] [Op:__inference_train_function_250757]
Function call stack:
train_function
在进行了一些搜索之后,我尝试将其中一个参数更改为 categorical_crossentropy
,而没有 sparse
,但这会产生另一个错误。
我不知道该怎么做才能让它发挥作用。
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
# %matplotlib inline
import numpy as np
import pandas as pd
import cv2 as cv
from tensorflow.keras import layers
from tensorflow.keras import models
from tensorflow.keras.utils import to_categorical
mnist_train = pd.read_csv(r"train_digit_recognition.csv")
mnist_test = pd.read_csv(r"test_digit_recognition.csv")
X_train=mnist_train.drop(['label'],axis=1) #df= df.drop(['Date'],axis=1)
y_train=mnist_train['label']
X_test=(mnist_test)
train_images = X_train.to_numpy()
train_labels=y_train.to_numpy()
test_images=X_test.to_numpy()
train_images = train_images.reshape((42000,28,1))
test_images = test_images.reshape((28000,1))
# Normalize the train dataset
train_images = tf.keras.utils.normalize(train_images,axis=1)
# Normalize the test dataset
test_images = tf.keras.utils.normalize(test_images,axis=1)
#to categorical
train_labels = to_categorical(train_labels)
#Build the model object
model = tf.keras.models.Sequential()
# Add the Flatten Layer
model.add(tf.keras.layers.Flatten())
# Build the input and the hidden layers
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128,activation=tf.nn.relu))
# Build the output layer
model.add(tf.keras.layers.Dense(10,activation=tf.nn.softmax))
# Compile the model
model.compile(optimizer="adam",loss="sparse_categorical_crossentropy",metrics=["accuracy"])
model.fit(x=train_images,y=train_labels,epochs=15)
最后两行给了我错误。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。