代码之家  ›  专栏  ›  技术社区  ›  paulc1111

无法将feed\u dict键解释为批处理和测试的张量

  •  0
  • paulc1111  · 技术社区  · 6 年前

    我是Tensorflow的新手,正在尝试用Python3.6做MNIST示例。

    feed_dict sess.run .

    下面是我的代码,

    import tensorflow as tf
    import numpy as np
    import functools
    import sys
    sys.path.append('./utils')
    
    from mnist import MNIST
    
    def lazy_property(function):
        attribute = '_cache_' + function.__name__
    
    @property
    @functools.wraps(function)
    def decorator(self):
        if not hasattr(self, attribute):
            setattr(self, attribute, function(self))
            return getattr(self, attribute)
    
        return decorator
    
    class Model:
    
      def __init__(self, image, label):
        self.image = image
        self.label = label
        self.logits
        self.prediction
        self.optimize
        self.error
    
    
      @lazy_property
      def logits(self):
        weight = tf.Variable(tf.zeros([img_size_flat, num_classes]))
        #print (img_size_flat)
        biases = tf.Variable(tf.zeros([num_classes]))
        #print (num_classes)
        equation = tf.matmul(self.image, weight) + biases
        return equation
    
      @lazy_property
      def prediction(self):
        return tf.nn.softmax(self.logits)
    
      @lazy_property
      def optimize(self):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits, labels=self.label)
        cost = tf.reduce_mean(cross_entropy)
        return tf.train.GradientDescentOptimizer(learning_rate=0.5).minimize(cost)
    
    
      @lazy_property
      def error(self):
        y_pred_cls = tf.argmax(self.prediction, axis=1)
        mistakes = tf.not_equal(y_true_cls, y_pred_cls)
        #print(mistakes)
        error_from_acc = tf.reduce_mean(tf.cast(mistakes, tf.float32))
        return error_from_acc
    
    
    batch_size = 100
    num_steps = 1000
    
    tf.reset_default_graph()
    
    data = MNIST(data_dir="data/MNIST/")
    img_size_flat = data.img_size_flat
    img_shape = data.img_shape
    num_classes = data.num_classes
    image = tf.placeholder(tf.float32, [None, img_size_flat])
    label = tf.placeholder(tf.float32, [None, num_classes])
    y_true_cls = tf.placeholder(tf.int64, [None])
    #print (y_true_cls)
    model = Model(image, label)
    
    
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
    
        for step in range(num_steps):
            error = session.run(model.error, {x: data.x_test, y_true: data.y_test}) # Gives me an error message from HERE!!!!
            x_batch, y_true_batch, _ = data.random_batch(batch_size=batch_size)
            session.run(model.optimize, {x: x_batch, y_true: y_true_batch})
            if (step % 100 == 0):
                print("Error rate @ iter %d : %f" % (step, error))
    

    我做错了什么?

    {x: data.x_test, y_true: data.y_test} {x: x_batch, y_true: y_true_batch}

    还有,代码里有我做错的地方吗?

    请帮帮我。。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Tim    6 年前

    您已将占位符定义为:

    image = tf.placeholder(tf.float32, [None, img_size_flat])
    label = tf.placeholder(tf.float32, [None, num_classes])
    

    ,但那你就过去了 x y_true 作为占位符进入 session.run :

    session.run(model.error, {x: data.x_test, y_true: data.y_test})
    

    因此需要更换 你说得对 通过 image label 你应该没事的:

    session.run(model.error, {image : data.x_test, label : data.y_test})