### 1、python中的容器
```
python ->list
np->np.array
tf->tf.Tensor
```
### 2、TF的格式
```
scalar:1.1
vector:[1.1],[1.2,1.3...]
matrix:[[1.1,1.2],[1.3,1.4]...]
tensor:rank>2
```
### 3、TF支持的数据类型
```
int,float,double
bool
string
```
### 4、创建
```py
# 创建一个int的TF对象
tf.constant(1)
# 创建一个float的TF对象
tf.constant(1.)
# 创建一个bool的TF对象
tf.constant([True,False])
# 创建一个string的TF对象
tf.constant('hello world.')
```
### 5、TF 常用属性
#### 5.1、查看设备
```py
with tf.device('cup'):
a=tf.constant([1])
# 查看设备名称
a.device
```
#### 5.2、设备转换
```py
# tf从cpu转换为gpu
aa=a.gpu()
```
#### 5.3、转换为Numpy
```
a.numpy()
```
#### 5.4、查看维度
```
# 获取维度
a.ndim
# tf获取对象维度
tf.rank(a)
# 例如
tf.rank(tf.ones([3,4,2]))
# 结果
<tf.Tensor:id=25,...,numpy=3>
# 获取每个维度的长度
a.shape
```
### 6、判断是否是Tensor
```
# 结果返回True or False
tf.is_tensor(a)
```
### 7、获取Tensor类型
```
# 获取tf类型
tf.dtype
# 类型比较
if a.dtype==tf.float32:
xxx
```
### 8、数据类型转换
```
# 创建一个numpy数据类型
a=np.arange(5)
# 得到dtype('int64')
print(a.dtype)
# numpy转换为tf
a_tf=tf.convert_to_tensor(a)
# 设置转换的类型
a_tf=tf.convert_to_tensor(a, dtype=tf.int32)
# tf自身转换类型
a_tf=tf.cast(a_tf, dtype=tf.float32)
```
### 9、tf.Variable
在线性回归中,y=wx+b,其中w就是一个variable
```
# 创建一个自变量
w=tf.Variable(w)
```
#### 9.1、参数说明
trainable 代表这个变量是可更新的,例如:梯度下降中参数的更新
### 结尾感谢
**学习内容来自于: 网易云课堂 ,作者:龙良曲,只供本人学习**

Tensorflow 数据类型