【问题标题】:ValueError: cannot reshape array of size 445513728 into shape (2266,256,256,1)ValueError:无法将大小为 445513728 的数组重塑为形状 (2266,256,256,1)
【发布时间】:2020-01-29 10:15:39
【问题描述】:

这是我要运行的代码

labelled_data = [data, Label]
X,Y = [labelled_data[0],labelled_data[1]]

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.4, 
random_state=4)

x_test, x_validation, y_test, y_validation=train_test_split(X_test, 
Y_test, test_size=0.5,
 random_state=4)
import numpy as np


print(X_train.shape)

输出是:

(2266, 196608)

现在我想重塑训练集、验证集和测试集的矩阵。

X_train = X_train.reshape((X_train.shape[0],256,256,1))
x_validation = x_validation.reshape((x_validation.shape[0],256,256,1))
x_test =x_test.reshape((x_test.shape[0],256,256,1))

X_train = X_train.astype('float32')
x_validation = x_validation.astype('float32')
x_test = x_test.astype('float32')


X_train = X_train/255
x_validation = x_validation/255
x_test =x_test/255

from keras.utils import np_utils
Y_train = np_utils.to_categorical(Y_train,8)
y_validation =np_utils.to_categorical(y_validation,8)
y_test =np_utils.to_categorical(y_test,8)

在运行 tis 时,我遇到了一些错误。

ValueError                                Traceback (most recent call last)
<ipython-input-30-fc799feec008> in <module>
----> 1 X_train = X_train.reshape((X_train.shape[0],256,256,1))
      2 x_validation = x_validation.reshape((x_validation.shape[0],256,256,1))
      3 x_test =x_test.reshape((x_test.shape[0],256,256,1))
      4 
      5 X_train = X_train.astype('float32')

ValueError: cannot reshape array of size 445513728 into shape (2266,256,256,1)

请帮我解决这个问题

【问题讨论】:

    标签: python-3.x machine-learning image-processing keras


    【解决方案1】:

    如果 print(X_train.shape) 的输出是 (2266, 196608),那么 X_train.shape[0] 是 2266。

    如果你说

    X_train = X_train.reshape((X_train.shape[0],256,256,1))
    

    您正在尝试将 2266 x 196608 (=445513728) 重塑为 2266 x 256 x 256 x 1 (=148504576) 以便收到消息

    ValueError: cannot reshape array of size 445513728 into shape (2266,256,256,1)
    

    这 256 个值中的一个必须是 768 才能使重塑工作。

    【讨论】:

      猜你喜欢
      • 2021-08-02
      • 2021-03-20
      • 2021-03-21
      • 2018-10-22
      • 2019-11-29
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多