张量必须与张量来自同一张图

时间:2018-08-04 12:44:14

标签: python tensorflow

我正在做一些回归,然后尝试向其中添加L2正则化。但这显示了以下错误:

  

ValueError:Tensor(“ Placeholder:0”,dtype = float32)必须来自   与Tensor(“ w_hidden:0”,shape =(10,36),dtype = float32_ref)相同的图形。

代码如下:

def tensorGraph5Fold(initState = 'NSW'):
    weights_obj, biases_obj = loadKernelBias5Fold(initState)

    weights = [tf.convert_to_tensor(w, dtype=tf.float32) for w in weights_obj]
    biases = [tf.convert_to_tensor(b, dtype=tf.float32) for b in biases_obj]

    #RNN designning
    tf.reset_default_graph()

    inputs = x_size #input vector size
    output = y_size #output vector size
    learning_rate = 0.01

    x = tf.placeholder(tf.float32, [inputs, None])
    y = tf.placeholder(tf.float32, [output, None])

    #L2 regulizer
    regularizer = tf.contrib.layers.l2_regularizer(scale=0.2)
    weights = {
        'hidden': tf.get_variable("w_hidden", initializer = weights[0], regularizer=regularizer),
        'output': tf.get_variable("w_output", initializer = weights[1], regularizer=regularizer)
    }

    biases = {
        'hidden': tf.get_variable("b_hidden", initializer = biases[0]),
        'output': tf.get_variable("b_output", initializer = biases[1])
    }

    hidden_layer = tf.add(tf.matmul(weights['hidden'], x), biases['hidden'])
    hidden_layer = tf.nn.relu(hidden_layer)

    output_layer = tf.matmul(weights['output'], hidden_layer) + biases['output']

    loss = tf.reduce_mean(tf.square(output_layer - y))    #define the cost function which evaluates the quality of our model
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)          #gradient descent method
    training_op = optimizer.minimize(loss)          #train the result of the application of the cost_function                                 

    #L2 regulizer
    reg_variables = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    reg_term = tf.contrib.layers.apply_regularization(regularizer, reg_variables)
    loss += reg_term

    init = tf.global_variables_initializer()           #initialize all the variables
    epochs = 2000     #number of iterations or training cycles, includes both the FeedFoward and Backpropogation

    pred = {'NSW': [], 'QLD': [], 'SA': [], 'TAS': [], 'VIC': []}
    y_pred = {1: pred, 2: pred, 3: pred, 4: pred, 5: pred}

    print("Training the ANN...")
    for st in state.values():
        for fold in np.arange(1,6):
            print("State: ", st, end='\n')
            print("Fold : ", fold)

            with tf.Session() as sess:
                init.run()
                for ep in range(epochs):
                    sess.run(training_op, feed_dict={x: x_batches_train_fold[fold][st], y: y_batches_train_fold[fold][st]})

            print("\n")

该错误表明我正在使用两个图形,但不知道在哪里。

1 个答案:

答案 0 :(得分:0)

错误消息说明x的占位符与w_hidden张量不在同一个图形中-这意味着我们无法使用这两个张量完成操作(大概是在运行时抛出的) tf.matmul(weights['hidden'], x)

出现这种情况的原因是,您在创建tf.reset_default_graph()的引用之后使用了weights ,但是在创建占位符之前之前 x

为解决此问题,您可以将tf.reset_default_graph()调用移至所有操作之前(或将其完全删除)