目录

tf.dynamic_partition

  • 矩阵拆分

tf.case

import tensorflow as tf
x = tf.constant(7)
y =  tf.constant(27)
z = tf.constant(21)
def f1(): 
    return tf.constant(17)
def f2(): 
    return tf.constant(23)
def f3(): 
    return tf.constant(-1)
r = tf.case({tf.less(x, y): f1, tf.greater(x, z): f2},default=f3, exclusive=True);
参考:https://blog.csdn.net/AI_LX/article/details/89465395

tf.shape(a)

获取张量a各个方向上的维度。

tf.unstack(a,axis=)

将张量a根据axis从n+1维分解到n维

tf.stack([a,b],axis=)

将张量a,b根据axis合并

MutableHashTable

可以根据key,将张量a中的key替换成value的表,不会增加张量a的维度。

tf.placeholder

函数

tf.placeholder(dtype, shape=None, name=None)

参数说明

dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
name:名称。

注意事项

此函数可以理解为形参,用于定义过程,在执行时需要赋具体的值。

示例

x = tf.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)

with tf.Session() as sess:
  print(sess.run(y))  # ERROR: 此处x还没有赋值.

  rand_array = np.random.rand(1024, 1024)
  print(sess.run(y, feed_dict={x: rand_array}))  # Will succeed.

tf.nn.dynamic_rnn

tensorflow 的dynamic_rnn方法,我们用一个小例子来说明其用法,假设你的RNN的输入input是[2,20,128],其中2是batch_size,20是文本最大长度,128是embedding_size,可以看出,有两个example,我们假设第二个文本长度只有13,剩下的7个是使用0-padding方法填充的。dynamic返回的是两个参数:outputs,last_states,其中outputs是[2,20,128],也就是每一个迭代隐状态的输出,last_states是由(c,h)组成的tuple,均为[batch,128]。

到这里并没有什么不同,但是dynamic有个参数:sequence_length,这个参数用来指定每个example的长度,比如上面的例子中,我们令 sequence_length为[20,13],表示第一个example有效长度为20,第二个example有效长度为13,当我们传入这个参数的时候,对于第二个example,TensorFlow对于13以后的padding就不计算了,其last_states将重复第13步的last_states直至第20步,而outputs中超过13步的结果将会被置零。

tf.reshape(tensor,shape,name=None)

函数

tf.reshape(tensor,shape,name=None)

说明

函数的作用是将tensor变换为参数shape形式,其中的shape为一个列表形式,特殊的是列表可以实现逆序的遍历,即list(-1).-1所代表的含义是我们不用亲自去指定这一维的大小,函数会自动进行计算,但是列表中只能存在一个-1。(如果存在多个-1,就是一个存在多解的方程) 。

tf.nn.rnn_cell.MultiRNNCell.zero_state

tf.nn.rnn_cell.MultiRNNCell.zero_state(batch_size, dtype)

tf.train.ExponentialMovingAverage

tf.nn.moments

http://blog.csdn.net/u014595019/article/details/52805444

tf.contrib.rnn.BasicLSTMCell

BasicLSTMCell 是最简单的一个LSTM类,没有实现clipping,projection layer,peep-hole等一些LSTM的高级变种,仅作为一个基本的basicline结构存在,如果要使用这些高级变种,需用class tf.contrib.rnn.LSTMCell这个类。

使用方式

lstm = rnn.BasicLSTMCell(num_units, forget_bias=1.0, state_is_tuple=True)

参数说明

  • num_units: int, The number of units in the LSTM cell.

  • forget_bias: float, The bias added to forget gates.

  • state_is_tuple: If True, accepted and returned states are 2-tuples of the c_state and m_state. If False, they are concatenated along the column axis. The latter behavior will soon be deprecated.

  • activation: Activation function of the inner states.

tf.nn.embedding_lookup

代码

# -*- coding= utf-8 -*-
import tensorflow as tf
import numpy as np

a = [[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]]
a = np.asarray(a)
idx1 = tf.Variable([0, 2, 3, 1], tf.int32)
idx2 = tf.Variable([[0, 2, 3, 1], [4, 0, 2, 2]], tf.int32)
out1 = tf.nn.embedding_lookup(a, idx1)
out2 = tf.nn.embedding_lookup(a, idx2)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print sess.run(out1)
    print out1
    print '=================='
    print sess.run(out2)
    print out2

输出

[[ 0.1  0.2  0.3]
 [ 2.1  2.2  2.3]
 [ 3.1  3.2  3.3]
 [ 1.1  1.2  1.3]]
Tensor("embedding_lookup:0", shape=(4, 3), dtype=float64)
==================
[[[ 0.1  0.2  0.3]
  [ 2.1  2.2  2.3]
  [ 3.1  3.2  3.3]
  [ 1.1  1.2  1.3]]

 [[ 4.1  4.2  4.3]
  [ 0.1  0.2  0.3]
  [ 2.1  2.2  2.3]
  [ 2.1  2.2  2.3]]]
Tensor("embedding_lookup_1:0", shape=(2, 4, 3), dtype=float64)

tf.ones_like

tf.where

将非0转为1,0转为0

        dense_ids = tf.sparse.to_dense(sparse_ids, default_value=0, validate_indices=False, name=None)
        zero = tf.zeros_like(dense_ids)
        one = tf.ones_like(dense_ids)
        zero_one_result = tf.where(tf.equal(dense_ids, 0), x=zero, y=one)

参数