Esempio n. 1
0
##############################################################################
# Here we show the general cross validation scores for different values of the
# parameters given to the different smoothing methods.

param_values_knn = np.arange(1, 24, 2)
param_values_others = param_values_knn / 32

# Local linear regression kernel smoothing.
llr = val.SmoothingParameterSearch(ks.LocalLinearRegressionSmoother(),
                                   param_values_others)
llr.fit(fd)
llr_fd = llr.transform(fd)

# Nadaraya-Watson kernel smoothing.
nw = val.SmoothingParameterSearch(ks.NadarayaWatsonSmoother(),
                                  param_values_others)
nw.fit(fd)
nw_fd = nw.transform(fd)

# K-nearest neighbours kernel smoothing.
knn = val.SmoothingParameterSearch(ks.KNeighborsSmoother(), param_values_knn)
knn.fit(fd)
knn_fd = knn.transform(fd)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(param_values_knn,
        knn.cv_results_['mean_test_score'],
        label='k-nearest neighbors')
ax.plot(param_values_knn,
Esempio n. 2
0
    def test_other(self, method='kernel'):
        import skfda
        import skfda.preprocessing.smoothing.kernel_smoothers as ks
        import skfda.preprocessing.smoothing.validation as val

        self.restore_model(self.resume_iters)

        self.G.eval()
        self.set_requires_grad(self.G, False)

        self.iteration = self.test_len - self.num_clips + 1
        self.model_save_dir = os.path.join(self.checkpoint_dir, self.model_dir)

        graph_embedding = []

        node_abnormal = []
        graph_ab_list = []

        with torch.no_grad():
            idx = 0
            self.subject = 0
            while idx < self.iteration:
                # =================================================================================== #
                #                             1. Preprocess input data(Unfinished)                    #
                # =================================================================================== #
                node_feature, edge, sensor, abnormal, graph_abnormal, idx = self.load_data_test(
                    idx)

                node_abnormal.append(abnormal)
                graph_ab_list.append(graph_abnormal.view(self.batch_size))

                # =================================================================================== #
                #                             2. Train the Model                                      #
                # =================================================================================== #
                _, _, _, E = self.G(node_feature, edge, sensor)

                # if idx == 0:
                #     graph_embedding.append(E)
                # else:
                graph_embedding.append(torch.unsqueeze(E[-1], 0))

                del E
                del node_feature, edge
                torch.cuda.empty_cache()

                idx += 1

        print("Finish NN Part!")

        graph_embedding = torch.cat(graph_embedding)
        graph_embedding = graph_embedding.view(graph_embedding.shape[0],
                                               -1).cpu()
        residual = []

        for idx in range(len(self.subject_test)):
            start = int(self.subject_test[idx, 1]) - idx * (self.num_clips - 1)
            end = int(self.subject_test[idx,
                                        2]) - (idx + 1) * (self.num_clips - 1)

            if method == 'kernel':
                fd = skfda.representation.grid.FDataGrid(
                    graph_embedding[start:end + 1])
                n_neighbors = np.arange(1, 24)
                scale_factor = (
                    (fd.domain_range[0][1] - fd.domain_range[0][0]) /
                    len(fd.grid_points[0]))
                bandwidth = n_neighbors * scale_factor

                nw = val.SmoothingParameterSearch(ks.NadarayaWatsonSmoother(),
                                                  bandwidth)
                nw.fit(fd)
                nw_fd = nw.transform(fd)
                r = self.loss_function(torch.tensor(nw_fd.data_matrix),
                                       torch.tensor(fd.data_matrix),
                                       graph=False,
                                       device=self.device)
                residual.append(r)

        residual = torch.cat(residual).view(graph_embedding.shape[0],
                                            self.num_sensor_dev, -1)
        residual = torch.mean(residual, dim=-1)
        node_abnormal = torch.cat(node_abnormal)
        print(residual.shape)
        print(node_abnormal.shape)
        predict_result(residual, node_abnormal, "sensor")
        if graph_abnormal is not None:
            graph_predict = torch.mean(residual, dim=-1)
            graph_ab_list = torch.tensor(graph_ab_list)
            predict_result(graph_predict, graph_ab_list, 'graph')
    n_neighbors,
)
knn.fit(fd)
knn_fd = knn.transform(fd)

# Local linear regression kernel smoothing.
llr = val.SmoothingParameterSearch(
    ks.LocalLinearRegressionSmoother(),
    bandwidth,
)
llr.fit(fd)
llr_fd = llr.transform(fd)

# Nadaraya-Watson kernel smoothing.
nw = val.SmoothingParameterSearch(
    ks.NadarayaWatsonSmoother(),
    bandwidth,
)
nw.fit(fd)
nw_fd = nw.transform(fd)

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(
    n_neighbors,
    knn.cv_results_['mean_test_score'],
    label='k-nearest neighbors',
)
ax.plot(
    n_neighbors,
    llr.cv_results_['mean_test_score'],
Esempio n. 4
0
def k_smooth(y, smoothing_parameter=None):
    nw = ks.NadarayaWatsonSmoother(smoothing_parameter=smoothing_parameter)
    y = skfda.representation.grid.FDataGrid(y)
    nw.fit(y)
    nw_y = nw.transform(y)
    return nw_y.data_matrix.reshape(-1)