尝试使用未初始化的值 - 即使我进行了初始化

问题描述 投票:0回答:1

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

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

FailedPreconditionError:尝试使用未初始化的值偏差[[Node:biases / read = IdentityT = DT_FLOAT,_class = [“loc:@Adagrad / update_biases / ApplyAdagrad”],_ device =“/ job:localhost / replica:0 / task: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))
tensorflow initialization
1个回答
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))
© www.soinside.com 2019 - 2024. All rights reserved.