Tensorflow摘要导致错误:tensorflow.python.framework.错误_无效参数

2024-03-29 05:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中添加了一些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 [?,?,?]

你知道我的总结有什么不对吗?在

^{pr2}$

我的代码:

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()

Tags: selfconfiginputdatasizeistfwith
1条回答
网友
1楼 · 发布于 2024-03-29 05:28:39

这个错误与摘要无关。你提供的代码中有很多东西缺失,所以说起来有点难。。i、 e.tf.app.run甚至不调用main函数。在

其次,没有对接受feed_dict参数的MonitoredSession.run的调用,也就是一个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

相关问题 更多 >