【问题标题】:Cannot feed value of shape (200, 40) for Tensor u'rnn_cell_40/Tanh:0', which has shape '(200, 4)'无法为形状为 '(200, 4)' 的张量 u'rnn_cell_40/Tanh:0' 提供形状 (200, 40) 的值
【发布时间】:2017-08-27 14:06:03
【问题描述】:

这是我的代码,当我运行它时,我无法理解错误,为什么u'rnn_cell_40/Tanh:0 需要提要值?

import numpy as np
import tensorflow as tf
import reader
raw_data = reader.ptb_raw_data('simple-examples/data')
train_data, val_data, test_data, num_classes = raw_data
batch_size = 200
num_steps = 40

def build_graph(num_steps,
            bptt_steps = 20, batch_size = 200, num_classes = num_classes,
            state_size = 4, embed_size = 50, learning_rate = 0.01):
    g = tf.get_default_graph()

    x = tf.placeholder(tf.int32, [batch_size, None], name='input_placeholder')
    y = tf.placeholder(tf.int32, [batch_size, None], name='labels_placeholder')
    default_init_state = tf.zeros([batch_size, state_size])
    init_state = tf.placeholder_with_default(default_init_state,
                                             [batch_size, state_size], name='state_placeholder')
    dropout = tf.placeholder(tf.float32, [], name='dropout_placeholder')

    x_one_hot = tf.one_hot(x, num_classes)
    x_as_list = [tf.squeeze(i, squeeze_dims=[1]) for i in tf.split(x_one_hot, num_steps, 1)]

    with tf.variable_scope('embeddings'):
        embeddings = tf.get_variable('embedding_matrix', [num_classes, embed_size])

    def embedding_lookup(one_hot_input):
        with tf.variable_scope('embeddings', reuse=True):
            embeddings = tf.get_variable('embedding_matrix', [num_classes, embed_size])
            embeddings = tf.identity(embeddings)
            g.add_to_collection('embeddings', embeddings)
            return tf.matmul(one_hot_input, embeddings)

    rnn_inputs = [embedding_lookup(i) for i in x_as_list]

    rnn_inputs = [tf.nn.dropout(x, dropout) for x in rnn_inputs]

    # rnn_cells
    with tf.variable_scope('rnn_cell'):
        W = tf.get_variable('W', [embed_size + state_size, state_size])
        b = tf.get_variable('b', [state_size], initializer=tf.constant_initializer(0.0))

    def rnn_cell(rnn_input, state):
        with tf.variable_scope('rnn_cell', reuse=True):

            W = tf.get_variable('W', [embed_size + state_size, state_size])
            W = tf.identity(W)
            g.add_to_collection('Ws', W)

            b = tf.get_variable('b', [state_size], initializer=tf.constant_initializer(0.0))
            b = tf.identity(b)
            g.add_to_collection('bs', b)

            return tf.tanh(tf.matmul(tf.concat([rnn_input, state], 1), W) + b)

    state = init_state
    rnn_outputs = []
    for rnn_input in rnn_inputs:
        state = rnn_cell(rnn_input, state)
        print state.name
        rnn_outputs.append(state)

    print init_state.name
    #apply dropout to outputs
    rnn_outputs = [tf.nn.dropout(x, dropout) for x in rnn_outputs]

    final_state = rnn_outputs[-1]
    print final_state.name

    #logits and predictions
    with tf.variable_scope('softmax'):
        W = tf.get_variable('W_softmax', [state_size, num_classes])
        b = tf.get_variable('b_softmax', [num_classes], initializer=tf.constant_initializer(0.0))
    logits = [tf.matmul(rnn_output, W) + b for rnn_output in rnn_outputs]
    predictions = [tf.nn.softmax(logit) for logit in logits]

    #losses
    y_as_list = [tf.squeeze(i, squeeze_dims=[1]) for i in tf.split(y, num_steps, 1)]
    losses = [tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logit,labels=label) \
              for logit, label in zip(logits, y_as_list)]
    total_loss = tf.reduce_mean(losses)

    """
    Implementation of true truncated backprop using TF's high-level gradients function.

    Because I add gradient-ops for each error, this are a number of duplicate operations,
    making this a slow implementation. It would be considerably more effort to write an
    efficient implementation, however, so for testing purposes, it's OK that this goes slow.

    An efficient implementation would still require all of the same operations as the full
    backpropagation through time of errors in a sequence, and so any advantage would not come
    from speed, but from having a better distribution of backpropagated errors.
    """

    embed_by_step = g.get_collection('embeddings')
    Ws_by_step = g.get_collection('Ws')
    bs_by_step = g.get_collection('bs')

    # Collect gradients for each step in a list
    embed_grads = []
    W_grads = []
    b_grads = []

    # Keeping track of vanishing gradients for my own curiousity
    vanishing_grad_list = []

    # Loop through the errors, and backpropagate them to the relevant nodes
    for i in range(num_steps):
        start = max(0,i+1-bptt_steps)
        stop = i+1
        grad_list = tf.gradients(losses[i],
                                 embed_by_step[start:stop] +\
                                 Ws_by_step[start:stop] +\
                                 bs_by_step[start:stop])
        embed_grads += grad_list[0 : stop - start]
        W_grads += grad_list[stop - start : 2 * (stop - start)]
        b_grads += grad_list[2 * (stop - start) : ]

        if i >= bptt_steps:
            vanishing_grad_list.append(grad_list[stop - start : 2 * (stop - start)])

    grad_embed = tf.add_n(embed_grads) / (batch_size * bptt_steps)
    grad_W = tf.add_n(W_grads) / (batch_size * bptt_steps)
    grad_b = tf.add_n(b_grads) / (batch_size * bptt_steps)

    opt = tf.train.AdamOptimizer(learning_rate)
    grads_and_vars_tf_style = opt.compute_gradients(total_loss, tf.trainable_variables())
    grads_and_vars_true_bptt = \
        [(grad_embed, tf.trainable_variables()[0]),
         (grad_W, tf.trainable_variables()[1]),
         (grad_b, tf.trainable_variables()[2])] + \
        opt.compute_gradients(total_loss, tf.trainable_variables()[3:])
    train_tf_style = opt.apply_gradients(grads_and_vars_tf_style)
    train_true_bptt = opt.apply_gradients(grads_and_vars_true_bptt)

    return dict(
        train_tf_style = train_tf_style,
        train_true_bptt = train_true_bptt,
        gvs_tf_style = grads_and_vars_tf_style,
        gvs_true_bptt = grads_and_vars_true_bptt,
        gvs_gradient_check = opt.compute_gradients(losses[-1], tf.trainable_variables()),
        loss = total_loss,
        final_state = final_state,
        x=x,
        y=y,
        init_state=init_state,
        dropout=dropout,
        vanishing_grads=vanishing_grad_list
    )



