def main():
    user_input = get_input_args()

    image_datasets, dataloaders = utilfunc.create_dataloaders(
        user_input.data_dir)
    model = modfunc.create_model(user_input.arch,
                                 output_size=user_input.outsize,
                                 change_classifier=user_input.change_class,
                                 nodes_per_hlayer=user_input.npl)

    optimizer = optim.Adam(model.classifier.parameters(), lr=user_input.lr)
    criterion = nn.NLLLoss()

    network_training(model=model,
                     dataloaders=dataloaders,
                     epochs=user_input.epochs,
                     learning_rate=user_input.lr,
                     with_gpu=user_input.gpu)

    modfunc.save_checkpoint(filename=user_input.save_dir,
                            model=model,
                            image_datasets=image_datasets,
                            architecture=user_input.arch,
                            output_size=user_input.outsize,
                            hidden_layers=user_input.npl,
                            learning_rate=user_input.lr,
                            optimizer=optimizer,
                            epochs=user_input.epochs)
コード例 #2
0
ファイル: main.py プロジェクト: Tahlor/housing-prices
def main(path_training,
         path_test,
         training_test_seed=None,
         output_path=r"./output.csv",
         model_type=MODEL_TYPE,
         exclude_misc=EXC_MISC_VAL,
         keep_variables=None):
    """Processes data, predicts housing prices, saves predictions.

    Args:
        path_training  (str): Path to training data.
        path_test (str): Path to test data.
        training_test_seed (int, optional): Random seed for internal training/test split.
        output_path ( str): Location to save predicitions

    Returns:
        None
    """

    # Import Features
    training_df = import_data.process_data(path_training)
    test_df = import_data.process_data(path_test)

    # Preprocess features
    features, targets, test_features, _ = prep_features.main(
        training_df,
        test_df,
        use_log=USE_LOG,
        variable_combinations=keep_variables)

    # Optionally pull misc value out of sale price and add back in after
    targets = targets.drop(["SalePriceMiscVal"], axis=1)

    if not keep_variables is None:
        features = features[keep_variables]
        test_features = features[keep_variables]

    # Prep targets
    targets = targets["SalePrice"]
    my_model = model_functions.create_model(features,
                                            targets,
                                            ignore_features=IGNORE,
                                            type=model_type)
    model_functions.cv(my_model, features, targets)

    # Export results
    result_dict = model_functions.run_full_model(my_model,
                                                 features,
                                                 targets,
                                                 test_features,
                                                 exclude_misc=exclude_misc)
    cs_df = result_dict["submission_df"]

    if USE_LOG:
        cs_df["SalePrice"] = np.exp(cs_df["SalePrice"]) - 1
    export_data.export_data(cs_df, output_path)
コード例 #3
0
ファイル: train.py プロジェクト: ramiejleh/aipnd-project
    return parser.parse_args()


arguments = get_input_args()

if arguments.gpu:
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
else:
    device = "cpu"

#load datasets
loaded_datasets = load_datasets(arguments.dir)

#create the model
model = create_model(loaded_datasets['image_datasets'], arguments.arch,
                     arguments.hidden_units)

#train the model
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(),
                       lr=arguments.learning_rate)
train_network(loaded_datasets['dataloaders'], model, criterion, optimizer,
              arguments.epochs, device)

#save trained model
save_checkpoint(criterion, arguments.epochs, optimizer, model, arguments.arch,
                arguments.save_dir)

