【问题标题】:'RefVariable' object has no attribute '_id'“RefVariable”对象没有属性“_id”
【发布时间】:2023-03-14 23:34:01
【问题描述】:

我正在尝试训练线性回归模型来预测金县的房价。我一步一步地遵循了一个教程。但是,当我最小化损失函数时,我得到了错误:

'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())

【问题讨论】:

    标签: python tensorflow keras


    【解决方案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())
    

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多