Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 2.7 TensorFlow:如何确保张量在同一个图中_Python 2.7_Tensorflow - Fatal编程技术网

Python 2.7 TensorFlow:如何确保张量在同一个图中

Python 2.7 TensorFlow:如何确保张量在同一个图中,python-2.7,tensorflow,Python 2.7,Tensorflow,我试图开始使用python中的TensorFlow,构建一个简单的前馈NN。我有一个类保存网络权重(在训练期间更新的变量,在运行时应该保持不变),还有一个脚本用于训练网络,它获取训练数据,将它们分批分离,并分批训练网络。 当我尝试训练网络时,我得到一个错误,表明数据张量与NN张量不在同一个图形中: ValueError:Tensor(“占位符:0”,形状=(10,5),dtype=float32)必须来自与Tensor(“windows/Embedded/Cast:0”,形状=(100232,5

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

ValueError:Tensor(“占位符:0”,形状=(10,5),dtype=float32)必须来自与Tensor(“windows/Embedded/Cast:0”,形状=(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位于同一个图中


谢谢

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

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中图形的信息:

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

最简单的检查方法是打印出用于图中每个变量/op的图。您可以简单地通过以下方式实现:

print(variable_name.graph)

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

感谢您的快速回答!然而,我做了这个更改(注释了会话,直到我正确构建了图形),我仍然得到了相同的错误——“张量(…)必须来自与张量(…)相同的图形”。如果看不到完整的代码,很难说。但很可能您的代码正在使用g.as_default()范围之外构造运算符,或者您正在调用的某些代码正在构造自己的图形。你能给我看更多的代码吗?(老实说,我要尝试的下一件事是检测构建操作符的Tensorflow Python代码,并打印每个操作符要添加到的图形的标识。)