在类构造函数中初始化Tensorflow会话

时间:2018-06-20 07:25:36

标签: python oop tensorflow jupyter-notebook

在一个类的不同方法之间保持tensorflow会话存活的正确方法是什么?仅在创建对象时进行变量初始化? 如果我在fit方法内进行初始化,则以后无法使用该学习的模型进行预测方法 目前我正在这样做

class AlexNet_model:

    def __init__(self):
        self.sess = tf.Session()
        init = tf.global_variables_initializer()   
        self.sess.run(init)

然后使用同一个类,我调用fit方法

def fit(self, x, y, num_epochs = 20):
        X, Y = self.create_placeholders()

        Z = self.AlexNet(X)

        cost = self.cost_fn(Z, Y)
        opt = self.optimizer(cost)

        tot_cost = []

        for epoch in range(num_epochs):
            epoch_cost = 0

            mini_batches_X, mini_batches_Y = getRandomMiniBatches(train_x, train_y)
            for i in range(len(mini_batches_X)):
                mb_x = mini_batches_X[i]
                mb_y = np.array(mini_batches_Y[i])
                temp_Z, _, temp_cost = self.sess.run([Z, opt, cost], feed_dict = {X:mb_x, Y:mb_y.reshape(-1, 1)})

                epoch_cost += temp_cost / len(mini_batches_X)

            print('epoch : {0} \t cost : {1}'.format(epoch, epoch_cost))
            tot_cost.append(epoch_cost)

现在,当我创建此类的对象并调用fit方法

tf.reset_default_graph()
model = AlexNet_model()
model.fit(train_x, train_y)

我收到此错误,说未初始化的变量

FailedPreconditionError: Attempting to use uninitialized value beta1_power
     [[Node: beta1_power/read = Identity[T=DT_FLOAT, _class=["loc:@conv1/bias"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](beta1_power)]]

注意: 其余功能是通过类定义的,并且可以正常工作

2 个答案:

答案 0 :(得分:1)

在学习循环之前,必须在声明所有变量之后调用sess.run(init)。我建议在__init__中建立图形并在其后立即执行初始化程序。

类似这样的东西:

def __init__(self):
    self.X, self.Y, self.Z, self.cost, self.opt = self.build_graph()
    self.sess = tf.Session()

def build_graph(self):
    X, Y = self.create_placeholders()
    Z = self.AlexNet(X)
    cost = self.cost_fn(Z, Y)
    opt = self.optimizer(cost)
    return X, Y, Z, cost, opt

def fit(self, x, y, num_epochs = 20):
    init = tf.global_variables_initializer()
    self.sess.run(init)

    tot_cost = []
    for epoch in range(num_epochs):
        epoch_cost = 0

        mini_batches_X, mini_batches_Y = getRandomMiniBatches(train_x, train_y)
        for i in range(len(mini_batches_X)):
            mb_x = mini_batches_X[i]
            mb_y = np.array(mini_batches_Y[i])
            temp_Z, _, temp_cost = self.sess.run([self.Z, self.opt, self.cost], feed_dict = {self.X:mb_x, self.Y:mb_y.reshape(-1, 1)})

            epoch_cost += temp_cost / len(mini_batches_X)

        print('epoch : {0} \t cost : {1}'.format(epoch, epoch_cost))
        tot_cost.append(epoch_cost)

答案 1 :(得分:0)

您应该定义模型而不给模型定义内部图形。而是定义希望模型执行的操作,然后将所有内容包装在同一图形中。例如:

class AlexNet_model:


    def __init__(self):
       self.AlexNet=(imported AlexNet)
        self.optimizer=tf.train.SGD# use full name

    def fit(X):
        output=self.AlexNet(X)
        return output

    def loss(output,labels):
        return  tf.reduce_mean(tf.square(tf.sub(output,label)))# Or your cost function

    def train(loss):
        self.optimizer(lr).minimize(loss)

然后是培训的主要代码。

from tensorflow.contrib.framework import get_or_create_global_step
   with tf.Graph().as_default():
            global_step = get_or_create_global_step()
            model = AlexNet_model()
            X, Y = model.create_placeholders()
            # define operation so that you execute them in a chane of ops
            output =model.fit(X)
            loss = model.loss(output,Y)
            train_op = model.train(loss)
            #train_op when executed, it will calculate the loss, and the loss will call the output
            init_op = tf.global_variables_initializer()
            reset_op = tf.local_variables_initializer()
            #intialize global and local variables like Placeholders
            sess=tf.Session(graph=graph)
            sess.run([init_op, reset_op])# run init op

            for epoch in range(num_epochs):
                epoch_cost = 0

            mini_batches_X, mini_batches_Y = getRandomMiniBatches(train_x,train_y)
            for i in range(len(mini_batches_X)):
                 mb_x = mini_batches_X[i]
                 mb_y = np.array(mini_batches_Y[i])
                 temp_Z, _, temp_cost = sess.run([train_op], feed_dict = {X:mb_x, Y:mb_y.reshape(-1, 1)})

            epoch_cost += temp_cost / len(mini_batches_X)

` 在先前的代码中,主图中未将AlexNet()定义为AlexNet_Model类所定义的,因此某些操作未初始化。

我建议您MobileNet作为最佳模型设计,以供参考。