###python自带的zip函数 与 tf.data.Dataset.zip函数 功能用法相似 ‘‘‘ zip([iterator1,iterator2,]) 将可迭代对象中对应的元素打包成一个元祖,返回有这些元祖组成的对象,用list把这个对象转化成列表 ‘‘‘ a=[1,2,3] b = [4,5,6] c = [7,8,9,10,11] res1 = zip(a,b) res2 = zip(a,c) print(‘返回一个对象%s,用list转化成列表:‘%res1,list(res1)) print(‘长短不一,以最短者对应返回:‘,list(res2)) ‘‘‘ 返回一个对象<zip object at 0x0000019F644A8388>,用list转化成列表: [(1, 4), (2, 5), (3, 6)] 长短不一,以最短者对应返回: [(1, 7), (2, 8), (3, 9)] ‘‘‘ ###tf.data.Dataset.zip函数功能与zip()一致 import tensorflow as tf Dataset = tf.data.Dataset a = Dataset.from_tensor_slices([1,2,3]) b = Dataset.from_tensor_slices([4,5,6]) c = Dataset.from_tensor_slices([(7,8),(9,10),(11,12)]) #Dataset数据用迭代器一次取值,先定义一个迭代器函数 def getone(dataset): iterator = dataset.make_one_shot_iterator() #生成一个迭代器 one_element = iterator.get_next() #迭代器取值 return one_element dataset1 = Dataset.zip((a,b)) dataset2 = Dataset.zip((a,b,c)) one_element1 = getone(dataset1) one_element2 = getone(dataset2) #定义一个会话内调用的函数 def sess_get_one(one_element): for i in range(3): datav = sess.run(one_element) print(datav) #开启会话,调取数据 with tf.Session() as sess: sess_get_one(one_element1) sess_get_one(one_element2) ‘‘‘ (1, 4) (2, 5) (3, 6) (1, 4, array([7, 8])) (2, 5, array([ 9, 10])) (3, 6, array([11, 12])) ‘‘‘
原文地址:https://www.cnblogs.com/liuhuacai/p/11732198.html
时间: 2024-10-18 10:16:59