我正在尝试训练线性回归模型来预测金县的房价。我已逐步按照教程进行操作。但是,当我最小化损失函数时,会出现错误:
'RefVariable' object has no attribute '_id'
我正在关注一个简单的教程,以学习如何训练简单的线性回归模型。我还没有真正找到有关这种错误的任何信息。请注意,我正在为此项目使用Google Colab。这是整个错误:
'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
这是我到目前为止写的:
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())
答案 0 :(得分:1)
您忘记启用急切执行模式。
在导入语句之后添加以下行:
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())