tf.slice(inputs,begin,size,name=”)
import tensorflow as tf
import numpy as np
x=[[1,2,3],[4,5,6]]
y=np.arange(24).reshape([2,3,4])
sess=tf.Session()
begin_x=[1,0]
#第一个1,决定了从x的第二行[4,5,6]开始,第二个0,决定了从[4,5,6] 中的4开始抽取
size_x=[1,2]
# 第一个1决定了,从第二行以起始位置抽取1行,也就是只抽取[4,5,6] 这一行,在这一行中从4开始抽取2个元素
out=tf.slice(x,begin_x,size_x)
print sess.run(out) # 结果:[[4 5]]
begin_y=[1,0,0]
size_y=[1,2,3]
out=tf.slice(y,begin_y,size_y)
print sess.run(out) # 结果:[[[12 13 14] [16 17 18]]]
tf.concat
tf.concat(concat_dim, values, name=’concat’)
如果concat_dim是0,那么在某一个shape的第一个维度上连,对应到实际,就是叠放到列上
[python]
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(0, [t1, t2]) == > [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
如果concat_dim是1,那么在某一个shape的第二个维度上连
[python]
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat(1, [t1, t2]) ==> [[1, 2, 3, 7, 8, 9], [4, 5, 6, 10, 11, 12
tf.split
tf.split(split_dim, num_split, value, name=’split’)
着某一维度将tensor分离为num_split tensors
# ‘value’ is a tensor with shape [5, 30]
# Split ‘value’ into 3 tensors along dimension 1
split0, split1, split2 = tf.split(1, 3, value)
tf.shape(split0) ==> [5, 10]
tf.equal(A, B)
对比这两个矩阵或者向量的相等的元素,如果是相等的那就返回True,反正返回False
tf.gradient
tensorflow中有一个计算梯度的函数tf.gradients(ys, xs),要注意的是,xs中的x必须要与ys相关,不相关的话,会报错。
代码中定义了两个变量w1, w2, 但res只与w1相关
import tensorflow as tf
w1 = tf.Variable([[1,2]])
w2 = tf.Variable([[3,4]])
res = tf.matmul(w1, [[2],[1]])
grads = tf.gradients(res,[w1,w2])
with tf.Session() as sess:
tf.global_variables_initializer().run()
re = sess.run(grads)
print(re)
错误信息
TypeError: Fetch argument None has invalid type
tf.select(condition, t, e, name=None)
https://www.tensorflow.org/versions/r0.10/api_docs/python/control_flow_ops/comparison_operators#select
Selects elements from t or e, depending on condition.
a=2
b=3
sess.run(tf.select(True,a,b))
#outuput 2
sess.run(tf.select(False,a,b))
#output 3
tf.select
tf.select(condition,a,b)
a :一个张量tensor,shape与condition一致,类型一般为float32, float64, int32, int64.
b :一个张量tensor,类型和shape与a一致。
举例:
import tensorflow as tf
sess=tf.Session()
condition=[[True,False],[True,False]]
a=[[1,2],[3,4]]
b=[[5,6],[7,8]]
c=tf.select(condition,a,b)
print(sess.run(c))
输出:
[[1,6],[3,8]]
如果把condition改成[[True,True],[True,False]]
输出变为:
[[1,2],[3,8]]
tf.squeeze
tf.squeeze(input, squeeze_dims=None, name=None)
Removes dimensions of size 1 from the shape of a tensor.
从tensor中删除所有大小是1的维度
Given a tensor input, this operation returns a tensor of the same type with all dimensions of size 1 removed. If you don’t want to remove all size 1 dimensions, you can remove specific size 1 dimensions by specifying squeeze_dims.
如果不想删除所有大小是1的维度,可以通过squeeze_dims指定。
For example:
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t)) ==> [2, 3]
Or, to remove specific size 1 dimensions:
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1]
shape(squeeze(t, [2, 4])) ==> [1, 2, 3, 1]
本文介绍了TensorFlow中的一些基本操作函数,包括tf.slice用于按指定范围取子集,tf.concat和tf.split分别用于合并和分割张量,tf.equal用于比较张量元素的相等性,tf.gradient用于计算梯度,tf.select根据条件选择张量元素,以及tf.squeeze用于去除张量中大小为1的维度。这些函数在构建和操作TensorFlow模型时非常常用。
401

被折叠的 条评论
为什么被折叠?



