python - 失败前提条件错误 : Attempting to use uninitialized value W

标签 python tensorflow conv-neural-network

我在 Kaggle 内核上的手写 A_Z 数据集上使用 Tensorflow。

我已经一个接一个地使用了 2 个 conv 层和 2 个 maxpool,然后将上面的层 reshape 为 full_1 (-1,*7*7*64) 并将其进一步扩展到 fully_connected 层(我应用了 dropout 的 full_2 ) 并将其连接到一个名为 last of shape (None,26) 的层,最终得到表示 26 个英文字母的预测输出。

CONV->MAXPOOL->CONV->MAXPOOL->reshaped(named full_1)->FULLY_CONNECTED(full_2)->OUTPUT(last)

早期(有时回来)运行的训练过程给出了准确度的数值,但后来由于某些未知原因开始给出 NaNs

此外,在整个训练过程中,准确性的数值从未增加太多,并且一直很低,这让我担心我是否正确应用了卷积网络,因为随着数据批处理的增加,网络应该学得更好才能提供更高的准确性处理到训练过程中。精度较低是否是由于层数较少和模型不太复杂?

此外,我对代码中的 tf.nn.softmax_cross_entropy_with_logits(labels=output,logits=last) 语句表示怀疑,因为 relu 函数已经应用于表示输出的 last 变量我的转换网络中的图层并在上面用作逻辑。

错误说: FailedPreconditionError:尝试使用未初始化的值 W_4

代码是:

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn import preprocessing

import copy
import warnings
warnings.filterwarnings('ignore')

#dataset=pd.read_csv('/Users/ajay/Documents/IpyNote/A_Z Handwritten Data.csv')
dataset=pd.read_csv('../input/handwritten_data_785.csv')

#print(dataset.head(3))
#print(dataset.info())


dataset['0'].unique()
dataset=dataset.astype('float32')
X=copy.deepcopy(dataset)
X.head(1)
Y=X.loc[:,'0']

#print(Y.head(3))
Y=Y.astype('int64')
s=pd.get_dummies(Y)

list(s)

Y=s
Y=Y.astype('float32')
Y.head(2)

X.drop('0',axis=1,inplace=True)
X_train,X_test,Y_train,Y_test=train_test_split(X,Y,test_size=0.25,stratify=Y)

input=tf.placeholder(dtype=tf.float32,shape=(None,28*28))
output=tf.placeholder(dtype=tf.float32,shape=(None,26))
W1=tf.Variable(tf.truncated_normal(shape=(5,5,1,32)),name='W')#28,28,32
b1=tf.Variable(tf.truncated_normal(shape=(1,32)),name='b')#14,14,32

W2=tf.Variable(tf.truncated_normal(shape=(5,5,32,64)),name='W')#14,14,64
b2=tf.Variable(tf.truncated_normal(shape=(1,64)),name='b')#7,7,64

W3=tf.Variable(tf.truncated_normal(shape=(7*7*64,1024)),name='W')
b3=tf.Variable(tf.truncated_normal(shape=(1,1024)),name='b')

W4=tf.Variable(tf.truncated_normal(shape=(1024,26)),name='W')
b4=tf.Variable(tf.truncated_normal(shape=(1,26)),name='b')

def conv(input,W,b):
    return tf.nn.relu(tf.nn.conv2d(input=input,filter=W,strides=(1,1,1,1),padding='SAME')+b)

def maxpool(x):
    return tf.nn.max_pool(value=x,ksize=(1,2,2,1),strides=(1,2,2,1),padding='SAME')

def full_connected(x,W,b):
    return tf.nn.relu(tf.matmul(x,W)+b)

p=tf.reshape(input,[-1,28,28,1])


conv_1=conv(p,W1,b1)
print('conv_1.shape',conv_1.shape)
maxpool_1=maxpool(conv_1)
print('maxpool_1.shape',maxpool_1.shape)
conv_2=conv(maxpool_1,W2,b2)
print('conv_2.shape',conv_2.shape)
maxpool_2=maxpool(conv_2)
print('maxpool_2.shape',maxpool_2.shape)

