神经网络-> ValueError:无法将NumPy数组转换为张量(不支持的对象类型float)

时间:2020-11-05 08:43:05

标签: numpy neural-network tensor valueerror feature-scaling

我尝试建立具有回归的神经网络模型。我想e错误属于Feature Scaling,它试​​图从分类转换为数值(为性别列“ Male / Female”设置为OneHotEncoding)。

这是我的代码

import numpy as np 
import pandas as pd
import tensorflow as tf
import os
data1 = pd.read_csv(r"C:\Users\Cucu\Desktop\Mall_Customers.csv") 
x = data1.iloc [: , 1:-1].values #SEPARATE INDEPENDENT VARIABLES
print(x)
y = data1.iloc [: , -1].values #SEPARATE DEPENDENT VARIABLES
print(y)
#AFTER PRINTING, WE WILL HAVE A SIMPLER SHEET


#ENCODING CATEGORICAL DATA
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import OneHotEncoder
ct = ColumnTransformer( transformers = [('encoder' , OneHotEncoder() , [0] )], remainder = 'passthrough')
x = np.array(ct.fit_transform(x))
print (x)


#LET'S SPLIT INTO TRAIN SET & TEST SET
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 = 1 )
print(x_train)
print("------")
print(x_test)
print("------")
print(y_train)
print("------")
print(y_test)


#APPLYING FEATURE SCALING
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
x_train[: , 2:] = sc.fit_transform(x_train[: , 2:])
x_test[: , 2:] = sc.transform(x_test[: , 2:])
print(x_train)
#must be updated-------------------------


#INITIALZING THE ANN
ann = tf.keras.models.Sequential()

#BULDING THE ANN
ann.add(tf.keras.layers.Dense(units = 6 , activation = 'relu'))
#--units(the number of neurons--hyper-parameter--must be optimized)



#ADDING THE INPUT LAYER &THE FIRST HIDDEN LAYER
ann.add(tf.keras.layers.Dense(units = 6 , activation = 'relu'))

#ADDING THE OUPUT LAYER
ann.add(tf.keras.layers.Dense(units = 1 , activation = 'relu'))  #ReLU used in output for regression task



#TRAINING THE ANN
#1)compiling the ann
ann.compile(optimizer = 'adam' , loss = 'mean_squared_error' )
#mean_squared_error is specific loss fuction for regresion 


#2)training the ann on the training set
ann.fit(x_train , y_train , batch_size = 32 , epochs = 100)

错误是:

File "D:\.spyder-py3\ANN.py", line 73, in <module>
    ann.fit(x_train , y_train , batch_size = 32 , epochs = 100)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 819, in fit
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 235, in fit
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 593, in _process_training_inputs
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\training_v2.py", line 706, in _process_inputs
    use_multiprocessing=use_multiprocessing)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 357, in __init__
    dataset = self.slice_inputs(indices_dataset, inputs)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\keras\engine\data_adapter.py", line 383, in slice_inputs
    dataset_ops.DatasetV2.from_tensors(inputs).repeat()

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 566, in from_tensors
    return TensorDataset(tensors)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\data\ops\dataset_ops.py", line 2765, in __init__
    element = structure.normalize_element(element)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\data\util\structure.py", line 113, in normalize_element
    ops.convert_to_tensor(t, name="component_%d" % i))

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1314, in convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\framework\tensor_conversion_registry.py", line 52, in _default_conversion_function
    return constant_op.constant(value, dtype, name=name)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 258, in constant
    allow_broadcast=True)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 266, in _constant_impl
    t = convert_to_eager_tensor(value, ctx, dtype)

  File "C:\Users\Cucu\anaconda3\envs\lucky3\lib\site-packages\tensorflow_core\python\framework\constant_op.py", line 96, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)

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

我尝试使用Google搜索,但没有找到任何解决方案。任何建议和帮助将不胜感激。

0 个答案:

没有答案