Tensorflow值错误:变量已存在,不允许

时间:2017-09-05 13:28:05

标签: variables tensorflow valueerror

我使用tensorflow预测具有不同时间段的财务时间序列。为了划分输入数据,我制作了子样本并用于循环。 但是,我得到了像这样的ValueError;

ValueError:变量rnn / basic_lstm_cell / weights已经存在,不允许。你的意思是在VarScope中设置reuse = True吗?最初定义于:

没有子样本,这段代码效果很好。 以下是我的代码。

    import tensorflow as tf
    import numpy as np
    import matplotlib
    import os
    import matplotlib.pyplot as plt

    class lstm:
        def __init__(self, x, y):
            # train Parameters
            self.seq_length = 50
            self.data_dim = x.shape[1]
            self.hidden_dim = self.data_dim*2
            self.output_dim = 1
            self.learning_rate = 0.0001
            self.iterations = 5 # originally 500

        def model(self,x,y):
            # build a dataset
            dataX = []
            dataY = []
            for i in range(0, len(y) - self.seq_length):
                _x = x[i:i + self.seq_length]
                _y = y[i + self.seq_length]
                dataX.append(_x)
                dataY.append(_y)

            train_size = int(len(dataY) * 0.7977)
            test_size = len(dataY) - train_size
            trainX, testX = np.array(dataX[0:train_size]), np.array(dataX[train_size:len(dataX)])
            trainY, testY = np.array(dataY[0:train_size]), np.array(dataY[train_size:len(dataY)])
            print(train_size,test_size)

            # input place holders
            X = tf.placeholder(tf.float32, [None, self.seq_length,         self.data_dim])
            Y = tf.placeholder(tf.float32, [None, 1])

            # build a LSTM network
            cell = tf.contrib.rnn.BasicLSTMCell(num_units=self.hidden_dim,state_is_tuple=True,  activation=tf.tanh) 
            outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
            self.Y_pred = tf.contrib.layers.fully_connected(outputs[:, -1], self.output_dim, activation_fn=None) 
            # We use the last cell's output

            # cost/loss
            loss = tf.reduce_sum(tf.square(self.Y_pred - Y))  # sum of the squares
            # optimizer
            optimizer = tf.train.AdamOptimizer(self.learning_rate)
            train = optimizer.minimize(loss)

            # RMSE
            targets = tf.placeholder(tf.float32, [None, 1])
            predictions = tf.placeholder(tf.float32, [None, 1])
            rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

            # training
            with tf.Session() as sess:
                init = tf.global_variables_initializer()
                sess.run(init)

                # Training step
                for i in range(self.iterations):
                    _, step_loss = sess.run([train, loss], feed_dict={X: trainX, Y: trainY})

                # prediction
                train_predict = sess.run(self.Y_pred, feed_dict={X: trainX})
                test_predict = sess.run(self.Y_pred, feed_dict={X: testX})

            return train_predict, test_predict 

    # variables definition
    tsx = []
    tsy = []
    tsr = []
    trp = []
    tep = []

    x = np.loadtxt('data.csv', delimiter=',') # data for analysis
    y = x[:,[-1]]
    z = np.loadtxt('rb.csv', delimiter=',')   # data for time series
    z1 = z[:,0] # start cell
    z2 = z[:,1] # end cell

    for i in range(1): # need to change to len(z)
        globals()['x_%s' % i] = x[int(z1[i]):int(z2[i]),:] # definition of x
        tsx.append(globals()["x_%s" % i])

        globals()['y_%s' % i] = y[int(z1[i])+1:int(z2[i])+1,:] # definition of y
        tsy.append(globals()["y_%s" % i])

        globals()['a_%s' % i] = lstm(tsx[i],tsy[i]) # definition of class  

        globals()['trp_%s' % i],globals()['tep_%s' % i] = globals()["a_%s" % i].model(tsx[i],tsy[i])
        trp.append(globals()["trp_%s" % i])
        tep.append(globals()["tep_%s" % i])

2 个答案:

答案 0 :(得分:6)

每次调用model方法时,您都在构建LSTM的计算图。第二次调用model方法时,tensorflow发现您已经创建了具有相同名称的变量。如果创建变量的范围的reuse标志设置为False,则会引发ValueError

要解决此问题,您必须通过在循环结束时调用True将重用标记设置为tf.get_variable_scope().reuse_variables()

请注意,您不能在循环开始时添加它,因为那时您正在尝试重用尚未创建的变量。

您可以在tensorflow文档here

中找到更多信息

答案 1 :(得分:2)

您可以在"模型中定义一些变量"功能。 当你想打电话给#34; model"功能多次:

with tf.variable_scope("model_fn") as scope:
  train_predict, test_predict = model(input1)
with tf.variable_scope(scope, reuse=True):
  train_predict, test_predict = model(input2)