how to manipulate array in array

谁都会走 提交于 2020-02-25 04:12:33

问题


I have got a X_train np.array with shape of (1433, 1). The first dimension (1433) is the number of images for training. The second dimension (1) is an np.array which itself has a shape (224, 224, 3). I could confirm it by X_train[0][0].shape. I need to fit X_train to the model:

model.fit([X_train, y_train[:,1:]], y_train[:,0], epochs=50, batch_size=32,  verbose=1)

Error output is self-explanatory:

    Traceback (most recent call last):
  File "/home/combined/file_01.py", line 97, in <module>
    img_output = Flatten()(x_1)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py", line 414, in __call__
    self.assert_input_compatibility(inputs)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py", line 327, in assert_input_compatibility
    str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer flatten_1: expected min_ndim=3, found ndim=2

y_train[:,1:] seems to be OK with a shape (1433, 9).

What do I need to do with X_train in model.fit to successfully be able to input as (1433, 224, 224, 3)?


回答1:


It seems that you have a case like this:

import numpy as np
x_train = np.zeros((1433, 1), dtype=object)
for i in range(x_train.shape[0]):
    x_train[i, 0] = np.random.random((224, 224, 3))

x_train.shape        # (1433, 1)
x_train[0, 0].shape  # (224, 224, 3)

Where x_train is an object array (like a nested list) not an numeric array.

You need to change x_train to a pure numeric array:

x_train = np.array([x for x in x_train.flatten()], dtype=float)
x_train.shape       # (1433, 224, 224, 3)
x_train[0].shape    # (224, 224, 3)


来源:https://stackoverflow.com/questions/60331256/how-to-manipulate-array-in-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!