首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow检验简单线性回归时的意外误差

Tensorflow检验简单线性回归时的意外误差
EN

Stack Overflow用户
提问于 2018-09-11 15:08:32
回答 1查看 408关注 0票数 0

我正在从一本名为“Tensorflow简明手册”的书中学习TensorFlow,有一段代码使用Tensorflow进行线性回归,但是我在测试时得到了一个AttributeError。

代码语言:javascript
复制
import numpy as np
X_raw = np.array([2013, 2014, 2015, 2016, 2017])
y_raw = np.array([12000, 14000, 15000, 16500, 17500])
X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())

import tensorflow as tf
X = tf.constant(X)
y = tf.constant(y)
a = tf.get_variable('a', dtype=tf.float64, shape=[], initializer=tf.zeros_initializer)
b = tf.get_variable('b', dtype=tf.float64, shape=[], initializer=tf.zeros_initializer)
variables = [a, b]
num_epoch = 10000
optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)
for e in range(num_epoch):
    with tf.GradientTape() as tape:
        y_pred = a * X + b
        loss = 0.5 * tf.reduce_sum(tf.square(y_pred - y))
    grads = tape.gradient(loss, variables)
    optimizer.apply_gradients(grads_and_vars=zip(grads, variables))

有关错误的信息:

代码语言:javascript
复制
Traceback (most recent call last):
  File "test.py", line 19, in <module>
    grads = tape.gradient(loss, variables)
  File "C:\Python35\lib\site-packages\tensorflow\python\eager\backprop.py", line 858, in gradient
    output_gradients=output_gradients)
  File "C:\Python35\lib\site-packages\tensorflow\python\eager\imperative_grad.py", line 63, in imperative_grad
    tape._tape, vspace, target, sources, output_gradients)  # pylint: disable=protected-access
AttributeError: 'Variable' object has no attribute '_id'

我不知道为什么会有错误,我无法调试它。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-11 16:02:50

下面的工作--我认为通过直接使用numpy数组更直观一些--

代码语言:javascript
复制
import numpy as np
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.reset_default_graph()
tf.enable_eager_execution()

X_raw = np.array([2013, 2014, 2015, 2016, 2017])
y_raw = np.array([12000, 14000, 15000, 16500, 17500])
X = (X_raw - X_raw.min()) / (X_raw.max() - X_raw.min())
Y = (y_raw - y_raw.min()) / (y_raw.max() - y_raw.min())

x = tf.constant(X)
y = tf.constant(Y)
a = tfe.Variable(0.0, name='a', dtype=tf.float64)
b = tfe.Variable(0.0, name='b', dtype=tf.float64)

def loss(x, y):
    return 0.5 * tf.reduce_sum(tf.square(a * x + b - y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-3)

num_epoch = 1000
for e in range(num_epoch):
    with tf.GradientTape() as t:
        l = loss(x, y)
    grads = t.gradient(l, [a, b])
    optimizer.apply_gradients(grads_and_vars=zip(grads, [a, b]))

给出输出

代码语言:javascript
复制
In [2]: a
Out[2]: <tf.Variable 'a:0' shape=() dtype=float64, numpy=0.5352067771256968>

In [3]: b
Out[3]: <tf.Variable 'b:0' shape=() dtype=float64, numpy=0.30109001612382946>

建议阅读https://www.tensorflow.org/guide/graphs,以便在tf.session中生成图形

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

https://stackoverflow.com/questions/52279048

复制
相关文章

相似问题

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