Esempio n. 1
0
def test_real(n_updates=100):
    """ Test RNN with real-valued outputs. """
    n_hidden = 10
    n_in = 5
    n_out = 3
    n_steps = 10
    n_seq = 1000

    np.random.seed(0)
    # simple lag test
    seq = np.random.randn(n_seq, n_steps, n_in)
    print 'seq1'
    print seq

    targets = np.zeros((n_seq, n_steps, n_out))
    targets[:, 1:, 0] = seq[:, :-1, 3]  # delayed 1
    targets[:, 1:, 1] = seq[:, :-1, 2]  # delayed 1
    targets[:, 2:, 2] = seq[:, :-2, 0]  # delayed 2

    targets += 0.01 * np.random.standard_normal(targets.shape)
    print 'targets'
    print targets

    # SequenceDataset wants a list of sequences
    # this allows them to be different lengths, but here they're not
    seq = [i for i in seq]
    print 'seq2'
    print seq
    targets = [i for i in targets]

    gradient_dataset = SequenceDataset([seq, targets], batch_size=None,
                                       number_batches=100)
    cg_dataset = SequenceDataset([seq, targets], batch_size=None,
                                 number_batches=20)

    model = MetaRNN(n_in=n_in, n_hidden=n_hidden, n_out=n_out,
                    activation='tanh')

    opt = hf_optimizer(p=model.rnn.params, inputs=[model.x, model.y],
                       s=model.rnn.y_pred,
                       costs=[model.rnn.loss(model.y)], h=model.rnn.h)
    print model

    opt.train(gradient_dataset, cg_dataset, num_updates=n_updates)

    plt.close('all')
    fig = plt.figure()
    ax1 = plt.subplot(211)
    plt.plot(seq[0])
    ax1.set_title('input')
    ax2 = plt.subplot(212)
    true_targets = plt.plot(targets[0])

    guess = model.predict(seq[0])
    guessed_targets = plt.plot(guess, linestyle='--')
    for i, x in enumerate(guessed_targets):
        x.set_color(true_targets[i].get_color())
    ax2.set_title('solid: true output, dashed: model output')
Esempio n. 2
0
def test_Oscillator(n_updates=50):
    """ Test RNN with real-valued outputs. """
    n_hidden = 250
    n_in = 1
    n_out = 2
    n_steps = 200
    n_seq = 1
    fracZero = 2

    np.random.seed(np.random.randint(200)+52)
    baseFreq = np.linspace(0,numpy.pi*4,n_steps)
    seq = np.zeros((n_seq,n_steps,n_in))
    #sigDecay = np.zeros((n_steps/2))
    #sigDecay[:(n_steps/fracZero)/2] = np.linspace(1,0,(n_steps/fracZero)/2)
    #seqSig = sigDecay * np.cos(baseFreq)/2 + 1
    #seq[:,n_steps/4:n_steps*3/4,0] = np.tile(seqSig,(n_seq,1))
    #seq[:,n_steps/4:3*n_steps/4,0]]
    seq[:,:n_steps/fracZero,0] = np.tile(np.linspace(1,0,num=n_steps/fracZero) * np.cos(baseFreq[:n_steps/fracZero]),(n_seq,1))
    #seq = np.random.randn(n_seq, n_steps, n_in)
    targets = np.zeros((n_seq, n_steps, n_out))

    targets[:, :, 0] = np.tile(np.cos(baseFreq),(n_seq,1))
    targets[:, :, 1] = np.tile(np.sin(baseFreq),(n_seq,1))

    # SequenceDataset wants a list of sequences
    # this allows them to be different lengths, but here they're not
    seq = [i for i in seq]
    targets = [i for i in targets]

    gradient_dataset = SequenceDataset([seq, targets], batch_size=None,
                                       number_batches=2)
    cg_dataset = SequenceDataset([seq, targets], batch_size=None,
                                 number_batches=2)

    model = MetaRNN(n_in=n_in, n_hidden=n_hidden, n_out=n_out,
                    activation='cappedrelu')

    opt = hf_optimizer(p=model.rnn.params, inputs=[model.x, model.y],
                       s=model.rnn.y_pred,
                       costs=[model.rnn.loss(model.y)], h=model.rnn.h)

    opt.train(gradient_dataset, cg_dataset, num_updates=n_updates)

    plt.close('all')
    fig = plt.figure()
    ax1 = plt.subplot(211)
    plt.plot(seq[0])
    ax1.set_title('input')
    ax2 = plt.subplot(212)
    true_targets = plt.plot(targets[0])

    guess = model.predict(seq[0])
    guessed_targets = plt.plot(guess, linestyle='--')
    for i, x in enumerate(guessed_targets):
        x.set_color(true_targets[i].get_color())
    ax2.set_title('solid: true output, dashed: model output')
            
    return model
