【问题标题】:Error when checking target: expected dense_1 to have shape (5749,) but got array with shape (1,)检查目标时出错:预期 dense_1 的形状为 (5749,) 但得到的数组的形状为 (1,)
【发布时间】:2020-03-19 21:34:18
【问题描述】:

我是深度学习的初学者,正在构建一个程序,可以根据图像确定人物。但我的神经网络显示错误,我不知道如何修复它 -

model.fit(imgs_array,Y,batch_size = 401, epochs = 2, validation_split = 0.2)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1527, in fit
    x, y, sample_weights = self._standardize_user_data(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 991, in _standardize_user_data
    x, y, sample_weights = self._standardize_weights(x, y, sample_weight,
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py", line 1149, in _standardize_weights
    y = training_utils.standardize_input_data(
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/training_utils.py", line 329, in standardize_input_data
    raise ValueError(
ValueError: Error when checking target: expected dense_1 to have shape (5749,) but got array with shape (1,)

我的完整代码是

import os
import numpy as np
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from sklearn.model_selection import train_test_split
from tensorflow.python import keras
from tensorflow.keras.utils import to_categorical
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D, Dropout


folders = os.listdir('lfw/')
# print(len(folders))       #5749
folders_a = np.asarray(folders)

image_files = []
X = []
Y = []
for folder in folders:
    files = os.listdir('lfw/'+folder+'/')       # type of files is a list
    image_files.append(files)
    for file in files:
        X.append(file)
        Y.append(folder)

# print(len(Y))     #13233
img_paths = []
for i in range(0,13233):
    img_paths.append('lfw/'+Y[i]+'/'+X[i])      #img_paths is set now

print(len(img_paths))

imgs = []
for img_path in img_paths:
    img_1 = load_img(img_path, color_mode="grayscale")
    imgs.append(img_to_array(img_1))

    # print(imgs[0].shape)      #(250,250,1)



Y = np.array(Y)
# print(type(Y))
# print(Y.shape)        #(13233,)

#Building Neural Network
imgs_array = np.array(imgs)
imgs_array /= 255


model = Sequential()
model.add(Conv2D(20, kernel_size = 3, activation = 'relu', input_shape =(250,250,1)))
model.add(Conv2D(20, kernel_size =3, activation = 'relu'))
model.add(Flatten())
model.add(Dense(401, activation = 'relu'))
model.add(Dense(5749,activation = 'softmax'))
print("compiling initiated")
model.compile(loss = 'mean_squared_error',optimizer = 'sgd')
print("model compiled")
model.fit(imgs_array,Y,batch_size = 401, epochs = 2, validation_split = 0.2)

错误的原因是什么以及如何解决?上面代码中的 print(Y.shape) 行给了我输出 (13233,) 。为什么会在错误中显示(1, )

注意 - 我已经看到下面给出的链接,但它们没有解决我的问题。

Error when checking target: expected dense_1 to have shape (257, 257) but got array with shape (257, 1)

Error in fitting an RNN LSTM model

Error when checking target: expected dense_1 to have shape (1,) but got array with shape (256,)

【问题讨论】:

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


    【解决方案1】:

    乍一看,我认为您的问题如下: 您的网络将形状张量 (batch_size, 250, 250, 1) 作为输入 并给出形状为 (batch_size, 5749) 的张量作为输出

    当您开始拟合时,keras 会批量输入数据和相应的标签,因此它会为您的网络输入张量提供形状 (batch_size,) + imgs_array.shape[1:] 和形状标签 (batch_size,) + Y.shape[1:]

    对于批次中的每个图像,您的标签是一个标量(或者显然是一个一维向量,我没有仔细阅读),并且您与之比较的输出是一个大小为 5749 的张量,其中不是标量。

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2018-08-29
      • 2019-08-02
      • 2019-07-07
      • 2020-09-03
      • 2020-03-13
      相关资源
      最近更新 更多