Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

python - FailedPreconditionError when running output layer in session?

In tensorflow, I am experiencing an issue where I try to get the output of my last neuron by running it in a session but it gives me an error. I am running Python 3.8.5 and TensorFlow 1.15 (because of some hardware issues)

Here is my code:

import warnings
warnings.filterwarnings("ignore")

import numpy as np
import tensorflow as tf

def main():
    x_inputs = tf.Variable(np.array([[1, 2 ,3], [1, 2, 3]]), dtype=tf.float32)
    y_outputs = tf.Variable(np.array([[1, 2, 4], [2, 3, 4]]), dtype=tf.float32)
    model = create_model(x_inputs, y_outputs)

def create_model(x: np.array, y: np.array):
    var_init = tf.global_variables_initializer()

    with tf.Session() as sess:
        sess.run(var_init)

        x_val = sess.run(x).flatten()
        y_val = sess.run(y).flatten()
        
        # neural network architecture
        input_layer = tf.layers.dense(x, units=len(x_val), activation=tf.nn.sigmoid)
        hidden_layer = tf.layers.dense(input_layer, units=5, activation=tf.nn.relu)
        output_layer = tf.layers.dense(hidden_layer, units=2, activation=tf.nn.softmax)

        print(sess.run(output_layer))

if __name__ == "__main__":
    main()

Here's the error:

Traceback (most recent call last):
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1350, in _do_call
    return fn(*args)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1327, in _run_fn
    return tf_session.TF_Run(session, options,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/errors_impl.py", line 470, in __exit__
    raise _make_specific_exception(
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense_2/bias
     [[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:@dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "learning.py", line 29, in <module>
    main()
  File "learning.py", line 10, in main
    model = create_model(x_inputs, y_outputs)
  File "learning.py", line 26, in create_model
    print(sess.run(output_layer))
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 894, in run
    result = self._run(None, fetches, feed_dict, options_ptr,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1127, in _run
    results = self._do_run(handle, final_targets, final_fetches,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1343, in _do_run
    return self._do_call(_run_fn, self._session, feeds, fetches, targets,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/client/session.py", line 1363, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value dense_2/bias
     [[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:@dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]

Caused by op 'dense_2/bias/read', defined at:
  File "learning.py", line 29, in <module>
    main()
  File "learning.py", line 10, in main
    model = create_model(x_inputs, y_outputs)
  File "learning.py", line 24, in create_model
    output_layer = tf.layers.dense(hidden_layer, units=2, activation=tf.nn.softmax)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/core.py", line 253, in dense
    return layer.apply(inputs)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 762, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 636, in __call__
    self.build(input_shapes)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/core.py", line 139, in build
    self.bias = self.add_variable('bias',
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/layers/base.py", line 498, in add_variable
    variable = vs.get_variable(name,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 1256, in get_variable
    return get_variable_scope().get_variable(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 1091, in get_variable
    return var_store.get_variable(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 429, in get_variable
    return _true_getter(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 399, in _true_getter
    return self._get_single_variable(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variable_scope.py", line 798, in _get_single_variable
    v = variables.Variable(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 220, in __init__
    self._init_from_args(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/variables.py", line 376, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/array_ops.py", line 127, in identity
    return gen_array_ops.identity(input, name=name)
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2133, in identity
    _, _, _op = _op_def_lib._apply_op_helper(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/op_def_library.py", line 785, in _apply_op_helper
    op = g.create_op(op_type_name, inputs, output_types, name=scope,
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 3152, in create_op
    ret = Operation(
  File "/Applications/anaconda3/lib/python3.8/site-packages/tensorflow/python/framework/ops.py", line 1625, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value dense_2/bias
     [[Node: dense_2/bias/read = Identity[T=DT_FLOAT, _class=["loc:@dense_2/bias"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](dense_2/bias)]]

Please help me I'm really struggling. Thanks in advance!

question from:https://stackoverflow.com/questions/65918754/failedpreconditionerror-when-running-output-layer-in-session

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...