コード例 #1
0
    def __init__(self, UCR_dataset_name ):
        super(SingleLayerConfig, self).__init__()

        X_train, y_train, X_test, y_test, series_size = get_dataset_with_series_size(dataset=UCR_dataset_name)
        self.X_train = X_train.reshape((-1, series_size, 1))
        self.X_test = X_test.reshape((-1, series_size, 1))
        y_train = y_train.reshape((-1, 1))
        y_test = y_test.reshape((-1, 1))
        self.y_train = one_hot_to_int(y_train)
        self.y_test = one_hot_to_int(y_test)




        # Input data
        self.train_count = len(self.X_train)  # 7352 training series
        self.test_data_count = len(self.X_test)  # 2947 testing series
        self.n_steps = len(self.X_train[0])  # 128 time_steps per series

        # Trainging
        self.learning_rate = 0.005
        self.lambda_loss_amount = 0.0015
        self.training_epochs = 100
        self.batch_size = 1500

        # LSTM structure
        self.n_inputs = len(self.X_train[0][0])
        self.n_hidden = 28 # nb of neurons inside the neural network
        self.n_classes = len(self.y_train[0])  # Final output classes


        self.model_name = "single_lstm_" + UCR_dataset_name
        self.log_folder_suffix = self.attach_log_suffix()
        self.logs_path = "/tmp/LSTM_logs/"+self.log_folder_suffix

        self.tensor_board_logging_enabled = True
        self.tensorboard_cmd = "tensorboard --logdir="+ self.logs_path
        self.model_desc_attched_string = self.attach_mdoel_desc()
        self.matplot_lib_enabled = True
        self.matplot_lib_for_accuracy =True
        self.matplot_lib_for_single_ybundle=False
コード例 #2
0
def one_hot_to_int(y):
    """convert label from dense to one hot
      argument:
        label: ndarray dense label ,shape: [sample_num,1]
      return:
        one_hot_label: ndarray  one hot, shape: [sample_num,n_class]
    """
    # e.g.: [[5], [0], [3]] --> [[0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0]]

    y = y.reshape(len(y))
    n_values = int(np.max(y)) + 1
    return np.eye(n_values)[np.array(y, dtype=np.int32)]  # Returns FLOATS


################################## load data and config ##################################
X_train, y_train, X_test, y_test, series_size = get_dataset_with_series_size(
    dataset='ElectricDevices')
X_train = X_train.reshape((-1, series_size, 1))
X_test = X_test.reshape((-1, series_size, 1))
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
y_train = one_hot_to_int(y_train)
y_test = one_hot_to_int(y_test)


class SingleLayerConfig(Config):
    """
    define a class to store parameters,
    the input should be feature mat of training and testing
    """
    def __init__(self):
        super(SingleLayerConfig, self).__init__()
        # New shape: a list of lenght "time_step" containing tensors of shape [batch_size, n_hidden]

        hidden = get_stacked_highway_LSTM_layers(hidden, config.n_inputs,
                                                 config.n_hidden, config,
                                                 keep_prob_for_dropout)

        # Final fully-connected activation logits
        # Get the last output tensor of the inner loop output series, of shape [batch_size, n_classes]
        last_hidden = tf.nn.dropout(hidden[-1], keep_prob_for_dropout)
        last_logits = relu_fc([last_hidden], config.n_hidden, config.n_classes,
                              config)[0]
        return last_logits


################################## load data and config ##################################
X_train, y_train, X_test, y_test, series_size = get_dataset_with_series_size(
    dataset='synthetic_control')

#X_train, y_train, X_test, y_test, series_size = get_dataset_with_series_size(dataset='ElectricDevices')
X_train = X_train.reshape((-1, series_size, 1))
X_test = X_test.reshape((-1, series_size, 1))
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
y_train = one_hot_to_int(y_train)
y_test = one_hot_to_int(y_test)


class HighwayLSTMOnUCRConfig(Config):
    def __init__(self):
        super(HighwayLSTMOnUCRConfig, self).__init__()
        self.train_count = len(X_train)  # 7352 training series
        self.test_data_count = len(X_test)  # 2947 testing series