tensorflow基础操作

基础操作

import tensorflow as tf 
matrix1 = tf.constant([[3.,3.]])
matrix2 = tf.constant([[2.],[5.]])
product = tf.matmul(matrix1, matrix2)
with tf.Session() as sess:
    result1 = sess.run(product)
    result2 = sess.run([product])
    print(result1,'\n',result2)
[[ 21.]] 
 [array([[ 21.]], dtype=float32)]
with tf.Session() as sess:
    with tf.device("/gpu:0"):
        tf.global_variables_initializer()
        matrix1 = tf.constant([[3.,3.]])
        matrix2 = tf.constant([[2.],[5.]])
        product = tf.matmul(matrix1, matrix2)
    result1 = sess.run(product)
    result2 = sess.run([product])
    print(result1,'\n',result2)
[[ 21.]] 
 [array([[ 21.]], dtype=float32)]
state = tf.Variable(0, name = 'counter')
constant1 = tf.constant(3.0)
input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
output = tf.multiply(input1, input2)
with tf.Session() as sess:
    print(sess.run([output], feed_dict = {input1: [7.], input2 : [2.]}))
[array([ 14.], dtype=float32)]
#######################variable_scope###########################
with tf.variable_scope("foo",reuse = True):
    v = tf.get_variable("v", [1])
    v1 = tf.get_variable("v", [1])
assert v == v1
with tf.variable_scope("foo"):
    v = tf.get_variable("v", [1])
    v2 = tf.get_variable("v", [1])
assert v == v2
#输出ValueError
with tf.variable_scope("foo") as foo_scope:
    assert foo_scope.name == "foo"
with tf.variable_scope("bar"):
    with tf.variable_scope("cat") as other_scope:
        assert other_scope.name == "bar/cat"
        #如果在开启的一个变量作用域之前使用预先定义的作用域,
        #则会跳过当前变量的作用域,保持预先存在的作用域不变
        with tf.variable_scope(foo_scope) as foo_scope2:
            assert foo_scope2.name == "foo"

变量作用域初始化

import tensorflow as tf
#不能重复运行该程序,第二次运行因为变量名foo已经存在,所以要reuse =True,而首次运行由于不存在foo需要reuse=False(默认值)
#同时,w处如果是后写的,则无法操作,因为foo已经存在而w1不存在无法同时建立。reuse不同一
#如果需要再次运行,新建个jupyter notebook环境
with tf.variable_scope("foo", initializer = tf.constant_initializer(0.4)):
    v = tf.get_variable("v1", [1])
    w = tf.get_variable("w1",[1],initializer = tf.constant_initializer(0.2))
    with tf.variable_scope("bar"):
        v1 = tf.get_variable("v1", [1])         
    with tf.variable_scope("cat1", initializer = tf.constant_initializer(0.1)):
        v2 = tf.get_variable("v1", [1])      
sess = tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
assert v.eval() == 0.4
assert w.eval() == 0.2
assert v1.eval() == 0.4
assert v2.eval() == 0.1 
with tf.variable_scope("foo"):
    x = 1.0 + tf.get_variable("v123",[1])
with tf.variable_scope("foo"):
    #reuse = True是为了解决下面y的名称冲突,若y的名称与x相同,则产生新的foo
    #并在原来基础上foo n命名为foo n+1,但后续会报错。
    #若名字不同则在foo n+1 基础上产生foo n+2/add
    y = 1.0 +tf.get_variable("v122",[1])
print(x.op.name,y.op.name) 
foo_7/add foo_8/add
#name_scope会影响op_name,通过Variable()创建的变量
#不会影响get_variable()创建的变量
with tf.variable_scope("foo123"):
    with tf.name_scope("bar22"):
        v = tf.get_variable("v22", [1])
        b = tf.Variable(tf.zeros([1]), name = 'b')
        x = 1.0+v
assert v.name == "foo123/v22:0"
assert b.name == "foo123/bar22/b:0"
assert x.op.name == "foo123/bar22/add"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值