tensorflow一些常用代码

<规约计算>
具有降维功能,所有reduce_xxx系列操作中,都在降维
axis的默认值为None,即把input_tensor降为一个数
对于二维的input_tensor而言,axis=0,按列计算
axis=1, 按行计算

tf.reduce_mean:求平均值

<共享变量>(一个模型需要使用其他模型创建的变量,两个模型一起训练)
get_variable方法,variable_scope方法(变量作用域)

tf.get_variable(<name>, <shape>, <initializer>)

使用get_variable创建两个同样名字的变量不可行,可使用variable_scope分隔开

with tf.variable_scope("test1"):
    var1 = tf.get_variable("firstvar", shape=[1], dtype=tf.float32)
with tf.variable_scope("test2"):
    var2 = tf.get_variable("firstvar", shape=[1], dtype=tf.float32)

使用作用域中的reuse来实现共享变量功能
令reuse=True,表示使用已经定义过的变量

with tf.variable_scope("test1",reuse=True):
    var3 = tf.get_variable("firstvar", shape=[1], dtype=tf.float32)
with tf.variable_scope("test2",reuse=True):
    var4 = tf.get_variable("firstvar", shape=[1], dtype=tf.float32)

使用withvariable_scope(“name”) as XXX的方式定义作用域时,所定义的作用域变量XXXX不受外围scope的限制

with tf.variable_scope("scope1") as sp:
	var1 = tf.get_variable("v",[1])
with tf.variable_scope("scope2"):
	var2=tf.get_variable("v",[1])
	with tf.variable_scope(sp) as sp1:
		var3=tf.get_variable("v3",[1])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值