Tensorflow error due to uninitialized variable [How to Fix]

 

Initial code

import pandas as pd
import numpy as np
import tensorflow as tf train_input_data = pd.read_excel('new_data/4.12.2.xlsx',header=None,sheetname='train') train_input_data = np.array(train_input_data).ravel() train_input_data = list(train_input_data) train_input_data = tf.constant(train_input_data,dtype=tf.float32) train_output_data = train_input_data test_input_data = pd.read_excel('new_data/4.12.2.xlsx',header=None,sheetname='test') test_input_data = np.array(test_input_data).ravel() test_input_data = list(test_input_data) test_input_data = tf.constant(test_input_data,shape=[270,1],dtype=tf.float32) test_output_data = test_input_data print(type(train_input_data)) fc_mean, fc_var = tf.nn.moments(train_input_data,axes=[0]) scale = tf.Variable(tf.ones([1])) shift = tf.Variable(tf.zeros([1])) epslion = 0.001 train_input_data = tf.nn.batch_normalization(train_input_data,fc_mean,fc_var,shift,scale,epslion) sess = tf.Session() sess.run(train_input_data)

Error:

Traceback (most recent call last):
  File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1327, in _do_call
    return fn(*args)
  File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1306, in _run_fn status, run_metadata) File "/Users/zhangjia/anaconda/lib/python3.6/contextlib.py", line 89, in __exit__ next(self.gen) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable_1 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_1)]] During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Users/zhangjia/PycharmProjects/太阳能预测/test.py", line 30, in <module> a = sess.run(train_input_data) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 895, in run run_metadata_ptr) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1124, in _run feed_dict_tensor, options, run_metadata) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run options, run_metadata) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1340, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value Variable_1 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_1)]] Caused by op 'Variable_1/read', defined at: File "/Users/zhangjia/PycharmProjects/太阳能预测/test.py", line 21, in <module> shift = tf.Variable(tf.zeros([1])) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 199, in __init__ expected_shape=expected_shape) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 330, in _init_from_args self._snapshot = array_ops.identity(self._variable, name="read") File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1400, in identity result = _op_def_lib.apply_op("Identity", input=input, name=name) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op op_def=op_def) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2630, in create_op original_op=self._default_original_op, op_def=op_def) File "/Users/zhangjia/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1204, in __init__ self._traceback = self._graph._extract_stack() # pylint: disable=protected-access FailedPreconditionError (see above for traceback): Attempting to use uninitialized value Variable_1 [[Node: Variable_1/read = Identity[T=DT_FLOAT, _class=["loc:@Variable_1"], _device="/job:localhost/replica:0/task:0/cpu:0"](Variable_1)]]

Error reason:

in the initial code, because tf.global is not used_ variables_ Initializer () function to initialize variables, resulting in errors in code

Solution:

Add tf.global to the code_ variables_ After initializer () function is used to initialize the data, the problem of

can be solved

Correct code

import pandas as pd
import numpy as np
import tensorflow as tf train_input_data = pd.read_excel('new_data/4.12.2.xlsx',header=None,sheetname='train') train_input_data = np.array(train_input_data).ravel() train_input_data = list(train_input_data) train_input_data = tf.constant(train_input_data,dtype=tf.float32) train_output_data = train_input_data test_input_data = pd.read_excel('new_data/4.12.2.xlsx',header=None,sheetname='test') test_input_data = np.array(test_input_data).ravel() test_input_data = list(test_input_data) test_input_data = tf.constant(test_input_data,shape=[270,1],dtype=tf.float32) test_output_data = test_input_data fc_mean, fc_var = tf.nn.moments(train_input_data,axes=[0]) scale = tf.Variable(tf.ones([1])) shift = tf.Variable(tf.zeros([1])) epslion = 0.001 train_input_data = tf.nn.batch_normalization(train_input_data,fc_mean,fc_var,shift,scale,epslion) sess = tf.Session() init = tf.global_variables_initializer() sess.run(init)
data, vocabulary = prepare_test_data(config)
model = CaptionGenerator(config)
init = tf.global_variables_initializer()
sess.run(init)
model.load(sess, FLAGS.model_file)
tf.get_default_graph().finalize()
model.test(sess, data, vocabulary)

Similar Posts: