Пример #1
0
    def lag_module(params, use_filters):
        all_train_labels = load_data(
            os.path.join(params.save_data, "all_train_labels"))
        for ratio in params.ratios:
            time_start = time.time()
            if use_filters is True:
                filters = load_data(
                    os.path.join(params.save_data, "filters_" + str(ratio)))
            subset_label = all_train_labels[:int(all_train_labels.shape[0] *
                                                 ratio)].copy()
            for i in range(2, params.num_layers):  ## ATTENTION
                data = []
                for j in range(0, 50000, 5000):
                    data.append(
                        load_data(
                            os.path.join(params.save_data,
                                         'out_{}_{}'.format(j, i))))
                data = np.concatenate(data, axis=0)
                subset_data = data[:int(data.shape[0] * ratio)].copy()
                del data

                subset_data = np.reshape(subset_data,
                                         newshape=(subset_data.shape[0], -1))
                print("subset data shape:", subset_data.shape)
                if use_filters is True:
                    print("Number of selected channels:", np.sum(filters[i]))
                    subset_data = subset_data[:, filters[i]]

                lag = LAG(
                    num_clusters=[10, 10, 10, 10, 10, 10, 10, 10, 10, 10],
                    alpha=10,
                    learner=myLLSR(onehot=False))
                lag.fit(subset_data, subset_label)
                subset_predprob = lag.predict_proba(subset_data)
                save_data(
                    lag,
                    os.path.join(params.save_data,
                                 'LAG_{}_{}'.format(i, ratio)))
                save_data(
                    subset_predprob,
                    os.path.join(params.save_data,
                                 'lag_predict_{}_{}'.format(i, ratio)))
                print("RATIO=", ratio, " LAYER=", i, " DONE")
            print("Time cost - LAG:",
                  time.time() - time_start, "ratio=", ratio)
            save_data(
                subset_label,
                os.path.join(params.save_data,
                             'lag_labels_{}_{}'.format(i, ratio)))
idx1 = np.argsort(output1Feature)
slice1 = idx1[:4018]
output_1 = output_1.reshape(len(output_1), -1)
t1 = (np.transpose(output_1))
in_lag1 = t1[slice1]
in_lag1 = in_lag1.reshape(len(in_lag1), -1)
in_lag_1 = np.transpose(in_lag1)
print(" input feature shape before reshape: %s" % str(in_lag_1.shape))
print(" > This is a test example: ")
lag1 = LAG(encode='distance',
           num_clusters=[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
           alpha=10,
           learner=myLLSR(onehot=False))
lag1.fit(in_lag_1, label_train)
X_train_trans1 = lag1.transform(in_lag_1)
X_train_predprob1 = lag1.predict_proba(in_lag_1)
print(X_train_trans1.shape)
print("------- DONE -------\n")

#2nd LAG Layer
from sklearn.model_selection import train_test_split
from llsr import LLSR as myLLSR
from lag import LAG
idx2 = np.argsort(output2Feature)
slice2 = idx2[:2763]
output_2 = output_2.reshape(len(output_2), -1)
t2 = (np.transpose(output_2))
in_lag2 = t2[slice2]
in_lag2 = in_lag2.reshape(len(in_lag2), -1)
in_lag_2 = np.transpose(in_lag2)
print(" input feature shape before reshape: %s" % str(in_lag_2.shape))