full_1=tf.reshape(maxpool_2,[-1,7*7*64])
full_2=full_connected(full_1,W3,b3)#full_1->full_2
print('full_2.shape',full_2.shape)

keep_prob=tf.placeholder(tf.float32)
full_2_dropout=tf.nn.dropout(full_2,keep_prob)

last=full_connected(full_2_dropout,W4,b4)
last = tf.clip_by_value(last, 1e-10, 0.9999999)

print('last.shape',last.shape)
loss=tf.nn.softmax_cross_entropy_with_logits(labels=output,logits=last)#loss=tf.nn.softmax(logits=last)

train_step=tf.train.AdamOptimizer(0.005).minimize(loss)
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(output,1), tf.argmax(last,1) ) , tf.float32))
init=tf.global_variables_initializer()


with tf.Session() as sess:
    epoch=1
    n_iterations=10
    sess.run(init)
    for i in range(n_iterations):
        j=i*50
        k=i*50+50
        print('j=',j,'k=',k)
        x = X_train.iloc[i*50:j,:]
        y = Y_train.iloc[i*50:j,:]
        #sess.run(accuracy,feed_dict={input:X_train,output:Y_train,keep_prob:1.0})
        print('Train_accuracy : ',sess.run(accuracy, feed_dict={input: x, output: y,keep_prob:1.0}))
        sess.run(train_step,feed_dict={input:x,output:y,keep_prob:1.0})

with tf.Session() as sess:
    n_iterations=20
    for i in range(n_iterations):
        j=i*50
        k=i*50+50
        print('j=',j,'k=',k)
        x = X_test.iloc[i*50:j,:]
        y = Y_test.iloc[i*50:j,:]
        print('Test_accuracy : ',sess.run(accuracy, feed_dict={input: x, output: y,keep_prob:1.0}))

错误显示如下:

conv_1.shape (?, 28, 28, 32)
maxpool_1.shape (?, 14, 14, 32)
conv_2.shape (?, 14, 14, 64)
maxpool_2.shape (?, 7, 7, 64)
full_2.shape (?, 1024)
last.shape (?, 26)
j= 0 k= 50
Train_accuracy :  nan
j= 50 k= 100
Train_accuracy :  nan
j= 100 k= 150
Train_accuracy :  nan
j= 150 k= 200
Train_accuracy :  nan
j= 200 k= 250
Train_accuracy :  nan
j= 250 k= 300
Train_accuracy :  nan
j= 300 k= 350
Train_accuracy :  nan
j= 350 k= 400
Train_accuracy :  nan
j= 400 k= 450
Train_accuracy :  nan
j= 450 k= 500
Train_accuracy :  nan
j= 0 k= 50

---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1329     try:
-> 1330       return fn(*args)
   1331     except errors.OpError as e:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1314       return self._call_tf_sessionrun(
-> 1315           options, feed_dict, fetch_list, target_list, run_metadata)
   1316 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1422             self._session, options, feed_dict, fetch_list, target_list,
-> 1423             status, run_metadata)
   1424 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py in __exit__(self, type_arg, value_arg, traceback_arg)
    515             compat.as_text(c_api.TF_Message(self.status.status)),
--> 516             c_api.TF_GetCode(self.status.status))
    517     # Delete the underlying status object from memory otherwise it stays alive

