Machine learning 使用keras为预测添加新功能时出错

Machine learning 使用keras为预测添加新功能时出错,machine-learning,keras,neural-network,Machine Learning,Keras,Neural Network,我做了一个预测,只使用了床的数量(效果很好),现在,我想通过添加第二个输入(平方英尺)来提高房价 我添加了如下代码: import tensorflow as tf import numpy as np from tensorflow import keras model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])]) xs = np.stack([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

我做了一个预测,只使用了床的数量(效果很好),现在,我想通过添加第二个输入(平方英尺)来提高房价

我添加了如下代码:

import tensorflow as tf
import numpy as np
from tensorflow import keras

model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[2])])
xs = np.stack([[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], [100, 150, 200, 250, 300, 350]], axis=1)
ys = np.array([100000, 150000, 200000, 250000, 300000, 350000], dtype=float)
model.fit(xs, ys, epochs=100)
print(model.predict([[7.0], [400.0]]))  # [7.0] number of beds / [400] square feet 
但我得到的错误如下:

ValueError: Input 0 of layer sequential_57 is incompatible with the layer: expected axis -1 of input shape to have value 2 but received input with shape [None, 1]
请,需要您的支持来修复它并使其正常工作


关于,

我从您的代码中更改了以下内容:

  • 在安装/训练模型之前,您需要编译模型(请参阅下面我的代码中的
    model.compile('adam','mae')
  • 要预测的输入数组的维数错误。它有维度
    (2,1)
    ,我将其更改为维度
    (1,2)
  • 如果我改变这两件事,代码对我有效

    model=keras.Sequential([keras.layers.density(units=1,input_shape=[2]))
    xs=np.stack([[1.0,2.0,3.0,4.0,5.0,6.0],[100150200250300350]],轴=1)
    ys=np.数组([100000,150000,200000,250000,300000,350000],dtype=float)
    model.compile('adam','mae')
    模型拟合(xs,ys,epochs=100)
    打印(model.predict(np.array([7.0400.0]]))#[7.0]床位数/[400]平方英尺#
    
    非常感谢你的回答,它奏效了:),而且,我把“亚当”改成了“新加坡元”,损失更大。