Exemple #1
0
from Dataset import Dataset
from Discova import Discova

data = Dataset()
data.load_house_dataframe(1)
data.add_windows(1, '00 mains')
data.add_windows(1, '05 refrigerator')
X_train, X_val, Y_train, Y_val = data.format_for_keras(1, '05 refrigerator')
'''
discova = Discova()
discova.load_data(X_train, X_val, Y_train, Y_val)
discova.construct_model()
discova.compile_model()
discova.load_tensorboard()
discova.train_model()
'''
Exemple #2
0
for train_house in [5]:
    discova = Autoencoder() # Turn off variance for prediction
    discova.construct_model()
    discova.compile_model()
    path = 'weights/ae_0' + str(train_house) + '_washerdryer.hdf5'
    discova.model.load_weights(path)
    print(('Loading weights for House ' + str(train_house)).ljust(40,'.'))

    for test_house in range(1,7):
        load_str = 'Loading test data for House ' + str(test_house)
        print(load_str.ljust(40,'.'))
        applianceLabel = applianceLabels[test_house - 1]
        data = Dataset()
        data.load_house_dataframe(test_house)
        data.add_windows(test_house, '00 mains')
        data.add_windows(test_house, applianceLabel)
        data.add_statistics(test_house)
        print('Predicting'.ljust(40,'.'))
        predRaw = discova.model.predict(data.windows[test_house]['00 mains'])
        print('Aligning by timestep'.ljust(40,'.'))
        predTimestep = data.recover_reverse_diagonals(predRaw)
        print('Taking median'.ljust(40,'.'))
        predMedian = np.median(predTimestep, axis = 1)
        print('Rescaling'.ljust(40,'.'))
        mean = data.means[test_house][applianceLabel]
        std = data.stddevs[test_house][applianceLabel]
        predMedianScaled = predMedian * std + mean
        print('Calculating relative MAE'.ljust(40,'.'))
        true = data.dataframes[test_house][applianceLabel][511:-511].as_matrix()
        mae = np.abs(true - predMedianScaled)
Exemple #3
0
    def evaluateInHouse(self, networkType):

        for appliance in self.applianceTypes:
            print('Adding results for {}'.format(appliance).ljust(40, '.'))
            for house in self.houses:
                try:
                    network = self.getNetworkWithWeights(
                        networkType, appliance, house)

                    load_str = 'Loading test data for House ' + str(house)
                    print(load_str.ljust(40, '.'))
                    data = Dataset()
                    data.load_house_dataframe(house)
                    data.add_windows(house, '00 mains')
                    mainsWindows = self.getMainsWindows(data,
                                                        house,
                                                        outOfHouse=False)

                    print('Predicting'.ljust(40, '.'))
                    predRaw = network.model.predict(mainsWindows)

                    print('Aligning by timestep'.ljust(40, '.'))
                    predTimestep = data.recover_reverse_diagonals(predRaw)

                    print('Taking median'.ljust(40, '.'))
                    predMedian = np.median(predTimestep, axis=1)

                    print('Rescaling'.ljust(40, '.'))
                    data.add_statistics(house)
                    applianceLabel = self.dfLabels.loc[house, appliance]
                    mean = data.means[house][applianceLabel]
                    std = data.stddevs[house][applianceLabel]
                    predMedianScaled = predMedian * std + mean

                    print('Calculating relative MAE'.ljust(40, '.'))
                    data.add_windows(house, applianceLabel)
                    m = data.dataframes[house].shape[0]
                    testStartIdx = int(-0.2 * m)
                    true = data.dataframes[house][applianceLabel]
                    true = true[testStartIdx:-511].as_matrix()
                    mae = np.abs(true - predMedianScaled)
                    relative_mae = mae.sum() / true.sum()

                    print('Appending results'.ljust(40, '.'))
                    row_dict = {
                        'network': networkType,
                        'appliance': appliance,
                        'train_house': house,
                        'predict_house': house,
                        'relative_mae': relative_mae
                    }
                except Exception:
                    traceback.print_exc()
                    row_dict = {
                        'network': networkType,
                        'appliance': appliance,
                        'train_house': house,
                        'predict_house': house,
                        'relative_mae': None
                    }
                    print('Error on House {} {}'.format(house,
                                                        appliance).ljust(
                                                            40, '.'))

                self.results = self.results.append(row_dict, ignore_index=True)
Exemple #4
0
    def evaluate_latent_space(self, networkType):

        for appliance in self.applianceTypes:
            print('Adding results for {}'.format(appliance).ljust(40, '.'))
            for house in self.houses:
                try:
                    network = self.getNetworkWithWeights(
                        networkType, appliance, house)
                    latent_model = Model(
                        inputs=network.model.input,
                        outputs=network.model.layers[13].output)

                    load_str = 'Loading test data for House ' + str(house)
                    print(load_str.ljust(40, '.'))
                    data = Dataset()
                    data.load_house_dataframe(house)
                    data.add_windows(house, '00 mains')
                    mainsWindows = self.getMainsWindows(data,
                                                        house,
                                                        outOfHouse=False)

                    print('Getting latent vectors'.ljust(40, '.'))
                    latent_vecs = latent_model.predict(mainsWindows)

                    print('Calculating neighbor distance'.ljust(40, '.'))
                    neighbor_diff = latent_vecs[1:] - latent_vecs[:-1]
                    mean_latent_dist = np.linalg.norm(neighbor_diff,
                                                      axis=1).mean()

                    print('Calculating neighbor cosine similarity'.ljust(
                        40, '.'))
                    mean_cosine_sim = paired_cosine_distances(
                        latent_vecs[1:], latent_vecs[:-1]).mean()

                    print('Appending results'.ljust(40, '.'))
                    row_dict = {
                        'network': networkType,
                        'appliance': appliance,
                        'train_house': house,
                        'predict_house': house,
                        'euclidean_dist': mean_latent_dist,
                        'cosine_similarity': mean_cosine_sim
                    }
                except Exception:
                    traceback.print_exc()
                    row_dict = {
                        'network': networkType,
                        'appliance': appliance,
                        'train_house': house,
                        'predict_house': house,
                        'euclidean_dist': None,
                        'cosine_similarity': None
                    }
                    print('Error on House {} {}'.format(house,
                                                        appliance).ljust(
                                                            40, '.'))

                self.results = self.results.append(row_dict, ignore_index=True)


#latent = LatentSpace()
#latent.evaluate_latent_space('vae')