Esempio n. 3
0
def test_real(n_updates=100):
    """ Test RNN with real-valued outputs. """
    n_hidden = 10
    n_in = 5
    n_out = 3
    n_steps = 10
    n_seq = 1000

    np.random.seed(0)
    # simple lag test
    seq = np.random.randn(n_seq, n_steps, n_in)

    targets = np.zeros((n_seq, n_steps, n_out))
    targets[:, 1:, 0] = seq[:, :-1, 3]  # delayed 1
    targets[:, 1:, 1] = seq[:, :-1, 2]  # delayed 1
    targets[:, 2:, 2] = seq[:, :-2, 0]  # delayed 2

    targets += 0.01 * np.random.standard_normal(targets.shape)

    # SequenceDataset wants a list of sequences
    # this allows them to be different lengths, but here they're not
    seq = [i for i in seq]
    targets = [i for i in targets]

    gradient_dataset = SequenceDataset([seq, targets],
                                       batch_size=None,
                                       number_batches=100)
    cg_dataset = SequenceDataset([seq, targets],
                                 batch_size=None,
                                 number_batches=20)

    model = MetaRNN(n_in=n_in,
                    n_hidden=n_hidden,
                    n_out=n_out,
                    activation='tanh')

    opt = hf_optimizer(p=model.rnn.params,
                       inputs=[model.x, model.y],
                       s=model.rnn.y_pred,
                       costs=[model.rnn.loss(model.y)],
                       h=model.rnn.h)

    opt.train(gradient_dataset, cg_dataset, num_updates=n_updates)

    plt.close('all')
    fig = plt.figure()
    ax1 = plt.subplot(211)
    plt.plot(seq[0])
    ax1.set_title('input')
    ax2 = plt.subplot(212)
    true_targets = plt.plot(targets[0])

    guess = model.predict(seq[0])
    guessed_targets = plt.plot(guess, linestyle='--')
    for i, x in enumerate(guessed_targets):
        x.set_color(true_targets[i].get_color())
    ax2.set_title('solid: true output, dashed: model output')
            model = MetaRNN(n_in=n_in,
                            n_mul=n_hidden,
                            n_out=n_out,
                            activation='tanh')

            opt = hf_optimizer(p=model.rnn.params,
                               inputs=[model.x, model.y],
                               s=model.rnn.y_pred,
                               costs=[model.rnn.loss(model.y)],
                               h=model.rnn.h)

            opt.train(gradient_dataset, cg_dataset, num_updates=30)

            path = '/root/chentian/share/201605/one_ped1/%d' % index
            os.mkdir(path)
            path0 = path + '/predict.csv'
            f3 = file(path0, "a")
            for seq_num in range(0, n_seq):
                guess = model.predict(test_seqs[seq_num])
                np.savetxt(f3, guess, delimiter=',')
            f3.close()
            pre_f = file(path0, "r")
            pre = np.loadtxt(pre_f, delimiter=',')
            pre_f.close()
            path3 = path + "/error.csv"
            f8 = file(path3, "a")
            error = (pre - test_target)**2
            np.savetxt(f8, error, delimiter=',')
            f8.close()
    print "traning over"
    print "Elapsed time: %f" % (time.time() - t0)
