Example #1
0
    def build_model(self, generate=False):
        '''
        Build a Keras model if one does not yet exist.

        Helper function for generate().
        '''

        if generate:
            t = self.args.generation_timesteps
        else:
            t = self.data_gen.max_seq_len
        if self.args.mrnn:
            m = models.MRNN(self.args.embed_size,
                            self.args.hidden_size,
                            self.vocab_len,
                            self.args.dropin,
                            self.args.optimiser,
                            self.args.l2reg,
                            hsn_size=self.hsn_size,
                            weights=self.args.checkpoint,
                            gru=self.args.gru,
                            clipnorm=self.args.clipnorm,
                            t=t)
        else:
            m = models.NIC(self.args.embed_size,
                           self.args.hidden_size,
                           self.vocab_len,
                           self.args.dropin,
                           self.args.optimiser,
                           self.args.l2reg,
                           hsn_size=self.hsn_size,
                           weights=self.args.checkpoint,
                           gru=self.args.gru,
                           clipnorm=self.args.clipnorm,
                           t=t)

        self.model = m.buildKerasModel(use_sourcelang=self.use_sourcelang,
                                       use_image=self.use_image)
Example #2
0
if FLAGS.algorithm == 'RNN':
    model = models.RNN(input_size=FLAGS.input_size,
                       hidden_size=FLAGS.hidden_size,
                       output_size=FLAGS.output_size)
elif FLAGS.algorithm == 'LSTM':
    model = models.LSTM(input_size=FLAGS.input_size,
                        hidden_size=FLAGS.hidden_size,
                        output_size=FLAGS.output_size)
elif FLAGS.algorithm == 'mRNN_fixD':
    model = models.MRNNFixD(input_size=FLAGS.input_size,
                            hidden_size=FLAGS.hidden_size,
                            output_size=FLAGS.output_size,
                            k=FLAGS.K)
elif FLAGS.algorithm == 'mRNN':
    model = models.MRNN(input_size=FLAGS.input_size,
                        hidden_size=FLAGS.hidden_size,
                        output_size=FLAGS.output_size,
                        k=FLAGS.K)
elif FLAGS.algorithm == 'mLSTM_fixD':
    model = models.MLSTMFixD(input_size=FLAGS.input_size,
                             hidden_size=FLAGS.hidden_size,
                             output_size=FLAGS.output_size,
                             k=FLAGS.K)
elif FLAGS.algorithm == 'mLSTM':
    model = models.MLSTM(input_size=FLAGS.input_size,
                         hidden_size=FLAGS.hidden_size,
                         output_size=FLAGS.output_size,
                         k=FLAGS.K)
