Python没有足够的值从tensorflow数据集中解包

2024-04-26 03:42:15 发布

您现在位置:Python中文网/ 问答频道 /正文

在Tensorflow example之后

TRAIN_DATA_URL = "https://storage.googleapis.com/tf-datasets/titanic/train.csv"
LABEL_COLUMN_TRAIN = 'Survived'
train_file_path = tf.keras.utils.get_file("train.csv", TRAIN_DATA_URL)
SELECT_COLUMNS = ['Survived', 'Age', 'SibSp', 'Fare']
DEFAULTS = [0, 0.0, 0.0, 0.0]
temp_dataset = get_dataset(
    train_file_path, 
    label_column_name=LABEL_COLUMN_TRAIN, 
    select_columns=SELECT_COLUMNS,
    column_defaults=DEFAULTS
)


def pack(features, label):
  return tf.stack(list(features.values()), axis=-1), label

packed_dataset = temp_dataset.map(pack)
for features, labels in packed_dataset.take(1):
  print(features.numpy())
  print(labels.numpy())

如下所示

[[ 0.     1.    15.5  ]
 [30.     3.    21.   ]
 [ 0.     0.     7.896]
 [21.     0.     8.663]
 [39.     0.     7.925]]
[0 1 0 0 0]

问题:

请帮助理解错误的原因

我相信packed_dataset.take(1)返回两个元素的元组

packed_dataset.take(1)
<TakeDataset shapes: ((None, 3), (None,)), types: (tf.float32, tf.int32)>

但是,下面是导致错误的原因

(features, labels) = packed_dataset.take(1)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-125-dceb31d47f78> in <module>
----> 1 features, labels = packed_dataset.take(1)

ValueError: not enough values to unpack (expected 2, got 1)

Tags: csvurldatalabelstfcolumntraindataset
1条回答
网友
1楼 · 发布于 2024-04-26 03:42:15

功能 pack(feature, labels) 需要2个参数,而函数调用 for features, labels in packed_dataset.take(1) 只传递一个值为1的参数,它应该 for features, labels in packed_dataset.take(1, ?)

相关问题 更多 >