【问题标题】:TensorFlow: How to ensure Tensors are in the same graphTensorFlow:如何确保张量在同一个图中
【发布时间】:2016-10-27 09:38:31
【问题描述】:

我正在尝试在 python 中开始使用 TensorFlow,构建一个简单的前馈神经网络。我有一个类保存网络权重(在训练期间更新的变量,并且应该在运行时保持不变)和另一个训练网络的脚本,它获取训练数据,将它们分成批次并分批训练网络. 当我尝试训练网络时,我收到一个错误,表明数据张量与 NN 张量不在同一个图中:

ValueError: Tensor("Placeholder:0", shape=(10, 5), dtype=float32) 必须与 Tensor("windows/embedding/Cast:0", shape=(100232, 50) 来自同一个图), dtype=float32)。

训练脚本中的相关部分是:

def placeholder_inputs(batch_size, ner):
  windows_placeholder = tf.placeholder(tf.float32, shape=(batch_size, ner.windowsize))
  labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size))
  return windows_placeholder, labels_placeholder

with tf.Session() as sess:
  windows_placeholder, labels_placeholder = placeholder_inputs(batch_size, ner)
  logits = ner.inference(windows_placeholder)

和网络类中相关的是:

class WindowNER(object):
def __init__(self, wv, windowsize=3, dims=[None, 100,5], reg=0.01):
    self.reg=reg
    self.windowsize=windowsize
    self.vocab_size = wv.shape[0]
    self.embedding_dim = wv.shape[1]
    with tf.name_scope("embedding"):
        self.L = tf.cast(tf.Variable(wv, trainable=True, name="L"), tf.float32)
    with tf.name_scope('hidden1'):
        self.W = tf.Variable(tf.truncated_normal([windowsize * self.embedding_dim, dims[1]],
            stddev=1.0 / math.sqrt(float(windowsize*self.embedding_dim))),
        name='weights')
        self.b1 = tf.Variable(tf.zeros([dims[1]]), name='biases')
    with tf.name_scope('output'):
        self.U = tf.Variable(tf.truncated_normal([dims[1], dims[2]], stddev = 1.0 / math.sqrt(float(dims[1]))), name='weights')
        self.b2 = tf.Variable(tf.zeros(dims[2], name='biases'))


def inference(self, windows):
    with tf.name_scope("embedding"):
        embedded_words = tf.reshape(tf.nn.embedding_lookup(self.L, windows), [windows.get_shape()[0], self.windowsize * self.embedding_dim])
    with tf.name_scope("hidden1"):
        h = tf.nn.tanh(tf.matmul(embedded_words, self.W) + self.b1)
    with tf.name_scope('output'):
        t = tf.matmul(h, self.U) + self.b2

为什么首先有两个图,如何确保数据占位符张量与 NN 在同一个图中?

谢谢!!

【问题讨论】:

    标签: python-2.7 tensorflow


    【解决方案1】:

    您应该能够通过执行以下操作在同一图表下创建所有张量:

    g = tf.Graph()
    with g.as_default():
      windows_placeholder, labels_placeholder = placeholder_inputs(batch_size, ner)
      logits = ner.inference(windows_placeholder)
    
    with tf.Session(graph=g) as sess:
      # Run a session etc
    

    您可以在此处阅读有关 TF 中图表的更多信息: https://www.tensorflow.org/versions/r0.8/api_docs/python/framework.html#Graph

    【讨论】:

    • 感谢您的快速回答!但是,我进行了此更改(注释掉会话,直到我正确构建图表),我仍然得到相同的错误 - “张量(...)必须来自与张量(...)相同的图表”。
    • 看不到完整代码很难说。但似乎您的代码可能在with g.as_default() 范围之外构建运算符,或者您正在调用的某些代码正在构建自己的图形。你能显示更多的代码吗? (说实话,接下来我要尝试的是检测构建运算符的 Tensorflow Python 代码并打印每个运算符被添加到的图形的标识。)
    【解决方案2】:

    有时当您遇到这样的错误时,错误(通常可能是使用来自不同图表的错误变量)可能发生得更早,并传播到最终引发错误的操作。因此,您可能只调查那条线并得出结论,张量应该来自同一个图,而错误实际上位于其他地方。

    最简单的检查方法是打印出图表中每个变量/操作使用的图表。您可以这样做:

    print(variable_name.graph)
    

    【讨论】:

      【解决方案3】:

      确保在定义所有变量之前重置图表...在我的例子中,我在定义变量之一之后使用了命令 tf.compat.v1.reset_default_graph()

      【讨论】:

        猜你喜欢
        • 2019-03-18
        • 2018-08-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多