ValueError:数据基数不明确.确保所有阵列都包含相同数量的样本 [英] ValueError: Data cardinality is ambiguous. Make sure all arrays contain the same number of samples

查看:759
本文介绍了ValueError:数据基数不明确.确保所有阵列都包含相同数量的样本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Colab上运行以下代码.这是一个回归问题,我想从每个大小为224 x 224的图像生成5个浮点值.据我的理解,要解决此问题,我应该在最后一层中使用具有5个节点的完全连接的网络.但是在keras上这样做会给我以下所述的错误.

I am running the following code on Colab. This is a regression problem, where I want to generate 5 float values from each image of size 224 x 224. As per my understanding, to solve this problem, I should use fully connected networks with 5 nodes in the last layer. But doing so on keras gave me an error described below.

import keras, os
import numpy as np
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.applications.inception_v3 import InceptionV3

## data_list = list of four 224x224 numpy arrays

inception = InceptionV3(weights='imagenet', include_top=False)
x = inception.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(5, activation='relu')(x)

y = [np.random.random(5),np.random.random(5),np.random.random(5),np.random.random(5)]

model = Model(inputs=inception.input, outputs=predictions)
opt = Adam(lr=0.001)
model.compile(optimizer=opt, loss="mae")
model.fit(data_list, y, verbose=0, epochs=100)

错误:

ValueError:数据基数不明确:
x尺寸:224、224、224、224
y尺寸:5、5、5、5
确保所有阵列都包含相同数量的样本.

ValueError: Data cardinality is ambiguous:
     x sizes: 224, 224, 224, 224
     y sizes: 5, 5, 5, 5
Make sure all arrays contain the same number of samples.

出什么问题了?

推荐答案

data_list y 转换为numpy数组或张量.

Convert data_list and y to numpy arrays or tensors.

在您的代码中,列表被视为四个输入,而模型只有一个输入- https://keras.io/api/models/model_training_apis/

In your code the list is treated as four inputs while your model has one input - https://keras.io/api/models/model_training_apis/

添加以下行:

data_list = tf.stack(data_list)
y = tf.stack(y)

这篇关于ValueError:数据基数不明确.确保所有阵列都包含相同数量的样本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