TensorFlow代码里常见的函数
tf.gather()用法详解
tf.gather()用法详解_sereasuesue的博客-CSDN博客_tf.gather
tf.expand_dims用法详解
tf.expand_dims用法详解_sereasuesue的博客-CSDN博客_tf.expand_dims
TensorFlow的reduce_sum()函数
TensorFlow的reduce_sum()函数_Sual-CSDN博客
tf.transpose函数的用法讲解(图解)
tf.transpose函数的用法讲解(图解)_sereasuesue的博客-CSDN博客_tf.transpose函数
tf.nn.embedding_lookup函数用法
tf.nn.embedding_lookup(params, ids):params可以是张量也可以是数组等,id就是对应的索引,其他的参数不介绍。
ids只有一行:
#c = np.random.random([10, 1]) # 随机生成一个10*1的数组
#b = tf.nn.embedding_lookup(c, [1, 3])#查找数组中的序号为1和3的
p=tf.Variable(tf.random_normal([10,1]))#生成10*1的张量
b = tf.nn.embedding_lookup(p, [1, 3])#查找张量中的序号为1和3的
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(b))
#print(c)
print(sess.run(p))
print(p)
print(type(p))
输出:
[[0.15791859]
[0.6468804 ]]
[[-0.2737084 ]
[ 0.15791859]
[-0.01315552]
[ 0.6468804 ]
[-1.4090979 ]
[ 2.1583703 ]
[ 1.4137447 ]
[ 0.20688428]
[-0.32815856]
[-1.0601649 ]]
<tf.Variable 'Variable:0' shape=(10, 1) dtype=float32_ref>
<class 'tensorflow.python.ops.variables.Variable'>
分析:输出为张量的第一和第三个元素。
如果ids是多行:
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.nn.embedding_lookup函数的用法 - 每天坚持一点点 - 博客园 (cnblogs.com)
TensorFlow - tf.reshape 函数
reshape(
tensor,
shape,
name=None
)
重塑张量.
给定tensor,这个操作返回一个张量,它与带有形状shape的tensor具有相同的值.
如果shape的一个分量是特殊值-1,则计算该维度的大小,以使总大小保持不变.特别地情况为,一个[-1]维的shape变平成1维.至多能有一个shape的分量可以是-1.
如果shape是1-D或更高,则操作返回形状为shape的张量,其填充为tensor的值.在这种情况下,隐含的shape元素数量必须与tensor元素数量相同.
例如:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]]
# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7
参数:
- tensor:一个Tensor.
- shape:一个Tensor;必须是以下类型之一:int32,int64;用于定义输出张量的形状.
- name:操作的名称(可选).
返回值:
该操作返回一个Tensor.与tensor具有相同的类型.
tf.cond函数的使用
z = tf.multiply( a , b )
result = tf.cond( X < Y, lambda:tf.add( X ,Z), lambda: tf.square( Y ))
如果x < y,tf.add 将执行并且 tf.square 操作不执行.因为 z 是需要的至少一个分支的条件,因为 tf.multiply 操作始终无条件地执行.虽然这种行为与 TensorFlow 的数据流模型是一致的,但有时候,有些用户会期待一种较为惰性的语义.
请注意,cond 调用 true_fn 和 false_fn 一次(在调用 cond 的内部,而不是在 Session.run()期间 ).cond 将在 true_fn 和 false_fn 期间创建的图形片段一起使用一些附加的图形节点来确保右分支根据 pred 的值执行.
tf.cond 支持嵌套结构在 tensorflow.python.util.nest 中的实现.true_fn 和 false_fn 必须返回列表,元组和/或命名元组的相同(可能嵌套的)值结构.
例:
x = tf.constant(2 )
y = tf.constant(5 )
def f1 (): return tf .multiply( x , 17 )
def f2 (): return tf .add ( y , 23 )
r = tf .cond( tf.less( X ,y ), f1 , f2 )
#r 设置为f1().
#f2 中的操作(例如,tf.add)不执行.
tf.matmul函数:
matmul(
a,
b,
transpose_a=False,
transpose_b=False,
adjoint_a=False,
adjoint_b=False,
a_is_sparse=False,
b_is_sparse=False,
name=None
)
将矩阵 a 乘以矩阵 b,生成a * b
输入必须在任何转换之后是 rank> = 2 的张量,其中内部 2 维度指定有效的矩阵乘法参数,并且任何其他外部维度匹配.
两个矩阵必须是相同类型.支持的类型有:float16,float32,float64,int32,complex64,complex128.
通过将相应的标志之一设置为 True,矩阵可以被转置或 adjointed(共轭和转置).默认情况下,这些都是 False.
如果一个或两个矩阵包含很多的零,则可以通过将相应的 a_is_sparse 或 b_is_sparse 标志设置为 True 来使用更有效的乘法算法,默认为 false.这个优化仅适用于具有数据类型为bfloat16 或 float32 的纯矩阵(rank 为2的张量).
例如:
# 2-D tensor `a`
a = tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3]) => [[1. 2. 3.]
[4. 5. 6.]]
# 2-D tensor `b`
b = tf.constant([7, 8, 9, 10, 11, 12], shape=[3, 2]) => [[7. 8.]
[9. 10.]
[11. 12.]]
c = tf.matmul(a, b) => [[58 64]
[139 154]]
# 3-D tensor `a`
a = tf.constant(np.arange(1, 13, dtype=np.int32),
shape=[2, 2, 3]) => [[[ 1. 2. 3.]
[ 4. 5. 6.]],
[[ 7. 8. 9.]
[10. 11. 12.]]]
# 3-D tensor `b`
b = tf.constant(np.arange(13, 25, dtype=np.int32),
shape=[2, 3, 2]) => [[[13. 14.]
[15. 16.]
[17. 18.]],
[[19. 20.]
[21. 22.]
[23. 24.]]]
c = tf.matmul(a, b) => [[[ 94 100]
[229 244]],
[[508 532]
[697 730]]]
# Since python >= 3.5 the @ operator is supported (see PEP 465).
# In TensorFlow, it simply calls the `tf.matmul()` function, so the
# following lines are equivalent:
d = a @ b @ [[10.], [11.]]
d = tf.matmul(tf.matmul(a, b), [[10.], [11.]])
参数:
- a:类型为 float16,float32,float64,int32,complex64,complex128 和 rank > 1的张量.
- b:与 a 具有相同类型和 rank.
- transpose_a:如果 True,a 在乘法之前转置.
- transpose_b:如果 True,b 在乘法之前转置.
- adjoint_a:如果 True,a 在乘法之前共轭和转置.
- adjoint_b:如果 True,b 在乘法之前共轭和转置.
- a_is_sparse:如果 True,a 被视为稀疏矩阵.
- b_is_sparse:如果 True,b 被视为稀疏矩阵.
- name:操作名称(可选).
返回:
该函数返回与 a 和 b 具有相同类型的张量,其中每个最内矩阵是 a 和 b 中对应矩阵的乘积,例如,如果所有转置或伴随的属性为 False:
output[..., i, j] = sum_k (a[..., i, k] * b[..., k, j]), for all indices i, j
Note:这是矩阵乘积,而不是元素的乘积.
可能引发的异常:
- ValueError:如果 transpose_a 和 adjoint_a,或者 transpose_b 和 adjoint_b 都设置为 True.