Exemple #1
0
    def fit(self, sess, h_np, b_np, h_len, b_len, y, dev_h, dev_b, dev_h_len,
            dev_b_len, dev_y):  #M
        #losses = []
        losses_epochs = []  #M
        dev_performances_epochs = []  # M
        dev_predictions_epochs = []  #M
        dev_predicted_classes_epochs = []  #M

        for epoch in range(self.config.n_epochs):
            print('-------new epoch---------')
            loss = self.run_epoch(sess, h_np, b_np, h_len, b_len, y)

            # Computing predictions #MODIF
            dev_predictions = self.predict_on_batch(sess, dev_h, dev_b,
                                                    dev_h_len, dev_b_len)

            # Computing development performance #MODIF
            dev_predictions = softmax(np.array(dev_predictions))
            dev_predicted_classes = np.argmax(dev_predictions, axis=1)
            dev_performance = get_performance(dev_predicted_classes,
                                              dev_y,
                                              n_classes=4)

            # Adding to global outputs #MODIF
            dev_predictions_epochs.append(dev_predictions)
            dev_predicted_classes_epochs.append(dev_predicted_classes)
            dev_performances_epochs.append(dev_performance)
            losses_epochs.append(loss)

            print('EPOCH: ', epoch, ', LOSS: ', np.mean(loss))

        return losses_epochs, dev_performances_epochs, dev_predicted_classes_epochs, dev_predictions_epochs
    def fit(self, sess, train, dev_data_np, dev_seqlen,
            dev_labels):  # MODIF # CAREFUL DEV/dev
        '''
            Returns LISTS:
                - losses_epochs
                - dev_performances_epochs
                - dev_predictions_epochs
                - dev_predicted_classes_epochs
        '''
        losses_epochs = []  #M
        dev_performances_epochs = []  # MODIF
        dev_predictions_epochs = []  #M
        dev_predicted_classes_epochs = []  #M
        for epoch in range(self.config.n_epochs):
            logger.info("Epoch %d out of %d", epoch + 1, self.config.n_epochs)
            loss = self.run_epoch(sess, train)

            # Computing predictions # MODIF
            dev_predictions = self.predict_on_batch(sess, dev_data_np,
                                                    dev_seqlen)  #OUCH

            # Computing development performance #MODIF
            dev_predictions = softmax(np.array(dev_predictions))
            dev_predicted_classes = np.argmax(dev_predictions, axis=1)
            dev_performance = get_performance(dev_predicted_classes,
                                              dev_labels,
                                              n_classes=4)

            # Adding to global outputs #MODIF
            dev_predictions_epochs.append(dev_predictions)
            dev_predicted_classes_epochs.append(dev_predicted_classes)
            dev_performances_epochs.append(dev_performance)
            losses_epochs.append(loss)

        return losses_epochs, dev_performances_epochs, dev_predicted_classes_epochs, dev_predictions_epochs
    def fit(self, sess, h_np, b_np, h_len, b_len, y, dev_h, dev_b, dev_h_len,
            dev_b_len, dev_y):  #M
        #losses = []
        losses_epochs = []  #M
        dev_performances_epochs = []  # M
        dev_predictions_epochs = []  #M
        dev_predicted_classes_epochs = []  #M
        LABELS = ['agree', 'disagree', 'discuss', 'unrelated']

        for epoch in range(self.config.n_epochs):
            print('-------new epoch---------')
            loss = self.run_epoch(sess, h_np, b_np, h_len, b_len, y)

            # Computing predictions #MODIF
            dev_predictions = self.predict_on_batch(sess, dev_h, dev_b,
                                                    dev_h_len, dev_b_len)

            # Computing development performance #MODIF
            dev_predictions = softmax(np.array(dev_predictions))
            dev_predicted_classes = np.argmax(dev_predictions, axis=1)
            dev_performance = get_performance(dev_predicted_classes,
                                              dev_y,
                                              n_classes=4)

            # Adding to global outputs #MODIF
            dev_predictions_epochs.append(dev_predictions)
            dev_predicted_classes_epochs.append(dev_predicted_classes)
            dev_performances_epochs.append(dev_performance)
            losses_epochs.append(loss)

            print("=================================================")

            for i, (x, y) in enumerate(zip(dev_y, dev_predicted_classes)):
                print("Given class -> ", LABELS[dev_y[i]],
                      "Predicted class -> ", LABELS[dev_predicted_classes[i]])

            print("=================================================")

            print("prediction epochs: ", dev_performances_epochs)
            print("predicted classes epochs: ", dev_predicted_classes_epochs)
            print("performances epochs: ", dev_performances_epochs)
            print('EPOCH: ', epoch, ', LOSS: ', np.mean(loss))

        return losses_epochs, dev_performances_epochs, dev_predicted_classes_epochs, dev_predictions_epochs
