def run_ae(step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter, X, TX):

    print step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter
    seed = 3453
    np.random.seed(seed)
    batch_size = batch_size
    #max_iter = max_passes * X.shape[ 0] / batch_size
    max_iter = 10000000
    n_report = X.shape[0] / batch_size
    weights = []
    #X = X.reshape(X.shape[0], 23*23)
    #TX = TX.reshape(TX.shape[0], 23*23)

    input_size = X.shape[1]
    print input_size

    stop = climin.stops.AfterNIterations(max_iter)
    pause = climin.stops.ModuloNIterations(n_report)


    optimizer = 'gd', {'step_rate': step, 'momentum': momentum, 'decay': decay}


    typ = 'dae'
    if typ == 'cae':
        m = ContractiveAutoEncoder(X.shape[1], n_hidden, X,
                hidden_transfers=hidden_transfers, out_transfer='identity', loss='squared', optimizer=optimizer,
                                   batch_size=batch_size, max_iter=max_iter, c_jacobian=1)

    elif typ == 'dae':
        m = DenoisingAutoEncoder(X.shape[1], n_hidden, X,
                hidden_transfers=hidden_transfers, out_transfer='identity', loss='squared', optimizer=optimizer,
                                 batch_size=batch_size, max_iter=max_iter, noise_type='gauss', c_noise=.2)


    climin.initialize.randomize_normal(m.parameters.data, 0, par_std)



    # Transform the test data
    TX = m.transformedData(TX)
    #m.init_weights()
    #TX = np.array([TX for _ in range(10)]).mean(axis=0)
    print TX.shape

    losses = []
    print 'max iter', max_iter

    X, TX = [breze.learn.base.cast_array_to_local_type(i) for i in (X, TX)]


    start = time.time()
    # Set up a nice printout.
    keys = '#', 'seconds', 'loss', 'val loss'
    max_len = max(len(i) for i in keys)
    header = '\t'.join(i for i in keys)
    print header
    print '-' * len(header)
    results = open('result_hp.txt', 'a')
    results.write(header + '\n')
    results.write('-' * len(header) + '\n')
    results.close()

    EXP_DIR = os.getcwd()
    base_path = os.path.join(EXP_DIR, "pars_hp"+str(counter)+".pkl")
    n_iter = 0

    if os.path.isfile(base_path):
        with open("pars_hp"+str(counter)+".pkl", 'rb') as tp:
            n_iter, best_pars, best_loss = cp.load(tp)
            m.parameters.data[...] = best_pars

    for i, info in enumerate(m.powerfit((X,), (TX,), stop, pause)):
        if info['n_iter'] % n_report != 0:
            continue
        if math.isnan(info['loss']) == True:
            n_iter = info['n_iter']
            break
        passed = time.time() - start
        losses.append((info['loss'], info['val_loss']))
        info.update({
            'time': passed,

        })

        best_pars = info['best_pars']
        best_loss = info['best_loss']
        info['n_iter'] += n_iter

        row = '%(n_iter)i\t%(time)g\t%(loss)f\t%(val_loss)f' % info
        results = open('result_hp.txt', 'a')
        print row
        results.write(row + '\n')
        results.close()
        with open("pars_hp"+str(counter)+".pkl", 'wb') as fp:
            cp.dump((info['n_iter'], info['best_pars'], info['best_loss']), fp)
        with open("hps"+str(counter)+".pkl", 'wb') as tp:
            cp.dump((step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter, info['n_iter']), tp)


    m.parameters.data[...] = best_pars
    cp.dump((best_pars, best_loss), open("best_pars"+str(counter)+".pkl", 'wb'))
    i = m.mlp.layers.__len__() - 2
    f_reconstruct = m.function([m.exprs['inpt']], m.mlp.layers[i].output)
    L = f_reconstruct(m.transformedData(X))
    LT = f_reconstruct(TX)
    print L.shape, LT.shape

    mydict = {'X': np.array(L), 'Z': Z, 'TZ': TZ, 'TX': np.array(LT), 'P': CV}
    output = open("autoencoder"+str(counter)+".pkl", 'wb')
    cp.dump(mydict, output)
    output.close()
#max_iter = max_passes * X.shape[ 0] / batch_size
max_iter = 1000
n_report = X.shape[0] / batch_size


stop = climin.stops.AfterNIterations(max_iter)
pause = climin.stops.ModuloNIterations(n_report)



optimizer = 'gd', {'step_rate': 0.001, 'momentum': 0}

typ = 'dae'
if typ == 'cae':
    m = ContractiveAutoEncoder(X.shape[1], [2700], X,
            hidden_transfers=['tanh'], out_transfer='identity', loss='squared', optimizer=optimizer,
                               batch_size=batch_size, max_iter=max_iter, c_jacobian=1)

elif typ == 'dae':
    m = DenoisingAutoEncoder(X.shape[1], [2500], X,
            hidden_transfers=['tanh'], out_transfer='identity', loss='squared', optimizer=optimizer,
                             batch_size=batch_size, max_iter=max_iter, noise_type='gauss', c_noise=.2)

#climin.initialize.randomize_normal(m.parameters.data, 0, 1 / np.sqrt(m.n_inpt))


