From_tensor_slice error

Hi,

This is my code and the error I get when I use “from_tensor_slice”. Anyone knows the solution (Keras clear_output doesn’t help)?

import tensorflow as tf 
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

tf.compat.v1.disable_eager_execution()

df = pd.read_csv('insurance.csv')
X = pd.get_dummies(df, columns = ['sex', 'smoker', 'region'])
y = X.pop('charges')

ds = tf.data.Dataset.from_tensor_slices((X.values, y.values))

The error:
Traceback (most recent call last):
File “C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\training\tracking\tracking.py”, line 269, in del
File “C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-
packages\tensorflow\python\framework\ops.py”, line 4011, in as_default
AttributeError: ‘NoneType’ object has no attribute ‘get_controller’

The given code is working as expected. Looks like data has some NAN values. Make sure address all the NAN values before converting it to tf.data.Dataset.

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.model_selection import train_test_split

tf.compat.v1.disable_eager_execution()

#df = pd.read_csv(‘insurance.csv’)
#X = pd.get_dummies(df, columns = [‘sex’, ‘smoker’, ‘region’])
#y = X.pop(‘charges’)

data = {‘sex’:[‘female’, ‘male’, ‘female’, ‘male’],
‘Age’:[‘yes’, ‘no’, ‘no’,‘yes’ ],
‘region’:[‘USA’,‘India’,‘India’,‘USA’]}

data1 = {‘charges’:[100,200,300,400]}

Create DataFrame
X = pd.DataFrame(data)
y = pd.DataFrame(data1)

ds = tf.data.Dataset.from_tensor_slices((X.values, y.values))

Output

<TensorSliceDataset shapes: ((3,), (1,)), types: (tf.string, tf.int64)>