2

I have tried multiple solutions posted online but to no avail. Am I missing something in my code?

My data contains multiple float variables with one string variable which was OneHotEncoded.

My Model:

import numpy as np
import pandas as pd
import tensorflow as tf

dataset = pd.read_csv('Processed Temp.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values

from sklearn.preprocessing import OneHotEncoder
from sklearn.compose import ColumnTransformer
ct = ColumnTransformer(transformers = [('encoder', OneHotEncoder(), [0])], remainder = 'passthrough')
X = np.array(ct.fit_transform(X))

X = np.asarray(X)
y = np.asarray(y)

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units=19, activation='relu'))
ann.add(tf.keras.layers.Dense(units=19, activation='relu'))
ann.add(tf.keras.layers.Dense(units=1))

ann.compile(optimizer = 'adam', loss = 'mean_squared_error')
ann.fit(X_train, y_train ,batch_size = 32, epochs = 100)

y_pred = ann.predict(X_test)
np.set_printoptions(precision=2)
print(np.concatenate((y_pred.reshape(len(y_pred),1), y_test.reshape(len(y_test),1)),1))

The error:

ValueError                                Traceback (most recent call last)
<ipython-input-14-4d1188c7d3d7> in <module>()
      1 
----> 2 ann.fit(X_train, y_train ,batch_size = 32, epochs = 100)

12 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     95   ctx.ensure_initialized()
---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
     97 
     98 

ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).

Appreciate all the help I can get. Thanks!

1

1 Answer 1

0

Can you provide the dataset related to this Question? Without Dataset it is hard to say whether your variable or approach is wrong. Anyway to convert any array to tensor we can use "tensorflow.convert_to_tensor()" syntax. arr=np.array([[1,2,3,4,5],[6,7,8,9,1]]) arr=tf.convert_to_tensor(arr) print(type(arr)) This will convert a numpy array to tensor. For more you can visit here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.