tensorflow故障彙總
- 1.問題:TensorFlow 1.5.0與tensorboard版本對照
- 2.問題:TensorFlow與python版本對照
- 3.故障:tensorboard localhost:6006 無法訪問此網站
- 4.故障:tensorboard No graph definition files were found
- 5.故障:Unsuccessful TensorSliceReader constructor: Failed to find any matching files
- 6.故障:Assign requires shapes of both tensors to match
- 7.故障:module 'tensorflow' has no attribute 'unpack'
- 8.故障:Only call softmax_cross_entropy_with_logits with named arguments (labels=…, logits=…, …)
- 9.故障:No gradients provided for any variable, check your graph for ops that do not support gradients
- 10.故障:TypeError: object() takes no parameters
- 11.故障:module 'tensorflow.python.ops.nn' has no attribute 'seq2seq' ‘rnn_cell’
- 12.故障:TypeError: msr_error() got an unexpected keyword argument 'labels'
- 13.故障:AttributeError: module 'tensorflow' has no attribute 'scalar_summary'
- 14.故障:AttributeError: module 'tensorflow' has no attribute 'merge_all_summaries'
- 15.故障:AttributeError: module 'tensorflow.python.training.training' has no attribute 'SummaryWriter'
- 16.故障:ImportError:cannot import name 'tf_utils'
1.問題:TensorFlow 1.5.0與tensorboard版本對照
解決:tensorflow 1.5.0配tensorboard 1.6.0,使用PyCharm或pip install對應的版本即可使用
2.問題:TensorFlow與python版本對照
解決:這個直接參考官網推薦,會區分Linux,window,macOS,我自己使用window pycharm一個很穩的版本對照,tensorflow 1.5.0+python3.5,至於網上很多使用anaconda環境安裝的覺得多此一舉了,直接pycharm搞定,官網說明地址在這https://www.tensorflow.org/install/pip?lang=python2,使用PyCharm或pip install對應的版本即可使用
以下是對照表
3.故障:tensorboard localhost:6006 無法訪問此網站
4.故障:tensorboard No graph definition files were found
說明:tensorboard沒有找到圖表文件,這是由於路徑問題導致找不到文件
解決:在圖表文件的路徑下,shift+鼠標右擊,在此處打開命令窗口,輸入tensorboard --logdir=C:\logfile,其中等號右邊改爲絕對路徑,確保當前路徑和等於右邊的路徑是文件存在的絕對路徑,問題解決
5.故障:Unsuccessful TensorSliceReader constructor: Failed to find any matching files
說明:輸入tensorflow中Saver.restore方法中參數有誤,第一個參數是Session(),第二個參數是路徑,一般是路徑或者文件有問題。
解決:
- 檢查文件是否存在
- 換成絕對路徑
6.故障:Assign requires shapes of both tensors to match
說明:保存和讀取的數據參數或格式不同導致的錯誤。
解決:
- 檢查保存和讀取的代碼定義參數格式是否相符
- 刪除原保存文件,重新讀取
ps:我這裏報錯是因爲保存數組時,格式寫作,讀取時定義參數爲理想格式,故報錯,修改代碼,刪除,重新保存讀取,問題解決
7.故障:module ‘tensorflow’ has no attribute ‘unpack’
說明:Tensorflow1.0+版本更新後,方法名發生改變。
解決:
- tf.unpack()替換成tf.unstack()
- 同理:tf.pack()替換成tf.stack()
8.故障:Only call softmax_cross_entropy_with_logits with named arguments (labels=…, logits=…, …)
說明:調用tf.nn.softmax_cross_entropy_with_logits()出現以上故障。
解決:
- 只能使用命名參數賦值變量來調用。調用函數改爲:tf.nn.softmax_cross_entropy_with_logits(labels=tf_train_labels, logits=logits)。
9.故障:No gradients provided for any variable, check your graph for ops that do not support gradients
說明:調用tf.nn.softmax_cross_entropy_with_logits(labels=…, logits=…, …)故障,與傳入參數有關。
解決:
- 確保abels與logits傳入參數正確性
- 如果是tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits( logits=pred,labels=y))中,logits和labels寫反了,互換後問題解決。
10.故障:TypeError: object() takes no parameters
說明:使用自定義方法時,讀不到相應參數,注意構造函數__init__是兩個下劃線。
解決:
- 檢查構造函數__init__是否拼寫錯誤
11.故障:module ‘tensorflow.python.ops.nn’ has no attribute ‘seq2seq’ ‘rnn_cell’
說明:Tensorflow 1.0後修改了seq2swq和rnn_cell的表達方式。
解決:
- tf.nn.seq2seq.sequence_loss_by_example改爲tf.contrib.legacy_seq2seq.sequence_loss_by_example
- tf.nn.rnn_cell.改爲tf.contrib.rnn.
12.故障:TypeError: msr_error() got an unexpected keyword argument ‘labels’
說明:msr_error() 函數得到一個意外的關鍵參數 ‘lables’,爲了避免混淆,函數需要接受命名參數。
解決:
- 定義 msr_error() 函數時,使用 labels,logits 指定
def msr_error(self, y_pre, y_target):
return tf.square(tf.subtract(y_pre, y_target))
改爲
def msr_error(self, logits, labels):
return tf.square(tf.subtract(logits, labels))
13.故障:AttributeError: module ‘tensorflow’ has no attribute ‘scalar_summary’
說明:Tensorflow沒有tf.scalar_summary()方法,與Tensorflow版本有關。
解決:
- tf.scalar_summary改爲tf.summary.scalar
14.故障:AttributeError: module ‘tensorflow’ has no attribute ‘merge_all_summaries’
說明:Tensorflow沒有tf.merge_all_summaries()方法,與Tensorflow版本有關。
解決:
- tf.merge_all_summaries改爲tf.summary.merge_all
15.故障:AttributeError: module ‘tensorflow.python.training.training’ has no attribute ‘SummaryWriter’
說明:Tensorflow沒有tf.train.SummaryWriter()方法,與Tensorflow版本有關。
解決:
- tf.train.SummaryWriter改爲tf.summary.FileWriter
16.故障:ImportError:cannot import name ‘tf_utils’
說明:tensorflow與keras版本兼容問題。
解決:
- 選擇合適keras版本與Tensorflow版本對照,我這裏是tensorflow1.5與keras2.1.4