TensorFlow 学习

来源:互联网 发布:淘宝促销免费模板 编辑:程序博客网 时间:2024/06/05 14:26

truncated_normal

initial = tf.truncated_normal(shape, stddev=0.1)//tf.truncated_normal(shape, mean=0.0, stddev=1.0, dtype=tf.float32, seed=None, name=None)

参数:

shape: [path_height, path_weight, in_channels, out_channels ]。
mean: 正态分布的均值。
stddev: 正态分布的标准差。
dtype: 输出的类型。
seed: 一个整数,当设置之后,每次生成的随机数都一样。
name: 操作的名字。

conv2d

tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None)

input 是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一
filter 卷积核[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数]
strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4
padding:string类型的量,只能是”SAME”,”VALID”其中之一,这个值决定了不同的卷积方式
use_cudnn_on_gpu: 默认为true

max_pool

tf.nn.max_pool(value, ksize, strides, padding, name=None)

第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape
第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1
第三个参数strides:步长,和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride,stride, 1]
第四个参数padding:和卷积类似,可以取’VALID’ 或者’SAME’
返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式

reshape
tf.reshape(x, [-1,28,28,1])
[-1, width, height, channels]

padding
如果padding = ‘VALID’
new_height = new_width = (W – F + 1) / S (结果向上取整)
如果padding = ‘SAME’
new_height = new_width = W / S (结果向上取整)
其中W等于图片长宽,S等于步长,F是filter长度

embedding的查看资料
one-hot
http://blog.csdn.net/google19890102/article/details/44039761
http://blog.csdn.net/ariessurfer/article/details/42526673

nce_loss

def nce_loss(weights, biases, inputs, labels, num_sampled, num_classes,             num_true=1,             sampled_values=None,             remove_accidental_hits=False,             partition_strategy="mod",             name="nce_loss")

loss = tf.reduce_mean(
tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,
num_sampled, vocabulary_size))

weight :(N, K) N个样本,K维度embedding
bias (N)
这里用的是softmax_cross_entropy_with_logits函数

embedding_lookup
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
embeddings 是一个随机生成的[vocabulary_size, embedding_size]大小的矩阵,train_inputs转化为embedding的初始化输入

def bidirectional_dynamic_rnn(cell_fw, # 前向RNNcell_bw, # 后向RNNinputs, # 输入sequence_length=None,# 输入序列的实际长度(可选,默认为输入序列的最大长度)initial_state_fw=None,  # 前向的初始化状态(可选)initial_state_bw=None,  # 后向的初始化状态(可选)dtype=None, # 初始化和输出的数据类型(可选)parallel_iterations=None,swap_memory=False, time_major=False,# 决定了输入输出tensor的格式:如果为true, 向量的形状必须为 `[max_time, batch_size, depth]`. # 如果为false, tensor的形状必须为`[batch_size, max_time, depth]`. scope=None)返回值:一个(outputs, output_states)的元组其中,1. outputs为(output_fw, output_bw),是一个包含前向cell输出tensor和后向cell输出tensor组成的元组。假设time_major=false,tensor的shape为[batch_size, max_time, depth]。实验中使用tf.concat(outputs, 2)将其拼接。2. output_states为(output_state_fw, output_state_bw),包含了前向和后向最后的隐藏状态的组成的元组。output_state_fw和output_state_bw的类型为LSTMStateTuple。LSTMStateTuple由(c,h)组成,分别代表memory cell和hidden state。# lstm模型 正方向传播的RNNlstm_fw_cell = tf.nn.rnn_cell.BasicLSTMCell(embedding_size, forget_bias=1.0)# 反方向传播的RNNlstm_bw_cell = tf.nn.rnn_cell.BasicLSTMCell(embedding_size, forget_bias=1.0)
nn.bidirectional_dynamic_rnn(lstm_fw_cell, lstm_bw_cell, embedded_chars,  dtype=tf.float32)

embedded_chars为输入的tensor,[batch_szie, max_time,
depth]。batch_size为模型当中batch的大小,应用在文本中时,max_time可以为句子的长度(一般以最长的句子为准,短句需要做padding),depth为输入句子词向量的维度。

tf.reduce_sum

# 'x' is [[1, 1, 1]#         [1, 1, 1]]tf.reduce_sum(x) ==> 6tf.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
原创粉丝点击