Tensorflow2.0学习——张量与操作
Contents
张量
TensorFlow使用一种叫 tensor 的数据结构去定义所有的数据,我们可以把tensor 看成 是 n 维的 array 或者 list 。在 TensorFlow 的各部分图形间流动传递的只能是 tensor。
编写TensorFlow 程序时,操纵并传递的主要对象是 tf.Tensor:
– 一个 数据类型 (例如 float32 int32 ,或 string)
– 以及 shape
代码示例:
# Rank 0
mammal=tf.Variable("Elephant",tf.string)
tf.print(tf.rank(mammal))
tf.print(tf.shape(mammal))
# 输出为
# 0
# []
#Rank 1
mystr=tf.Variable(["Hello"],tf.string)
tf.print(tf.rank(mystr))
tf.print(tf.shape(mystr))
# 输出为
# 1
# [1]
#Rank 2
mymat=tf.Variable([[7],[11]],tf.int16)
tf.print(tf.rank(mymat))
tf.print(tf.shape(mymat))
# 输出为
# 2
# [2 1]
创建张量
tf.constant([1,2,3],dtype=tf.int16)
#输出:
#<tf.Tensor: shape=(3,), dtype=int16, numpy=array([1, 2, 3], dtype=int16)>
tf.zeros((2,2),dtype=tf.int16)
#输出:
"""<tf.Tensor: shape=(2, 2), dtype=int16, numpy=
array([[0, 0],
[0, 0]], dtype=int16)>"""
#reshape
rank_three_tensor=tf.ones([3,4,5])
matrix=tf.reshape(rank_three_tensor,[6,10])
Numpy 和 tensorflow 中的张量对比
Numpy | Tensorflow |
---|---|
a=np.zeros((2,2));b=np.ones((2,2)) | a=tf.zeros((2,2));b=tf.ones((2,2)) |
np.sum(b,axis=1) | tf.reduce_sum(a,axis=1) |
a.shape | a.get_shape() |
np.reshape(a,(1,4)) | tf.reshape(a,(1,4)) |
b*5+1 | b*5+1 |
np.dot(a,b) | tf.matmul(a,b) |
a[0,0]; a[:,0]; a[0,:] | a[0,0]; a[:,0]; a[0,:] |
操作
- tf.strings (常用于 推荐算法场景、 NLP 场景)
- tf.debugging
- tf.dtypes
- tf.math
- tf.random
- tf.feature_column 参考
tf.strings
#字符切割
tf.strings.bytes_split('hello')
# 输出
"""
<tf.Tensor: shape=(5,), dtype=string, numpy=array([b'h', b'e', b'l', b'l', b'o'], dtype=object)>
"""
# 查看帮助文档
help(tf.strings.split)
#单词切割
tf.strings.split('hello world')
# 输出
"""
<tf.Tensor: shape=(2,), dtype=int64, numpy=array([8, 1], dtype=int64)>
"""
tf.debugging
# 查看帮助文档
help(tf.debugging.assert_equal)
#tf自带debug函数
a=tf.random.uniform((10,10))
tf.debugging.assert_equal(x=a.shape,y=(10,10))
#错误示范
tf.debugging.assert_equal(x=a.shape,y=(20,10))
tf.random
a = tf.random.uniform(shape=(10,5),minval=0,maxval=10)
a
# 输出
"""
<tf.Tensor: shape=(10, 5), dtype=float32, numpy=
array([[2.6776767 , 4.8693347 , 4.2088356 , 1.5307021 , 9.345246 ],
[7.8309717 , 8.555967 , 4.6933947 , 1.3813853 , 5.568446 ],
[0.4318142 , 5.8466196 , 7.31678 , 1.1768234 , 5.278156 ],
[5.3494453 , 4.738872 , 8.855001 , 6.465087 , 0.37382483],
[7.9675827 , 6.9250307 , 8.076605 , 5.45236 , 5.1550474 ],
[4.027458 , 0.22787213, 2.271235 , 0.9705901 , 7.0026255 ],
[3.6111033 , 8.581781 , 4.876813 , 2.4775088 , 9.321402 ],
[7.0894957 , 9.5003805 , 1.8834174 , 1.2829685 , 1.5913701 ],
[5.129411 , 4.647763 , 1.9383168 , 5.0370703 , 4.287212 ],
[4.5405188 , 4.393653 , 9.808304 , 5.149974 , 1.2062919 ]],
dtype=float32)>
"""
tf.math
a = tf.constant([[1,2],[3,4]])
b = tf.constant([[5,6],[7,8]])
tf.print(tf.math.add(a,b))
tf.print(tf.math.subtract(a,b))
tf.print(tf.math.multiply(a,b))
tf.print(tf.math.divide(a,b))
# 输出
"""
[[6 8]
[10 12]]
[[-4 -4]
[-4 -4]]
[[5 12]
[21 32]]
[[0.2 0.33333333333333331]
[0.42857142857142855 0.5]]
"""
tf.dtypes
x =tf.constant([1.8,2.2],dtype=tf.float32)
x1=tf.dtypes.cast(x,tf.int32) #类型转换
x1
# 输出
"""
<tf.Tensor: id=796, shape=(2,), dtype=int32, numpy=array([1, 2])>
"""