elif FLAGS.algorithm.startswith('DORNN'):
    model = dornn.DORNN(input_size=FLAGS.input_size,
                        hidden_size=FLAGS.hidden_size,
                        output_size=FLAGS.output_size,
Example #3
0
def main():
    batch_size = 1
    start = 0
    end = 100
    # read data
    df_data = pd.read_csv('data/' + FLAGS.dataset + '.csv')
    # split train/val/test
    if FLAGS.dataset == 'tree7':
        train_size = 2500
        validate_size = 1000
    if FLAGS.dataset == 'DJI':
        train_size = 2500
        validate_size = 1500
    if FLAGS.dataset == 'traffic':
        train_size = 1200
        validate_size = 200
    if FLAGS.dataset == 'arfima':
        train_size = 2000
        validate_size = 1200
    rmse_list = []
    mae_list = []
    for i in range(start, end):
        seed = i
        print('seed ----------------------------------', seed)
        x = np.array(df_data['x'])
        y = np.array(df_data['x'])
        x = x.reshape(-1, FLAGS.input_size)
        y = y.reshape(-1, FLAGS.output_size)
        # normalize the data
        scaler = MinMaxScaler(feature_range=(0, 1))
        x = scaler.fit_transform(x)
        y = scaler.fit_transform(y)
        # use this function to prepare the data for modeling
        data_x, data_y = create_dataset(x, y)

        # split into train and test sets
        train_x, train_y = data_x[0:train_size], data_y[0:train_size]
        validate_x, validate_y = data_x[train_size:train_size +
                                                    validate_size], \
                                 data_y[train_size:train_size +
                                                   validate_size]
        test_x, test_y = data_x[train_size + validate_size:len(data_y)], \
                         data_y[train_size + validate_size:len(data_y)]

        # reshape input to be [time steps,samples,features]
        train_x = np.reshape(train_x,
                             (train_x.shape[0], batch_size, FLAGS.input_size))
        validate_x = np.reshape(
            validate_x, (validate_x.shape[0], batch_size, FLAGS.input_size))

        test_x = np.reshape(test_x,
                            (test_x.shape[0], batch_size, FLAGS.input_size))
        train_y = np.reshape(train_y,
                             (train_y.shape[0], batch_size, FLAGS.output_size))
        validate_y = np.reshape(
            validate_y, (validate_y.shape[0], batch_size, FLAGS.output_size))
        test_y = np.reshape(test_y,
                            (test_y.shape[0], batch_size, FLAGS.output_size))

        torch.manual_seed(seed)
        # initialize model
        if FLAGS.algorithm == 'RNN':
            model = models.RNN(input_size=FLAGS.input_size,
                               hidden_size=FLAGS.hidden_size,
                               output_size=FLAGS.output_size)
        elif FLAGS.algorithm == 'LSTM':
            model = models.LSTM(input_size=FLAGS.input_size,
                                hidden_size=FLAGS.hidden_size,
                                output_size=FLAGS.output_size)
        elif FLAGS.algorithm == 'mRNN_fixD':
            model = models.MRNNFixD(input_size=FLAGS.input_size,
                                    hidden_size=FLAGS.hidden_size,
                                    output_size=FLAGS.output_size,
                                    k=FLAGS.K)
        elif FLAGS.algorithm == 'mRNN':
            model = models.MRNN(input_size=FLAGS.input_size,
                                hidden_size=FLAGS.hidden_size,
                                output_size=FLAGS.output_size,
                                k=FLAGS.K)
        elif FLAGS.algorithm == 'mLSTM_fixD':
            model = models.MLSTMFixD(input_size=FLAGS.input_size,
                                     hidden_size=FLAGS.hidden_size,
                                     output_size=FLAGS.output_size,
                                     k=FLAGS.K)
        elif FLAGS.algorithm == 'mLSTM':
            model = models.MLSTM(input_size=FLAGS.input_size,
                                 hidden_size=FLAGS.hidden_size,
                                 output_size=FLAGS.output_size,
                                 k=FLAGS.K)
        else:
            print('Algorithm selection ERROR!!!')
        criterion = nn.MSELoss()
        optimizer = optim.Adam(model.parameters(), lr=FLAGS.lr)
        best_loss = np.infty
        best_train_loss = np.infty
        stop_criterion = 1e-5
        rec = np.zeros((FLAGS.epochs, 3))
        epoch = 0
        val_loss = -1
        train_loss = -1
        cnt = 0

        def train():
            model.train()
            optimizer.zero_grad()
            target = torch.from_numpy(train_y).float()
            output, hidden_state = model(torch.from_numpy(train_x).float())
            with torch.no_grad():
                val_y, _ = model(
                    torch.from_numpy(validate_x).float(), hidden_state)
                target_val = torch.from_numpy(validate_y).float()
                val_loss = criterion(val_y, target_val)

            loss = criterion(output, target)
            loss.backward()
            optimizer.step()
            return loss, val_loss

        def compute_test(best_model):
            model = best_model
            train_predict, hidden_state = model(to_torch(train_x))
            train_predict = train_predict.detach().numpy()
            val_predict, hidden_state = model(to_torch(validate_x),
                                              hidden_state)
            test_predict, _ = model(to_torch(test_x), hidden_state)
            test_predict = test_predict.detach().numpy()
            # invert predictions
            test_predict_r = scaler.inverse_transform(test_predict[:, 0, :])
            test_y_r = scaler.inverse_transform(test_y[:, 0, :])
            # calculate error
            test_rmse = math.sqrt(
                mean_squared_error(test_y_r[:, 0], test_predict_r[:, 0]))
            test_mape = (abs((test_predict_r[:, 0] - test_y_r[:, 0]) /
                             test_y_r[:, 0])).mean()
            test_mae = mean_absolute_error(test_predict_r[:, 0], test_y_r[:,
                                                                          0])
            return test_rmse, test_mape, test_mae

        while epoch < FLAGS.epochs:
            _time = time.time()
            loss, val_loss = train()
            if val_loss < best_loss:
                best_loss = val_loss
                best_epoch = epoch
                best_model = deepcopy(model)
            # stop_criteria = abs(criterion(val_Y, target_val) - val_loss)
            if (best_train_loss - loss) > stop_criterion:
                best_train_loss = loss
                cnt = 0
            else:
                cnt += 1
            if cnt == FLAGS.patience:
                break
            # save training records
            time_elapsed = time.time() - _time
            rec[epoch, :] = np.array([loss, val_loss, time_elapsed])
            print("epoch: {:2.0f} train_loss: {:2.5f} val_loss: {:2.5f} "
                  "time: {:2.1f}s".format(epoch, loss.item(), val_loss.item(),
                                          time_elapsed))
            epoch = epoch + 1

        # make predictions
        test_rmse, test_mape, test_mae = compute_test(best_model)

        rmse_list.append(test_rmse)
        mae_list.append(test_mae)
        print('RMSE:{}'.format(rmse_list))
        print('MAE:{}'.format(mae_list))
Example #4
0
    def train_model(self):
        '''
        Initialise the data generator to process the data in a memory-friendly
        manner. Then build the Keras model, given the user-specified arguments
        (or the initial defaults). Train the model for self.args.max_epochs
        and return the training and validation losses.

        The losses object contains a history variable. The history variable is
        a dictionary with a list of training and validation losses:

        losses.history.['loss']
        losses.history.['val_loss']
        '''

        if not self.use_sourcelang:
            hsn_size = 0
        else:
            hsn_size = self.data_generator.hsn_size  # ick

        if self.args.mrnn:
            m = models.MRNN(self.args.embed_size,
                            self.args.hidden_size,
                            self.V,
                            self.args.dropin,
                            self.args.optimiser,
                            self.args.l2reg,
                            hsn_size=hsn_size,
                            weights=self.args.init_from_checkpoint,
                            gru=self.args.gru,
                            clipnorm=self.args.clipnorm,
                            t=self.data_generator.max_seq_len,
                            lr=self.args.lr)
        else:
            m = models.NIC(self.args.embed_size,
                           self.args.hidden_size,
                           self.V,
                           self.args.dropin,
                           self.args.optimiser,
                           self.args.l2reg,
                           hsn_size=hsn_size,
                           weights=self.args.init_from_checkpoint,
                           gru=self.args.gru,
                           clipnorm=self.args.clipnorm,
                           t=self.data_generator.max_seq_len,
                           lr=self.args.lr)

        model = m.buildKerasModel(use_sourcelang=self.use_sourcelang,
                                  use_image=self.use_image)

        callbacks = CompilationOfCallbacks(self.data_generator.word2index,
                                           self.data_generator.index2word,
                                           self.args,
                                           self.args.dataset,
                                           self.data_generator,
                                           use_sourcelang=self.use_sourcelang,
                                           use_image=self.use_image)

        train_generator = self.data_generator.random_generator('train')
        train_size = self.data_generator.split_sizes['train']
        val_generator = self.data_generator.fixed_generator('val')
        val_size = self.data_generator.split_sizes['val']

        losses = model.fit_generator(generator=train_generator,
                                     samples_per_epoch=train_size,
                                     nb_epoch=self.args.max_epochs,
                                     verbose=1,
                                     callbacks=[callbacks],
                                     nb_worker=1,
                                     validation_data=val_generator,
                                     nb_val_samples=val_size)

        return losses