【问题标题】:ValueError: Error when checking input: expected dense_10_input to have 2 dimensions, but got array with shape (60000, 28, 28)ValueError:检查输入时出错:预期的dense_10_input有2维,但得到了形状为(60000、28、28)的数组
【发布时间】:2020-04-29 07:38:24
【问题描述】:

我正在尝试训练我的深度神经网络识别手写 数字,但我不断收到标题中所述的错误它 给我一个错误说:“ValueError:检查输入时出错: 预计 dense_7_input 有 2 维,但得到了形状的数组 (60000, 28, 28)”,我不知道为什么。我检查了以前的答案 解决这个问题,但没有任何效果。 :所以当我尝试最后一段代码时,它给了我这个错误: ValueError:输入数组的样本数应与目标数组相同。找到 60000 个输入样本和 10000 个目标样本。

enter image description here

# Imports
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical

# Configuration options
feature_vector_length = 784
num_classes = 60000

# Load the data
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# Reshape the data - MLPs do not understand such things as '2D'.
# Reshape to 28 x 28 pixels = 784 features
X_train = X_train.reshape(X_train.shape[0], feature_vector_length)
X_test = X_test.reshape(X_test.shape[0], feature_vector_length)

# Convert into greyscale
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# Convert target classes to categorical ones
Y_train = to_categorical(Y_train, num_classes)
Y_test = to_categorical(Y_test, num_classes)

# Imports
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical

# Configuration options
feature_vector_length = 784
num_classes = 60000

# Load the data
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# Visualize one sample
import matplotlib.pyplot as plt
plt.imshow(X_train[0], cmap='Greys')
plt.show()

# Set the input shape
input_shape = (feature_vector_length,)
print(f'Feature shape: {input_shape}')

# Create the model
model = Sequential()
model.add(Dense(350, input_shape=input_shape, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

# Configure the model and start training
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy'])
model.fit(X_train, Y_train, epochs=10, batch_size=250, verbose=1, 
validation_split=0.2)

# New one #######
# Test the model after training
test_results = model.evaluate(X_test, Y_test, verbose=1)
print(f'Test results - Loss: {test_results[0]} - Accuracy: 
{test_results[1]}%')

【问题讨论】:

  • 你能告诉我们dense_7_input是如何创建的吗?
  • 如果您的模型中的所有层都只有Dense 层并且您正在处理MNIST 数据集,那么您需要添加Flatten 层作为模型的第一层。

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

我查看了您的代码,并让它工作。我添加了 flatten 层,它将输入转换为可行的格式。我还使用 tensorflow.keras 而不是 keras,因为我认为那个版本更好,但你可以同时使用它。您的类型也有问题,所以我将您的 X_train 和 X_test 更改为 float32 类型的 numpy 数组。

# Imports
import tensorflow
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical

# Configuration options
feature_vector_length = 784
num_classes = 60000

# Load the data
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# Reshape the data - MLPs do not understand such things as '2D'.
# Reshape to 28 x 28 pixels = 784 features
X_train = X_train.reshape(X_train.shape[0], feature_vector_length)
X_test = X_test.reshape(X_test.shape[0], feature_vector_length)

# Convert into greyscale
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255

# Convert target classes to categorical ones
Y_train = to_categorical(Y_train, num_classes)
Y_test = to_categorical(Y_test, num_classes)

# Imports
import tensorflow
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten
from tensorflow.keras.utils import to_categorical
import numpy as np
# Configuration options
feature_vector_length = 784
num_classes = 60000

# Load the data
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

# Visualize one sample
import matplotlib.pyplot as plt
plt.imshow(X_train[0], cmap='Greys')
plt.show()

# Set the input shape
input_shape = (feature_vector_length,)
print(f'Feature shape: {input_shape}')
X_train=np.array(X_train,dtype="float32")
X_test=np.array(X_train,dtype="float32")
# Create the model
model = Sequential()
model.add(Flatten())
model.add(Dense(350, input_shape=input_shape, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

# Configure the model and start training

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy'])
model.fit(X_train, Y_train, epochs=10, batch_size=250, verbose=1, 
validation_split=0.2)


【讨论】:

  • 很抱歉打扰您,但我还有一个关于代码的问题...如果您能帮助我,我将不胜感激(它是 New 部分)
  • 我编辑了问题并添加了代码的最后一部分,但它给出了一个错误(我已经在主要问题中写了)
  • 你能告诉我 x_test 和 y_test 的大小/形状吗?
  • 这是您要问的吗? # 重塑为 28 x 28 像素 = 784 个特征代码 line1: X_train = X_train.reshape(X_train.shape[0], feature_vector_length) 代码 line2:X_test = X_test.reshape(X_test.shape[0], feature_vector_length)
  • @BernardoAugusto 请不要将 cmets 用于后续问题,特别是如果有问题的代码已以任何方式修改(包括由于响应);您可以(并且始终应该)使用新的状态和问题打开一个新问题。
猜你喜欢
  • 2019-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-01
  • 2020-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多