コード例 #1
0
def get_num_and_data(args):

    if not args.num:
        print('use test data')
        pre = Preprocessing('digits')
        pre.load_data(filename='test.csv', name='test')

        X_test = torch.tensor(pre.get('test').drop(columns=['0']).values,
                              device=device,
                              dtype=dtype)
        y_test = torch.tensor(pre.get('test')['0'].values,
                              device=device,
                              dtype=dtype)

        index = random.randint(0, len(y_test))
        print('index {}'.format(index))

        num = int(y_test[index].item())
        print('real_numer {}'.format(num))
        data_to_predict = X_test[index:index + 1, :]
        return num, data_to_predict

    else:
        print('use written images')
        num = int(args.num)
        im_imp = Image_Importer('digits')
        im_imp.load_image_as_grey(num)

        print('real_numer {}'.format(num))
        data_to_predict = im_imp.get_image_as_256px_array(num)

        return num, data_to_predict
コード例 #2
0
import matplotlib.pyplot as plt

if not __name__ == '__main_':

    parser = argparse.ArgumentParser(description='Digits')
    parser.add_argument('--s_model', default=True, help='save trained model')

    args=parser.parse_args()

    n_classes = 10
    n_epochs = 200

    pre = Preprocessing('digits')
    pre.load_data(filename='train.csv', name='train')

    X_df = pre.get(name='train').drop(columns=['0'])
    y_df = pre.get(name='train')['0']

    dtype = torch.float
    device = torch.device("cpu")

    model_name = 'logreg_digits'
    model = LogReg(model_name, 256, n_classes)

    learning_rate = 0.0001
    batch_size = 32

    train_classifier = TrainClassifier(model, X_df, y_df)
    trained_model , optimizer, criterion, loss_hist, loss_val_hist, best_param = train_classifier.run_train(n_epochs = n_epochs, lr=learning_rate, batch_size=batch_size)
    pre.save_results(loss_hist, loss_val_hist, f'{model_name}')
コード例 #3
0
if not __name__ == '__main_':

    parser = argparse.ArgumentParser(description='IMDBData')
    parser.add_argument('--n_feat', default=1000, help='number of features')
    parser.add_argument('--s_model', default=False, help='save trained model')

    args=parser.parse_args()

    pre = Preprocessing('IMDB')

    n_classes = 2
    n_features = int(args.n_feat)
    n_epochs = 100
    pre.load_data(filename=f'training_data_{n_features}.csv', name='training_data')

    X_df = pre.get(name='training_data').drop(columns=['target'])
    y_df = pre.get(name='training_data')['target']

    model = LogReg('log_reg', n_features, n_classes)

    train_classifier = TrainClassifier(model, X_df, y_df)
    trained_model, optimizer, criterion, loss_hist, loss_validate_hist = train_classifier.run_train(n_epochs = n_epochs)
    pre.save_results(loss_hist, loss_validate_hist, f'log_reg_{100}')

    m_exporter = ModelExporter('IMDB')
    m_exporter.save_nn_model(trained_model, optimizer, n_features, n_classes, n_epochs)

    ##teeeeeest part
    pre.load_data(filename=f'test_data_{n_features}.csv', name='test_data')

    X_test_df = pre.get(name='test_data').drop(columns=['target'])
コード例 #4
0
import pickle

import torch
import torch.nn as nn

from preprocessing_utils import Preprocessing

if not __name__ == '__main_':

    pre = Preprocessing('digits')
    pre.load_data(filename='test.csv', name='test')

    dtype = torch.float
    device = torch.device("cpu")

    X_test = torch.tensor(pre.get('test').drop(columns=['0']).values,
                          device=device,
                          dtype=dtype)
    y_test = torch.tensor(pre.get('test')['0'].values,
                          device=device,
                          dtype=dtype)
    #print(y_test)

    filename = "../data/digits/softmax_torch_digits_model.sav"
    model = pickle.load(open(filename, 'rb'))

    y_pred = model(X_test)

    softmax = torch.nn.Softmax(dim=1)
    y_pred = softmax(y_pred).argmax(1)
    #print(y_pred)
