1.基礎概念
- Tensor:類型化的多維數組,圖的邊;Tensor所引用的並不持有具體的值,而是保持一個計算過程,可以使用session.run()或者t.eval()對tensor的值進行計算。
- Operation:執行計算的單元,圖的節點;這里大概可總結為Tensor創建,Tensor轉換,邏輯判斷,數學運算,聚合運算,序列比較與索引提取等。
- Graph:一張有邊與點的圖,其表示了需要進行計算的任務;
- Session:稱之為會話的上下文,用於執行圖。用戶管理CPU和GPU和網絡連接。
2.Tensor
2.1 數據結構
Rank |
Shape |
Dimension number |
Example |
0 |
[] |
0-D |
A 0-D tensor. A scalar. |
1 |
[D0] |
1-D |
A 1-D tensor with shape [5]. |
2 |
[D0, D1] |
2-D |
A 2-D tensor with shape [3, 4]. |
3 |
[D0, D1, D2] |
3-D |
A 3-D tensor with shape [1, 4, 3]. |
n |
[D0, D1, ... Dn-1] |
n-D |
A tensor with shape [D0, D1, ... Dn-1]. |
- data type:單個數據的類型。下圖表示了所有的types。
Data type |
Python type |
Description |
DT_FLOAT |
tf.float32 |
32 bits floating point. |
DT_DOUBLE |
tf.float64 |
64 bits floating point. |
DT_INT8 |
tf.int8 |
8 bits signed integer. |
DT_INT16 |
tf.int16 |
16 bits signed integer. |
DT_INT32 |
tf.int32 |
32 bits signed integer. |
DT_INT64 |
tf.int64 |
64 bits signed integer. |
DT_UINT8 |
tf.uint8 |
8 bits unsigned integer. |
DT_UINT16 |
tf.uint16 |
16 bits unsigned integer. |
DT_STRING |
tf.string |
Variable length byte arrays. Each element of a Tensor is a byte array. |
DT_BOOL |
tf.bool |
Boolean. |
DT_COMPLEX64 |
tf.complex64 |
Complex number made of two 32 bits floating points: real and imaginary parts. |
DT_COMPLEX128 |
tf.complex128 |
Complex number made of two 64 bits floating points: real and imaginary parts. |
DT_QINT8 |
tf.qint8 |
8 bits signed integer used in quantized Ops. |
DT_QINT32 |
tf.qint32 |
32 bits signed integer used in quantized Ops. |
DT_QUINT8 |
tf.quint8 |
8 bits unsigned integer used in quantized Ops. |
2.2 稀疏張量(SparseTensor)
用於處理高維稀疏數據,包含indices,values,dense_shape三個屬性。
indices:形狀為(N, ndims)的Tensor,N為非0元素個數,ndims表示張量階數
values:形狀為(N)的Tensor,保存indices中指定的非0元素的值
dense_shape:形狀為(ndims)的Tensor,表示該稀疏張量對應稠密張量的形狀
3.Operation
3.1 Tensor創建函數
用法 |
說明 |
tf.zeros(shape, dtype=tf.float32, name=None) |
創建所有元素設置為零的張量 |
tf.zeros_like(tensor, dtype=None, name=None) |
返回tensor與所有元素設置為零相同的類型和形狀的張量 |
tf.ones(shape, dtype=tf.float32, name=None) |
創建一個所有元素設置為1的張量。 |
tf.ones_like(tensor, dtype=None, name=None) |
返回tensor與所有元素設置為1相同的類型和形狀的張量 |
tf.fill(dims, value, name=None) |
創建一個填充了標量值的張量 |
tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) |
從截斷的正態分布中輸出隨機值 |
tf.random_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None) |
從正態分布中輸出隨機值 |
tf.random_uniform(shape, minval=0.0, maxval=1.0, dtype=tf.float32, seed=None, name=None) |
從均勻分布輸出隨機值 |
tf.eye(num_rows, num_columns=None, batch_shape=None, dtype=tf.float32, name=None) |
構建一個單位矩陣, 或者 batch 個矩陣,batch_shape 以 list 的形式傳入 |
tf.diag(diagonal, name=None) |
構建一個對角矩陣 |
tf.global_variables_initializer() |
初始化全部變量 |
3.2 Tensor轉換函數
用法 |
說明 |
tf.random_shuffle(value, seed=None, name=None) |
沿其第一維度隨機打亂 |
tf.set_random_seed(seed) |
設置圖級隨機種子 |
tf.string_to_number(string_tensor, out_type=None, name=None) |
張量變換 |
tf.to_double(x, name='ToDouble') |
張量變換 |
tf.to_float(x, name='ToFloat') |
張量變換 |
tf.to_bfloat16(x, name='ToBFloat16') |
張量變換 |
tf.to_int32(x, name='ToInt32') |
張量變換 |
tf.to_int64(x, name='ToInt64') |
張量變換 |
tf.cast(x, dtype, name=None) |
張量變換 |
tf.shape(input, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.size(input, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.rank(input, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.reshape(tensor, shape, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.squeeze(input, squeeze_dims=None, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.expand_dims(input, dim, name=None) |
用於確定張量的形狀並更改張量的形狀 |
tf.slice(input_, begin, size, name=None) |
切片與擴展 |
tf.split(split_dim, num_split, value, name='split') |
切片與擴展 |
tf.tile(input, multiples, name=None) |
切片與擴展 |
tf.pad(input, paddings, name=None) |
切片與擴展 |
tf.concat(concat_dim, values, name='concat') |
切片與擴展 |
tf.pack(values, name='pack') |
切片與擴展 |
tf.unpack(value, num=None, name='unpack') |
切片與擴展 |
tf.reverse_sequence(input, seq_lengths, seq_dim, name=None) |
切片與擴展 |
tf.reverse(tensor, dims, name=None) |
切片與擴展 |
tf.transpose(a, perm=None, name='transpose') |
切片與擴展 |
tf.gather(params, indices, name=None) |
切片與擴展 |
tf.dynamic_partition(data, partitions, num_partitions, name=None) |
切片與擴展 |
tf.dynamic_stitch(indices, data, name=None) |
切片與擴展 |
3.3 邏輯判斷
用法 |
說明 |
tf.logical_and(x, y, name=None) |
邏輯運算符 |
tf.logical_not(x, name=None) |
邏輯運算符 |
tf.logical_or(x, y, name=None) |
邏輯運算符 |
tf.logical_xor(x, y, name='LogicalXor') |
邏輯運算符 |
tf.equal(x, y, name=None) |
比較運算符 |
tf.not_equal(x, y, name=None) |
比較運算符 |
tf.less(x, y, name=None) |
比較運算符 |
tf.less_equal(x, y, name=None) |
比較運算符 |
tf.greater(x, y, name=None) |
比較運算符 |
tf.greater_equal(x, y, name=None) |
比較運算符 |
tf.select(condition, t, e, name=None) |
比較運算符 |
tf.where(input, name=None) |
比較運算符 |
tf.is_finite(x, name=None) |
判斷檢查 |
tf.is_inf(x, name=None) |
判斷檢查 |
tf.is_nan(x, name=None) |
判斷檢查 |
tf.verify_tensor_all_finite(t, msg, name=None) 斷言張量不包含任何NaN或Inf |
判斷檢查 |
tf.check_numerics(tensor, message, name=None) |
判斷檢查 |
tf.add_check_numerics_ops() |
判斷檢查 |
tf.Assert(condition, data, summarize=None, name=None) |
判斷檢查 |
tf.Print(input_, data, message=None, first_n=None, summarize=None, name=None) |
判斷檢查 |
3.4 數學函數
用法 |
說明 |
tf.add(x, y, name=None) |
加法(支持 broadcasting) |
tf.subtract(x, y, name=None) |
減 |
tf.multiply(x, y, name=None) |
乘 |
tf.divide(x, y, name=None) |
除 |
tf.mod(x, y, name=None) |
取余 |
tf.pow(x, y, name=None) |
冪 |
tf.square(x, name=None) |
求平方 |
tf.sqrt(x, name=None) |
開方 |
tf.exp(x, name=None) |
自然指數 |
tf.log(x, name=None) |
自然對數 |
tf.negative(x, name=None) |
取相反數 |
tf.sign(x, name=None) |
返回 x 的符號 |
tf.reciprocal(x, name=None) |
取倒數 |
tf.abs(x, name=None) |
求絕對值 |
tf.round(x, name=None) |
四舍五入 |
tf.ceil(x, name=None) |
向上取整 |
tf.floor(x, name=None) |
向下取整 |
tf.rint(x, name=None) |
取最接近的整數 |
tf.maximum(x, y, name=None) |
返回兩tensor中的最大值 (x > y ? x : y) |
tf.minimum(x, y, name=None) |
返回兩tensor中的最小值 (x < y ? x : y) |
tf.cos(x, name=None) |
三角函數和反三角函數 |
tf.sin(x, name=None) |
三角函數和反三角函數 |
tf.tan(x, name=None) |
三角函數和反三角函數 |
tf.acos(x, name=None) |
三角函數和反三角函數 |
tf.asin(x, name=None) |
三角函數和反三角函數 |
tf.atan(x, name=None) |
三角函數和反三角函數 |
tf.matmul(a,b,name=None) |
矩陣乘法(tensors of rank >= 2) |
tf.transpose(a, perm=None, name='transpose') |
轉置,可以通過指定 perm=[1, 0] 來進行軸變換 |
tf.trace(x, name=None) |
求矩陣的跡 |
tf.matrix_determinant(input, name=None) |
計算方陣行列式的值 |
tf.matrix_inverse(input, adjoint=None, name=None) |
求解可逆方陣的逆 |
tf.svd(tensor, name=None) |
奇異值分解 |
tf.qr(input, full_matrices=None, name=None) |
QR 分解 |
tf.norm(tensor, ord='euclidean', axis=None, keep_dims=False, name=None) |
求張量的范數(默認2) |
3.5 聚合相關
用法 |
說明 |
tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None) |
計算輸入 tensor 所有元素的和,或者計算指定的軸所有元素的和 |
tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None) |
求均值 |
tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None) |
求最大值 |
tf.reduce_min(input_tensor, axis=None, keep_dims=False, name=None) |
求最小值 |
tf.reduce_prod(input_tensor, axis=None, keep_dims=False, name=None) |
求累乘 |
tf.reduce_all(input_tensor, axis=None, keep_dims=False, name=None) |
全部滿足條件 |
tf.reduce_any(input_tensor, axis=None, keep_dims=False, name=None) |
至少有一個滿足條件 |
3.6 序列比較與索引提取
用法 |
說明 |
tf.setdiff1d(x, y, index_dtype=tf.int32, name=None) |
比較兩個 list 或者 string 的不同,並返回不同的值和索引 |
tf.unique(x, out_idx=None, name=None) |
返回 x 中的唯一值所組成的tensor 和原 tensor 中元素在現 tensor 中的索引 |
tf.where(condition, x=None, y=None, name=None) |
x if condition else y, condition 為 bool 類型的 |
tf.argmax(input, axis=None, name=None, output_type=tf.int64) |
返回沿着坐標軸方向的最大值的索引 |
tf.argmin(input, axis=None, name=None, output_type=tf.int64) |
返回沿着坐標軸方向的最小值的索引 |
tf.invert_permutation(x, name=None) |
x 的值當作 y 的索引,range(len(x)) 索引當作 y 的值 |
tf.edit_distance(x,y) |
編輯距離 |
4.Graph
用法 |
說明 |
tf.get_default_graph() |
訪問默認圖 |
tf.Graph.seed |
此圖內使用的隨機種子 |
tf.Graph.init() |
創建一個新的空的圖 |
tf.Graph.as_default() |
返回一個使得當前圖成為默認圖的上下文管理器 |
tf.Graph.as_graph_def(from_version=None, add_shapes=False) |
返回一個表示這個圖的序列化的 GraphDef。 |
tf.Graph.as_graph_element(obj, allow_tensor=True, allow_operation=True) |
給定一個obj,看它能否對應到圖中的元素 |
tf.Graph.get_operation_by_name(name) |
根據名字獲取某個operation |
tf.Graph.get_tensor_by_name(name) |
根據名字獲取某個tensor |
tf.Graph.get_operations() |
獲取所有operations |
tf.Graph.is_feedable(tensor) |
判斷是否可feed或可fetch |
tf.Graph.is_fetchable(tensor_or_op) |
判斷是否可feed或可fetch |
tf.Graph.prevent_feeding(tensor) |
設置不可feed或不可fetch |
tf.Graph.prevent_fetching(op) |
設置不可feed或不可fetch |
tf.Graph.finalize() |
結束這個圖,使它只讀,不能向g添加任何新的操作 |
tf.Graph.finalized |
如果這個圖已經結束,它為真 |
tf.Graph.control_dependencies(control_inputs) |
返回一個明確控制依賴(control dependencies)的上下文管理器 |
tf.Graph.devide(device_name_or_function) |
返回一個明確默認設備的使用的上下文管理器 |
tf.Graph.name_scope(name) |
返回為操作創建分層的上下文管理器 |
tf.Graph.add_to_collection(name,value) |
將value值存入給定name的collection |
tf.Graph.add_to_collections(names,value) |
將value存入給定的names的collections中 |
tf.Graph.get_collection(name,scope=None) |
返回給定名稱集合的值的列表 |
5.Session
用法 |
說明 |
tf.Session() |
|
tf.InteractiveSession() |
|
tf.get_default_session() |
獲取默認session |
tf.Session().graph |
|
tf.Session(). init(self, target='', graph=None, config=None) |
|
tf.Session().as_default() |
返回使該對象成為默認session的上下文管理器. |
tf.Session().close |
關閉這個session |
tf.Session().list_devices() |
列出此session中的可用設備. |
tf.Session().run(fetches,feed_dict=None) |
執行 |
tf.Session().reset(target) |
在target上重置資源容器,並關閉所有連接的會話. |
附錄
https://www.jianshu.com/p/55a47b1720ba
https://www.cnblogs.com/qjoanven/p/7736025.html
https://blog.csdn.net/xun527/article/details/79690226
https://blog.bitsrc.io/learn-tensorflow-fundamentals-in-20-minutes-cdef2dec331a
https://blog.csdn.net/kmsj0x00/article/details/80698794
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-slp52jz8.html