首页 > 其他 > 详细

Tensorflow细节-P54-变量

时间:2019-10-01 23:44:26      阅读:167      评论:0      收藏:0      [点我收藏+]

1、首先复习前面所学知识:
(1)g = tf.Graph()
(2)别忘了初始化时的initializer
(3)with tf.name_scope("generate_constant"): x = tf.constant([[0.7, 0.9]], name="x")这个东西发现没球用
(4)最好是用with tf.variable_scope("generate_variable"):tf.get_variable相结合,不要用tf.Variable()出什么幺蛾子

import tensorflow as tf
g = tf.Graph()
with g.as_default():
    with tf.variable_scope("generate_variable"):
        w1 = tf.get_variable("w1", [2, 3], tf.float32, initializer=tf.random_normal_initializer(stddev=0.1, seed=1))
        w2 = tf.get_variable("w2", [3, 1], tf.float32, initializer=tf.truncated_normal_initializer(stddev=0.1, seed=1))
    with tf.name_scope("generate_constant"):
        x = tf.constant([[0.7, 0.9]], name="x")

    with tf.name_scope("matmul"):
        a = tf.matmul(x, w1)
        y = tf.matmul(a, w2)

writer = tf.summary.FileWriter("./path", graph=g)
writer.close()

with tf.Session(graph=g) as sess:
    tf.global_variables_initializer().run()
    print(sess.run(y))

技术分享图片
2、所有的变量都会被加入到Graphkeys.VARIABLES集合,在变量声明时可控制trainable是之训练/不训练

Tensorflow细节-P54-变量

原文:https://www.cnblogs.com/liuboblog/p/11616327.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!