转载请注明出处
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=learningrate∗decay_ratedecay_stepglobal_step