FailedPreconditionError: Attempting to use uninitialized value W_4
     [[Node: W_4/read = Identity[T=DT_FLOAT, _class=["loc:@W_4"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](W_4)]]

During handling of the above exception, another exception occurred:

FailedPreconditionError                   Traceback (most recent call last)
<ipython-input-2-496ec024fd3b> in <module>()
    114         x = X_test.iloc[i*50:j,:]
    115         y = Y_test.iloc[i*50:j,:]
--> 116         print('Test_accuracy : ',sess.run(accuracy, feed_dict={input: x, output: y,keep_prob:1.0}))

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    906     try:
    907       result = self._run(None, fetches, feed_dict, options_ptr,
--> 908                          run_metadata_ptr)
    909       if run_metadata:
    910         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1141     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1142       results = self._do_run(handle, final_targets, final_fetches,
-> 1143                              feed_dict_tensor, options, run_metadata)
   1144     else:
   1145       results = []

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1322     if handle is None:
   1323       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1324                            run_metadata)
   1325     else:
   1326       return self._do_call(_prun_fn, handle, feeds, fetches)

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1341         except KeyError:
   1342           pass
-> 1343       raise type(e)(node_def, op, message)
   1344 
   1345   def _extend_graph(self):

FailedPreconditionError: Attempting to use uninitialized value W_4
     [[Node: W_4/read = Identity[T=DT_FLOAT, _class=["loc:@W_4"], _device="/job:localhost/replica:0/task:0/device:GPU:0"](W_4)]]

Caused by op 'W_4/read', defined at:
  File "/opt/conda/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/opt/conda/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/opt/conda/lib/python3.6/site-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelapp.py", line 477, in start
    ioloop.IOLoop.instance().start()
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "/opt/conda/lib/python3.6/site-packages/tornado/ioloop.py", line 888, in start
    handler_func(fd_obj, events)
  File "/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/opt/conda/lib/python3.6/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/tornado/stack_context.py", line 277, in null_wrapper
    return fn(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 235, in dispatch_shell
    handler(stream, idents, msg)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/kernelbase.py", line 399, in execute_request
    user_expressions, allow_stdin)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/opt/conda/lib/python3.6/site-packages/ipykernel/zmqshell.py", line 533, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2698, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2802, in run_ast_nodes
    if self.run_code(code, result):
  File "/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-496ec024fd3b>", line 42, in <module>
    W1=tf.Variable(tf.truncated_normal(shape=(5,5,1,32)),name='W')#28,28,32
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 235, in __init__
    constraint=constraint)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 397, in _init_from_args
    self._snapshot = array_ops.identity(self._variable, name="read")
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py", line 142, in identity
    return gen_array_ops.identity(input, name=name)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3052, in identity
    "Identity", input=input, name=name)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3306, in create_op
    op_def=op_def)
  File "/opt/conda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1669, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

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

最佳答案

给出 NaN 的准确性的原因:您已将训练数据拆分为 X_train 和 X_test,因此您的索引受到干扰,并且训练数据集相对于索引变得非常随机,当您分批输入 X_train 时,索引来自 [0:50] 的训练在训练时不存在,因此您最终不会向您的模型提供任何内容。

在训练模型之前,执行以下操作:

X_test.reset_index(drop=True)
Y_test.reset_index(drop=True)

这将重置您的索引,并且 drop=True 将防止原始索引成为转换数据框中的另一列。

权重偏差而言,不要使用另一个 session 来测试模型,因为所有经过训练的变量都将在此 session 中丢失,因此将发生错误 Attempting to use uninitialized value W_4

为了方便起见,您也可以尝试保存变量。

此外,请引用此作为您的 logits 部分:here

关于python - 失败前提条件错误 : Attempting to use uninitialized value W,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52227910/

相关文章:

Python:带参数的队列方法 wait_for 谓词

python - 为什么 tkinter 模块在通过命令行运行时会引发属性错误,而在通过 IDLE 运行时却不会?

python - '标签 [0] 不在 [索引] 中'

python - 无法在android studio中的解释器上运行tflite模型

python - 加入 Keras 中每个输出的指标(在多个输出中)

python - 在keras中使用Conv3d将多个图像输入到同一个CNN

c++ - 自定义caffe windows cpp中的卷积层

python - 使用泊松分布 "A"优于 "B"的可能性

machine-learning - 卷积神经网络中隐藏层的丢弃率指导

python - 如何在 Tensorflow 中对一批不同长度的序列使用 embedding_lookup?