TensorFlow常用函数说明

转载请注明出处

tf.concat()
组合两个张量,axis表示是把哪个维度进行组合即直接把对应维度相加

a = np.arange(6).reshape(2, 3)
b = np.arange(6).reshape(2, 3)
print(a.shape)
print(b.shape)
c = tf.concat((tf.convert_to_tensor(a), tf.convert_to_tensor(b)), 0)
print(c)
d = tf.concat((tf.convert_to_tensor(a), tf.convert_to_tensor(b)), 1)
print(d)

out:
(2, 3)
(2, 3)
Tensor("concat:0", shape=(4, 3), dtype=int32)
Tensor("concat_1:0", shape=(2, 6), dtype=int32)

tf.expand_dims()
扩展一个维度

a = tf.range(10)
print(a)
b = tf.expand_dims(a, 0)
print(b)

out:
Tensor("range:0", shape=(10,), dtype=int32)
Tensor("ExpandDims:0", shape=(1, 10), dtype=int32)

tf.linalg.LinearOperatorLowerTriangular()
给张量设置一个全是0的上三角

a = np.arange(1, 10).reshape(3, 3)
a = tf.convert_to_tensor(a, tf.float32)
b = tf.linalg.LinearOperatorLowerTriangular(a).to_dense()
with tf.Session() as sess:
    c = sess.run(b)
    print(c)

out:
[[1. 0. 0.]
 [4. 5. 0.]
 [7. 8. 9.]]

tf.where(tf.equal(a, 0),b,c)
a中为0的位置取b的值,不为0的位置取c的值

a = [[1, 1, 0], [0, 1, 0], [0, 0, 1]]
b = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
c = [[2, 2, 2], [2, 2, 2], [2, 2, 2]]
d = tf.where(tf.equal(a, 0), b, c)
with tf.Session() as sess:
    print(sess.run(d))

out:
[[2 2 1]
 [1 2 1]
 [1 1 2]]

tf.tile(a, [2, 3])
把a进行扩展,扩展后的结果维度不变,扩展的内容是继续接在每一个维度的值的后面

a = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
b = tf.tile(a, [2, 3])
with tf.Session() as sess:
    print(sess.run(b))
    
out:
[[1. 2.]
 [3. 4.]]
 
[[1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]
 [1. 2. 1. 2. 1. 2.]
 [3. 4. 3. 4. 3. 4.]]

tf.cond()
类似if else,控制数据流向

x = tf.constant(1)
y = tf.constant(2)
result = tf.cond(x < y, lambda: tf.add(x, y), lambda: tf.square(y))
with tf.Session()as sess:
    sess.run(tf.global_variables_initializer())
    c = sess.run(result)
    print(c)
    
out:
3

tf.train.exponential_decay()
学习率的指数衰减函数,其参数如下

tf.train.exponential_decay(learning_rate, 
						   global_step, 
						   decay_steps, 
						   decay_rate, 
						   staircase=False, 
						   name=None)
  • learning_rate - 初始学习率
  • global_step - 用于衰减计算的全局步骤。 一定不为负数。喂入一次 BACTH_SIZE 计为一次 global_step
  • decay_steps - 衰减速度,一定不能为负数,每间隔decay_steps次更新一次learning_rate值
  • decay_rate - 衰减系数,衰减速率,其具体意义参看函数计算方程(对应α^t中的α)。
  • staircase - 若 ‘ True ’ ,则学习率衰减呈 ‘ 离散间隔 ’ (discrete intervals),具体地讲,global_step / decay_steps是整数除法,衰减学习率( the decayed learning rate )遵循阶梯函数;若为 ’ False ‘ ,则更新学习率的值是一个连续的过程,每步都会更新学习率。

d e c a y e d _ l e a r n i n g = l e a r n i n g r a t e ∗ d e c a y _ r a t e g l o b a l _ s t e p d e c a y _ s t e p decayed_{\_}learning=learning_rate*decay_{\_}rate^{\frac{global_{\_}step}{decay_{\_}step}} decayed_learning=learningratedecay_ratedecay_stepglobal_step

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值