data-science-notebook
data-science-notebook copied to clipboard
构建模型时需要定义命名空间
在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)”
Hi, i have tried your code and there is not any error. My tensorflow is version 1.8.0.
Also import TensorFlow as tf
should be import tensorflow as tf
.
Please check out your version and have another try.