【问题标题】:Cannot reshape array无法重塑数组
【发布时间】:2020-09-23 06:43:04
【问题描述】:

验证和测试数据形状:

x_train = train_data.reshape(train_data.shape[0], train_data.shape[1], train_data.shape[2], INPUT_DIMENSION)
#x_t =x_train = train_data.reshape(train_data.shape[0], train_data.shape[1], train_data.shape[2])
x_test_all = test_data.reshape(test_data.shape[0], test_data.shape[1], test_data.shape[2], INPUT_DIMENSION)

x_val = x_test_all[-VAL_SIZE:]
y_val = y_test[-VAL_SIZE:]

x_test = x_test_all[:-VAL_SIZE]
y_test = y_test[:-VAL_SIZE]


history_fdssc = model_fdssc.fit(
        [x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], x_train.shape[3], 1), 
         x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], x_train.shape[3])], [y_train, y_train, y_train],
        validation_data=(x_val.reshape(x_val.shape[0], x_val.shape[1], x_val.shape[2], x_val.shape[3], 1), y_val),
        batch_size=batch_size, epochs=nb_epoch, shuffle=True,
        callbacks=[early_Stopping, save_Best_Model, reduce_LR_On_Plateau, history, tensor_board])

当我运行程序时,我收到以下错误:

Please input the name of Dataset(IN, UP, KSC or SS):KSC
(512, 614, 176)
The class numbers of the HSI data is: 13
-----Importing Setting Parameters-----
-----Starting the  1 Iteration-----
Train size:  1048
Test size:  4163
Validation size:  524

-----Selecting Small Pieces from the Original Cube Data-----

Traceback(最近一次调用最后一次): 文件“hyb.py”,第 189 行,在 x_t =x_train = train_data.reshape(train_data.shape[0], train_data.shape[1], train_data.shape[2]) ValueError: 无法将大小为 14940288 的数组重塑为 (1048,9,9)

【问题讨论】:

    标签: python tensorflow image-processing reshape conv-neural-network


    【解决方案1】:

    错误源于第一行,因为数组的总大小不能被给定的整形参数整除。这是一个玩具示例::

    x_train = train_data.reshape(train_data.shape[0], train_data.shape[1], train_data.shape[2], INPUT_DIMENSION)
    
    >>> x = np.arange(14940288)
    >>> x.reshape(1049,9,9)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: cannot reshape array of size 14940288 into shape (1049,9,9)
    

    这是您应该尝试的。另外,请注意您没有提供变量INPUT_DIMENSION

    >>> x.reshape(-1,9,9).shape  #-1 takes the whole length
    (184448, 9, 9)
    >>> INPUT_DIMENSION = 16 
    >>> x.reshape(-1,9,9,INPUT_DIMENSION).shape
    (11528, 9, 9, 16)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-08-29
      • 2020-06-13
      • 2016-05-22
      • 2022-01-08
      相关资源
      最近更新 更多