使用数据集API从两个不同数据源获取数据时了解张量流行为

我正在尝试dataset使用张量流从两个不同的来源获取数据。我写了下面的代码:


首先,我尝试了以下方法:


import tensorflow as tf

import numpy as np


iters = []


def return_data1():

    d1 = tf.data.Dataset.range(1, 2000)

    iter1 = d1.make_initializable_iterator()

    iters.append(iter1)

    data1 = iter1.get_next()

    return data1


def return_data2():

    d2 = tf.data.Dataset.range(2000, 4000)

    iter2 = d2.make_initializable_iterator()

    iters.append(iter2)

    data2 = iter2.get_next()

    return data2


test = tf.placeholder(dtype=tf.bool)


data = tf.cond(test, lambda: return_data1(), lambda: return_data2())


iter1 = iters[0]

iter2 = iters[1]


init_op = tf.global_variables_initializer()


with tf.Session() as sess:

    sess.run(init_op)

    sess.run([iter1.initializer, iter2.initializer])


    for i in range(2000):

        if i < 1000:

            print(sess.run(data, feed_dict={test: True}), "..")

        else:

            print(sess.run(data, feed_dict={test: False}), "--")

我得到了以下错误:


ValueError: Operation 'cond/MakeIterator' has been marked as not fetchable.

1-我想知道为什么我会出现这种现象。


FFIVE
浏览 136回答 1
1回答

ABOUTYOU

这是如何分别从多个数据集中获取数据的方法。但是我想知道其他有关张量流行为以及为什么应data2 = iter2.get_next()在方法中定义的问题的答案。import tensorflow as tfimport numpy as npd1 = tf.data.Dataset.range(1, 1000)iter1 = d1.make_initializable_iterator()d2 = tf.data.Dataset.range(1000, 2000)iter2 = d2.make_initializable_iterator()d3 = tf.data.Dataset.range(2000, 3000)iter3 = d3.make_initializable_iterator()d4 = tf.data.Dataset.range(3000, 4000)iter4 = d4.make_initializable_iterator()def return_data1_2():&nbsp; &nbsp; data1 = iter1.get_next()&nbsp; &nbsp; data2 = iter2.get_next()&nbsp; &nbsp; return data1, data2def return_data2_3():&nbsp; &nbsp; data2 = iter2.get_next()&nbsp; &nbsp; data3 = iter3.get_next()&nbsp; &nbsp; return data2, data3def return_data3_4():&nbsp; &nbsp; data3 = iter3.get_next()&nbsp; &nbsp; data4 = iter4.get_next()&nbsp; &nbsp; return data3, data4def return_data4_1():&nbsp; &nbsp; data4 = iter4.get_next()&nbsp; &nbsp; data1 = iter1.get_next()&nbsp; &nbsp; return data4, data1index1 = tf.placeholder(dtype=tf.int32)index2 = tf.placeholder(dtype=tf.int32)data = tf.case(pred_fn_pairs=[&nbsp; &nbsp; (tf.logical_and(tf.equal(index1, 1), tf.equal(index2, 2)), lambda: return_data1_2()),&nbsp;&nbsp; &nbsp; (tf.logical_and(tf.equal(index1, 2), tf.equal(index2, 3)), lambda: return_data2_3()),&nbsp; &nbsp; (tf.logical_and(tf.equal(index1, 3), tf.equal(index2, 4)), lambda: return_data3_4()),&nbsp; &nbsp; (tf.logical_and(tf.equal(index1, 4), tf.equal(index2, 1)), lambda: return_data4_1())], exclusive=False)init_op = tf.global_variables_initializer()with tf.Session() as sess:&nbsp; &nbsp; sess.run(init_op)&nbsp; &nbsp; sess.run([iter1.initializer, iter2.initializer, iter3.initializer, iter4.initializer])&nbsp; &nbsp; for i in range(2000):&nbsp; &nbsp; &nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if i < 500:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(sess.run(data, feed_dict={index1: 1, index2: 2}), "1-2")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif i < 1000:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(sess.run(data, feed_dict={index1: 2, index2: 3}), "2-3")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif i < 1500:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(sess.run(data, feed_dict={index1: 3, index2: 4}), "3-4")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif i < 2000:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(sess.run(data, feed_dict={index1: 4, index2: 1}), "4-1")&nbsp; &nbsp; &nbsp; &nbsp; except tf.errors.OutOfRangeError as error:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("error")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python