tensorflow篇-深度学习常用函数解释

tf.image.resize_images

用途:对图像张量大小调整
完整函数参数: img_resized = tf.image.resize_images(image_data, [300, 300], method=0)

image_data:表示输入的图像张量
[300,300]:变形后的图像尺寸(H*W)
method:指定所进行的变化形式:
method=0:双线性插值
method=1:最近邻居法
method=2:双三次插值法
method=2:面积插值法
e.g:
img = cv2.imread(img_path) #224,224,3
im_resize = tf.image.resize_images(img, [300, 300], method=0) # 300,300,3
这种方式会导致部分信息丢失,目前其他的上采样方式比如有DUC(密集上采样卷积)以及DUGC(密集上采样分组卷积)

tf.split()

用途:用于对某个张量在某个维度上进行切割
完整函数参数:tf.split(value, num_or_size_splits, axis=0, num=None, name='split')
value:表示要分割的张量
num_or_size_splits:准备将张量切分为几份,
如果 num_or_size_splits 是一个整数,这就代表在第axis维度上将张量切分为 num_or_size_splits 个小的张量,
如果 num_or_size_splits 是一个一维张量,这就代表在第 axis 个维度上 根据此张量进行切割,
e.g :
split0, split1, split2 = tf.split(value, [4, 15, 11], 1) # value 是个shape为 [5, 30]的张量,注意到4+15+11 = 30
tf.shape(split0) #[5, 4]
tf.shape(split1) #[5, 15]
tf.shape(split2) #[5, 11]
axis:表示维度 0,1,2,3…
num:可选,用于指定无法从size_splits的形状推断出输出时的输出数量。
name:此运算的名称

tf.Variable()/tf.compat.v2.Variable

用途:用于声明变量
完整函数参数:tf.Variable(
initial_value=None, trainable=None, validate_shape=True, caching_device=None,
name=None, variable_def=None, dtype=None, import_scope=None, constraint=None,
synchronization=tf.VariableSynchronization.AUTO,
aggregation=tf.compat.v1.VariableAggregation.NONE, shape=None
)

initial_value:所有可以转换为张量的类型,是变量初始值
trainable:只有当trainable为 true 时才能对其使用Optimizer
collections:类型为 list 指定该变量的类型
在 Tensorflow 中,变量的定义与初始化是分开的,一开始,tf.Variable()得到的是张量,而张量并不是具体的值,而是计算过程
在 Tensorflow 中变量只有被定义为(Variable)变量后才是变量
e.g:
state = tf.Variable(0,name=‘counter’) #定义变量,名为 ‘counter’
one = tf.constant(1) #定义常量
new_value = tf.add(state,one) #变量+常量=变量
update = tf.assign(state,new_value) #更新,state的值变成 new_value
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
for i in range(3):
sess.run(update)
print(sess.run(state)) #----->1,2,3

tf.placeholder

用途:声明一个未知量,需要时再声明
完整函数参数:tf.compat.v1.placeholder( dtype, shape=None, name=None)
tf.placeholder()所声明的变量必须在Session.run(),Tensor.eval(),Operation.run()中使用 feed_dict= 传入值
dtype:表示将要传入值的类型
shape:表示要传入只的shape
e.g:
x = tf.compat.v1.placeholder(tf.float32, shape=(1024, 1024))
y = tf.matmul(x, x)
with tf.compat.v1.Session() as sess:
print(sess.run(y)) # ERROR: will fail because x was not fed.
rand_array = np.random.rand(1024, 1024)
print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值