房价的多个输入

问题描述 投票:0回答:1

[我仅使用床位数(工作正常)进行了预测,现在,我想通过添加第二个输入(方英尺)来改善房价。

我添加了如下代码:

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]

[请,需要您的支持来修复它并使它工作。

问候,

machine-learning keras neural-network
1个回答
1
投票

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

  1. 您需要在对模型进行拟合/训练之前对其进行编译(请参见下面我的代码中的model.compile('adam', 'mae')
  2. 您要预测其输入数组的尺寸错误。它的尺寸为(2,1),我更改为尺寸(1,2)

如果我更改这两件事,代码将对我有用。

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.compile('adam', 'mae')
model.fit(xs, ys, epochs=100)
print(model.predict(np.array([[7.0, 400.0]]))) # [7.0] number of beds / [400] square feet #
© www.soinside.com 2019 - 2024. All rights reserved.