TensorFlow工具快速入門教程7 TensorFlow基礎
在機器學習中,模型被提供稱為特徵向量的物件列表。特徵向量可以是任何資料型別。特徵向量通常是填充張量的主要輸入。這些值將通過張量流入op節點,此操作/計算的結果將建立一個新的張量,該張量又將用於新的操作。所有這些操作都可以在圖表中檢視。
張量的表示
在TensorFlow中,張量是n維的特徵向量(即陣列)的集合。例如,2x3矩陣,其值為1到6

圖片.png
TensorFlow將此矩陣表示為:
[[1,2,3], [4,5,6]]
如果我們建立一個值為1到8的三維矩陣

圖片.png
TensorFlow將此矩陣表示為:
[[[1,2], [[3,4], [[5,6], [[7,8]]
注意:張量可以用標量表示,也可以有三維以上的形狀。視覺化更高維度級別更加複雜。
張量的型別
張量具有三個屬性的物件:name、shape、dtype
TensorFlow執行的每個操作都涉及張量的操作。您可以建立四個主要張量:tf.Variable,tf.constant,tf.placeholder,tf.SparseTensor
張量的維度
tf.constant的使用說明:
tf.constant(value, dtype, name = "") arguments - `value`: Value of n dimension to define the tensor. Optional - `dtype`: Define the type of data: - `tf.string`: String variable - `tf.float32`: Flot variable - `tf.int16`: Integer variable - "name": Name of the tensor. Optional. By default, `Const_1:0`
0維,即標量:
>>> r1 = tf.constant(1, tf.int16) >>> r1 <tf.Tensor 'Const:0' shape=() dtype=int16>

圖片.png
可以給標量取個名字:
>>> r2 = tf.constant(1, tf.int16, name = "my_scalar") >>> r2 <tf.Tensor 'my_scalar:0' shape=() dtype=int16> >>> r1_decimal = tf.constant(1.12345, tf.float32) >>> r1_decimal <tf.Tensor 'Const_1:0' shape=() dtype=float32> >>> r1_string = tf.constant("https://china-testing.github.io", tf.string) >>> r1_string <tf.Tensor 'Const_2:0' shape=() dtype=string>
維度:
>>> r1_vector = tf.constant([1,3,5], tf.int16) >>> r1_vector <tf.Tensor 'Const_3:0' shape=(3,) dtype=int16> >>> r2_boolean = tf.constant([True, True, False], tf.bool) >>> r2_boolean <tf.Tensor 'Const_4:0' shape=(3,) dtype=bool> >>> r2_matrix = tf.constant([ [1, 2], [3, 4] ],tf.int16) >>> r2_matrix <tf.Tensor 'Const_5:0' shape=(2, 2) dtype=int16> >>> r3_matrix = tf.constant([ [[1, 2], [3, 4], [5, 6]] ], tf.int16) >>> r3_matrix <tf.Tensor 'Const_6:0' shape=(1, 3, 2) dtype=int16>
張量的Shape
>>> m_shape = tf.constant([ [10, 11], [12, 13],[14, 15] ] ) >>> m_shape.shape TensorShape([Dimension(3), Dimension(2)]) >>> tf.zeros(10) <tf.Tensor 'zeros:0' shape=(10,) dtype=float32> >>> tf.ones([10, 10]) <tf.Tensor 'ones:0' shape=(10, 10) dtype=float32> >>> tf.ones(m_shape.shape[0]) <tf.Tensor 'ones_2:0' shape=(3,) dtype=float32> >>> tf.ones(m_shape.shape[1]) <tf.Tensor 'ones_4:0' shape=(2,) dtype=float32> >>> tf.ones(m_shape.shape) <tf.Tensor 'ones_5:0' shape=(3, 2) dtype=float32>
張量的資料型別
>>> m_shape.dtype tf.int32 >>> type_float = tf.constant(3.123456789, tf.float32) >>> type_int = tf.cast(type_float, dtype=tf.int32) >>> type_float.dtype tf.float32 >>> type_int.dtype tf.int32
操作
- tf.add(a, b)
- tf.substract(a, b)
- tf.multiply(a, b)
- tf.div(a, b)
- tf.pow(a, b)
- tf.exp(a)
- tf.sqrt(a)
>> tensor_a = tf.constant([[1,2]], dtype = tf.int32) >>> tensor_b = tf.constant([[3, 4]], dtype = tf.int32) >>> tensor_add = tf.add(tensor_a, tensor_b) >>> tensor_add <tf.Tensor 'Add:0' shape=(1, 2) dtype=int32> >>> tensor_multiply = tf.multiply(tensor_a, tensor_b) >>> tensor_multiply <tf.Tensor 'Mul:0' shape=(1, 2) dtype=int32>
參考資料
- 討論qq群144081101 591302926 567351477 釘釘群21745728
- ofollow,noindex">本文最新版本地址
- 本文涉及的python測試開發庫 謝謝點贊!
- 本文相關海量書籍下載
- 2018最佳人工智慧機器學習工具書及下載(持續更新)
變數
tf.get_variable(name = "", values, dtype, initializer) argument - `name = ""`: Name of the variable - `values`: Dimension of the tensor - `dtype`: Type of data. Optional - `initializer`: How to initialize the tensor. Optional If initializer is specified, there is no need to include the `values` as the shape of `initializer` is used.
>>> var = tf.get_variable("var", [1, 2]) >>> var.shape TensorShape([Dimension(1), Dimension(2)]) >>> var_init_1 = tf.get_variable("var_init_1", [1, 2], dtype=tf.int32,initializer=tf.zeros_initializer) >>> var_init_1.shape TensorShape([Dimension(1), Dimension(2)]) >>> tensor_const = tf.constant([[10, 20], [30, 40]]) >>> var_init_2 = tf.get_variable("var_init_2", dtype=tf.int32,initializer=tensor_const) >>> print(var_init_2.shape) (2, 2)

圖片.png
佔位符
tf.placeholder(dtype,shape=None,name=None ) arguments: - `dtype`: Type of data - `shape`: dimension of the placeholder. Optional. By default, shape of the data - `name`: Name of the placeholder. Optional
>>> data_placeholder_a = tf.placeholder(tf.float32, name = "data_placeholder_a") >>> print(data_placeholder_a) Tensor("data_placeholder_a:0", dtype=float32)
Session
-
Graph
TensorFlow的基礎。 所有數學運算(ops)都在Graph中執行。 您可以將Graph想象為每個操作都已完成的專案。 節點代表這些操作,它們可以吸收或建立新的張量。
-
Tensor
張量表示在操作之間進行的資料。 您之前看到過如何初始化張量。 常量和變數之間的差異是變數的初始值將隨時間變化。
- Session
會話將從圖中執行操作。 要使用張量值來提供圖形,您需要開啟會話。 在會話內,執行操作以建立輸出。
>>> x = tf.constant([2]) >>> y = tf.constant([4]) >>> multiply = tf.multiply(x, y) >>> sess = tf.Session() 2018-11-21 22:41:34.184786: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA >>> result_1 = sess.run(multiply) >>> print(result_1) [8] >>> sess.close() >>> with tf.Session() as sess: ...result_2 = multiply.eval() ...print(result_2) ... [8] >>> sess = tf.Session() >>> print(sess.run(r1)) 1 >>> print(sess.run(r2_matrix)) [[1 2] [3 4]] >>> print(sess.run(r3_matrix)) [[[1 2] [3 4] [5 6]]] >>> sess.run(tf.global_variables_initializer()) >>> print(sess.run(var)) [[ 0.5487809 -0.9846178]] >>> print(sess.run(var_init_1)) [[0 0]] >>> print(sess.run(var_init_2)) [[10 20] [30 40]] >>> import numpy as np >>> power_a = tf.pow(data_placeholder_a, 2) >>> with tf.Session() as sess: ...data = np.random.rand(1, 10) ...print(sess.run(power_a, feed_dict={data_placeholder_a: data}))# Will succeed. ... [[0.00214566 0.22329086 0.03267581 0.97980934 0.10616333 0.08555447 0.06780323 0.23336452 0.10076617 0.01539159]]

圖片.png

圖片.png
x = tf.get_variable("x", dtype=tf.int32,initializer=tf.constant([5])) z = tf.get_variable("z", dtype=tf.int32,initializer=tf.constant([6])) c = tf.constant([5], name = "constant") square = tf.constant([2], name ="square") f = tf.multiply(x, z) + tf.pow(x, square) + z + c init = tf.global_variables_initializer() # prepare to initialize all variables with tf.Session() as sess: init.run() # Initialize x and y function_result = f.eval() print(function_result)
執行結果: [66]

圖片.png