如何在数组中操作数组 [英] how to manipulate array in array
问题描述
我有一个 X_train
np.array,形状为 (1433, 1)
.第一个维度 (1433
) 是用于训练的图像数量.第二个维度 (1
) 是一个 np.array,它本身的形状为 (224, 224, 3)
.我可以通过 X_train[0][0].shape
来确认.我需要将 X_train
拟合到模型中:
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)
错误输出一目了然:
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:]
对于形状 (1433, 9)
似乎没问题.
y_train[:,1:]
seems to be OK with a shape (1433, 9)
.
我需要对 model.fit
中的 X_train
做什么才能成功输入为 (1433, 224, 224, 3)?
What do I need to do with X_train
in model.fit
to successfully be able to input as (1433, 224, 224, 3)?
推荐答案
看来你有这样一个案例:
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)
其中 x_train
是 object
数组(如嵌套列表)而不是 numeric
数组.
Where x_train
is an object
array (like a nested list) not an numeric
array.
您需要将 x_train
更改为纯 numeric
数组:
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)
这篇关于如何在数组中操作数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!