Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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())
like image 224
bbbb Avatar asked Jul 29 '19 09:07

bbbb


1 Answers

You forgot to enable eager execution mode.

Add below line after the import statements:

tf.enable_eager_execution()

Updated Code:

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())
like image 92
Anubhav Singh Avatar answered Nov 07 '22 13:11

Anubhav Singh