【问题标题】:The node 'Merge/MergeSummary' has inputs from different frames: what does it mean?节点“Merge/MergeSummary”有来自不同帧的输入:这是什么意思?
【发布时间】:2017-02-13 17:21:39
【问题描述】:

试图合并我所有的摘要,我有一个错误,说Merge/MergeSummary 的输入来自不同的帧。那么,首先:什么是frame?您能否在 TF 文档中指出我有关此类内容的某个地方? ——当然,我用谷歌搜索了一下,但几乎一无所获。我该如何解决这个问题?下面的代码重现错误。提前致谢。

import numpy as np
import tensorflow as tf

tf.reset_default_graph()
tf.set_random_seed(23)

BATCH = 2
LENGTH = 4
SIZE = 5
ATT_SIZE = 3
NUM_QUERIES = 2

def linear(inputs, output_size, use_bias=True, activation_fn=None):
    """Linear projection."""

    input_shape = inputs.get_shape().as_list()
    input_size = input_shape[-1]
    output_shape = input_shape[:-1] + [output_size]
    if len(output_shape) > 2:
        output_shape_tensor = tf.unstack(tf.shape(inputs))
        output_shape_tensor[-1] = output_size
        output_shape_tensor = tf.stack(output_shape_tensor)
        inputs = tf.reshape(inputs, [-1, input_size])

    kernel = tf.get_variable("kernel", [input_size, output_size])
    output = tf.matmul(inputs, kernel)
    if use_bias:
        output = output + tf.get_variable('bias', [output_size])

    if len(output_shape) > 2:
        output = tf.reshape(output, output_shape_tensor)
        output.set_shape(output_shape)  # pylint: disable=I0011,E1101

    if activation_fn is not None:
        return activation_fn(output)
    return output


class Attention(object):
    """Attention mechanism implementation."""

    def __init__(self, attention_states, attention_size):
        """Initializes a new instance of the Attention class."""
        self._states = attention_states
        self._attention_size = attention_size
        self._batch = tf.shape(self._states)[0]
        self._length = tf.shape(self._states)[1]
        self._size = self._states.get_shape()[2].value
        self._features = None

    def _init_features(self):
        states = tf.reshape(
            self._states, [self._batch, self._length, 1, self._size])
        weights = tf.get_variable(
            "kernel", [1, 1, self._size, self._attention_size])
        self._features = tf.nn.conv2d(states, weights, [1, 1, 1, 1], "SAME")

    def get_weights(self, query, scope=None):
        """Reurns the attention weights for the given query."""
        with tf.variable_scope(scope or "Attention"):
            if self._features is None:
                self._init_features()
            else:
                tf.get_variable_scope().reuse_variables()
            vect = tf.get_variable("Vector", [self._attention_size])
            with tf.variable_scope("Query"):
                query_features = linear(query, self._attention_size, False)
                query_features = tf.reshape(
                    query_features, [-1, 1, 1, self._attention_size])

        activations = vect * tf.tanh(self._features + query_features)
        activations = tf.reduce_sum(activations, [2, 3])
        with tf.name_scope('summaries'):
            tf.summary.histogram('histogram', activations)
        return tf.nn.softmax(activations)

states = tf.placeholder(tf.float32, shape=[BATCH, None, SIZE])  # unknown length
queries = tf.placeholder(tf.float32, shape=[NUM_QUERIES, BATCH, ATT_SIZE])
attention = Attention(states, ATT_SIZE)
func = lambda x: attention.get_weights(x, "Softmax")
weights = tf.map_fn(func, queries)
for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
    name = var.name.replace(':', '_')
    tf.summary.histogram(name, var)
summary_op = tf.summary.merge_all()

states_np = np.random.rand(BATCH, LENGTH, SIZE)
queries_np = np.random.rand(NUM_QUERIES, BATCH, ATT_SIZE)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    weights_np, summary_str = sess.run([weights, summary_op], {states: states_np, queries: queries_np})
    print weights_np

【问题讨论】:

    标签: python tensorflow deep-learning summary


    【解决方案1】:

    错误消息确实对用户不友好。已更新为

    ValueError: Cannot use 'map/while/summaries/histogram' as input to 'Merge/MergeSummary' because 'map/while/summaries/histogram' is in a while loop. See info log for more details.
    

    正如新消息所说,问题在于您无法从 while 循环内部生成摘要。原始消息所指的frame 是while 循环的“执行帧”——while 循环每次迭代的所有状态都保存在frame 中。

    在这种情况下,while_looptf.map_fn 创建,其中的摘要为tf.summary.histogram('histogram', activations)

    有几种方法可以解决这个问题。您可以从get_weights 中提取摘要,也可以让get_weights 返回激活,使用来自tf.map_fn 调用的新返回的激活创建摘要。

    另一种方法,如果 NUM_QUERIES 是常量且很小,则可以静态展开循环,而不是使用 tf.map_fn。这是执行此操作的代码:

    # TOP PART OF THE CODE IS THE SAME
    
    states = tf.placeholder(tf.float32, shape=[BATCH, None, SIZE])  # unknown length
    queries = tf.placeholder(tf.float32, shape=[NUM_QUERIES, BATCH, ATT_SIZE])
    attention = Attention(states, ATT_SIZE)
    func = lambda x: attention.get_weights(x, "Softmax")
    
    # NEW CODE BEGIN
    split_queries = tf.split(queries, NUM_QUERIES)
    weights = []
    for query in split_queries:
        weights.append(func(query))
    # NEW CODE END
    
    for var in tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES):
        name = var.name.replace(':', '_')
        tf.summary.histogram(name, var)
    summary_op = tf.summary.merge_all()
    
    states_np = np.random.rand(BATCH, LENGTH, SIZE)
    queries_np = np.random.rand(NUM_QUERIES, BATCH, ATT_SIZE)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        # NEW CODE BEGIN
        results = sess.run(weights + [summary_op], {states: states_np, queries: queries_np})
        weights_np, summary_str = results[:-1], results[-1]
        # NEW CODE END
        print weights_np
    

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 2011-11-05
      • 2022-12-08
      • 2016-08-08
      • 2015-04-11
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多