简体   繁体   English

从数据集的元组创建列表

[英]Create a list from tuples of dataset

I use a function that takes raw dataset and returns train data and test data.我使用 function 获取原始数据集并返回训练数据和测试数据。 (Doesn't only split the data, but also does some slicing, shuffling, processing on data) (不仅对数据进行拆分,还会对数据进行一些切片、洗牌、处理)

def create_dataset():
            ...
            ...

            train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))
            train_data = train_data.cache().shuffle(buffer_size).batch(batch_size).repeat()
            test_data = tf.data.Dataset.from_tensor_slices((x_test, y_test))
            test_data = test_data.batch(batch_size).repeat() 

            return train_data,test_data

My target is to make a list of the tuples of train and test data returned from the function.我的目标是列出从 function 返回的训练和测试数据的元组。 Which i tried kinda look like this.我试过的有点像这样。

td = []
vd = []
for k in range(0,5):
    td[k],vd[k] = create_dataset()

    
datasets = [(td[0],vd[0]),(td[1],vd[1]),(td[2],vd[2]),(td[3],vd[3]),(td[4],vd[4])]

But it seems i can not store data like this.但似乎我不能像这样存储数据。 How would I create a list of tuples of my (train_data,test_data)?我将如何创建我的(train_data,test_data)的元组列表? Thanks in advance.提前致谢。

I don't know if I miss something here, but this should work for your goal:我不知道我是否在这里遗漏了一些东西,但这应该可以实现您的目标:

datasets = []
for _ in range(5):
   x, y = create_dataset()
   datasets.append((x,y))

If your lists are empy you have to use append(...) to add a avlue to this list, or init it at the right size first.如果您的列表是空的,您必须使用 append(...) 向此列表添加一个 avlue,或者首先以正确的大小对其进行初始化。 You cannot access/read/write td[k] and vd[k] if the k th elements are not already existing/initialized.如果第 k 个元素尚不存在/未初始化,则您无法访问/读取/写入 td[k] 和 vd[k]。

One solution is to init your lists with 5 empty tuples, so it will work,一种解决方案是用 5 个空元组初始化列表,这样它就可以工作了,

td = [(), (), (), (), () ]
vd = [(), (), (), (), () ]
for k in range(0,5):
    td[k],vd[k] = create_dataset()

    
datasets = [(td[0],vd[0]),(td[1],vd[1]),(td[2],vd[2]),(td[3],vd[3]),(td[4],vd[4])]

Another solution is to build the datasets list directlty from an empty list and append the results of create_dataset() calls:另一种解决方案是直接从一个空列表和 append 构建数据集列表 create_dataset() 调用的结果:

datasets = []
for k in range(0,5):
    datasets.append(create_dataset())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM