"RefVariable"对象没有属性"_id" [英] 'RefVariable' object has no attribute '_id'
问题描述
我正在尝试训练线性回归模型来预测金县的房价.我已逐步按照教程进行操作.但是,当我最小化损失函数时,会出现错误:
I am trying to train the linear regression model for predicting house prices in King County. I have followed a tutorial step by step. However, when I get to minimize the loss function I get the error:
'RefVariable' object has no attribute '_id'
我正在关注一个简单的教程,以学习如何训练简单的线性回归模型.我还没有真正找到关于这种错误的任何信息.请注意,我正在为此项目使用Google Colab.这是整个错误:
I am following a simple tutorial in order to learn how to train simple linear regression models. I have not really been able to find anything about this kind of error. Please note that I am using Google Colab for this project. This is the whole error:
'RefVariable' object has no attribute '_id'
The above exception was the direct cause of the following exception:
SystemError Traceback (most recent call last)
<ipython-input-31-17eaadb45902> in <module>()
15 #minimize the loss function
16
---> 17 opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch),var_list=[intercept,slope])
18
19
3 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/tape.py in watch(tape, tensor)
57 def watch(tape, tensor):
58 """Marks this tensor to be watched by the given tape."""
---> 59 pywrap_tensorflow.TFE_Py_TapeWatch(tape._tape, tensor) # pylint: disable=protected-access
60
61
SystemError: <built-in function TFE_Py_TapeWatch> returned a result with an error set
这是我到目前为止写的:
This is what I've written so far:
import tensorflow as tf
import numpy as np
import pandas as pd
#define trainable variables
#for linear regression this is the intercept and the slope
intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)
#define a linear regression function
def linear_regression(intercept,slope, features):
return intercept + slope*features
#compute predicted values and return loss function
def loss_function (intercept,slope,targets,features):
predictions = linear_regression(intercept,slope,features)
return tf.keras.losses.mse(targets,predictions)
#OPTIMIZER
opt = tf.keras.optimizers.Adam()
for batch in pd.read_csv('kc_house_data.csv', chunksize = 100):
#extract the target and feature columns
price_batch = np.array(batch['price'], np.float32)
size_batch = np.array(batch['sqft_lot'], np.float32)
#minimize the loss function
opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch),var_list=[intercept,slope])
print(intercept.numpy(), slope.numpy())
推荐答案
您忘记启用急切的执行模式.
You forgot to enable eager execution mode.
在导入语句之后添加以下行:
Add below line after the import statements:
tf.enable_eager_execution()
更新代码:
import tensorflow as tf
import numpy as np
import pandas as pd
tf.enable_eager_execution()
#define trainable variables
#for linear regression this is the intercept and the slope
intercept = tf.Variable(0.1, tf.float32)
slope = tf.Variable(0.1, tf.float32)
#define a linear regression function
def linear_regression(intercept,slope, features):
return intercept + slope*features
#compute predicted values and return loss function
def loss_function (intercept,slope,targets,features):
predictions = linear_regression(intercept,slope,features)
return tf.keras.losses.mse(targets,predictions)
#OPTIMIZER
opt = tf.train.AdamOptimizer()
for batch in pd.read_csv('kc_house_data.csv', chunksize = 100):
#extract the target and feature columns
price_batch = np.array(batch['price'], np.float32)
size_batch = np.array(batch['sqft_lot'], np.float32)
loss_function(intercept,slope,price_batch,size_batch)
#minimize the loss function
opt.minimize(lambda: loss_function(intercept,slope,price_batch,size_batch), var_list=[intercept,slope])
print(intercept.numpy(), slope.numpy())
这篇关于"RefVariable"对象没有属性"_id"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!