Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow如何更改数据集_Python_Tensorflow_Tensorflow Datasets - Fatal编程技术网

Python tensorflow如何更改数据集

Python tensorflow如何更改数据集,python,tensorflow,tensorflow-datasets,Python,Tensorflow,Tensorflow Datasets,我有一个数据集API doohickey,它是我的tensorflow图的一部分。当我想使用不同的数据时,如何交换它 dataset = tf.data.Dataset.range(3) iterator = dataset.make_one_shot_iterator() next_element = iterator.get_next() variable = tf.Variable(3, dtype=tf.int64) model = variable*next_element #pr

我有一个数据集API doohickey,它是我的tensorflow图的一部分。当我想使用不同的数据时,如何交换它

dataset = tf.data.Dataset.range(3)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()

variable = tf.Variable(3, dtype=tf.int64)
model = variable*next_element

#pretend like this is me training my model, or something
with tf.Session() as sess:
    sess.run(variable.initializer)
    try:
        while True:
            print(sess.run(model)) # (0,3,6)
    except:
        pass

dataset = tf.data.Dataset.range(2)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()  

### HOW TO DO THIS THING?
with tf.Session() as sess:
    sess.run(variable.initializer) #This would be a saver restore operation, normally...
    try:
        while True:
            print(sess.run(model)) # (0,3)... hopefully
    except:
        pass

我认为这是不可能的。您要求更改计算图本身,这在tensorflow中是不允许的。我并没有亲自解释这一点,而是发现这篇文章中公认的答案在解释这一点时特别清晰

这就是说,我认为有一个相当简单/干净的方法来实现你的目标。基本上,您需要重置图形并重建
数据集
部分。当然,您希望重用代码的
模型
部分。因此,只需将该模型放在一个类或函数中,以允许重用。一个基于您的代码构建的简单示例:

# the part of the graph you want to reuse
def get_model(next_element):
    variable = tf.Variable(3,dtype=tf.int64)
    return variable*next_element

# the first graph you want to build
tf.reset_default_graph()

# the part of the graph you don't want to reuse
dataset = tf.data.Dataset.range(3)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()

# reusable part
model = get_model(next_element)

#pretend like this is me training my model, or something
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    try:
        while True:
            print(sess.run(model)) # (0,3,6)
    except:
        pass

# now the second graph
tf.reset_default_graph()

# the part of the graph you don't want to reuse
dataset = tf.data.Dataset.range(2)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()  

# reusable part
model = get_model(next_element)

### HOW TO DO THIS THING?
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    try:
        while True:
            print(sess.run(model)) # (0,3)... hopefully
    except:
        pass

最后一个注释:您还将在这里和那里看到一些对
tf.contrib.graph\u编辑器
的参考。他们特别指出,使用graph_编辑器无法完成您想要的内容(请参见该链接中的“这是一个您无法完成的内容的示例”;但您可以非常接近)。尽管如此,这不是一个好的做法;他们有很好的理由只添加图形,我认为我建议的上述方法是实现您所寻求的目标的更干净的方法。

我建议的一种方法是使用
place\u holder
,然后使用
tf.data.dataset
。因此,您将拥有以下功能:

train_data = tf.placeholder(dtype=tf.float32, shape=[None, None, 1]) # just an example
# Then add the tf.data.dataset here
train_data = tf.data.Dataset.from_tensor_slices(train_data).shuffle(10000).batch(batch_size)
现在,在会话中运行图形时,必须使用占位符输入数据。所以你想喂什么就喂什么


希望这有帮助

这就是我一直在做的事情,它正在发挥作用。当我问这个问题时,我在恢复变量时遇到了问题,因为我没有重置默认图(duh!)。这绝对是一条路要走。