# TensorFlow基礎

## 張量資料結構

`Type`描述分配給Tensor元素的資料型別。

• 構建一個n維陣列
• 轉換n維陣列。

## TensorFlow的各種尺度

TensorFlow包括各種尺度。尺度簡述如下 -

#### 一維張量

``````>>> import numpy as np
>>> tensor_1d = np.array([1.3, 1, 4.0, 23.99])
>>> print tensor_1d
``````

``````>>> print tensor_1d[0]
1.3
>>> print tensor_1d[2]
4.0
``````

#### 二維張量

``````>>> import numpy as np
>>> tensor_2d = np.array([(1,2,3,4),(4,5,6,7),(8,9,10,11),(12,13,14,15)])
>>> print(tensor_2d)
[[ 1 2 3 4]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
>>>
``````

``````>>> tensor_2d[3][2]
14
``````

#### 張量處理和操作

``````import tensorflow as tf
import numpy as np

matrix1 = np.array([(2,2,2),(2,2,2),(2,2,2)],dtype = 'int32')
matrix2 = np.array([(1,1,1),(1,1,1),(1,1,1)],dtype = 'int32')

print (matrix1)
print (matrix2)

matrix1 = tf.constant(matrix1)
matrix2 = tf.constant(matrix2)
matrix_product = tf.matmul(matrix1, matrix2)
matrix_3 = np.array([(2,7,2),(1,4,2),(9,0,2)],dtype = 'float32')
print (matrix_3)

matrix_det = tf.matrix_determinant(matrix_3)
with tf.Session() as sess:
result1 = sess.run(matrix_product)
result2 = sess.run(matrix_sum)
result3 = sess.run(matrix_det)

print (result1)
print (result2)
print (result3)
``````