3
$\begingroup$

I have written the following code for a neural network to perform regression on a dataset, but I am getting a ValueError. I have looked up to different answers and they suggested to use df = df.values to get a numpy array. I tried it but it still produced the same error. How to fix this?

CODE

from keras import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split

#Define Features and Label
features = ['posted_by', 'under_construction', 'rera', 'bhk_no.', 'bhk_or_rk',
            'square_ft', 'ready_to_move', 'resale', 'longitude',
            'latitude'] 

X=train[features].values
y=train['target(price_in_lacs)'].values

#Train Test Split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state = 23, shuffle = True)

#Model
model = Sequential()
model.add(Dense(10, activation='relu', kernel_initializer='random_normal', input_dim = 10))
model.add(Dense(1, activation = 'relu', kernel_initializer='random_normal'))

#Compiling the neural network
model.compile(optimizer = Adam(learning_rate=0.1) ,loss='mean_squared_logarithmic_error', metrics =['mse'])

#Fitting the data to the training dataset  
model.fit(X_train,y_train, batch_size=256, epochs=100, verbose=0)

ERROR

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type int).
$\endgroup$
1
  • $\begingroup$ I am having the same problem. I don't know why it is happening $\endgroup$
    – 324
    Oct 12 '20 at 0:46
0
$\begingroup$

Before fitting the model the training data write the below 2 lines:

X_train=np.asarray(X_train).astype(np.int)

y_train=np.asarray(y_train).astype(np.int)
$\endgroup$
0
$\begingroup$

To convert numpy array to tensor,

import tensor as tf
#Considering y variable holds numpy array
y_tensor = tf.convert_to_tensor(y, dtype=tf.int64) 

#You can use any of the available datatypes that suits best - https://www.tensorflow.org/api_docs/python/tf/dtypes/DType

$\endgroup$
0
$\begingroup$

As X_train and y_train are pandas.core.series.Series they can't be parsed.

Try converting them to list as below:

X=train[features].to_list()

y=train['target(price_in_lacs)'].to_list()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.