gpt4 book ai didi

python - 如何操作数组中的数组

转载 作者:行者123 更新时间:2023-12-01 06:21:36 24 4
gpt4 key购买 nike

我有一个 X_train np.array,形状为 (1433, 1)。第一个维度 (1433) 是用于训练的图像数量。第二个维度 (1) 是一个 np.array,其本身的形状为 (224, 224, 3)。我可以通过 X_train[0][0].shape 来确认。我需要将 X_train 适合模型:

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)

我需要如何处理 model.fit 中的 X_train 才能成功输入 (1433, 224, 224, 3)?

最佳答案

您似乎遇到过这样的情况:

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_trainobject 数组(如嵌套列表),而不是 numeric 数组。

您需要将x_train更改为纯数字数组:

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)

关于python - 如何操作数组中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60331256/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com