-
Notifications
You must be signed in to change notification settings - Fork 0
/
Benchmark.4.Housing.Price.Regression.py
executable file
·58 lines (44 loc) · 1.94 KB
/
Benchmark.4.Housing.Price.Regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from tensorflow.keras.datasets import boston_housing
from tensorflow.keras import models, layers
import numpy as np
import time
(train_data, train_targets), (test_data, test_targets) = boston_housing.load_data()
# Feature Normalization: Putting data into a normal distribution
mean = train_data.mean(axis=0)
train_data -= mean
std = train_data.std(axis=0)
train_data /= std
# Use only information derived from training info
test_data -= mean
test_data /= std
def build_model():
model = models.Sequential()
model.add(layers.Dense(64, activation='relu',input_shape=(train_data.shape[1],)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1))
model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
return model
# K-fold validation
k = 4
num_val_samples = len(train_data) // k
num_epochs = 100
# num_epochs = 500
all_mae_histories = []
import time
start_fit = time.time()
for i in range(k):
print("Processing fold #", i)
val_data = train_data[i * num_val_samples: (i+1)*num_val_samples]
val_targets = train_targets[i * num_val_samples: (i + 1) * num_val_samples]
partial_train_data = np.concatenate([train_data[:i*num_val_samples], train_data[(i+1)*num_val_samples:]], axis=0)
partial_train_targets = np.concatenate([train_targets[:i * num_val_samples], train_targets[(i + 1) * num_val_samples:]], axis=0)
model = build_model()
history = model.fit(partial_train_data, partial_train_targets, validation_data=(val_data, val_targets), epochs=num_epochs, batch_size=1, verbose=0)
mae_history = history.history['val_mae']
average_mae_history = [np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]
end_fit = time.time()
model = build_model()
model.fit(train_data, train_targets, epochs=80, batch_size=16, verbose=0)
test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)
print(test_mae_score)
print("--- %s seconds ---" % (end_fit - start_fit), " fit time.")