コード例 #1
0
def train(config_path):
    modelArgs = NetworkConfigParser.constructModelArgs(config_path, ModelArgs)
    nn = NetworkConfigParser.constructNetwork(config_path)
    train_path, test_path, save_path = NetworkConfigParser.getDataInfo(
        config_path)
    print nn
    # TODO : Arguments
    num_hid = nn.layers[1].num_units
    shape = (None, nn.layers[0].num_units)
    train, test, cold = loadTrainTest(train_path, test_path, shape=shape)
    ae = AE(nn, modelArgs)
    evaluate = EvaluateNN(ae)
    theta = ae.nn.getFlattenParams()
    ae.setParameters(theta)
    iterCounter = Counter()
    optimizer = getOptimizer(modelArgs.optimizer, ae, evaluate, theta, train,
                             test, nn, modelArgs, iterCounter,
                             modelArgs.batch_size, modelArgs.max_iter[0])

    optimizer.step_grow = 5.0
    k = 0
    for info in optimizer:
        print "Iteration %d" % k
        if k == 5:
            optimizer.step_grow = 1.2
        if k % 5 == 0:
            ae.setParameters(theta)
            rmse, mae = evaluate.calculateRMSEandMAE(train, test)
            print "Fold :%d Test RMSE: %f Test MAE: %f" % (i, rmse, mae)
        if k > modelArgs.max_iter[0]:
            break
        k += 1
    if save_path:
        _theta = ae.getParameters()
        np.save(save_path, _theta)
コード例 #2
0
ファイル: learner.py プロジェクト: fangzheng354/NNRec
def train(config_path):
    configparser = NetworkConfigParser()
    nn = configparser.constructNetwork(config_path)
    modelArgs = configparser.constructModelArgs(config_path, ModelArgs)
    train_path, test_path, save_path = configparser.getDataInfo(config_path)
    print nn

    data = yaml.load(open(config_path))
    params = data["params"]
    k = params["k"]

    n_vis = int(nn.layers[0].num_units / k)
    train, test, cold_ratings = loadTrainTest(train_path, test_path, shape=(None, n_vis))

    min_rating, max_rating = train.data.min(), train.data.max()
    increment = 1
    mapping = dict(zip(np.arange(min_rating, max_rating + increment, increment), np.arange(k)))
    modelArgs.mapping = mapping
    modelArgs.k = k
    bintrain = binarizeSparseMatrix(train, k, mapping)
    bintest = binarizeSparseMatrix(test, k, mapping)
    del train
    model = RBM(nn, modelArgs)
    optimizer = RbmOptimizer(model)
    optimizer.minibatchTrain(bintrain, bintest, test, modelArgs.batch_size)
コード例 #3
0
def train(config_path):
    configparser = NetworkConfigParser()
    nn = configparser.constructNetwork(config_path)
    modelArgs = configparser.constructModelArgs(config_path, ModelArgs)
    train_path, test_path, save_path = configparser.getDataInfo(config_path)
    print nn

    data = yaml.load(open(config_path))
    params = data["params"]
    k = params["k"]

    n_vis = int(nn.layers[0].num_units / k)
    train, test, cold_ratings = loadTrainTest(train_path,
                                              test_path,
                                              shape=(None, n_vis))

    min_rating, max_rating = train.data.min(), train.data.max()
    increment = 1
    mapping = dict(
        zip(np.arange(min_rating, max_rating + increment, increment),
            np.arange(k)))
    modelArgs.mapping = mapping
    modelArgs.k = k
    bintrain = binarizeSparseMatrix(train, k, mapping)
    bintest = binarizeSparseMatrix(test, k, mapping)
    del train
    model = RBM(nn, modelArgs)
    optimizer = RbmOptimizer(model)
    optimizer.minibatchTrain(bintrain, bintest, test, modelArgs.batch_size)
コード例 #4
0
ファイル: learner.py プロジェクト: fangzheng354/NNRec
def train(config_path):
    modelArgs = NetworkConfigParser.constructModelArgs(config_path, ModelArgs)
    nn = NetworkConfigParser.constructNetwork(config_path)
    train_path, test_path, save_path = NetworkConfigParser.getDataInfo(config_path)
    print nn
    # TODO : Arguments
    num_hid = nn.layers[1].num_units
    shape = (None, nn.layers[0].num_units)
    train, test, cold = loadTrainTest(train_path, test_path, shape=shape)
    ae = AE(nn, modelArgs)
    evaluate = EvaluateNN(ae)
    theta = ae.nn.getFlattenParams()
    ae.setParameters(theta)
    iterCounter = Counter()
    optimizer = getOptimizer(
        modelArgs.optimizer,
        ae,
        evaluate,
        theta,
        train,
        test,
        nn,
        modelArgs,
        iterCounter,
        modelArgs.batch_size,
        modelArgs.max_iter[0],
    )

    optimizer.step_grow = 5.0
    k = 0
    for info in optimizer:
        print "Iteration %d" % k
        if k == 5:
            optimizer.step_grow = 1.2
        if k % 5 == 0:
            ae.setParameters(theta)
            rmse, mae = evaluate.calculateRMSEandMAE(train, test)
            print "Fold :%d Final Train RMSE: %f Train MAE: %f" % (i, rmse, mae)
        if k > modelArgs.max_iter[0]:
            break
        k += 1
    if save_path:
        _theta = ae.getParameters()
        np.save(save_path, _theta)