Tensorflow Code Snippets

Tensor创建与修改

placeholder

https://www.tensorflow.org/api_docs/python/tf/placeholder

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

元素访问

按mask访问特定元素

import numpy as np
import tensorflow as tf;

a = tf.placeholder(dtype = tf.float32)
mask = a > 1;
mask.set_shape([None, None]); # the shape of mask must not be none.
b = tf.placeholder(dtype = tf.float32)
c = tf.boolean_mask(b, mask);
with tf.Session() as sess:
    a_val = np.ones((3,))
    a_val[1] = 3;
    b_val = a_val
    c_val = sess.run(c, feed_dict = {a:a_val, b:b_val})
    print c_val

Shape

升维

input = tf.expand_dims(
    input,
    axis=None,
    name=None
)

set shape

一个常见的场景是经过某个操作后, tensor的shape信息都变成了None,但是操作者(就是我们)知道它哪个dim的shape信息是确定的,例如下面的图片shape,第三个维度能确定它会保持为3不变,这里将这个信息设置进行,可以为后面的编程省去很多麻烦。

# Crop the image to the specified bounding box.
cropped_image = tf.slice(image, bbox_begin, bbox_size)
# Restore the shape since the dynamic slice loses 3rd dimension.
cropped_image.set_shape([None, None, 3])

与变量名相关

tf.identity

import tensorflow as tf
a = tf.Variable(1, name = 'a')
b = tf.Variable(2, name = 'b')
with tf.name_scope('ss'):
    c = a + b
    c = tf.identity(c, 'c')
    #c = tf.add(a, b, name = 'c')

print c.name

ss/c:0

类型转换

fnmask = tf.cast(nmask, dtype)

基本运算

求和

两个变量相加

import tensorflow as tf

a = tf.Variable(1, name = 'a')
b = tf.Variable(2, name = 'b')

c = tf.add(a, b, name = 'c')
c = a + b

所有元素加和

scalar = tf.reduce_sum(tensor)

max/min

一个tensor中最大/小元素

import numpy as np
import tensorflow as tf;

a = tf.placeholder(dtype = tf.float32)
b = tf.reduce_max(a) # reduce_min

with tf.Session() as sess:
    a_val = np.ones((3, 3))
    a_val[0, 2] = 100
    b_val = sess.run(b, feed_dict = {a:a_val})
    print b_val

100.0

两个tensor按元素取最大/小

import numpy as np
import tensorflow as tf;

a = tf.placeholder(dtype = tf.float32)
b = tf.placeholder(dtype = tf.float32)
c = tf.maximum(a, b)# minimum
with tf.Session() as sess:
    a_val = np.ones((3, 3))
    a_val[0, 2] = 100
    b_val = a_val
    c_val = sess.run(c, feed_dict = {a:a_val, b:b_val})
    print c_val

[[ 1. 1. 100.]
[ 1. 1. 1.]
[ 1. 1. 1.]]

Bool

and

mask = tf.logical_and(mask1, mask2);

Loss

Weighted Loss

loss_pos = tf.losses.compute_weighted_loss(
    losses,
    weights=1.0,
    scope=None,
    loss_collection=tf.GraphKeys.LOSSES
)

会使用weights里的非0个数来计算平均值。

Optimization

Gradient Clip

https://stackoverflow.com/questions/36498127/how-to-effectively-apply-gradient-clipping-in-tensor-flow

optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
gvs = optimizer.compute_gradients(cost)
capped_gvs = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gvs]
train_op = optimizer.apply_gradients(capped_gvs)

但这种clip不能处理nan

import numpy as np
import tensorflow as tf
val = float('nan')
a = np.ones((3, 3)) * 0.5
a[0,0] = 2
a[0,1] = val
tf_a = tf.Variable(a)
tf_b = tf.clip_by_value(a, -1, 1)
with tf.device('/cpu:0'):
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print sess.run([tf_a, tf_b])
[array([[ 2. ,  nan,  0.5],
       [ 0.5,  0.5,  0.5],
       [ 0.5,  0.5,  0.5]]), 
       array([[ 1. ,  nan,  0.5],
       [ 0.5,  0.5,  0.5],
       [ 0.5,  0.5,  0.5]])]

使用tf.is_finite可同时处理naninfty

import numpy as np
import tensorflow as tf
val = float('nan')
a = np.ones((3, 3)) * 0.5
a[0,0] = np.infty
a[0,1] = val
tf_a = tf.Variable(a)
#tf_b = tf.clip_by_value(a, -1, 1)
mask = tf.is_finite(tf_a)
tf_c = tf.where(mask, tf_a, tf.zeros_like(a))
with tf.device('/cpu:0'):
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        print sess.run([tf_a, tf_c])
[array([[ inf,  nan,  0.5],
       [ 0.5,  0.5,  0.5],
       [ 0.5,  0.5,  0.5]]), 
 array([[ 0. ,  0. ,  0.5],
       [ 0.5,  0.5,  0.5],
       [ 0.5,  0.5,  0.5]])]

py_func

https://www.tensorflow.org/api_docs/python/tf/py_func

import numpy as np
import tensorflow as tf

def add(a, b):
    return a + b;


def tf_add(a, b):
    return tf.py_func(add, [a, b], tf.float32);


ta = tf.placeholder(dtype = tf.float32)
tb = tf.placeholder(dtype = tf.float32)
tc = tf_add(ta, tb);
with tf.Session() as sess:
    a_val = np.ones((2, 2))
    b_val = a_val * 2;
    print sess.run([tc], feed_dict = {ta: a_val, tb: b_val})

py_func接收三个参数:函数对象, 要传入的tensor, 返回值的tf类型。 类型参数如果是个list, 例如[tf.float32], 那么返回值也会是个list。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值