0

在 tensorflow 中,我遇到了一个问题,我尝试通过在会话中运行它来获取最后一个神经元的输出,但它给了我一个错误。我正在运行 Python 3.8.5 和 TensorFlow 1.15(由于一些硬件问题)

这是我的代码:

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()

这是错误:

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)]]

请帮助我,我真的很挣扎。提前致谢!

4

0 回答 0