尝试使用未初始化的值-即使我已初始化

时间:2018-09-12 12:55:41

标签: tensorflow initialization

即使在全局初始化之后,也会发生初始化错误

关于初始化的错误是这样的:

  

FailedPreconditionError:尝试使用未初始化的值偏差        [[节点:偏好/读取= IdentityT = DT_FLOAT,_class = [“ loc:@ Adagrad / update_biases / ApplyAdagrad”],_ device =“ / job:localhost /副本:0 /任务:0 /设备:CPU:0”] ]

import functools

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, data, target):
        self.data = data
        self.target = target
        self._logits = None
        self._prediction = None
        self._optimize = None
        self._error = None

    @lazy_property
    def logits(self):
        w = tf.Variable(tf.truncated_normal([784, 1]), name='weights')
        b = tf.Variable(tf.zeros([1]), name='biases')
        self._logits = tf.matmul(self.data, w) + b
        return self._logits

    @lazy_property
    def prediction(self):
        self._prediction = tf.nn.softmax(self.logits)
        return self._prediction

    @lazy_property
    def optimize(self):
        labels = tf.to_int64(self.target)
        logits = self.prediction
        cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=labels, name='xentropy')
        loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
        self._optimize = tf.train.AdagradOptimizer(0.05).minimize(loss)
        return self._optimize

    @lazy_property
    def error(self):
        mistakes = tf.not_equal(tf.argmax(self.target, 1), tf.argmax(self.prediction, 1))
        return tf.reduce_mean(tf.cast(mistakes, tf.float32))

batch_size = 100
num_steps = 1000

tf.reset_default_graph()

data = MNIST(data_dir="data/MNIST/")
X = tf.placeholder(tf.float32, [batch_size, 784], name='Placeholder_Input')
Y = tf.placeholder(tf.int64, [batch_size], name='Placeholder_Output')
model = Model(X, Y)


with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    for step in range(num_steps):
        model = Model(X,Y)
        for _ in range(100):
            x_batch, y_true_batch, _ = data.random_batch(batch_size=batch_size)
            y_true_batch = np.argmax(y_true_batch, axis=1)
            error,_ = session.run(model.optimize, feed_dict={X: x_batch, Y: y_true_batch})
        if (step % 100 == 0):
            print("Error rate @ iter %d : %f" % (step, error))

1 个答案:

答案 0 :(得分:1)

模型完全定义后,您应该运行session.run(tf.global_variables_initializer())。请注意,您正在每个步骤定义一个新模型,并且仅在调用model.optimize时实例化变量。这是我的建议:

model = Model(X,Y)
optimize = model.optimize
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    for step in range(num_steps):
        for _ in range(100):
            x_batch, y_true_batch, _ = data.random_batch(batch_size=batch_size)
            y_true_batch = np.argmax(y_true_batch, axis=1)
            error,_ = session.run(optimize, feed_dict={X: x_batch, Y: y_true_batch})
        if (step % 100 == 0):
            print("Error rate @ iter %d : %f" % (step, error))