0%

Tensorflow由于未初始化变量所导致的错误

初始代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)

报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Traceback (most recent call last):
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1327, in _do_call
return fn(*args)
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1306, in _run_fn
status, run_metadata)
File "/Users/anaconda/lib/python3.6/contextlib.py", line 89, in __exit__
next(self.gen)
File "/Users/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/PycharmProjects/太阳能预测/test.py", line 30, in <module>
a = sess.run(train_input_data)
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 895, in run
run_metadata_ptr)
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1124, in _run
feed_dict_tensor, options, run_metadata)
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1321, in _do_run
options, run_metadata)
File "/Users/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/PycharmProjects/太阳能预测/test.py", line 21, in <module>
shift = tf.Variable(tf.zeros([1]))
File "/Users/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 199, in __init__
expected_shape=expected_shape)
File "/Users/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/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/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/Users/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/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)]]

错误原因:

在初始的代码中,由于没有使用tf.global_variables_initializer()函数来对变量初始化,导致代码出现错误

解决方法:

在代码中加入tf.global_variables_initializer()函数来对数据进行初始化之后便可解决

正确代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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)