Reputation: 2296
I was doing some regression and then I tried to add L2 regularization into it. But it showing me following error:
ValueError: Tensor("Placeholder:0", dtype=float32) must be from the same graph as Tensor("w_hidden:0", shape=(10, 36), dtype=float32_ref).
The code looks like as follows:
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")
The error shows that I'm using two graphs but I don't know where.
Upvotes: 2
Views: 4602
Reputation: 14485
The error message explains that your placeholder for x
is not in the same graph as the w_hidden
tensor - this means that we cannot complete an operation using these two tensors (presumably this is thrown when running tf.matmul(weights['hidden'], x)
)
The reason this has come about is that you have used tf.reset_default_graph()
after you created the reference to weights
but before you created the placeholder x
.
In order to fix this, you could move the tf.reset_default_graph()
call before all your operations (or remove it altogether)
Upvotes: 1