# print("Command Line Arguments:\n    dir =", arguments.dir,
#       "\n    arch =", arguments.arch,
#       "\n    learning_rate =", arguments.learning_rate,
コード例 #4
0
def main(_):
    with tf.device('/gpu:0'):

        for regularization_type in ['Blackout', 'None', 'L1', 'L2']:

            dataset_sizes = np.linspace(2500, 55000, num=22)
            for size in dataset_sizes:
                # Getting the appropriate dataset

                print(int(size))
                train_x, train_y, valid_x, valid_y, test_x, test_y = split_data(
                    dataset, int(size))

                # Resetting the graph incase of multiple runs on the same console
                tf.reset_default_graph()
                for i in range(numOfTests):
                    num_layers = random.choice([5, 6, 7, 8, 9, 10])
                    num_nodes = random.choice([200, 400, 600])
                    num_inputs = int(train_x.shape[1])
                    num_steps = random.choice([50, 100, 150, 200])
                    regularization_scale = random.choice(
                        [0.01, 0.005, 0.001, 0.0005])
                    percent_connections_kept = random.choice([0.9, 0.95, 0.85])
                    num_classes = len(np.unique(train_y))

                    print('Test No. ' + str(i) + '/' + str(numOfTests))
                    print('Parameters: ' + str(size) + ',' +
                          regularization_type + ',' + str(num_layers) + ',' +
                          str(num_nodes) + ',' + str(num_steps) + ',' +
                          str(regularization_scale) + ',' +
                          str(percent_connections_kept))

                    # Create the model
                    x = tf.placeholder(tf.float32, [None, num_inputs])
                    y = create_model(x, num_layers, num_nodes, num_classes)

                    # Define loss and optimizer
                    y_ = tf.placeholder(tf.int64, [None])

                    # Retrieving weights and defining regularization penalty
                    weights = tf.trainable_variables()
                    regularization_penalty, blackout_weights = get_regularization_penalty(
                        weights, regularization_scale,
                        percent_connections_kept, regularization_type)

                    # Defining loss and optimizer
                    cross = tf.losses.sparse_softmax_cross_entropy(labels=y_,
                                                                   logits=y)
                    loss = cross + regularization_penalty
                    train_step = tf.train.RMSPropOptimizer(0.001).minimize(
                        loss)

                    # Evaluate Model
                    correct_prediction = tf.equal(tf.argmax(y, 1), y_)
                    accuracy = tf.reduce_mean(
                        tf.cast(correct_prediction, tf.float32))
                    config = tf.ConfigProto()
                    config.gpu_options.allow_growth = True

                    # Initializing session
                    sess = tf.InteractiveSession(config=config)
                    tf.global_variables_initializer().run()

                    # Train
                    #                PercentageOfConnOff=[]
                    #                LossFunctionRegu=[]
                    #                LossFunctionCrossTrain=[]
                    #                LossFunctionCrossValid=[]
                    #
                    numOfBatches = 50
                    all_batches_x, all_batches_y = get_batches(
                        train_x, train_y, numOfBatches)

                    # Train
                    for i in range(num_steps):
                        randomPick = random.randint(0, numOfBatches)
                        #print(str(len(all_batches_x)) + " getting " + str(randomPick))
                        if randomPick == 50:
                            randomPick = 49
                        currentBatchX = all_batches_x[randomPick]
                        currentBatchY = all_batches_y[randomPick]
                        sess.run(train_step,
                                 feed_dict={
                                     x: currentBatchX,
                                     y_: currentBatchY
                                 })
                        # Test trained model
                        if i % 20 == 1:
                            print('Accuracy: ' + str(
                                sess.run(accuracy,
                                         feed_dict={
                                             x: valid_x,
                                             y_: valid_y
                                         })))
    #                            if regularization_type=='Blackout':
    #                                currentWeights=sess.run(blackout_weights)
    #                                part1=currentWeights>-0.01
    #                                part2=currentWeights<0.01
    #                                turnedOff=np.sum(np.logical_and(part1,part2))
    #                                TotalNumOfWeights=float(currentWeights.shape[0])
    #                                LossFunctionCrossTrain.append(sess.run(cross, feed_dict={x: train_x, y_: train_y}))
    #                                LossFunctionCrossValid.append(sess.run(cross, feed_dict={x: valid_x, y_: valid_y}))
    #                                LossFunctionRegu.append(sess.run(regularization_penalty))
    #                                PercentageOfConnOff.append((TotalNumOfWeights-turnedOff)/TotalNumOfWeights)
    #if regularization_type=='Blackout':
    #    fig = plt.figure()
    #    ax1 = fig.add_subplot(1, 2, 1)
    #   ax2 = fig.add_subplot(1, 2, 2)
    #    ax1.plot(PercentageOfConnOff)
    #    ax2.plot(LossFunctionCrossTrain,label='Cross-Entropy Train')
    #    ax2.plot(LossFunctionCrossValid,label='Cross-Entropy Validation')
    #    ax2.plot(LossFunctionRegu,label='Regularization')
    #    ax2.legend()
    #    fig.show()
                    accuracyVal = sess.run(accuracy,
                                           feed_dict={
                                               x: valid_x,
                                               y_: valid_y
                                           })
                    accuracyTest = sess.run(accuracy,
                                            feed_dict={
                                                x: test_x,
                                                y_: test_y
                                            })
                    tf.reset_default_graph()
                    store_results(dataset, regularization_type, num_layers,
                                  num_nodes, num_steps, regularization_scale,
                                  percent_connections_kept, accuracyVal,
                                  accuracyTest, size)
                    print('Accuracy Val: ' + str(accuracyVal) +
                          ' , Accuracy Test: ' + str(accuracyTest))
コード例 #5
0
                    "--dropout",
                    help="Setting the dropout rate in the classifier",
                    type=float,
                    default=0.5)
parser.add_argument("-ep",
                    "--epochs",
                    help="Setting the dropout rate in the classifier",
                    type=int,
                    default=15)
parser.add_argument("--gpu", help="Use GPU for training", action="store_true")
args = parser.parse_args()

image_datasets, data_loaders, _ = modfunc.transform_load(args.data_directory)
model, criterion, optimizer = modfunc.create_model(
    arch=args.arch,
    dropout=args.dropout,
    hidden_units=args.hidden_units,
    learning_rate=args.learning_rate)

if args.gpu:
    device = 'cuda'
else:
    device = 'cpu'

modfunc.train_model(image_datasets, data_loaders, model, criterion, optimizer,
                    args.epochs, device)
modfunc.model_test(image_datasets, data_loaders, model, 'test', criterion,
                   device)

if args.save_dir:
    othfunc.save_model(image_datasets,
コード例 #6
0
                df_test = df_data[(df_data.date >= str_test_start)
                                  & (df_data.date <= str_test_end)]
                df_train_test = pd.concat([df_train, df_test])

                df_test_for_sequence = df_train_test[len(df_train_test) -
                                                     len(df_test) -
                                                     series_length:]

                X_train, y_train = preprocess(df_train[selected_features],
                                              True, predict_length,
                                              series_length)
                X_test, y_test = preprocess(
                    df_test_for_sequence[selected_features], False,
                    predict_length, series_length)

                model = create_model(X_train.shape[1:])

                model.compile(loss='sparse_categorical_crossentropy',
                              optimizer="adam",
                              metrics=['accuracy'])

                history = model.fit(X_train,
                                    y_train,
                                    batch_size=batch_size,
                                    epochs=epochs,
                                    verbose=False,
                                    validation_split=0.1)

                test_score, test_accuracy = model.evaluate(
                    X_test, y_test, batch_size=batch_size)
                test_score = round(test_score, 2)