美文网首页
tensorflow: 在开始前对tensor做一些处理

tensorflow: 在开始前对tensor做一些处理

作者: 世界上的一道风 | 来源:发表于2018-10-19 12:05 被阅读0次

    tf.boolean_mask

    这个操作可以用于留下指定的元素,类似于numpy的操作。

    import numpy as np
    tensor = tf.range(4)
    mask = np.array([True, False, True, False])
    bool_mask = tf.boolean_mask(tensor, mask)
    print sess.run(bool_mask)
    [0 2]
    

    tf.greater

    首先张量x和张量y的尺寸要相同,输出的tf.greater(x, y)也是一个和x,y尺寸相同的张量。如果x的某个元素比y中对应位置的元素大,则tf.greater(x, y)对应位置返回True,否则返回False。

    import tensorflow as tf 
    
    x = tf.Variable([[1,2,3], [6,7,8], [11,12,13]])
    y = tf.Variable([[0,1,2], [5,6,7], [10,11,12]])
    
    x1 = tf.Variable([[1,2,3], [6,7,8], [11,12,13]])
    y1 = tf.Variable([[10,1,2], [15,6,7], [10,21,12]])
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print(sess.run(tf.greater(x, y)))
        print(sess.run(tf.greater(x1, y1)))
    
    
    
    [[ True  True  True]
     [ True  True  True]
     [ True  True  True]]
    [[False  True  True]
     [False  True  True]
     [ True False  True]]
    
    

    tf.py_func

    py_func(
        func,
        inp,
        Tout,
        stateful=True,
        name=None
    ) 
    

    参数:
    func: 一个 Python 函数, 它接受 NumPy 数组作为输入和输出,并且数组的类型和大小必须和输入和输出用来衔接的 Tensor 大小和数据类型相匹配.
    inp: 输入的 Tensor 列表.
    Tout: 输出 Tensor 数据类型的列表或元祖.
    stateful: 状态,布尔值.
    name: 节点 OP 的名称.

    i = tf.constant([[0,1,2,3,4],
                    [9,8,0,3,0]])
    a  = tf.cast(i,tf.bool)
    b = tf.gather(i,1)
    c = tf.not_equal(b,0)
    neg_c = tf.logical_not(c)
    indices = tf.where(c)
    neg_indices = tf.where(neg_c)
    def choose(x):
        return np.random.choice(np.ravel(x))
    d = tf.py_func(choose,[indices],tf.int64)
    with tf.Session() as sess:
        print(sess.run(a))
        print(sess.run(b))
        print(sess.run(c))
        print("neg_c:",sess.run(neg_c))
        print("indices:",sess.run(indices))
        print("neg_indices:",sess.run(neg_indices))
        print("....",sess.run(d))
    
    
    
    [[False  True  True  True  True]
     [ True  True False  True False]]
    [9 8 0 3 0]
    [ True  True False  True False]
    neg_c: [False False  True False  True]
    indices: [[0]
     [1]
     [3]]
    neg_indices: [[2]
     [4]]
    .... 1
    
    

    tf.cond

    tf.cond(pred, true_fn=None, false_fn=None, strict=False, name=None, fn1=None, fn2=None)
    

    Return true_fn() if the predicate pred is true else false_fn()

    import tensorflow as tf
    
    a = tf.placeholder(tf.bool)  #placeholder for a single boolean value
    b = tf.cond(tf.equal(a, tf.constant(True)), lambda: tf.constant(10), lambda: tf.constant(0))
    sess = tf.InteractiveSession()
    res = sess.run(b, feed_dict = {a: True})
    sess.close()
    print(res)
    
    10
    

    tf.while_loop

    tf.while_loop(
    cond,
    body,
    loop_vars,
    shape_invariants=None,
    parallel_iterations=10,
    back_prop=True,
    swap_memory=False,
    name=None,
    maximum_iterations=None,
    return_same_structure=False
    )
    

    作用:Repeat body while the condition cond is true

    注意的是:loop_vars 是一个传递进去condbodytuple, namedtuple or list of tensors . condbody同时接受 both与 loop_vars一样多的参数。

    例子:

    def body(x):
        a = tf.random_uniform(shape=[2, 2], dtype=tf.int32, maxval=100)
        b = tf.constant(np.array([[1, 2], [3, 4]]), dtype=tf.int32)
        c = a + b
        return tf.nn.relu(x + c)
    def condition(x):
        return tf.reduce_sum(x) < 100x = tf.Variable(tf.constant(0, shape=[2, 2]))with tf.Session():
        tf.initialize_all_variables().run()
        result = tf.while_loop(condition, body, [x])
        print(result.eval())
    

    相关文章

      网友评论

          本文标题:tensorflow: 在开始前对tensor做一些处理

          本文链接:https://www.haomeiwen.com/subject/zssyzftx.html