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
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
可同时处理nan
与infty
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。