变量的使用

一、变量

  • 为了训练模型,需要能够修改图以调节一些对象,比如权重值、偏置量。简单来说,变 量让你能够给图添加可训练的参数,它们在创建时候就带有类型属性和初始值。

二、实例

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf 
x = tf.constant([10,20,30,40])
# 定义变量
y = tf.Variable(initial_value=x*20+10)
# 修改命令空间
with tf.variable_scope('my_scope'):  
    Z = tf.Variable(initial_value=100)
print('x:',x)
print('y:',y)
print('Z:',Z)
# 初始化变量
model = tf.global_variables_initializer()
# 开启会话
with tf.Session() as sess:
    # 运行初始化变量
    sess.run(model)
    y_value = sess.run(y)
    print('y_value:',y_value)
    Z_value = sess.run(Z)
    print('Z_value:',Z_value)

三、结果

正是江南好风景
原文地址:https://www.cnblogs.com/monsterhy123/p/13088529.html