コード例 #5
0
from preprocessing_utils import Preprocessing, ModelImporter

import torch

if not __name__ == '__main_':

    pre = Preprocessing('digits')
    pre.load_data(filename='test.csv', name='test')

    X_df = pre.get(name='test').drop(columns=['0'])
    y_df = pre.get(name='test')['0']

    dtype = torch.float
    device = torch.device("cpu")

    model_name = 'cnn_digits'

    m_importer = ModelImporter('digits')

    model = m_importer.load_nn_model(model_name, 0, 10, 100)

    X_test = model.reshape_data(
        torch.tensor(X_df.values, device=device, dtype=dtype))
    y_test = torch.tensor(y_df.values, device=device, dtype=torch.long)

    y_pred = model(X_test).argmax(1)

    accuracy_soft = (y_pred == y_test).float().mean()

    print(f'test accuracy {accuracy_soft.item()}')
コード例 #6
0
    y_train_mini = y_train.view(len(y_train), 1)[idx, :]

    return X_train_mini, y_train_mini.view(len(y_train_mini))


if not __name__ == '__main_':
    parser = argparse.ArgumentParser(description='digits')
    parser.add_argument('--s_model', default=False, help='save trained model')

    args = parser.parse_args()

    pre = Preprocessing('digits')
    pre.load_data(filename='train.csv', name='train')

    X_train_df, X_val_df, y_train_df, y_val_df = train_test_split(
        pre.get('train').drop(columns=['0']),
        pre.get('train')['0'],
        test_size=0.01)

    #transfom to torch striuctures
    dtype = torch.float
    device = torch.device("cpu")

    X_train = torch.tensor(X_train_df.values, device=device, dtype=dtype)
    y_train = torch.tensor(y_train_df.values, device=device, dtype=dtype)

    # Softmax regression model

    n_features = X_train.size()[1]
    n_classes = len(np.unique(y_train.round().numpy()))
コード例 #7
0
import matplotlib.pyplot as plt

if not __name__ == '__main_':

    parser = argparse.ArgumentParser(description='Digits')
    parser.add_argument('--s_model', default=True, help='save trained model')

    args = parser.parse_args()

    n_epochs = 50

    pre = Preprocessing('digits')
    pre.load_data(filename='train.csv', name='train')

    X_train_df = pre.get(name='train').drop(columns=['0'])
    y_train_df = pre.get(name='train')['0']

    dtype = torch.float
    device = torch.device("cpu")

    H1 = 128
    n_features = len(X_train_df.columns)
    n_features_encoded = 1
    print(f'features {n_features}')
    print(f'H1 {H1}')
    print(f'n_features_encoded {n_features_encoded}')

    model_name = 'ann_encoder'
    model = AnnAutoencoder(model_name, n_features, H1, n_features_encoded)
コード例 #8
0
from sklearn.model_selection import train_test_split

if not __name__ == '__main_':

    pre_train = Preprocessing('digits')
    kwarg = {'header': None, 'sep': ' '}
    pre_train.load_data(filename='zip.train', name='raw', **kwarg)

    pre_train.cleanup(name='raw',
                      drop_duplicates=True,
                      dropna={
                          'axis': 1,
                          'thresh': 2
                      })

    print(pre_train.get('clean').head())

    #classes = ['0_0.0', '0_1.0', '0_2.0', '0_3.0', '0_4.0', '0_5.0', '0_6.0', '0_7.0', '0_8.0', '0_9.0']
    X = pre_train.get('clean').drop(columns=[0])
    y = pre_train.get('clean')[0]

    pre_train.set(name='train', value=pre_train.get('clean'))

    pre_train.save(name='train')

    pre_test = Preprocessing('digits')
    kwarg = {'header': None, 'sep': ' '}
    pre_test.load_data(filename='zip.test', name='raw', **kwarg)

    pre_test.cleanup(name='raw',
                     drop_duplicates=True,