#m.init_weights()
#Transform the test data
#TX = m.transformedData(TX)
TX = np.array([m.transformedData(TX) for _ in range(10)]).mean(axis=0)
print TX.shape
def run_ae(step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter, X, TX):

    print step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter
    seed = 3453
    np.random.seed(seed)
    batch_size = batch_size
    #max_iter = max_passes * X.shape[ 0] / batch_size
    max_iter = 5000000
    n_report = X.shape[0] / batch_size
    weights = []
    #X = X.reshape(X.shape[0], 23*23)
    #TX = TX.reshape(TX.shape[0], 23*23)

    input_size = X.shape[1]
    print input_size

    stop = climin.stops.AfterNIterations(max_iter)
    pause = climin.stops.ModuloNIterations(n_report)


    optimizer = 'gd', {'step_rate': step, 'momentum': momentum, 'decay': decay}


    typ = 'cae'
    if typ == 'cae':
        m = ContractiveAutoEncoder(X.shape[1], n_hidden, X,
                hidden_transfers=hidden_transfers, out_transfer='identity', loss='squared', optimizer=optimizer,
                                   batch_size=batch_size, max_iter=max_iter, c_jacobian=1)

    elif typ == 'dae':
        m = DenoisingAutoEncoder(X.shape[1], n_hidden, X,
                hidden_transfers=hidden_transfers, out_transfer='identity', loss='squared', optimizer=optimizer,
                                 batch_size=batch_size, max_iter=max_iter, noise_type='gauss', c_noise=.2)


    climin.initialize.randomize_normal(m.parameters.data, 0, par_std)



    # Transform the test data
    TX = m.transformedData(TX)
    #m.init_weights()
    #TX = np.array([TX for _ in range(10)]).mean(axis=0)
    print TX.shape

    losses = []
    print 'max iter', max_iter

    X, TX = [breze.learn.base.cast_array_to_local_type(i) for i in (X, TX)]


    start = time.time()
    # Set up a nice printout.
    keys = '#', 'seconds', 'loss', 'val loss'
    max_len = max(len(i) for i in keys)
    header = '\t'.join(i for i in keys)
    print header
    print '-' * len(header)
    results = open('result_hp.txt', 'a')
    results.write(header + '\n')
    results.write('-' * len(header) + '\n')
    results.close()

    EXP_DIR = os.getcwd()
    base_path = os.path.join(EXP_DIR, "pars_hp"+str(counter)+".pkl")
    n_iter = 0

    if os.path.isfile(base_path):
        with open("pars_hp"+str(counter)+".pkl", 'rb') as tp:
            n_iter, best_pars, best_loss = cp.load(tp)
            m.parameters.data[...] = best_pars

    for i, info in enumerate(m.powerfit((X,), (TX,), stop, pause)):
        if info['n_iter'] % n_report != 0:
            continue

        if 0 <= info['val_loss'] < 1:
            with open("pars_bestloss.pkl", 'wb') as fp:
                cp.dump((info['n_iter'], info['best_pars'], info['best_loss']), fp)
            m.parameters.data[...] = info['best_pars']
    	    i = m.mlp.layers.__len__() - 2
    	    f_reconstruct = m.function([m.exprs['inpt']], m.mlp.layers[i].output)
    	    L = f_reconstruct(m.transformedData(X))
    	    LT = f_reconstruct(TX)
            mydict = {'X': np.array(L), 'Z': Z, 'TZ': TZ, 'TX': np.array(LT), 'P': CV}
            output = open("autoencoder_best.pkl", 'wb')
            cp.dump(mydict, output)
            output.close()

        if math.isnan(info['val_loss']) == True:
            n_iter = info['n_iter']
            break
        passed = time.time() - start
        losses.append((info['loss'], info['val_loss']))
        info.update({
            'time': passed,

        })

        best_pars = info['best_pars']
        best_loss = info['best_loss']
        info['n_iter'] += n_iter

        row = '%(n_iter)i\t%(time)g\t%(loss)f\t%(val_loss)f' % info
        results = open('result_hp.txt', 'a')
        print row
        results.write(row + '\n')
        results.close()
        with open("pars_hp"+str(counter)+".pkl", 'wb') as fp:
            cp.dump((info['n_iter'], info['best_pars'], info['best_loss']), fp)
        with open("hps"+str(counter)+".pkl", 'wb') as tp:
            cp.dump((step, momentum, decay, n_hidden, hidden_transfers, par_std, batch_size, counter, info['n_iter']), tp)


    m.parameters.data[...] = best_pars
    cp.dump((best_pars, best_loss), open("best_pars"+str(counter)+".pkl", 'wb'))
    i = m.mlp.layers.__len__() - 2
    f_reconstruct = m.function([m.exprs['inpt']], m.mlp.layers[i].output)
    L = f_reconstruct(m.transformedData(X))
    LT = f_reconstruct(TX)
    print L.shape, LT.shape

    mydict = {'X': np.array(L), 'Z': Z, 'TZ': TZ, 'TX': np.array(LT), 'P': CV}
    output = open("autoencoder"+str(counter)+".pkl", 'wb')
    cp.dump(mydict, output)
    output.close()
Exemplo n.º 4
0
#max_iter = max_passes * X.shape[ 0] / batch_size
max_iter = 1000
n_report = X.shape[0] / batch_size

stop = climin.stops.AfterNIterations(max_iter)
pause = climin.stops.ModuloNIterations(n_report)

optimizer = 'gd', {'step_rate': 0.001, 'momentum': 0}

typ = 'dae'
if typ == 'cae':
    m = ContractiveAutoEncoder(X.shape[1], [2700],
                               X,
                               hidden_transfers=['tanh'],
                               out_transfer='identity',
                               loss='squared',
                               optimizer=optimizer,
                               batch_size=batch_size,
                               max_iter=max_iter,
                               c_jacobian=1)

elif typ == 'dae':
    m = DenoisingAutoEncoder(X.shape[1], [2500],
                             X,
                             hidden_transfers=['tanh'],
                             out_transfer='identity',
                             loss='squared',
                             optimizer=optimizer,
                             batch_size=batch_size,
                             max_iter=max_iter,
                             noise_type='gauss',