Neural Network TypeError: unsupported operand type(s) for +=: 'Dense' and 'str'

Neural Network TypeError: unsupported operand type(s) for +=: 'Dense' and '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, 430000, 445000, 485000, 430000, 445000, 485000, 430000, 445000],
                   'Beds': [3, 4, 3, 3, 4, 3, 3, 4, 3, 3, 4, 3],
                   'SqFt': [2336, 2106, 1410, 2336, 2106, 1410, 2336, 2106, 1410, 2336, 2106, 1410],
                   'Built': [2004, 2005, 1999, 2004, 2005, 1999, 2004, 2005, 1999, 2004, 2005, 1999],
                   'Garage': [2, 2, 1, 2, 2, 1, 2, 2, 1, 2, 2, 1],
                   'FullBaths': [2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0],
                   'HalfBaths': [1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 0.0],
                   'LotSqFt': [2178.0, 2178.0, 3049.0, 2178.0, 2178.0, 3049.0, 2178.0, 2178.0, 3049.0, 2178.0, 2178.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, Y_train, verbose=1, validation_data=(X_val, Y_val))
model.evaluate(X_test, Y_test) ##Type Error is here!

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