简体   繁体   English

ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (595、10083) 的数组

[英]ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083)

I was trying to build a CNN model using Keras.我正在尝试使用 Keras 构建 CNN model。 Here is my code:这是我的代码:

import keras
import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential
from keras.models import load_model
from keras.callbacks import EarlyStopping  
from sklearn.model_selection import train_test_split
from __future__ import print_function
import pandas as pd
import numpy as np
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D
from google.colab import drive
drive.mount('/content/drive')
                  ...

# Define x_train...data
x_data=df.iloc[:,1:10084].values-25 
# x_data = np.array(x_data).tolist()  
y_data=df[['type1','type2']].values
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data,test_size=0.2)

model.fit(X_train, y_train,
          batch_size=100,
          epochs=100,
          verbose=1,
          validation_data=(X_test, y_test),
          callbacks=[history])

It returned the error它返回了错误

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (595、10083) 的数组

After referring to other questions I tried to reshape the dimension of the data array using在参考其他问题后,我尝试使用重塑数据数组的维度

X_train = X_train[np.newaxis, :, :, :]

which changed it to 3-dimentional and returned the error:将其更改为 3 维并返回错误:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1, 595, 10083) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (1, 595, 10083) 的数组

How should I increase the dimension of the data to 4?我应该如何将数据的维度增加到4?

The first layer of the model, which a 2D convolution, is expecting (I believe) 2D image data with multiple channels. model 的第一层是一个 2D 卷积,它期望(我相信)具有多个通道的 2D 图像数据。 Something like:就像是:

(img_height, img_width, num_channels). (img_height、img_width、num_channels)。

In addition, you need to have another dimension for the "batch" or "sample."此外,您需要为“批次”或“样本”提供另一个维度。 Therefore the final shape will be:因此最终的形状将是:

(num_samples, img_height, img_width, num_channels). (num_samples、img_height、img_width、num_channels)。

So, you'll need to reshape your input data to be in the above pattern, with the various dimensions having the interpretation I've indicated, based on what I infer from your code.因此,您需要根据我从您的代码中推断出的内容,将您的输入数据重塑为上述模式,并具有我所指出的解释的各种维度。

You might look at functions like np.reshape and np.expand_dims, depending on how your input data is formatted in the first place.您可能会查看 np.reshape 和 np.expand_dims 之类的函数,具体取决于您的输入数据的格式。

You can create some test data like this:您可以像这样创建一些测试数据:

x_train = np.random.random((4,32,32,3))

# then try out the model prediction

model.predict(x_train)  

which is four images of 32x32 size, and 3 channels, to verify your processing.这是四个 32x32 大小的图像和 3 个通道,以验证您的处理。

You'll also need to create something appropriate for the y_train when you do the actual fitting.在进行实际拟合时,您还需要创建适合 y_train 的内容。

I hope this helps.我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但在与 model 拟合时得到形状为 (999, 12, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (999, 12, 1) while fitting with model ValueError:检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(454,512,512) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (454, 512, 512) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (117, 1, 32, 32, 3) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (117, 1, 32, 32, 3) ValueError:检查输入时出错:预期 conv2d_1_input 的形状为 (224, 224, 1) 但得到的数组的形状为 (224, 224, 8) - ValueError: Error when checking input: expected conv2d_1_input to have shape (224, 224, 1) but got array with shape (224, 224, 8) ValueError:检查时出错:预期conv2d_1_input具有形状(28,28,1),但数组的形状为(58000,28,28) - ValueError: Error when checking : expected conv2d_1_input to have shape (28, 28, 1) but got array with shape (58000, 28, 28) ValueError:检查输入时出错:预期 conv2d_1_input 具有形状 (128, 75, 1) 但得到形状为 (1, 128, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have shape (128, 75, 1) but got array with shape (1, 128, 1) Python 神经网络 - 检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到形状为 (700, 128, 33) 的数组 - Python Neural Networks - Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (700, 128, 33) 维度问题:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (26, 26, 1) 的数组 - Dimension problems: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (26, 26, 1) ValueError:检查输入时出错:预期conv3d_1_input具有5个维,但数组的形状为(7,9,384,1) - ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM