首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow,如何从数组中恢复var?

Tensorflow,如何从数组中恢复var?
EN

Stack Overflow用户
提问于 2017-06-22 23:33:10
回答 1查看 49关注 0票数 0

我使用了一个具有以下变量的示例

代码语言:javascript
复制
weights = {
    # 5x5 conv, 1 input, 32 outputs
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 32])),
    # 5x5 conv, 32 inputs, 64 outputs
    'wc2': tf.Variable(tf.random_normal([5, 5, 32, 64])),
    # fully connected, 7*7*64 inputs, 1024 outputs
    'wd1': tf.Variable(tf.random_normal([7*7*64, 1024])),
    # 1024 inputs, 10 outputs (class prediction)
    'out': tf.Variable(tf.random_normal([1024, n_classes]))
}

biases = {
    'bc1': tf.Variable(tf.random_normal([32])),
    'bc2': tf.Variable(tf.random_normal([64])),
    'bd1': tf.Variable(tf.random_normal([1024])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

这样我就不能使用下面代码来恢复var

代码语言:javascript
复制
wc1 = tf.get_variables("weights[wc1]")

那么我如何使用tensorflow恢复变量呢?

EN

回答 1

Stack Overflow用户

发布于 2017-06-23 00:41:29

您只需通过以下方式引用变量

代码语言:javascript
复制
weights["wc1"]

tf.get_variable命令以另一种方式使用,如果您想使用它来恢复已经创建的变量,您需要使用reuse = True在变量作用域中,并使用tensorflow与变量关联的名称,而不是python指针。例如:

代码语言:javascript
复制
with tf.variable_scope('var_scope'):
    v = tf.Variable(5, shape=(), dtype=tf.float32, name='my_var')

with tf.variable_scope('var_scope', reuse=True):
    v_again = tf.get_variable(name='my_var', dtype=tf.float32)

现在vv_again是两个指向相同tensorflow变量的python变量。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44703774

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档