重用类

时间:2018-03-17 10:58:03

标签: python oop tensorflow dry

我想在tensorflow中训练一个模型,只定义一次图形和变量。所以我在这个功能无意义的最小例子中将它封装在一个类中:

import tensorflow as tf
import numpy as np


class Model:
    weights = tf.get_variable("weights", (10, 1))
    bias = tf.get_variable("bias", 1)

    x = tf.placeholder(tf.float32, (100, 10), "x")
    y = tf.placeholder(tf.float32, 100, "y")

    output = tf.matmul(x, weights) + bias
    cost = tf.reduce_sum(tf.abs(output - y))
    optimizer = tf.train.MomentumOptimizer(0.5, 0.9).minimize(cost)

    def train(self, data, lbls):
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for i in range(10):
                _ = sess.run(self.optimizer, {self.x: data, self.y: lbls})

    def predict(self, data):
        with tf.Session() as sess:
            return sess.run(self.output, {self.x: data})


data = np.random.randint(0, 100, (100, 10))
lbls = np.random.randint(0, 1, (100, ))
mdl = Model()
mdl.train(data, lbls)
mdl.predict(data)

我希望预测函数重用已训练的变量self.weightsself.bias,但我得到的FailedPreconditionError (see above for traceback): Attempting to use uninitialized value bias函数的返回行中只有Model.predict()

这对我来说听起来很简单,但在某些地方我必须做出错误的假设。 official Tensorflow tutorial只是声明"明确地传递tf.Variable对象"作为共享变量的一种方式。将变量定义为类变量应该在我的理解中做到这一点。我已广泛搜索,但无法找到像这样的类的tensorflow的简单示例。 在我的实际项目中,我摆弄了变量范围(tf.variable_scope(.., reuse=True)),这仍然引发了一个未初始化的变量警告,我只能通过初始化来修复,但当然会重置训练的变量。

整个班级的方式是错误的吗?如何在没有保护程序或其他更复杂的结构的情况下简单地重用我的变量?并且:我对张量流变量,图表等的理解在哪里,错了?

1 个答案:

答案 0 :(得分:1)

一般情况下,您应该使用saver保存模型然后加载它,但解决方法是将tensorflow会话也保存为变量,并将其用于训练和预测。

import tensorflow as tf
import numpy as np

class Model:
    weights = tf.get_variable("weights", (10, 1))
    bias = tf.get_variable("bias", 1)

    x = tf.placeholder(tf.float32, (100, 10), "x")
    y = tf.placeholder(tf.float32, 100, "y")

    output = tf.matmul(x, weights) + bias
    cost = tf.reduce_sum(tf.abs(output - y))
    optimizer = tf.train.MomentumOptimizer(0.5, 0.9).minimize(cost)
    sess = tf.InteractiveSession()
    sess.run(tf.global_variables_initializer())

    def train(self, data, lbls):
        for i in range(10):
            _ = self.sess.run(self.optimizer, {self.x: data, self.y: lbls})

    def predict(self, data):
        return self.sess.run(self.output, {self.x: data})


data = np.random.randint(0, 100, (100, 10))
lbls = np.random.randint(0, 1, (100, ))
mdl = Model()
mdl.train(data, lbls)
mdl.predict(data)