tf.split
tf.split(
value,
num_or_size_splits,
axis=0,
num=None,
name='split'
)
value:准备切分的张量
num_or_size_splits:准备切成几份
axis : 准备在第几个维度上进行切割,1表示按照列切分,0表示按照行切分。
其中分割方式分为两种
- 如果num_or_size_splits 传入的 是一个整数,那直接在axis=D这个维度上把张量平均切分成几个小张量
- 如果num_or_size_splits 传入的是一个向量(这里向量各个元素的和要跟原本这个维度的数值相等)就根据这个向量有几个元素分为几项)
# 张量为(5, 30)
# 这个时候5是axis=0, 30是axis=1,如果要在axis=1这个维度上把这个张量拆分成三个子张量
#传入向量时
import tensorflow as tf
import numpy as np
t = np.random.randint(1,10,(5,30))
split0, split1, split2 = tf.split(t, [4, 15, 11], 1)
sess = tf.Session()
print(sess.run(split0))
tf.shape(split0) # [5, 4]
tf.shape(split1) # [5, 15]
tf.shape(split2) # [5, 11]
# 传入整数时
split0, split1, split2 = tf.split(t, num_or_size_splits=3, axis=1)
tf.shape(split0) # [5, 10]
tf.pack/tf.stack/tf.concat
TensorFlow 1.2.1 tf.stack() 替代 tf.pack()
tf.concat与tf.stack这两个函数作用类似,都是在某个维度上对矩阵(向量)进行拼接,不同点在于前者拼接后的矩阵维度不变,后者则会增加一个维度。
import tensorflow as tf
a = tf.constant([[1,2,3],[4,5,6]])
b = tf.constant([[7,8,9],[10,11,12]])
ab1 = tf.concat([a,b],axis=0)
ab2 = tf.stack([a,b], axis=0)
sess = tf.Session()
print(sess.run(ab1))
print(sess.run(ab2))
print(sess.run(ab1).shape)
print(sess.run(ab2).shape)
输出:
#ab1
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
#ab2
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
(4, 3)
(2, 2, 3)
a = tf.pack([5*4,8])
sess = tf.Session()
print(sess.run(a))
[20 8]
print(sess.run(a).shape)
(2,)
tf.train.exponential_decay()
https://blog.csdn.net/lllxxq141592654/article/details/84110600
tf.train.exponential_decay(
learning_rate,
global_step,
decay_steps,
decay_rate,
staircase=False,
name=None
)
decayed_learning_rate = learning_rate *
decay_rate ^ (global_step / decay_steps)
staircase=True,每隔DECAY_STEPS改变一次
staircase=False,每一步都改变学习率
tensorflow中自带四种交叉熵函数
https://blog.csdn.net/qq_35203425/article/details/79773459
tf.nn.softmax_cross_entropy_with_logits()
tf.nn.sparse_softmax_cross_entropy_with_logits()
tf.nn.sigmoid_cross_entropy_with_logits()
tf.nn.weighted_cross_entropy_with_logits()
注意:
- 它在函数内部进行sigmoid或softmax操作
- output不是一个数,而是一个batch中每个样本的loss,所以一般配合tf.reduce_mea(loss)使用
softmax_cross_entropy_with_logits中:
labels和logits具有相同的type(float)和shape的张量(tensor),即数据类型和张量维度都一致。
sparse_softmax_cross_entropy_with_logits中:
labels: shape为[batch_size],labels[i]是{0,1,2,……,num_classes-1}的一个索引,当使用这个函数时,tf自动将原来的类别索引转换成one_hot形式,然后与label表示的one_hot向量比较,计算交叉熵。
其他
旧版本的TensorFlow肯定涉及tf.Variable和tf.get_variable,tf.name_scope和tf.variable_scope
这些每次看都不是很懂,看了也记不住。果断放弃了,看代码知道是啥就好
https://www.bbsmax.com/A/RnJWLOwy5q/ 这个说了name_scope和variable_scope的区别
tf.gather,tf.expand_dims,tf.where这些都是容易忘记,但经常用到。
模型加载与保存
https://www.cnblogs.com/USTC-ZCC/p/11249625.html