神经网络TypeError:+ =不支持的操作数类型:'Dense'和'str'

问题描述

我正在尝试使用神经网络来预测房屋价格。这是数据集顶部的样子:

    Price   Beds    SqFt    Built   Garage  FullBaths   HalfBaths   LotSqFt
    485000  3       2336    2004    2       2.0          1.0        2178.0
    430000  4       2106    2005    2       2.0          1.0        2178.0
    445000  3       1410    1999    1       2.0          0.0        3049.0

...

我正在使用ReLU激活功能。当我尝试根据测试数据评估模型时,得到了TypeError: unsupported operand type(s) for +=: 'Dense' and 'str'

我查看了原始数据框中的列类型,一切看起来都很好。

print(df.dtypes)
## Output
#Price          int64
#Beds           int64
#SqFt           int64
#Built          int64
#Garage         int64
#FullBaths    float64
#HalfBaths    float64
#LotSqFt      float64
#dtype: object

我不确定我是否在神经网络中弄乱了某些东西以导致此错误。任何帮助表示赞赏!这是我的代码供参考。

  • 准备网络数据
dataset = df.values
X = dataset[:,1:8]
Y = dataset[:,0]

## Normalize X-Values
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)
X_scale

##Partition Data
from sklearn.model_selection import train_test_split
X_train,X_val_and_test,Y_train,Y_val_and_test = train_test_split(X_scale,Y,test_size=0.3)
X_val,X_test,Y_val,Y_test = train_test_split(X_val_and_test,Y_val_and_test,test_size=0.5)
print(X_train.shape,X_val.shape,X_test.shape,Y_train.shape,Y_val.shape,Y_test.shape)
  • 开始建立模型
from keras.models import Sequential
from keras.layers import Dense

model = Sequential(
    Dense(32,activation='relu',input_shape=(7,)),Dense(1,activation='linear'))

model.compile(optimizer='sgd',loss='mse',metrics=['mean_squared_error'])

model.evaluate(X_test,Y_test)[1] ##Type Error is here!

解决方法

我试图重新创建您的代码的最少示例(不起作用)。看来您只是忘记了Sequential()模型定义中的一对方括号。

import pandas as pd
from keras import backend as K

# Tried to recreate your dataset
df = pd.DataFrame({'Price': [485000,430000,445000,485000,445000],'Beds': [3,4,3,3],'SqFt': [2336,2106,1410,2336,1410],'Built': [2004,2005,1999,2004,1999],'Garage': [2,2,1,1],'FullBaths': [2.0,2.0,2.0],'HalfBaths': [1.0,1.0,0.0,0.0],'LotSqFt': [2178.0,2178.0,3049.0,3049.0]})

dataset = df.values
X = dataset[:,1:8]
Y = dataset[:,0]

## Normalize X-Values
from sklearn import preprocessing
min_max_scaler = preprocessing.MinMaxScaler()
X_scale = min_max_scaler.fit_transform(X)

##Partition Data
from sklearn.model_selection import train_test_split
X_train,X_val_and_test,Y_train,Y_val_and_test = train_test_split(X_scale,Y,test_size=0.3)
X_val,X_test,Y_val,Y_test = train_test_split(X_val_and_test,Y_val_and_test,test_size=0.5)
print(X_train.shape,X_val.shape,X_test.shape,Y_train.shape,Y_val.shape,Y_test.shape)
from keras.models import Sequential
from keras.layers import Dense

model = Sequential([
    Dense(32,activation='relu',input_shape=(7,)),Dense(1,activation='linear')]) # Layers are enclosed in square brackets

model.compile(optimizer='sgd',loss='mse',metrics=['mean_squared_error'])

model.fit(X_train,verbose=1,validation_data=(X_val,Y_val))
model.evaluate(X_test,Y_test) ##Type Error is here!

此外,我将在测试模型之前对模型进行训练和评估(通过调用model.fit(X_train,Y_val)))。否则,您将在具有随机初始化权重的神经网络上评估测试集。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...