神经网络ValueError

时间:2020-10-11 17:46:37

标签: python python-3.x keras error-handling neural-network

我目前正在尝试运行神经网络,以预测给定房屋是高于还是低于房屋中位数。我有六个输入,包括平方英尺,卧室数量,浴室数量,车库空间数量等。响应变量是一个二进制变量,指示价格是低于中位售价(0)还是高于(1)。

我对神经网络非常陌生,因此我只是在尝试学习。我正在学习本教程:https://hackernoon.com/build-your-first-neural-network-to-predict-house-prices-with-keras-3fb0839680f4,但是使用了不同的数据。这是我的数据的标题:

Beds  Baths   SqFt   LotSize  Built   Garage   AboveMedianPrice
3     2.5     2336   0.050    2004    2        0.0
4     3.5     3430   0.069    1999    2        1.0
...

这就是我建立神经网络的方式。

  • 数据扩展
min_max_scaler = preprocessing.MinMaxScaler() #requires numeric values
X_scale = min_max_scaler.fit_transform(X) #scales dataset so that all input features are in [0, 1].

#Partition the dataset between training and validation.
X_train, X_val_and_test, Y_train, Y_val_and_test = train_test_split(X_scale, Y, test_size=0.3)

#Seperates into seperate validation set and test set.
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) ## output: (382, 6) (82, 6) (83, 6) (382,) (82,) (83,)
  • 模型构建
from keras.models import Sequential 
from keras.layers import Dense 

model = Sequential([
    Dense(32, activation='relu', input_shape=(6,)), #input shape is 6: one for each predictor variable
    Dense(32, activation='relu'),
    Dense(1, activation='sigmoid'),
])

model.compile(optimizer='sgd', #sgd = stochastic gradient descent
              loss='binary_crossentropy', #binary = output values are 0 or 1
              metrics=['accuracy']) #metrics will track the accuracy of the loss function

然后,我尝试使用以下代码段运行模型

hist = model.fit(X_train, Y_train,
          batch_size=32, epochs=100,
          validation_data=(X_val, Y_val))

但是,当第一个纪元开始运行时,我收到一个ValueError,我不知道该如何解决。这是有关该错误的更多详细信息:

ValueError                                Traceback (most recent call last)
<ipython-input-19-7ee038121eec> in <module>
      4 #yVal = np.asarray(Y_val)
      5 
----> 6 hist = model.fit(X_train, Y_train,
      7           batch_size=32, epochs=100,
      8           validation_data=(X_val, Y_val))

...

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

我缺少一些小东西吗?

0 个答案:

没有答案