我正在尝试训练线性回归模型来预测金县的房价。我一步一步地遵循了教程。但是,当我最小化损失函数时,我得到了错误:
'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())
最佳答案
您忘记启用急切执行模式。
在导入语句后添加以下行:
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())
关于python - 'RefVariable' 对象没有属性 '_id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57250679/