数据类型
- 数据载体
-
list
支持不同的数据类型,效率低 -
np.array
相同类型的载体,效率高;但是不支持GPC,不支持自动求导 -
tf.Tensor
TensorFlow中存储大量连续数据的载体
-
基本数据类型
tf.int32tf.constant(1)
tf.float32tf.constant(1.)
tf.float64tf.constant(1., dtype=tf.double)
tf.booltf.constant(True)
tf.stringtf.constant('hello')
-
基本属性
import tensorflow as tf
a = tf.range(4) # <tf.Tensor: shape=(4,), dtype=int32, numpy=array([0, 1, 2, 3], dtype=int32)>
a.numpy() # array([0, 1, 2, 3], dtype=int32)
a.ndim # 1 (0的话就是标量)
tf.rank(a) # <tf.Tensor: shape=(), dtype=int32, numpy=1>
a.shape # TensorShape([4])
tf.is_tensor(a) # True
tf.rank和a.ndim的区别在于返回的类型不同
HelloWord
import tensorflow as tf
hello = tf.constant("hello tensorflow!")
print(hello.numpy()) # b'hello tensorflow!'
alist = tf.constant([1,2,3])
alist.numpy() # array([1, 2, 3], dtype=int32)
创建变量
为了区分需要计算梯度信息的张量与不需要计算梯度信息的张量, TensorFlow 增加了
一种专门的数据类型来支持梯度信息的记录: tf.Variable。 tf.Variable 类型在普通的张量类
型基础上添加了 name, trainable 等属性来支持计算图的构建。
# tf.random.normal 方法返回形状为(1,4)的张量。它的4个元素符合均值为100、标准差为0.35的正态分布。
W = tf.Variable(initial_value=tf.random.normal(shape=(1, 4), mean=100, stddev=0.35), name="W")
b = tf.Variable(tf.zeros([4]), name="b")
c = tf.Variable(tf.zeros([4]))
(<tf.Variable 'W:0' shape=(1, 4) dtype=float32, numpy=array([[100.11003 , 100.05794 , 100.005936, 100.06278 ]], dtype=float32)>,
<tf.Variable 'b:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>,
<tf.Variable 'Variable:0' shape=(4,) dtype=float32, numpy=array([0., 0., 0., 0.], dtype=float32)>,
True)
变量赋值
b.assign_add([1, 1, 1, 1]) # <tf.Variable 'UnreadVariable' shape=(4,) dtype=float32, numpy=array([1., 1., 1., 1.], dtype=float32)>
b.assign([3,4,5,6]) # <tf.Variable 'UnreadVariable' shape=(4,) dtype=float32, numpy=array([3., 4., 5., 6.], dtype=float32)>
tensor
0阶张量
mammal = tf.Variable("Elephant", tf.string)
ignition = tf.Variable(451, tf.int16)
floating = tf.Variable(3.14159265359, tf.float64)
its_complicated = tf.Variable(12.3 - 4.85j, tf.complex64)
1阶张量
mystr = tf.Variable(["Hello", "World"], tf.string)
cool_numbers = tf.Variable([3.14159, 2.71828], tf.float32)
first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32)
its_very_complicated = tf.Variable([12.3 - 4.85j, 7.5 - 6.23j], tf.complex64)
2阶张量
mymat = tf.Variable([[7],[11]], tf.int16)
myxor = tf.Variable([[False, True],[True, False]], tf.bool)
linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32)
squarish_squares = tf.Variable([ [4, 9], [16, 25] ], tf.int32)
rank_of_squares = tf.rank(squarish_squares)
mymatC = tf.Variable([[7],[11]], tf.int32)
4阶张量
my_image = tf.zeros([10, 299, 299, 3]) # batch x height x width x color
my_image.shape, tf.rank(my_image)
# (TensorShape([10, 299, 299, 3]), <tf.Tensor: shape=(), dtype=int32, numpy=4>)
常用函数
tf.convert_to_tensor(data)
tf.zeros(shape)
tf.ones(1)
生成一个一维tensor,包含一个1
tf.ones([])
生成一个标量1
tf.ones([2])
生成一个一维tensor,包含两个1
tf.ones_like(a)
相当于tf.ones(a.shape)
tf.fill([3,4], 9)
全部填充9
tf.random.normal([3,4], mean=1, stddev=1)
tf.random.truncated_normal([3,4], mean=0, stddev=1)
带截断的正态分布,(大于某个值重新采样),比如在经过sigmoid激活后,如果用不带截断的,容易出现梯度消失问题。
tf.random.uniform([3,4], minval=0, maxval=100, dtype=tf.int32)
平均分布
类型转换
- 字符串编码转换
docs = tf.constant([u'Everything not saved will be lost.'.encode('UTF-16-BE'), u'Sad☹'.encode('UTF-16-BE')])
utf8_docs = tf.strings.unicode_transcode(docs, input_encoding='UTF-16-BE', output_encoding='UTF-8')
print(utf8_docs.numpy())
# [b'Everything not saved will be lost.' b'Sad\xe2\x98\xb9']
- numpy转换tensor
import numpy as np
a=np.arange(5)
a.dtype # dtype('int64')
aa=tf.convert_to_tensor(a) # <tf.Tensor: id=466678, shape=(5,), dtype=int64, numpy=array([0, 1, 2, 3, 4])>
- 类型转换
aa = tf.convert_to_tensor(a, dtype=tf.int32)
x = tf.cast(aa, tf.float32) # <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
简单求导
# 求w*w 在w=1.0处的导数
w = tf.Variable([[1.0]])
with tf.GradientTape() as t:
loss = w * w
dw = t.gradient(loss, w)
dw.numpy()
# array([[2.]], dtype=float32)
额外一例子
x = tf.constant(3.0)
with tf.GradientTape(persistent=True) as t:
t.watch(x) # Ensures that `tensor` is being traced by this tape.
y = x * x
z = y * y
dz_dx = t.gradient(z, x) # 108.0 (4*x^3 at x = 3)
dy_dx = t.gradient(y, x) # 6.0
print("dz/dx=", dz_dx.numpy())
print("dy/dx=", dy_dx.numpy())
del t # Drop the reference to the tape
网友评论