我正在练习使用 Iris 数据集进行迁移学习。
对于以下代码,我收到以下错误消息:
无法将 NumPy 数组转换为张量(不支持的对象类型浮点数)
我需要帮助来解决这个错误。
在导入的库下方
import pandas as pd
import io
import requests
import numpy as np
from sklearn import metrics
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.callbacks import EarlyStopping
使用 pandas 读取 csv 文件
df = pd.read_csv("Iris.csv", na_values=['NA', '?'])
df.columns
#output of df.colums
Index(['Id', 'SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm',
'Species'],
dtype='object')
转换为numpy数组进行分类
x = df[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'Species']].values
dummies = pd.get_dummies(df['Species']) #classification
species = dummies.columns
y = dummies.values
构建神经网络,
model = Sequential()
model.add(Dense(50, input_dim = x.shape[1], activation= 'relu')) #Hidden Layer-->1
model.add(Dense(25, activation= 'relu')) #Hidden Layer-->2
model.add(Dense(y.shape[1], activation= 'softmax')) #Output
编译NN模型
model.compile(loss ='categorical_crossentropy', optimizer ='adam')
适合模型,请关注这部分
model_fit=model.fit(x,y, verbose=2, epochs=10, steps_per_epoch=3)
错误如下,
ValueError Traceback (most recent call last)
<ipython-input-48-0ff464178023> in <module>()
----> 1 model_fit=model.fit(x,verbose=2, epochs=10, steps_per_epoch=3)
13 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
96 dtype = dtypes.as_dtype(dtype).as_datatype_enum
97 ctx.ensure_initialized()
---> 98 return ops.EagerTensor(value, ctx.device_name, dtype)
99
100
ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type float).