文章目录
- 1.random
- 2.for xxx in xxx if xxx
- 3.tf.ones_like()和tf.zeros_like()
- 4.tf.concat()
- 5.tf.gather(params,indices,axis=0 )
- 6.tf.tile()
- 7.x.get_shape().as_list()
- 8.tf.expand_dims(input, dim, name=None)
- 9.tf.add( x,y, name=None)
- 10.tf.less(x, y,name=None)
- 11.tf.cast()
- 12.tf.reduce_any和all
- 13.tf.math.top_k 和tf.nn.top_k(input, k, name=None)
- 14.tf.batch_gather
1.random
random.randint(a,b) [a,b]
np.random.randint(a,b) [a,b)
2.for xxx in xxx if xxx
links.extend(link for link in get_links(html) if re.match(link_regex, link))
# 上面的语句分开写,如下
# 但下面需要使用links.append,如果依然使用links.extend会出现错误的结果
for link in get_links(html):
if re.match(link_regex, link):
links.append(link)
3.tf.ones_like()和tf.zeros_like()
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-wy5d2fsl.html
给定一个tensor(tensor 参数),该操作返回一个具有和给定tensor相同形状(shape)和相同数据类型(dtype),但是所有的元素都被设置为1或者0的tensor。也可以为返回的tensor指定一个新的数据类型。
import tensorflow as tf
import numpy as np
#tf.convert_to_tensor将python的数据类型转换成TensorFlow可用的tensor数据类型
a = tf.convert_to_tensor(np.random.random([2, 3, 4]), dtype=tf.float32)## 生成一个shape=[2, 3, 4]的tensor,
b = tf.ones_like(a, dtype=tf.float32, name='ones_like')# ones_like
c = tf.zeros_like(a, dtype=tf.float32, name='zeros_like')# zeros_like
print(b)
print('##########################')
print(c)
with tf.Session() as sess:
b_, c_ = sess.run([b, c])
print("b's shape: ", b_.shape)
print("c's shape: ", c_.shape)
print('##########################')
print(b_)
print('##########################')
print(c_)
运行结果
Tensor("ones_like:0", shape=(2, 3, 4), dtype=float32)
##########################
Tensor("zeros_like:0", shape=(2, 3, 4), dtype=float32)
2020-03-24 17:22:08.210991: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
b's shape: (2, 3, 4)
c's shape: (2, 3, 4)
##########################
[[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]]
##########################
[[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
Process finished with exit code 0
4.tf.concat()
按维度拼接
import tensorflow as tf
import numpy as np
a = tf.convert_to_tensor(np.random.randint(1,9,[2, 3, 4]), dtype=tf.float32)
b=tf.convert_to_tensor(np.random.randint(1,9,[2, 3, 4]), dtype=tf.float32)
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
with tf.Session() as sess:
print(sess.run(tf.concat([t1, t2], 0)))
print(sess.run(tf.concat([t1, t2], 1)))
print('张量a')
print(sess.run(a))
print('张量b')
print(sess.run(b))
print('一维拼接')
print(sess.run(tf.concat([a, b], 0)))
print('二维拼接')
print(sess.run(tf.concat([a, b], 1)))
print('三维拼接')
print(sess.run(tf.concat([a, b], 2)))
运行结果
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
张量a
[[[6. 3. 5. 2.]
[8. 7. 7. 5.]
[2. 4. 2. 5.]]
[[3. 7. 2. 4.]
[6. 5. 7. 6.]
[3. 8. 8. 4.]]]
张量b
[[[8. 5. 7. 1.]
[6. 8. 5. 4.]
[1. 1. 1. 7.]]
[[3. 3. 6. 1.]
[8. 2. 5. 1.]
[6. 8. 6. 6.]]]
一维拼接
[[[6. 3. 5. 2.]
[8. 7. 7. 5.]
[2. 4. 2. 5.]]
[[3. 7. 2. 4.]
[6. 5. 7. 6.]
[3. 8. 8. 4.]]
[[8. 5. 7. 1.]
[6. 8. 5. 4.]
[1. 1. 1. 7.]]
[[3. 3. 6. 1.]
[8. 2. 5. 1.]
[6. 8. 6. 6.]]]
二维拼接
[[[6. 3. 5. 2.]
[8. 7. 7. 5.]
[2. 4. 2. 5.]
[8. 5. 7. 1.]
[6. 8. 5. 4.]
[1. 1. 1. 7.]]
[[3. 7. 2. 4.]
[6. 5. 7. 6.]
[3. 8. 8. 4.]
[3. 3. 6. 1.]
[8. 2. 5. 1.]
[6. 8. 6. 6.]]]
三维拼接
[[[6. 3. 5. 2. 8. 5. 7. 1.]
[8. 7. 7. 5. 6. 8. 5. 4.]
[2. 4. 2. 5. 1. 1. 1. 7.]]
[[3. 7. 2. 4. 3. 3. 6. 1.]
[6. 5. 7. 6. 8. 2. 5. 1.]
[3. 8. 8. 4. 6. 8. 6. 6.]]]
5.tf.gather(params,indices,axis=0 )
从params的axis维根据indices的参数值获取切片
import tensorflow as tf
import numpy as np
print("先测试一维张量")
t = np.random.randint(1, 10, 5)
g1 = tf.gather(t, [2, 1, 4])
sess = tf.Session()
print(t)
print(sess.run(g1))
print("再测试二维张量")
t = np.random.randint(1, 10, [4, 5])
t1 = np.random.randint(0, 4, [2, 3])
g2 = tf.gather(t, t1, axis=0)
g3 = tf.gather(t, t1, axis=1)
print('t为 ')
print(t)
print('t1为 ')
print(t1)
print('g2为 ')
print(sess.run(g2))
print('g3为 ')
print(sess.run(g3))
运行结果
先测试一维张量
[7 8 4 3 8]
[4 8 8]
再测试二维张量
t为 #shape(4,5)
[[7 1 4 9 6]
[1 9 3 1 9]
[4 9 1 4 3]
[6 9 1 9 3]]
t1为 #主要看t1的shape(2,3)
[[1 3 0]
[1 3 0]]
g2为 #shape(2,3,5)
[[[1 9 3 1 9]
[6 9 1 9 3]
[7 1 4 9 6]]
[[1 9 3 1 9]
[6 9 1 9 3]
[7 1 4 9 6]]]
g3为 #shape(4,2,3)
[[[1 9 7]
[1 9 7]]
[[9 1 1]
[9 1 1]]
[[9 4 4]
[9 4 4]]
[[9 9 6]
[9 9 6]]]
6.tf.tile()
在一个维度上复制n遍,不复制的维度为1
https://blog.csdn.net/xwd18280820053/article/details/72867818
7.x.get_shape().as_list()
把shape这个元组变成列表
https://blog.csdn.net/abc13526222160/article/details/85135517
8.tf.expand_dims(input, dim, name=None)
维度增加
https://blog.csdn.net/jasonzzj/article/details/60811035
# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]
# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]
9.tf.add( x,y, name=None)
矩阵相加
https://blog.csdn.net/xc_zhou/article/details/85415202
10.tf.less(x, y,name=None)
返回两个张量各元素比较(x<y)得到的真假值组成的张量
https://blog.csdn.net/stt12345678/article/details/83987675
11.tf.cast()
tf.cast()函数的作用是执行 tensorflow 中张量数据类型转换
https://blog.csdn.net/dcrmg/article/details/79747814
12.tf.reduce_any和all
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-d2y32hgr.html
与numpy相同
np.array.any()是或操作,任意一个元素为True,输出为True。
np.array.all()是与操作,所有元素为True,输出为True。
13.tf.math.top_k 和tf.nn.top_k(input, k, name=None)
返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。
https://www.w3cschool.cn/tensorflow_python/tensorflow_python-d2y32hgr.html
import tensorflow as tf
import numpy as np
input = tf.constant(np.random.randint(1,9,[3, 4]))
k = 2
output = tf.nn.top_k(input, k)
with tf.Session() as sess:
print(sess.run(input))
print(sess.run(output))
运行结果
[[8 4 4 7]
[4 3 6 7]
[4 3 4 6]]
TopKV2(values=array([[8, 7],
[7, 6],
[6, 4]]), indices=array([[0, 3],
[3, 2],
[3, 0]]))
14.tf.batch_gather
批引用
https://blog.csdn.net/zby1001/article/details/86551667
import tensorflow as tf
tensor_a = tf.Variable([[1,2,3],[4,5,6],[7,8,9]])
tensor_b = tf.Variable([[0],[1],[2]],dtype=tf.int32)
tensor_c = tf.Variable([[0],[0],[0]],dtype=tf.int32)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(tf.batch_gather(tensor_a,tensor_b)))
print(sess.run(tf.batch_gather(tensor_a,tensor_c)))
运行结果
[[1]
[5]
[9]]
[[1]
[4]
[7]]