Tensor flow throws an uninitialized error even after the variable is initialized











up vote
0
down vote

favorite












I am trying to write a program in python using tensorflow to fit data with different patterns using a neural network with a hidden layer. I am facing an error with the code which states that the variable b2 is uninitialized. But I have initialized it already and dont understand what I am missing here.



This is part of an assignment (the dataset can be understood here) and got stuck here while solving it.



The link to the colab notebook is here.



The initialization lines look like



W1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 2)), name="W1")
b1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 1)), name="b1")
W2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, hidden)), name="W2")
b2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, 1)), name="b2")


The code snippet that shows the computation graph is below.



operation = "ReLU" # "Sigmoid"
o = tf.add(tf.matmul(W1, p), b1)
# ReLU or Sigmoid
if operation == "ReLU":
z = tf.zeros((hidden, 1), dtype=tf.float64)
output = tf.maximum(o, z)
else:
output = tf.sigmoid(o)
foutput = tf.add(tf.matmul(W2, output), b2)
crossentropy = tf.log(tf.exp(foutput) / tf.reduce_sum(tf.exp(foutput), 0))
init = tf.initialize_all_variables()
with tf.Session() as sess:
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})
print(ce)


Error message



FailedPreconditionError                   Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408

FailedPreconditionError: Attempting to use uninitialized value b2
[[{{node b2/read}} = Identity[T=DT_DOUBLE, _device="/job:localhost/replica:0/task:0/device:CPU:0"](b2)]]









share|improve this question






















  • For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
    – Tobias Ernst
    Nov 19 at 23:40















up vote
0
down vote

favorite












I am trying to write a program in python using tensorflow to fit data with different patterns using a neural network with a hidden layer. I am facing an error with the code which states that the variable b2 is uninitialized. But I have initialized it already and dont understand what I am missing here.



This is part of an assignment (the dataset can be understood here) and got stuck here while solving it.



The link to the colab notebook is here.



The initialization lines look like



W1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 2)), name="W1")
b1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 1)), name="b1")
W2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, hidden)), name="W2")
b2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, 1)), name="b2")


The code snippet that shows the computation graph is below.



operation = "ReLU" # "Sigmoid"
o = tf.add(tf.matmul(W1, p), b1)
# ReLU or Sigmoid
if operation == "ReLU":
z = tf.zeros((hidden, 1), dtype=tf.float64)
output = tf.maximum(o, z)
else:
output = tf.sigmoid(o)
foutput = tf.add(tf.matmul(W2, output), b2)
crossentropy = tf.log(tf.exp(foutput) / tf.reduce_sum(tf.exp(foutput), 0))
init = tf.initialize_all_variables()
with tf.Session() as sess:
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})
print(ce)


Error message



FailedPreconditionError                   Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408

FailedPreconditionError: Attempting to use uninitialized value b2
[[{{node b2/read}} = Identity[T=DT_DOUBLE, _device="/job:localhost/replica:0/task:0/device:CPU:0"](b2)]]









share|improve this question






















  • For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
    – Tobias Ernst
    Nov 19 at 23:40













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to write a program in python using tensorflow to fit data with different patterns using a neural network with a hidden layer. I am facing an error with the code which states that the variable b2 is uninitialized. But I have initialized it already and dont understand what I am missing here.



This is part of an assignment (the dataset can be understood here) and got stuck here while solving it.



The link to the colab notebook is here.



The initialization lines look like



W1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 2)), name="W1")
b1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 1)), name="b1")
W2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, hidden)), name="W2")
b2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, 1)), name="b2")


The code snippet that shows the computation graph is below.



operation = "ReLU" # "Sigmoid"
o = tf.add(tf.matmul(W1, p), b1)
# ReLU or Sigmoid
if operation == "ReLU":
z = tf.zeros((hidden, 1), dtype=tf.float64)
output = tf.maximum(o, z)
else:
output = tf.sigmoid(o)
foutput = tf.add(tf.matmul(W2, output), b2)
crossentropy = tf.log(tf.exp(foutput) / tf.reduce_sum(tf.exp(foutput), 0))
init = tf.initialize_all_variables()
with tf.Session() as sess:
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})
print(ce)


Error message



FailedPreconditionError                   Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408

FailedPreconditionError: Attempting to use uninitialized value b2
[[{{node b2/read}} = Identity[T=DT_DOUBLE, _device="/job:localhost/replica:0/task:0/device:CPU:0"](b2)]]









