# -*- coding: utf-8 -*-
"""
Created on Sun Dec 13 12:16:47 2020

@author: Kireed
"""

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


def build_model():
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Dense(64, activation='relu',
                                    input_shape=(train_data.shape[1],)))
    model.add(tf.keras.layers.Dense(64, activation='relu'))
    model.add(tf.keras.layers.Dense(1))
    model.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
    return model


def smooth_curve(points, factor=0.9):
    smoothed_points = []
    for point in points:
        if smoothed_points:
            previous = smoothed_points[-1]
            smoothed_points.append(previous * factor + point * (1 - factor))
        else:
            smoothed_points.append(point)
    return smoothed_points
    

(train_data, train_targets), (test_data, test_targets) = \
    tf.keras.datasets.boston_housing.load_data()

# normalizing the data
mean = train_data.mean(axis=0)
std = train_data.std(axis=0)

train_data -= mean
train_data /= std

test_data -= mean
test_data /= std

# dividing data for K-fold validation alrgotithm
k = 4
num_val_samples = len(train_data) // k
num_epochs = 500
all_scores = []
all_mae_histories = []

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)
    # build and fitting model
    model = build_model()
    history = model.fit(partial_train_data, partial_train_targets, epochs=num_epochs,
              batch_size=1, verbose=0)
    #saving mean absolute error (mae) and medium squared error (mse)
    val_mse, val_mae = model.evaluate(val_data, val_targets, verbose=0)
    all_scores.append(val_mae)
    print(history.history.keys())
    mae_history = history.history['mae']
    all_mae_histories.append(mae_history)

average_mae_history = [
    np.mean([x[i] for x in all_mae_histories for i in range(num_epochs)])]

average_mae_history = [
np.mean([x[i] for x in all_mae_histories]) for i in range(num_epochs)]

plt.plot(range(1, len(average_mae_history) + 1), average_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()
    
smooth_mae_history = smooth_curve(average_mae_history[10:])

plt.clf()
plt.plot(range(1, len(smooth_mae_history) + 1), smooth_mae_history)
plt.xlabel('Epochs')
plt.ylabel('Validation MAE')
plt.show()

test_mse_score, test_mae_score = model.evaluate(test_data, test_targets)
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    