Skip to content

构建模型时需要定义命名空间 #1

@charmpeng

Description

@charmpeng

在effective-tf.md中定义模型函数的时候,

import numpy as np
import TensorFlow as tf
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
w = tf.get_variable("w", shape=[3, 1])
f = tf.stack([tf.square(x), x, tf.ones_like(x)], 1)
yhat = tf.squeeze(tf.matmul(f, w), 1)
loss = tf.nn.l2_loss(yhat - y) + 0.1 * tf.nn.l2_loss(w)
train_op = tf.train.AdamOptimizer(0.1).minimize(loss)

代码会报错,说“ValueError: Variable w already exists, disallowed. ”
需要显示的说明,完成变量共享。即在前面加上“tf.variable_scope("f", reuse=tf.AUTO_REUSE)”

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions