Ejemplo n.º 1
0
    loss_hist = []

    # Train
    start = time.time()
    for t in range(5000):

        X_train_mini, y_train_mini = get_mini_batching(X_train, y_train,
                                                       size_batch)

        # Berechne die Vorhersage (foward step)
        outputs = model(X_train_mini)

        # Berechne den Fehler (Ausgabe des Fehlers alle 50 Iterationen)
        loss = criterion(outputs, y_train_mini.long())

        # Berechne die Gradienten und Aktualisiere die Gewichte (backward step)
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # Berechne den Fehler (Ausgabe des Fehlers alle 100 Iterationen)
        if t % 50 == 0:
            loss_hist.append(loss.item())
            #print(t, loss.item())

    print(time.time() - start)

    if args.s_model:
        name = f'logreg_{n_features}.sav'
        pre.save_model(model, name=name)
Ejemplo n.º 2
0
    #shuffle datafram in-place
    train_features = train_features.sample(frac=1).reset_index(drop=True)

    pre.set('training_data' + '_' + str(n_words), train_features)
    pre.save('training_data' + '_' + str(n_words))

    #transform the test data

    TEST_PATH_POS = 'test/pos/'
    TEST_PATH_NEG = 'test/neg/'

    pre.load_all_texts_from_directory(path=TEST_PATH_POS, name='raw_pos_test')
    pre.load_all_texts_from_directory(path=TEST_PATH_NEG, name='raw_neg_test')

    print('transform test data')

    texts_list_test = pd.concat(
        [pre.data['raw_pos_test'], pre.data['raw_neg_test']])
    test_features = text_transformer.transform(texts_list_test)
    test_features['target'] = np.append(
        np.ones(len(pre.data['raw_pos_test'])),
        np.zeros(len(pre.data['raw_neg_test'])))
    test_features = test_features.sample(frac=1).reset_index(drop=True)

    pre.set('test_data' + '_' + str(n_words), test_features)
    pre.save('test_data' + '_' + str(n_words))
    if args.s_model:
        name = f'word_transformer_{n_words}.sav'
        pre.save_model(text_transformer, name=name)