TensorFlow學習日記29

1.tf.nn.rnn_cell
解析:Module for constructing RNN Cells.
(1)class BasicLSTMCell: Basic LSTM recurrent network cell.
(2)class BasicRNNCell: The most basic RNN cell.
(3)class DeviceWrapper: Operator that ensures an RNNCell runs on a particular device.
(4)class DropoutWrapper: Operator adding dropout to inputs and outputs of the given cell.
(5)class GRUCell: Gated Recurrent Unit cell (cf. http://arxiv.org/abs/1406.1078).
(6)class LSTMCell: Long short-term memory unit (LSTM) recurrent network cell.
(7)class LSTMStateTuple: Tuple used by LSTM Cells for state_size, zero_state, and output state.
(8)class MultiRNNCell: RNN cell composed sequentially of multiple simple cells.
(9)class RNNCell: Abstract object representing an RNN cell.
(10)class ResidualWrapper: RNNCell wrapper that ensures cell inputs are added to the outputs.

2.tf.contrib.rnn.MultiRNNCell和tf.nn.rnn_cell.MultiRNNCell
解析:RNN cell composed sequentially of multiple simple cells.

3.tf.nn.softmax
解析:

import tensorflow as tf
logits = tf.constant([[1.0,2.0,3.0],[1.0,4.0,3.0],[1.0,2.0,3.0]])
y = tf.nn.softmax(logits)
with tf.Session() as sess:
    softmax = sess.run(y)
    print(softmax)

結果輸出,如下所示:

[[ 0.09003057  0.24472848  0.66524094]
 [ 0.03511903  0.70538455  0.25949648]
 [ 0.09003057  0.24472848  0.66524094]]


4.tf.nn.softmax_cross_entropy_with_logits
解析:

import tensorflow as tf
logits = tf.constant([[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]])
y1 = tf.nn.softmax(logits)
y2 = tf.constant([[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,0.0,1.0]])
cross_entropy1 = -tf.reduce_sum(y2 * tf.log(y1))
cross_entropy2 = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y2, logits=logits))
with tf.Session() as sess:
    result1 = sess.run(cross_entropy1)
    result2 = sess.run(cross_entropy2)
    print(result1)
    print(result2)

結果輸出,如下所示:

1.22282
1.22282


5.format()
解析:

# 填充與對齊
# ^、<、>分別是居中、左對齊、右對齊,後面帶寬度
# :號後面帶填充的字符,只能是一個字符,不指定的話默認是用空格填充
print('{:>8}'.format('3.14'))
print('{:<8}'.format('3.14'))
print('{:^8}'.format('3.14'))
print('{:0>8}'.format('3.14'))
print('{:a>8}'.format('3.14'))
# 浮點數精度
print('{:.4f}'.format(3.1415926))
print('{:0>10.4f}'.format(3.1415926))

結果輸出,如下所示:

  3.14
3.14    
  3.14  
00003.14
aaaa3.14
3.1416
00003.1416

6.tf.ones_like(tensor, dtype=None, name=None)
解析:創建一個所有參數均爲1的tensor對象。

7.tf.rint(x,name=None)
解析:計算離x最近的整數,若爲中間值,取偶數值。如下所示:

x=tf.constant([[-1.7,-1.5,-1.1,0.1,0.5,0.4,1.5]],tf.float64)
result=tf.rint(x)
result==>[[-2. -2. -1. 0. 0. 0. 2.]]


8.with tf.Session() as sess
解析:對象在使用完後自動關閉以釋放資源。

9.sess = tf.Session()和sess.close()
解析:任務完成,需要顯式關閉會話。

10.InteractiveSession
解析:使用Tensor.eval()和Operation.run()方法代替Session.run()。

11.tf.summary.scalar
解析:通常用於loss,accuracy標量數據彙總。

12.tf.summary.histogram
解析:通常用於W,b張量數據彙總。

13.tf.constant
解析:創建一個常量tensor,按照給出value來賦值,可以用shape來指定其形狀。

14.Embedding Projector
解析:分析embeddings高維數據的交互式的可視化工具,需要從checkpoint文件中讀取embeddings。默認情況下,用PCA主成分分析方法將高維數據投影到3D空間,還有一種投影方法是T-SNE。

15.DropoutWrapper
解析:
這裏寫圖片描述
當t-1時,輸入傳入第一層cell,這個過程有dropout。從t-2時刻的第一層cell傳到t-1,t,t+1的第一層cell,這個過程沒有dropout。從t+1時刻的第一層cell向同一時刻的cell傳遞時,這個過程有dropout。

16.zero_state(batch_size, dtype)
解析:return zero-filled state tensor(s).

17.stateful LSTM
解析:stateful LSTM的特點是,在處理過一個batch的訓練數據後,其內部狀態(記憶)會被作爲下一個batch的訓練數據的初始狀態。狀態LSTM使得可以在合理的計算複雜度內處理較長序列。

18.model.summary()
解析:打印出模型概況,它實際調用的是keras.utils.print_summary。

19.model.get_config()
解析:返回包含模型配置信息的Python字典。模型也可以從它的config信息中重構回去。

20.top_k_categorical_accuracy
解析:top_k_categorical_accuracy(y_true, y_pred, k=5)。計算top-k正確率,默認值爲5。

21.CS 20:TensorFlow for Deep Learning Research
解析:
(1)課程地址:https://web.stanford.edu/class/cs20si/syllabus.html
(2)Github:https://github.com/chiphuyen/stanford-tensorflow-tutorials

22.tensorflow: interrupted by signal 9: SIGKILL
解析:內存不足。

23.GPU顯存設置
解析:

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) 
config = tf.ConfigProto(allow_soft_placement=True, gpu_options=gpu_options)
tf.Session(condig=config)

說明:per_process_gpu_memory_fraction=0.5表示使用50%的GPU顯存資源。TensorFlow默認使用最大顯存運行。

24.EarlyStopping
解析:EarlyStopping是用於提前停止訓練的Callbacks,當訓練集上的loss不在減小(即減小的程度小於某個閾值)的時候停止繼續訓練。參數patience表示能夠容忍多少個epoch內沒有improvement。

25.numpy.ravel()和numpy.flatten()
解析:兩者實現的功能都是將多維數組降爲一維,兩者的區別在於返回copy還是返回view。numpy.flatten()返回拷貝,numpy.ravel()返回視圖。如下所示:

>>> x = np.array([[1, 2], [3, 4]])
>>> x.flatten()[1] = 100
>>> x
array([[1, 2],
       [3, 4]])
>>> x.ravel()[1] = 100
>>> x
array([[  1, 100],
       [  3,   4]])

參考文獻:
[1] 深度學習
[2] 什麼是TensorBoard:http://geek.csdn.net/news/detail/197155

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章