n_out = 1     # K
n_steps = 1  # the length of each sequence
n_seq = len(food_sel_scaled_train)   # the number of datapoints (i.e. sequences)

#Creating input and output arrays for training RNN         
rating = np.array(food_sel_scaled_train[['rating','price.tier','stats.checkinsCount']]).reshape(n_seq,n_steps,n_in)
score = np.array(food_sel_scaled_train['SCORE']).reshape(n_seq,n_steps,n_out)


#Creating the model and feeding it with training data set
model = MetaRNN(n_in=n_in, n_hidden=n_hidden, n_out=n_out,
                    learning_rate=0.001, learning_rate_decay=0.999,
                    n_epochs=50, activation='tanh')
model.fit(rating, score, validation_frequency=5000)

guess = model.predict(rating.reshape(len(rating), n_in))
scores_pred = pd.DataFrame(guess)

scores_pred.columns = ['predictions']

scores_pred.predictions.plot(kind='hist', bins=20, figsize=(6,4), grid=True,                             title = "Histogram of predicted DOH normalized Scores (IS)", alpha=0.8)

food_sel_scaled_train.SCORE.plot(kind='hist', bins=20, figsize=(6,4), grid=True,                             title = "Histogram of actual DOH normalized Scores (IS)", alpha=0.8)

#Creating parameters for training RNN
n_hidden = 5 # M
n_in = 3      # D
n_out = 1     # K
n_steps = 1  # the length of each sequence
n_seq = len(food_sel_scaled_test)   # the number of datapoints (i.e. sequences)
Esempio n. 6
0
    opt = hf_optimizer(p=model.rnn.params,
                       inputs=[model.x, model.y],
                       s=model.rnn.y_pred,
                       costs=[model.rnn.loss(model.y)],
                       h=model.rnn.h)

    opt.train(gradient_dataset, cg_dataset, num_updates=300)

    print "traning over"

    path = '/root/chentian/share/201605/ped1'
    #    os.makedirs(path)
    path0 = path + '/predict.csv'
    f3 = file(path0, "a")
    for k in range(0, n_seqs):
        guess = model.predict(test_seqs[k])
        np.savetxt(f3, guess, delimiter=',')
    f3.close()

    error1 = np.zeros([6800, 1104])
    pre_f = file(path0, "r")
    pre = np.loadtxt(pre_f, delimiter=',')
    pre_f.close()
    path3 = path + "/error.csv"
    f8 = file(path3, "a")
    error = (pre - test_targets)**2
    for i in range(0, 46):
        error1[:, i * 24:(i + 1) * 24] = error[i * 6800:(i + 1) * 6800, :]
    np.savetxt(f8, error1, delimiter=',')
    f8.close()
rating = np.array(food_sel_scaled_train[[
    'rating', 'price.tier', 'stats.checkinsCount'
]]).reshape(n_seq, n_steps, n_in)
score = np.array(food_sel_scaled_train['SCORE']).reshape(n_seq, n_steps, n_out)

#Creating the model and feeding it with training data set
model = MetaRNN(n_in=n_in,
                n_hidden=n_hidden,
                n_out=n_out,
                learning_rate=0.001,
                learning_rate_decay=0.999,
                n_epochs=50,
                activation='tanh')
model.fit(rating, score, validation_frequency=5000)

guess = model.predict(rating.reshape(len(rating), n_in))
scores_pred = pd.DataFrame(guess)

scores_pred.columns = ['predictions']

scores_pred.predictions.plot(
    kind='hist',
    bins=20,
    figsize=(6, 4),
    grid=True,
    title="Histogram of predicted DOH normalized Scores (IS)",
    alpha=0.8)

food_sel_scaled_train.SCORE.plot(
    kind='hist',
    bins=20,