share|improve this question













I am trying to write a program in python using tensorflow to fit data with different patterns using a neural network with a hidden layer. I am facing an error with the code which states that the variable b2 is uninitialized. But I have initialized it already and dont understand what I am missing here.



This is part of an assignment (the dataset can be understood here) and got stuck here while solving it.



The link to the colab notebook is here.



The initialization lines look like



W1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 2)), name="W1")
b1 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(hidden, 1)), name="b1")
W2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, hidden)), name="W2")
b2 = tf.Variable(np.random.uniform(low=-0.01, high=0.01, size=(classes, 1)), name="b2")


The code snippet that shows the computation graph is below.



operation = "ReLU" # "Sigmoid"
o = tf.add(tf.matmul(W1, p), b1)
# ReLU or Sigmoid
if operation == "ReLU":
z = tf.zeros((hidden, 1), dtype=tf.float64)
output = tf.maximum(o, z)
else:
output = tf.sigmoid(o)
foutput = tf.add(tf.matmul(W2, output), b2)
crossentropy = tf.log(tf.exp(foutput) / tf.reduce_sum(tf.exp(foutput), 0))
init = tf.initialize_all_variables()
with tf.Session() as sess:
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})
print(ce)


Error message



FailedPreconditionError                   Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
1333 try:
-> 1334 return fn(*args)
1335 except errors.OpError as e:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
1318 return self._call_tf_sessionrun(
-> 1319 options, feed_dict, fetch_list, target_list, run_metadata)
1320

/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
1406 self._session, options, feed_dict, fetch_list, target_list,
-> 1407 run_metadata)
1408

FailedPreconditionError: Attempting to use uninitialized value b2
[[{{node b2/read}} = Identity[T=DT_DOUBLE, _device="/job:localhost/replica:0/task:0/device:CPU:0"](b2)]]






python tensorflow






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 23:35









suraj

52




52












  • For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
    – Tobias Ernst
    Nov 19 at 23:40


















  • For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
    – Tobias Ernst
    Nov 19 at 23:40
















For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
– Tobias Ernst
Nov 19 at 23:40




For newer tensorflow versions you should use tf.global_variables_initializer(). Does that work?
– Tobias Ernst
Nov 19 at 23:40












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You also need to run the init function in the graph before computing cross entropy:



with tf.Session() as sess:
sess.run(init)
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})


Also, tf.initialize_all_variables() is deprecated. Use tf.global_variables_initializer() instead.






share|improve this answer





















  • Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
    – suraj
    Nov 20 at 0:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384184%2ftensor-flow-throws-an-uninitialized-error-even-after-the-variable-is-initialized%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










You also need to run the init function in the graph before computing cross entropy:



with tf.Session() as sess:
sess.run(init)
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})


Also, tf.initialize_all_variables() is deprecated. Use tf.global_variables_initializer() instead.






share|improve this answer





















  • Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
    – suraj
    Nov 20 at 0:08















up vote
0
down vote



accepted










You also need to run the init function in the graph before computing cross entropy:



with tf.Session() as sess:
sess.run(init)
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})


Also, tf.initialize_all_variables() is deprecated. Use tf.global_variables_initializer() instead.






share|improve this answer





















  • Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
    – suraj
    Nov 20 at 0:08













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You also need to run the init function in the graph before computing cross entropy:



with tf.Session() as sess:
sess.run(init)
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})


Also, tf.initialize_all_variables() is deprecated. Use tf.global_variables_initializer() instead.






share|improve this answer












You also need to run the init function in the graph before computing cross entropy:



with tf.Session() as sess:
sess.run(init)
ce = sess.run([crossentropy], feed_dict={p : inputs, t : targets, lr : 0.01})


Also, tf.initialize_all_variables() is deprecated. Use tf.global_variables_initializer() instead.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 23:40









Gerges Dib

2,7231719




2,7231719












  • Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
    – suraj
    Nov 20 at 0:08


















  • Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
    – suraj
    Nov 20 at 0:08
















Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
– suraj
Nov 20 at 0:08




Thank you. This solution works for me. Does it mean that I cant initialize variables outside a session ?
– suraj
Nov 20 at 0:08


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53384184%2ftensor-flow-throws-an-uninitialized-error-even-after-the-variable-is-initialized%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python