tf2杂记
tf.shape()
关于python函数中shape的解释:
shape包含在numpy库,是矩阵(ndarray)的属性,可以获取矩阵的形状(例如二维数组的行列),获取的结果是一个元组,因此相关代码如下:
import numpy as np
x = np.array([[1,2,3,4,5],[6,7,8,9,10],[10,9,8,7,6],[5,4,3]])
#输出数组的行和列数
print x.shape #结果: (4, 5)
#只输出行数
print x.shape[0] #结果: 4
#只输出列数
print x.shape[1] #结果: 5
————————————————
原文链接:https://blog.csdn.net/weixin_44804700/article/details/98874358
tf.split()
原来的维数不变,只是将要分割的维数的数据分割提取出来
tf.split(
value,
num_or_size_splits,
axis=0,
num=None,
name='split'
)
这个函数是用来切割张量的。输入切割的张量和参数,返回切割的结果。
value传入的就是需要切割的张量。
这个函数有两种切割的方式:
以三个维度的张量为例,比如说一个20 * 30 * 40的张量my_tensor,就如同一个长20厘米宽30厘米高40厘米的蛋糕,每立方厘米都是一个分量。
有两种切割方式:
- 如果num_or_size_splits传入的是一个整数,这个整数代表这个张量最后会被切成几个小张量。此时,传入
axis
的数值就代表切割哪个维度(从0开始计数)。调用tf.split(my_tensor, 2,0)返回两个10 * 30 * 40的小张量。 - 如果num_or_size_splits传入的是一个向量,那么向量有几个分量就分成几份,切割的维度还是由axis决定。比如调用tf.split(my_tensor, [10, 5, 25], 2),则返回三个张量分别大小为 20 * 30 * 10、20 * 30 * 5、20 * 30 * 25。很显然,传入的这个向量各个分量加和必须等于axis所指示原张量维度的大小 (10 + 5 + 25 = 40)。
————————————————
原文链接:https://blog.csdn.net/wangxuecheng666/article/details/112118065
ZeroPadding2D
原文连接:https://www.cnblogs.com/LGJC1314/p/13403811.html
init(
padding=(1, 1),
data_format=None,
**kwargs
)
inp = np.random.randint(1,9,(2,2))
print(inp)
padding 为一个整数n 上下左右各补n个0
x = ZeroPadding2D(1)(input)
out = tf.keras.models.Model(input, x)(inp)
tf.print(tf.squeeze(out))
padding 为一维元祖(n, m)上下补n个0 左右补m个
x = ZeroPadding2D((1,0))(input)
out = tf.keras.models.Model(input, x)(inp)
tf.print(tf.squeeze(out))
padding 为二维元祖((n, m), (x, y))上补n个0 下补m个 左补x个0 右补y个0
x = ZeroPadding2D(((1,0), (1, 0)))(input)
out = tf.keras.models.Model(input, x)(inp)
tf.print(tf.squeeze(out))
SAME与VALID区别
原文链接:TensorFlow学习--SAME与VALID区别
卷积后大小计算
原文链接:超详细的卷积后大小的计算公式
tf.concat()
t1 = [[1, 2, 3], [4, 5, 6]]
t2 = [[7, 8, 9], [10, 11, 12]]
tf.concat([t1, t2], 0)
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]], dtype=int32)>
tf.concat([t1, t2], 1)
<tf.Tensor: shape=(2, 6), dtype=int32, numpy=
array([[ 1, 2, 3, 7, 8, 9],
[ 4, 5, 6, 10, 11, 12]], dtype=int32)>
t1 = [[[1, 2], [2, 3]], [[4, 4], [5, 3]]]
t2 = [[[7, 4], [8, 4]], [[2, 10], [15, 11]]]
tf.concat([t1, t2], -1)
<tf.Tensor: shape=(2, 2, 4), dtype=int32, numpy=
array([[[ 1, 2, 7, 4],
[ 2, 3, 8, 4]],
[[ 4, 4, 2, 10],
[ 5, 3, 15, 11]]], dtype=int32)>
原文链接:tf.concat()详解
tf.tile()
a = tf.constant([[1,2,3],[4,5,6]], tf.int32)
b = tf.constant([1,2], tf.int32)
tf.tile(a, b)
<tf.Tensor: shape=(2, 6), dtype=int32, numpy=
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]], dtype=int32)>
c = tf.constant([2,1], tf.int32)
tf.tile(a, c)
<tf.Tensor: shape=(4, 3), dtype=int32, numpy=
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]], dtype=int32)>
d = tf.constant([2,2], tf.int32)
tf.tile(a, d)
<tf.Tensor: shape=(4, 6), dtype=int32, numpy=
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6],
[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]], dtype=int32)>
原文链接:直观的理解tensorflow中的tf.tile()函数
tf.range()
原文链接:https://www.cnblogs.com/cvtoEyes/p/9002843.html
tf.repeat()
repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=0)
<tf.Tensor: shape=(5, 2), dtype=int32, numpy=
array([[1, 2],
[1, 2],
[3, 4],
[3, 4],
[3, 4]], dtype=int32)>
repeat([[1, 2], [3, 4]], repeats=[2, 3], axis=1)
<tf.Tensor: shape=(2, 5), dtype=int32, numpy=
array([[1, 1, 2, 2, 2],
[3, 3, 4, 4, 4]], dtype=int32)>
repeat(3, repeats=4)
<tf.Tensor: shape=(4,), dtype=int32, numpy=array([3, 3, 3, 3], dtype=int32)>
repeat([[1,2], [3,4]], repeats=2)
<tf.Tensor: shape=(8,), dtype=int32,
numpy=array([1, 1, 2, 2, 3, 3, 4, 4], dtype=int32)>
原文链接:https://blog.csdn.net/weixin_43834407/article/details/106540816
tf.squeeze()
tf.squeeze()函数用于从张量形状中移除大小为1的维度
‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t)) # [2, 3]
‘t’ is a tensor of shape [1, 2, 1, 3, 1, 1]
tf.shape(tf.squeeze(t, [2, 4])) # [1, 2, 3, 1]
tf.reduce_max()函数的用法详解
求最大值
import tensorflow as tf
import numpy as np
a=np.array([[1, 2],
[5, 3],
[2, 6]])
b = tf.Variable(a)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(b))
print('************')
# 对于二维矩阵,axis=0轴可以理解为行增长方向(向下)即按列求最值,axis=1轴可以理解为列增长方向(向右)按列行求最值
print(sess.run(tf.reduce_max(b, axis=1, keepdims=False))) # keepdims=False,axis=1被消减,不保持原状,本来shape为(3,1),后来变成(1,3)了
print('************')
print(sess.run(tf.reduce_max(b, axis=1, keepdims=True)))
print('************')
print(sess.run(tf.reduce_max(b, axis=0, keepdims=True)))
https://blog.csdn.net/hgnuxc_1993/article/details/116940059
tf.gather
从params的axis维根据indices的参数值获取切片