def model_train(model, val_version=21, cross_val=0, nb_epoch_per_record=2, nb_epochs=1000, batch_size=120, input_shape=(128, 128), normal_proc=True): # here is where we really load the data X_train, X_val, Y_train, Y_val = load_train_data(val_version=val_version, cross_val=cross_val, normal_proc=normal_proc) # Y_train = Y_train*100 # Y_val = Y_val*100 # here is where we really train the model # hf.train_model_multi_task(model, X_train,X_val,Y_train,Y_val, nb_epochs=nb_epochs, nb_epoch_per_record=nb_epoch_per_record, input_shape=input_shape, batch_size = batch_size, lr_max = max_lr) hf.train_model(model, X_train, X_val, Y_train, Y_val, nb_epochs=nb_epochs, nb_epoch_per_record=nb_epoch_per_record, input_shape=input_shape, batch_size=batch_size, method='count_ception')
def regressions_checker(): n = 240000 d = 3 data_range = 100 data = np.floor(np.random.rand(n, d) * data_range) labels = np.floor(np.random.rand(n, 1) * data_range) weights = np.ones(n) Pset = WeightedSet(data, weights, labels) for solver in ["lasso", "ridge", "elastic"]: #########RIDGE REGRESSION############# clf = get_new_clf(solver) time_coreset, clf_coreset = coreset_train_model(Pset, clf, solver=solver) score_coreset = test_model(Pset, clf) clf = get_new_clf(solver) time_real, clf_real = train_model(Pset, clf) score_real = test_model(Pset, clf) """ print (" solver: {}\nscore_diff = {}\n---->coef diff = {}\n---->coreset_time = {}\n---->data time = {}".format( solver, np.abs(score_coreset - score_real), np.sum(np.abs(clf_real.coef_ - clf_coreset.coef_)), time_coreset, time_real)) """ if np.abs(score_coreset - score_real) > small_number: print("Not good. Error in LMS CORESET")
param.requires_grad = False num_ftrs = model_ft.fc.in_features model_ft.fc = nn.Linear(num_ftrs, 10) if use_gpu: model_ft = model_ft.cuda() criterion = nn.CrossEntropyLoss() # Observe that all parameters are being optimized optimizer_ft = optim.Adam(model_ft.fc.parameters(), lr=0.001) model = train_model(model_ft, dataloders, dataset_sizes, criterion, optimizer_ft, num_epochs=5, use_gpu=use_gpu) optimizer_ft = optim.Adam(model.fc.parameters(), lr=0.0001) model = train_model(model, dataloders, dataset_sizes, criterion, optimizer_ft, num_epochs=10, use_gpu=use_gpu) optimizer_ft = optim.Adam(model.fc.parameters(), lr=0.00005) model = train_model(model,
# training data selection train_folder = experiment + '/' + 'stage' + str(stage) + '/' train_pool_file = train_folder + 'train_pool.txt' #unnot_pool_file = train_folder+'unnot_pool.txt' f = open(train_pool_file) train_pool_list = [] for line in f.readlines(): train_pool_list.append(int(float(line.split('\n')[0]))) X_sl = X_train[train_pool_list, :, :] Y_sl = Y_train[train_pool_list, :, :] batch_size = int(X_train.shape[0] * 8 / 20) # scale the ground truth by multiplying 100 Y_train = Y_train * 100 Y_val = Y_val * 100 model_name = train_folder + 'round-' + str( round) + '-dt-' + date + '-lr-' + str(lr) + '-scaled' + '-batch-' + str( batch_size) + '-v' + str(val_version) + '-ResFCN' # model_name = '9.30-'+'lr-'+str(lr)+ '-scaled'+'-batch-'+str(batch_size)+'-v'+str(val_version)+'-FCN' model.name = model_name hf.train_model(model, X_sl, X_val, Y_sl, Y_val, nb_epochs=nb_epochs, nb_epoch_per_record=nb_epoch_per_record, input_shape=input_shape[0], batch_size=batch_size)
import pandas as pd import numpy as np from helper_functions import create_X, create_y_train, train_model, predict, score train = pd.read_csv('data/Train.csv', parse_dates=['saledate']) test = pd.read_csv('data/Test.csv', parse_dates=['saledate']) X_train = create_X(train) X_test = create_X(test) y_train = create_y_train(train) model = train_model(X_train, y_train) submit = predict(model, test, X_test, 'model_1') y_test = pd.read_csv('data/do_not_open/test_soln.csv') print(score(submit, y_test)) # final score: 0.4102042700770253
======= from helper_functions import create_X, create_y_train, train_model, predict, score >>>>>>> 0e453a6a82a8c1a46c61f3419a174391e7c7affd train = pd.read_csv('data/Train.csv', parse_dates=['saledate']) test = pd.read_csv('data/Test.csv', parse_dates=['saledate']) X_train = create_X(train) X_test = create_X(test) y_train = create_y_train(train) <<<<<<< HEAD X_train_normalized, X_test_normalized = normalize_X(X_train, X_test) model_linear = train_model(X_train, y_train, LinearRegression()) model_ridge = train_model(X_train_normalized, y_train, Ridge()) model_lasso = train_model(X_train_normalized, y_train, Lasso(alpha=0.00005, max_iter=120000)) submit_linear = predict(model_linear, test, X_test, 'model_lin') submit_ridge = predict(model_ridge, test, X_test_normalized, 'model_rid') submit_lasso = predict(model_lasso, test, X_test_normalized, 'model_las') y_test = pd.read_csv('data/do_not_open/test_soln.csv') print('Linear: ', score(submit_linear, y_test), '; Ridge: ', score(submit_ridge, y_test), '; Lasso: ', score(submit_lasso, y_test)) # Linear: 0.40826129534246886 ; Ridge: 0.40822991882415727 ; Lasso: 0.40834486305959367 # Pick Ridge ======= model = train_model(X_train, y_train)
#get data loaders trainloader, testloader, validationloader, train_data = hf.get_loaders( data_directory) #get cat_to_name with open('cat_to_name.json', 'r') as f: cat_to_name = json.load(f) # Building and training the classifier ################# model = getattr(models, arch)(pretrained=True) for param in model.parameters(): param.requires_grad = False #Define a new, untrained feed-forward network as a classifier, using ReLU activations and dropout model, criterion, optimizer = hf.define_untrained_network( model, model.classifier[0].in_features, hidden_units, len(cat_to_name), learning_rate, device) #input_size, hidden_sizes, output_size # Train Model trained_model = hf.train_model(model, epochs, trainloader, validationloader, device, optimizer, criterion) # Test Trained Model hf.test_trained_model(trained_model, testloader, criterion, device) # Save hf.save_model_architecture_needs(train_data, epochs, optimizer, model, save_dir, arch, 'model_architecture_needs.pth') hf.save_model_checkpoint(model, save_dir, 'model_checkpoint.pth')
parsed_results = argparser.parse_args() data_directory = parsed_results.data_directory checkpoint = parsed_results.save_path pretrained_model = parsed_results.pretrained_model lr = float(parsed_results.lr) hidden_units = int(parsed_results.hidden_units) epochs = int(parsed_results.num_epochs) device_flag = bool(parsed_results.use_gpu) # Load datasets train_data,test_data,validationdata,trainloader,testloader,validationloader = data_load(data_directory) # Load pre-trained model model = getattr(models,pretrained_model)(pretrained = True) # Modify classifier based on current dataset model = modify_classifier(model,model.classifier[0].in_features,device_flag) # Initialize loss and optimizer criterion = nn.NLLLoss() optimizer = optim.Adam(model.classifier.parameters(), lr) # Train the model on the dataset model, optimizer = train_model(3,50,model,trainloader,criterion,optimizer,validationloader,device_flag) # Use the test dataset for accuracy test_accuracy(model,testloader,device_flag) # Save the model onto disk save_model(model,checkpoint,train_data,optimizer)