tensorflow API r1.0

本文档记录了在学习 tensorflow 过程中对 tensorflow API r1.0 及以上版本中一些函数的总结

tf.nn.*

tf.nn.bidirectional_dynamic_rnn(cell_fw, cell_bw, inputs, sequence_length=None, initial_state_fw=None, initial_state_bw=None, dtype=None, parallel_iterations=None, swap_memory=False, time_major=False, scope=None)

Args:

cell_fw: 用于正向传递的一个 RNNCell 实例
cell_bw: 用于反向传递的一个 RNNCell 实例
inputs: RNN 的输入,当 time_major 参数置为 False(缺省情况下),无需进行变换,即输入形状为[batch_size, max_time, input_size]的 tensor.否则需要将 max_time 和 batch_size 调换位置 
sequence_length: 大小为 batch_size, 存储每个 batch 的真实序列长度
initial_state_fw: (optional) 见官方文档
initial_state_bw: (optional) 见官方文档
dtype: (optional) 见官方文档
parallel_iterations: (Default: 32) 见官方文档
swap_memory: 见官方文档
time_major: 见官方文档
dtype: (optional) 见官方文档
scope: 见官方文档

Returns:

outputs: 一个包含 ForwardRNN 和 BackwardRNN 每个时刻输出的 tuple(output_fw, output_bw)
         当 time_major == False (default) 时:
         output_fw 和 output_bw 均为 [batch_size, max_time, cell_fw.output_size]的 tensor;
         当 time_major == True 时:
         output_fw 和 output_bw 均为 [max_time, batch_size, cell_fw.output_size]的 tensor.
output_states: A tuple (output_state_fw, output_state_bw) containing the forward and the backward final states of bidirectional rnn.
               一个包含 ForwardRNN 和 BackwardRNN 最终状态的 tuple(output_state_fw, output_state_bw)

e.g.

import tensorflow as tf
x = tf.placeholder(dtype = "float", shape = [None, max_sentence_len, embedding_size])
sentence_len = tf.placeholder(tf.int32, [None])
lstm_fw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)
lstm_bw_cell = tf.contrib.rnn.BasicLSTMCell(n_hidden)
outputs, output_states = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x, sentence_len)

tf.nn.softmax(logits, dim=-1, name=None)

在指定 dim 维上计算每个位置上的 softmax 值,返回与 logits 相同 shape 的 tensor

tf.nn.softmax(logits,dim=1,name=None)=tensor(exp(logitsi)each j in dimexp(logitsj))

设 x = [[.1, .3, .5, .9]]

  • tf.nn.softmax(x) ==> [[0.16838508, 0.205666, 0.25120102, 0.37474789]]

tf.nn.softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, dim=-1, name=None)

Args:

* _sentinel: Used to prevent positional parameters. Internal, do not use.
* labels: Each row labels[i] must be a valid probability distribution.
* logits: Unscaled log probabilities.
* dim: The class dimension. Defaulted to -1 which is the last dimension.
* name: A name for the operation (optional).

在指定 dim 上计算 labels 和 logits 分别进行 softmax 操作过后的交叉熵

设 y = tf.nn.softmax(labels, dim), y’ = tf.nn.softmax(logits, dim), 则 y 和 y’ 的交叉熵计算如下:

Hy(y)=iyilog(yi)

具体操作如下:

设 labels = [[0.0,0.0,1.0],[0.0,0.0,1.0],[0.0,0.0,1.0]], logits = [[1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]]

cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels, logits) ===> 1.2228

等价于

cross_entropy = -tf.reduce_sum(logits*tf.log(labels)) ===> 1.22282

tf.concat

tf.concat(values, axis, name=’concat’)

设 x1 = [[1, 2, 3], [4, 5, 6]], x2 = [[7, 8, 9], [10, 11, 12]]

  • 根据指定的 axis 将 values 中的 axis 连接在一起

    tf.concat([x1, x2], 0) ==> [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

    tf.concat([x1, x2], 1) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12]]

tf.reduce*

tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

设 x 是一个 2*3 的 tensor [[1, 1, 1], [1, 1, 1]]

  • 当 axis 为 None 时,将所有元素求和

    tf.reduce_sum(x) ==> 6

  • 当 axis 指定时, 按照指定 axis 分别求和

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

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

  • 当 keep_dims 为 True 时,将保留对应维数长度为1, 如 2*3 ==> 2*1

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

  • 当 axis 为 vector 时, 将 vector 中每一维都分别求和并取总和

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

tf.reduce_max(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)

设 x 是一个 2*3 的 tensor [[2, 3, 2], [4, 5, 1]]

  • 当 axis 为 None 时,计算所有维度上的最大值中最大的那个

    tf.reduce_max(x) ==> 5

  • 当 axis 指定时, 按照指定 axis 分别找出最大值

    tf.reduce_max(x, 0) ==> [4, 5, 2]

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

  • 当 keep_dims 为 True 时,按照指定 axis 分别找出最大值, 并且返回为一个1维 tensor

    tf.reduce_max(x) ==> [[5]]

    tf.reduce_max(x, 0, True) ==> [[4, 5, 2]]

    tf.reduce_max(x, 1, True) ==> [[3], [5]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值