Tensorflow摘要导致错误:tensorflow.python.framework.errors_impl.InvalidArgumentError

时间:2018-02-09 13:42:53

标签: python tensorflow machine-learning

在我的代码中添加一些tf.summary之后,我再次运行代码时出现以下错误:

tensorflow.python.framework.errors_impl.__InvalidArgumentError__: 
You must feed a value for placeholder tensor 'Valid/Model/input/input_data_pl' with dtype int32 and shape [?,?,?]

关于我的摘要有什么问题的任何想法?

/Users/wuguohua/Envs/py3tf/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
Tensor("Train/Model/input/input_data_pl:0", shape=(?, ?, ?), dtype=int32)
Tensor("Train/Model/input/output_data_pl:0", shape=(?, ?), dtype=int32)
  [TL] EmbeddingInputlayer Model/input/token_emb: (1178, 100)
/Users/wuguohua/Envs/py3tf/lib/python3.6/site-packages/tensorflow/python/ops/gradients_impl.py:96: UserWarning: Converting sparse IndexedSlices to a dense Tensor of unknown shape. This may consume a large amount of memory.
  "Converting sparse IndexedSlices to a dense Tensor of unknown shape. "
Tensor("Valid/Model/input/input_data_pl:0", shape=(?, ?, ?), dtype=int32)
Tensor("Valid/Model/input/output_data_pl:0", shape=(?, ?), dtype=int32)
  [TL] EmbeddingInputlayer Model/input/token_emb: (1178, 100)
2018-02-09 21:31:08.723167: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
Traceback (most recent call last):
  File "/Users/wuguohua/Envs/py3tf/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1323, in _do_call
    return fn(*args)
  File "/Users/wuguohua/Envs/py3tf/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1302, in _run_fn
    status, run_metadata)
  File "/Users/wuguohua/Envs/py3tf/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Valid/Model/input/input_data_pl' with dtype int32 and shape [?,?,?]
         [[Node: Valid/Model/input/input_data_pl = Placeholder[dtype=DT_INT32, shape=[?,?,?], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]

During handling of the above exception, another exception occurred:

我的代码:

class SegmentModel(object):

    def __init__(self, is_training, config):
        self.is_training = is_training
        self.config = config
        embed_size, pre_win, sur_win, rnn_hidden = \
                 config.embed_size, config.pre_win, \
                 config.sur_win, config.rnn_hidden_size
        vocab_size, tag_size = config.vocab_size, config.tag_size

        with tf.variable_scope("input"):
            self.input_data = tf.placeholder(tf.int32, shape=[None, None, None],
                                             name="input_data_pl")
            self.output_data = tf.placeholder(tf.int32, shape=[None, None],
                                              name="output_data_pl")
            print(self.input_data)
            print(self.output_data)

            seq_length = tf.count_nonzero(tf.reduce_max(self.input_data, axis=2),
                                          axis=1)
            # check if the seq length right
            self.seq_length = seq_length

            token_emb_net = tl.layers.EmbeddingInputlayer(
                self.input_data, vocab_size, embed_size, name="token_emb")

            tok_emb_shape = tf.shape(token_emb_net.outputs)
            win_size = pre_win + sur_win + 1
            win_emb_size = win_size * embed_size
            batch_size = tok_emb_shape[0]

            token_emb_net.outputs = tf.reshape(token_emb_net.outputs,
                                               (tok_emb_shape[0],
                                                tok_emb_shape[1],
                                                win_emb_size))

        with tf.variable_scope("sentmodel"):
            input_keep_prob = 1.0 - config.dropout if is_training else 1.0
            rnn_cell = self._get_rnn_cell(is_training, rnn_hidden,
                                          config.rnn_hidden_layer,
                                          input_keep_prob,
                                          1.0)

            rnn_outputs, _ = tf.nn.dynamic_rnn(
                cell=rnn_cell,
                inputs=token_emb_net.outputs,
                sequence_length=seq_length,
                dtype=tf.float32,
            )

            rnn_outputs = tf.reshape(rnn_outputs, [-1, rnn_hidden])

        with tf.variable_scope("softmax"):
            softmax_w = tf.get_variable("w", shape=[rnn_hidden, tag_size])
            softmax_b = tf.get_variable("b", shape=[tag_size])
            logits = tf.nn.xw_plus_b(rnn_outputs, softmax_w, softmax_b)
            logits = tf.reshape(logits, [tok_emb_shape[0],
                                         tok_emb_shape[1],
                                         tag_size])
            self.logits = logits


        with tf.variable_scope("crf"):
            log_likelihood, trans_params = tf.contrib.crf.crf_log_likelihood(
                logits,
                tag_indices=self.output_data,
                sequence_lengths=seq_length)
            cost = tf.reduce_mean(-log_likelihood)
            self.cost = cost
            self.trans_params = trans_params

        if not is_training:
            pass
            # self.decode_sequence = SegmentModel._predict_op(
            #     logits, trans_params, seq_length)
        else:
            with tf.variable_scope("optim"):
                optimizer = tf.train.GradientDescentOptimizer(0.2)
                tvars = tf.trainable_variables()
                grad = tf.gradients(self.cost, tvars)
                self.train_op = optimizer.apply_gradients(
                    zip(grad, tvars),
                    global_step=tf.train.get_or_create_global_step()
                )
            self.global_step = tf.train.get_or_create_global_step()


def main(_):

    train_reader, dev_reader = prepare()
    config = ModelConfig()
    config.vocab_size = train_reader.word_vocab.size()
    config.tag_size = train_reader.tag_vocab.size()

    tf_config = tf.ConfigProto()
    tf_config.gpu_options.allow_growth = True

    with tf.Graph().as_default():
        with tf.variable_scope("Model", reuse=None):
            train_model = SegmentModel(is_training=True, config=config)
            tf.summary.scalar("Training Loss", train_model.cost)

        with tf.variable_scope("Model", reuse=True):
            tl.layers.set_name_reuse(True)
            dev_model = SegmentModel(is_training=False, config=config)
            tf.summary.scalar("Validation Loss", dev_model.cost)

        # MonitoredTrainingSession will auto save check point
        # and save the checkpoint after all epoch finish
        with tf.train.MonitoredTrainingSession(
                checkpoint_dir=FLAGS.train_dir,
                config=tf_config) as mon_sess:

            for epoch in range(config.max_max_epoch):
                train_model.train(mon_sess, train_reader, config.batch_size)
                dev_model.eval(mon_sess, dev_reader, config.batch_size)

if __name__ == "__main__":
    tf.app.run()

1 个答案:

答案 0 :(得分:0)

错误与摘要无关。您提供的代码中遗漏了很多东西,所以它有点难以辨别,但是tf.app.run甚至没有调用main函数。 / p>

其次,没有调用MonitoredSession.run接受feed_dict参数,这是一个dict,它提供Valid/Model/input/input_data_pl张量(因此错误)。

最后,您应该在定义张量的位置定义摘要 - 在相同的图形和变量空间中,如:

with tf.variable_scope("crf"):
        log_likelihood, trans_params = tf.contrib.crf.crf_log_likelihood(
            logits,
            tag_indices=self.output_data,
            sequence_lengths=seq_length)
        cost = tf.reduce_mean(-log_likelihood)           
        ===
        tf.summary.scalar(name="Training Loss", tensor=cost)
        ===
        self.cost = cost 
        self.trans_params = trans_params