Beispiel #1
0
# better training and convergence)
maxPrice = train["price"].max()
trainY = train["price"] / maxPrice
testY = test["price"] / maxPrice

# process the house attributes data by performing min-max scaling
# on continuous features, one-hot encoding on categorical features,
# and then finally concatenating them together
print("[INFO] processing data...")
(trainX, testX) = datasets.process_house_attributes(df, train, test)

# create our MLP and then compile the model using mean absolute
# percentage error as our loss, implying that we seek to minimize
# the absolute percentage difference between our price *predictions*
# and the *actual prices*
model = models.create_mlp(trainX.shape[1], regress=True)
opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss="mean_absolute_percentage_error", optimizer=opt)

# train the model
print("[INFO] training model...")
model.fit(trainX,
          trainY,
          validation_data=(testX, testY),
          epochs=200,
          batch_size=8)

# make predictions on the testing data
print("[INFO] predicting house prices...")
preds = model.predict(testX)
Beispiel #2
0
# find the largest house price in the training set and use it to
# scale our house prices to the range [0, 1] (will lead to better
# training and convergence)
maxPrice = trainAttrX["price"].max()
trainY = trainAttrX["price"] / maxPrice
testY = testAttrX["price"] / maxPrice

# process the house attributes data by performing min-max scaling
# on continuous features, one-hot encoding on categorical features,
# and then finally concatenating them together
(trainAttrX,
 testAttrX) = datasets.process_house_attributes(df, trainAttrX, testAttrX)

# create the MLP and CNN models
mlp = models.create_mlp(trainAttrX.shape[1], regress=False)
cnn = models.create_cnn(64, 64, 3, regress=False)

# create the input to our final set of layers as the *output* of both
# the MLP and CNN
combinedInput = concatenate([mlp.output, cnn.output])

# our final FC layer head will have two dense layers, the final one
# being our regression head
x = Dense(4, activation="relu")(combinedInput)
x = Dense(1, activation="linear")(x)

# our final model will accept categorical/numerical data on the MLP
# input and images on the CNN input, outputting a single value (the
# predicted price of the house)
model = Model(inputs=[mlp.input, cnn.input], outputs=x)
for feature in feature_columns:
    feature_layer = tf.keras.layers.DenseFeatures(feature)
    feature_array = feature_layer(dict(df)).numpy()
    if first:
        test_data = feature_array
        first = False
        continue
    test_data = np.concatenate((test_data, feature_array), axis=1)
    print(feature_layer(dict(df)).numpy())

#%%
import keras
from keras.layers import LeakyReLU
from keras.callbacks import EarlyStopping, ReduceLROnPlateau

mlp = models.create_mlp(np.asarray(test_data).shape[1], regress=True)
cnn_left = models.create_cnn(256, 128, 6, regress=False)
# cnn_right = models.create_cnn(256, 128, 6, regress=False)

# create the input to our final set of layers as the *output* of both
# the MLP and CNN
# combinedInput = concatenate([mlp.output, cnn_left.output, cnn_right.output])
combinedInput = concatenate([mlp.output, cnn_left.output])

# our final FC layer head will have two dense layers, the final one
# being our regression head
x = Dense(8, activation=LeakyReLU(alpha=0.2))(combinedInput)
x = Dense(4, activation=LeakyReLU(alpha=0.2))(x)
x = Dense(1)(x)

# our final model will accept categorical/numerical data on the MLP