Tensorflow笔记

Graph指定op
tf.constant()
tf.Variable() 创建变量维护图的状态,必须增加一个初始化 op 到图中
init_op = tf.initialize_all_variables()。启动图session.run时传入init_op
tf.placeholder() 创建op占位符,在session.run时传入feed_dict

tf.data.Dataset
函数列表
batch
concat
concatenate

tf.nn
tf.nn.softmax_cross_entropy_with_logits_v2(labels=None,logits=None)
, 此处传入的logits不能是经过softmax输出的scaled值,而应该是值域范围在正无穷负无穷之间的对数几率
tf.nn.sparse_softmax_cross_entropy_with_logits的label使用one-hot表示

tf.nn.l2loss
L2 norm of a tensor without the sqrt

tf.truncated_normal和tf.random_normal
截断正太分布生成(μ-2σ,μ+2σ)之间的值;random_normal生成正太分布的随机值

tf.nn.conv2d(
input,
filter=None,
strides=None,
padding=None,
use_cudnn_on_gpu=True,
data_format='NHWC',
dilations=[1, 1, 1, 1],
name=None,
filters=None
)
filter维度[kernel_height, kernel_width, in_channel, out_channel]
out_height/width = (n+2p-f)/s+1,对于filter每一个out_channel都有一个对应的偏置量
strides纬度[1, stride, stride, 1]
padding: value不pooling,same填充行数为f - n % s

tf.nn.max_pool(
value,
ksize,
strides,
padding,
data_format='NHWC',
name=None,
input=None
)

tf.nn.dropout(
x,
keep_prob=None,
noise_shape=None,
seed=None,
name=None,
rate=None
)
rate = 1 - keep_prob
每个元素x有rate的概率输出0,否则scale(scales up the input by 1 / (1-rate). The scaling is such that the expected sum is unchanged.);测试时keep_prob等于1

keras.model
compile和fit中的sample_weight参数
class_weight:字典,将不同的类别映射为不同的权值,该参数用来在训练过程中调整损失函数(只能用于训练)。该参数在处理非平衡的训练数据(某些类的训练样本数很少)时,可以使得损失函数对样本数不足的数据更加关注。
sample_weight:权值的numpy array,用于在训练时调整损失函数(仅用于训练)。可以传递一个1D的与样本等长的向量用于对样本进行1对1的加权,或者在面对时序数据时,传递一个的形式为(samples,sequence_length)的矩阵来为每个时间步上的样本赋不同的权。这种情况下请确定在编译模型时添加了sample_weight_mode='temporal'。
class_weight---主要针对的上数据不均衡问题,比如:异常检测的二项分类问题,异常数据仅占1%,正常数据占99%; 此时就要设置不同类对loss的影响。
sample_weigh---主要解决的是样本质量不同的问题,比如前1000个样本的可信度,那么它的权重就要高,后1000个样本可能有错、不可信,那么权重就要调低。

tf.boolean_mask(a,b) 将使a (m维)矩阵仅保留与b中“True”元素同下标的部分。使用tf.boolean_mask用来过滤概率值比较低的锚盒,

降维求和

求和

tf.reduce_sum(x) ==> 6

按列求和

tf.reduce_sum(x, 0) ==> [2, 2, 2]

按行求和

tf.reduce_sum(x, 1) ==> [3, 3]

按照行的维度求和

tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]

行列求和

tf.reduce_sum(x, [0, 1]) ==> 6

你可能感兴趣的:(Tensorflow笔记)