Exemple #4
0
    def fit(self, sess, h_np, b_np, h_len, b_len, y, dev_h, dev_b, dev_h_len,
            dev_b_len, dev_y):  #M
        #losses = []
        data_path = '/home/neha/sem2/nlu/project/stance_detection-master/code/models/lstm_conditional/'
        model_save_name = 'lstm-conditional-300blen-2l-50e'
        losses_epochs = []  #M
        dev_performances_epochs = []  # M
        dev_predictions_epochs = []  #M
        dev_predicted_classes_epochs = []  #M
        saver = tf.train.Saver()

        for epoch in range(self.config.n_epochs):
            print('-------new epoch---------')
            loss = self.run_epoch(sess, h_np, b_np, h_len, b_len, y)

            # Computing predictions #MODIF
            dev_predictions = self.predict_on_batch(sess, dev_h, dev_b,
                                                    dev_h_len, dev_b_len)

            # Computing development performance #MODIF
            dev_predictions = softmax(np.array(dev_predictions))
            dev_predicted_classes = np.argmax(dev_predictions, axis=1)
            dev_performance = get_performance(dev_predicted_classes,
                                              dev_y,
                                              n_classes=4)

            # Adding to global outputs #MODIF
            dev_predictions_epochs.append(dev_predictions)
            dev_predicted_classes_epochs.append(dev_predicted_classes)
            dev_performances_epochs.append(dev_performance)
            losses_epochs.append(loss)

            print('EPOCH: ', epoch, ', LOSS: ', np.mean(loss))
            if epoch % 5 == 0:
                saver.save(sess,
                           data_path + model_save_name,
                           global_step=epoch)

#        pred_prob=pd.DataFrame(dev_predictions_epochs[1])
#        pred_prob.to_csv('/home/neha/sem2/nlu/project/stance_detection-master/code/output/lstm_conditional'+'/pred_prob_' + '.csv',index = False)
        saver.save(sess, data_path + model_save_name + '-final')

        return losses_epochs, dev_performances_epochs, dev_predicted_classes_epochs, dev_predictions_epochs
Exemple #5
0
    def compute_pred(self, sess, dev_h, dev_b, dev_h_len, dev_b_len,
                     dev_y):  #M
        #losses = []
        losses_epochs = []  #M
        dev_performances_epochs = []  # M
        dev_predictions_epochs = []  #M
        dev_predicted_classes_epochs = []  #M

        #        for epoch in range(self.config.n_epochs):
        print('ready to predict')
        #        loss = self.run_epoch(sess, h_np, b_np, h_len, b_len, y)
        loss = [100]

        # Computing predictions #MODIF
        dev_predictions = self.predict_on_batch(sess, dev_h, dev_b, dev_h_len,
                                                dev_b_len)

        # Computing development performance #MODIF
        dev_predictions = softmax(np.array(dev_predictions))
        dev_predicted_classes = np.argmax(dev_predictions, axis=1)
        dev_performance = get_performance(dev_predicted_classes,
                                          dev_y,
                                          n_classes=4)

        # Adding to global outputs #MODIF
        dev_predictions_epochs.append(dev_predictions)
        dev_predicted_classes_epochs.append(dev_predicted_classes)
        dev_performances_epochs.append(dev_performance)
        losses_epochs.append(loss)

        print('Prediction complete')

        pred_prob = pd.DataFrame(dev_predictions_epochs[0])
        pred_prob.to_csv(
            '/home/neha/sem2/nlu/project/stance_detection-master/code/output/lstm_conditional'
            + '/pred_prob_' + '.csv',
            index=False)

        return losses_epochs, dev_performances_epochs, dev_predicted_classes_epochs, dev_predictions_epochs