在使用tensorflow中會遇到一些其基本的用法,再次作為記錄備忘!
在計算整體的loss是會將不同部分的loss放入乙個集合中,最後計算整體的loss,因此會用到tf.add_to_collection,具體參考tensorflow中的cifar10的例子,用法如下所示:
tf.add_to_collection:把變數放入乙個集合,把很多變數變成乙個列表
tf.get_collection:從乙個結合中取出全部變數,是乙個列表
tf.add_n:把乙個列表的東西都依次加起來
一般這樣用:
其中losses是乙個集合
關於tensorflow中softmax和entropy的計算:
pre = tf.variable([[10.0,0.0,0.0],[0.0,10.0,0.0],[0.0,0.0,10.0]])
lab =tf.variable([[1.0,0.0,0.0],[0.0,1.0,0.0],[0.0,0.0,1.0]])
#計算softmax
y = tf.nn.softmax(pre)
#計算交叉熵
cross_entropy = tf.reduce_mean(-tf.reduce_sum(lab*tf.log(y),reduction_indices=[1]))
#一次性計算softmax and entropy
loss = tf.contrib.losses.softmax_cross_entropy(pre,lab)
大部分應用中標識的分布都會比較稀疏,可以使用下面方式提高效率:
tf.contib
.losses
.sparse_softmax_cross_entropy(logits,labels)
import tensorflow as tf
from tensorflow.python.layers import core as layers_core
ones = tf.variable([[1.0,2],[3,4]])
output_layer = layers_core.dense(5)
logits = output_layer(ones)
with tf.session() as sess:
sess.run(tf.global_variables_initializer())
print sess.run(ones)
print sess.run(logits)
TensorFlow的一些基本概念
白天跟著tensorflow的官方文件把最簡單的mnist模型跑通了,基本過程算是大致理清了,但程式看一遍下來,發現tensorflow中很多基本概念還不是很理解,比如tensor這個東西怎麼理解,基於圖又是怎麼回事,於是打道回府從基本概念開始看起。慶幸的是目前有很多人在學這個,很多學習資料已經歸類...
TensorFlow的一些學習筆記
1 靈活性tensorflow不是乙個嚴格的神經網路工具包,只要你可以使用資料流圖來描述你的計算過程,你可以使用tensorflow做任何事情。你還可以方便地根據需要來構建資料流圖,用簡單的python語言來實現高層次的功能。2 可移植性tensorflow可以在任意具備cpu或者gpu的裝置上執行...
tensorflow遇到的一些錯誤
1 tensorflow dtype t.dtype.base dtype attributeerror float object has no attribute dtype 參考 我報錯的行是 disc gradients disc optimizer.compute gradients dis...