使用python API执行tensorflow图时实际发生了什么?

使用python API执行tensorflow图时实际发生了什么?,python,tensorflow,Python,Tensorflow,我是tensorflow的新手。但我认为必须了解tesnorflow的核心运营。如果我们以面向对象的方式使用tf python API,我们可以首先创建不同的图形操作作为定义 def _create_placeholders(self): """ Step 1: define the placeholders for input and output """ with tf.name_scope("data"): self.center_words = tf.pl

我是tensorflow的新手。但我认为必须了解tesnorflow的核心运营。如果我们以面向对象的方式使用tf python API,我们可以首先创建不同的图形操作作为定义

def _create_placeholders(self):
    """ Step 1: define the placeholders for input and output """
    with tf.name_scope("data"):
        self.center_words = tf.placeholder(tf.int32, shape=[self.batch_size], name='center_words')
        print("Extracting the op",self.center_words.op)
        self.target_words = tf.placeholder(tf.int32, shape=[self.batch_size, 1], name='target_words')
        print("so",self.center_words.op)

def _create_embedding(self):
    """ Step 2: define weights. In word2vec, it's actually the weights that we care about """
    # Assemble this part of the graph on the CPU. You can change it to GPU if you have GPU
    with tf.device('/cpu:0'):
        with tf.name_scope("embed"):
            self.embed_matrix = tf.Variable(tf.random_uniform([self.vocab_size, 
                                                                self.embed_size], -1.0, 1.0), 
                                                                name='embed_matrix')

def _create_loss(self):
    """ Step 3 + 4: define the model + the loss function """
    with tf.device('/cpu:0'):
        with tf.name_scope("loss"):
            # Step 3: define the inference
            embed = tf.nn.embedding_lookup(self.embed_matrix, self.center_words, name='embed')

            # Step 4: define loss function
            # construct variables for NCE loss
            nce_weight = tf.Variable(tf.truncated_normal([self.vocab_size, self.embed_size],
                                                        stddev=1.0 / (self.embed_size ** 0.5)), 
                                                        name='nce_weight')
            nce_bias = tf.Variable(tf.zeros([VOCAB_SIZE]), name='nce_bias')


            # define loss function to be NCE loss function
            self.loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weight, 
                                                biases=nce_bias, 
                                                labels=self.target_words, 
                                                inputs=embed, 
                                                num_sampled=self.num_sampled, 
                                                num_classes=self.vocab_size), name='loss')
这里我提到了两个定义,它们用于创建嵌入和计算损失。 因此,如果我使用\u create\u loss()运行其中一个def,它将在图中创建一个节点。我浏览了tf源代码,我看到的是在图形构建阶段,它会将每个操作加载到某种缓冲区。 然后,在会话期间,我们只需使用真实数据重新运行每一项

with tf.Session(config=tf.ConfigProto(log_device_placement=False)) as sess:
    sess.run(tf.global_variables_initializer())

    ckpt = tf.train.get_checkpoint_state(os.path.dirname('c/checkpointsq'))
    # if that checkpoint exists, restore from checkpoint
    if ckpt and ckpt.model_checkpoint_path:
        print("Restoring the checkpoins")
        saver.restore(sess, ckpt.model_checkpoint_path)

    total_loss = 0.0 # we use this to calculate late average loss in the last SKIP_STEP steps
    writer = tf.summary.FileWriter('./ improved_graph/lr' + str(LEARNING_RATE), sess.graph)
    initial_step = model.global_step.eval()

    for index in range(1):
        centers, targets = batch_gen.__next__()
        feed_dict={model.center_words: centers, model.target_words: targets}
        loss_batch, _, summary = sess.run([model.loss, model.optimizer, model.summary_op], 
                                          feed_dict=feed_dict)  

这是我的问题。在这里,sess.run tensorflow甚至不关心python API。它只关心从上述图形初始化代码加载的图形操作我的问题是所有这些操作在会话对象中执行的位置。我能理解它的核心。我们可以访问它吗?

我相信构建图形反向传播部分的代码是正确的

compute_gradients()由minimize()调用,然后由用户代码调用


在已经构建的TensorFlow图中,ops的调度和执行发生在这张图中。

您想要什么样的访问?你想调试图表并看到中间输入/输出吗?例如,我需要知道所有的图表在哪里被执行?它们是由TysFooC+CUDA部分的C++代码执行的,如果你使用GPUI得到这个问题,因为我在哪里寻找自ToSoFalm以来的自动微分的实现。只有向前执行。我在搜索它的反向API。因此,我们应该关心的是构建一个图,而没有简单的python表达式来查看tensorflow中的自动微分。明白了。因此,我的最后一个问题是:状态引导会话:运行合并所有的图表到C++实现并进行计算。是的,已经建立的图被传递到DICTSession::创建,和DICTSession::运行将在这个图中运行所有的OPS。