def reset_graph():
    if 'sess' in globals() and sess:
        sess.close()
    tf.reset_default_graph()


reset_graph()
g = build_graph(num_steps = 40, bptt_steps = 20)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())


X, Y = next(reader.ptb_iterator(train_data, batch_size=200, num_steps=40))

gvs_bptt = sess.run(g['gvs_true_bptt'], feed_dict={g['x']:X, g['y']:Y, g['dropout']: 1})

这是结果:

ValueError                                Traceback (most recent call last)
<ipython-input-1-a5d0b8513bfb> in <module>()
    205 #     gvs_bptt = sess.run(g['gvs_true_bptt'], feed_dict={g['x']:X, g['y']:Y, g['dropout']: 1})
    206 
--> 207 gvs_bptt = sess.run(g['gvs_true_bptt'], feed_dict={g['x']:X, g['y']:Y, g['dropout']: 1})

/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    776     try:
    777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
    779       if run_metadata:
    780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/root/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    959                 'Cannot feed value of shape %r for Tensor %r, '
    960                 'which has shape %r'
--> 961                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    962           if not self.graph.is_feedable(subfeed_t):
    963             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (200, 40) for Tensor u'rnn_cell_40/Tanh:0', which has shape '(200, 4)'

【问题讨论】:

    标签: python machine-learning tensorflow deep-learning


    【解决方案1】:

    你有一个形状为 [200, 4] 的 init_state 用于 RNN 单元:

    default_init_state = tf.zeros([batch_size, state_size])
    

    但是您以 [200, 40] 的批次读取数据,因此:

    Cannot feed value of shape (200, 40) for Tensor u'rnn_cell_40/Tanh:0', which has shape '(200, 4)'.
    

    你在哪里将你的 data-dim 精确地分成 10?我无法运行您的代码。但即使有虚拟数据,

    X = (np.random.randn(batch_size, 4) * 10).astype(np.int32)
    Y = (np.random.randn(batch_size, 4) * 10).astype(np.int32)
    
    gvs_bptt = sess.run(g['gvs_true_bptt'], feed_dict={
                        g['x']: X, g['y']: Y, g['dropout']: 1})
    

    它给了我:

    InvalidArgumentError (see above for traceback): Shape [200,?] is not fully defined
         [[Node: input_placeholder = Placeholder[dtype=DT_INT32, shape=[200,?], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
    

    我的建议是通过添加来调试您的图表

    print "tensorname", tensorname.get_shape()
    

    看看你的数据是否真的有正确的形状。

    【讨论】:

    • 数据为: X = (np.random.randn(batch_size, 40) * 10).astype(np.int32) Y = (np.random.randn(batch_size, 40) * 10) .astype(np.int32)
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2018-04-29
    • 2022-08-12
    • 2018-04-24
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多