예제 #1
0
'''
output = input
#targets=output.texts_to_sequences(text,n_sentence,n_maxlen)
targets = seq
n_words_y = output.nb_words

targets[:-1] = targets[1:]

seq, seq_mask, targets, targets_mask = prepare_data(seq, targets, n_maxlen)

####build model

mode = 'tr'

model = ENC_DEC(n_u, n_h, n_d, n_y, n_epochs, n_chapter, n_batch, n_gen_maxlen,
                n_words_x, n_words_y, dim_word, momentum_switchover, lr,
                learning_rate_decay, snapshot_Freq, sample_Freq)
model.add(BiDirectionGRU(n_u, n_h))
model.add(decoder(n_h, n_d, n_y))
model.build()

filepath = 'data/ted.pkl'

if mode == 'tr':
    if os.path.isfile(filepath): model.load(filepath)

    model.train(seq, seq_mask, targets, targets_mask, input, output, verbose,
                optimizer)
    model.save(filepath)

    ##draw error graph
예제 #2
0
파일: Main.py 프로젝트: chuckgu/RNN
targets_onehot=np.zeros((n_seq, time_steps_y,n_y), dtype=np.int)

targets_onehot[:,:,0][targets[:,:]==0]=1
targets_onehot[:,:,1][targets[:,:]==1]=1
targets_onehot[:,:,2][targets[:,:]==2]=1

targets_onehot=np.cast[theano.config.floatX](targets_onehot)


mode='tr'

seq=seq.transpose(1,0,2)
targets_onehot=targets_onehot.transpose(1,0,2)

model = ENC_DEC(n_u,n_h*2,n_d,n_y,0.001,n_epochs,n_batch,n_maxlen)
model.add(BiDirectionLSTM(n_u,n_h))
model.add(decoder(n_h*2,n_d,n_y))

model.build('softmax')



if mode=='tr':
    model.train(seq,targets_onehot)
    model.save('encdec_new.pkl')
else:model.load('encdec_new.pkl')

i=20
plt.close('all')
fig = plt.figure()
예제 #3
0
targets[:, 0][seq[:, 0, 1] < seq[:, 0, 0] - thresh] = 2
targets[:, -1][seq[:, -1, 1] > seq[:, -2, 0] + thresh] = 1
targets[:, -1][seq[:, -1, 1] < seq[:, -2, 0] - thresh] = 2
targets[:, 1:][seq[:, 1:-1, 1] > seq[:, :-2, 0] + thresh] = 1
targets[:, 1:][seq[:, 1:-1, 1] < seq[:, :-2, 0] - thresh] = 2
# otherwise class is 0

targets_onehot = np.zeros((n_seq, time_steps_y, n_y), dtype=np.int)

targets_onehot[:, :, 0][targets[:, :] == 0] = 1
targets_onehot[:, :, 1][targets[:, :] == 1] = 1
targets_onehot[:, :, 2][targets[:, :] == 2] = 1

mode = 'tr1'

model = ENC_DEC(n_u, n_h, n_d, n_y, time_steps_x, time_steps_y, 0.001, 200)
model.add(hidden(n_u, n_h))
model.add(decoder(n_h, n_d, n_y, time_steps_x, time_steps_y))

model.build('softmax')

if mode == 'tr':
    model.train(seq, targets)
    model.save('encdec_new.pkl')
else:
    model.load('encdec_new.pkl')

i = 20
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot(311)
예제 #4
0
파일: Main.py 프로젝트: rubeeny/RNN
# otherwise class is 0

targets_onehot = np.zeros((n_seq, time_steps_y, n_y), dtype=np.int)

targets_onehot[:, :, 0][targets[:, :] == 0] = 1
targets_onehot[:, :, 1][targets[:, :] == 1] = 1
targets_onehot[:, :, 2][targets[:, :] == 2] = 1

targets_onehot = np.cast[theano.config.floatX](targets_onehot)

mode = 'tr'

seq = seq.transpose(1, 0, 2)
targets_onehot = targets_onehot.transpose(1, 0, 2)

model = ENC_DEC(n_u, n_h * 2, n_d, n_y, 0.001, n_epochs, n_batch, n_maxlen)
model.add(BiDirectionLSTM(n_u, n_h))
model.add(decoder(n_h * 2, n_d, n_y))

model.build('softmax')

if mode == 'tr':
    model.train(seq, targets_onehot)
    model.save('encdec_new.pkl')
else:
    model.load('encdec_new.pkl')

i = 20
plt.close('all')
fig = plt.figure()
ax1 = plt.subplot(311)
output=Tokenizer(n_words)
output.fit_on_texts(text)
targets=output.texts_to_sequences(text,n_sentence,n_maxlen)

n_words_y=output.nb_words

targets[:-1]=targets[1:]

seq,seq_mask,targets,targets_mask=prepare_data(seq,targets,n_maxlen)

####build model

mode='tr'

model = ENC_DEC(n_u,n_h,n_d,n_y,n_epochs,n_chapter,n_batch,n_gen_maxlen,n_words_x,n_words_y,dim_word,
            momentum_switchover,lr,learning_rate_decay,snapshot_Freq,sample_Freq)
model.add(BiDirectionGRU(n_u,n_h))
model.add(decoder(n_h,n_d,n_y))
model.build()



filepath='data/ted.pkl'

if mode=='tr':
    if os.path.isfile(filepath): model.load(filepath)
    
    
    model.train(seq,seq_mask,targets,targets_mask,input,output,verbose,optimizer)
    model.save(filepath)
    
예제 #6
0
파일: Main.py 프로젝트: chuckgu/RNN
targets[:, 0][seq[:, 0, 1] < seq[:, 0, 0] - thresh] = 2
targets[:, -1][seq[:, -1, 1] > seq[:, -2, 0] + thresh] = 1
targets[:, -1][seq[:, -1, 1] < seq[:, -2, 0] - thresh] = 2
targets[:, 1:][seq[:, 1:-1, 1] > seq[:, :-2, 0] + thresh] = 1
targets[:, 1:][seq[:, 1:-1, 1] < seq[:, :-2, 0] - thresh] = 2
# otherwise class is 0

targets_onehot = np.zeros((n_seq, time_steps_y, n_y), dtype=np.int)

targets_onehot[:, :, 0][targets[:, :] == 0] = 1
targets_onehot[:, :, 1][targets[:, :] == 1] = 1
targets_onehot[:, :, 2][targets[:, :] == 2] = 1

mode = "tr1"

model = ENC_DEC(n_u, n_h, n_d, n_y, time_steps_x, time_steps_y, 0.001, 200)
model.add(hidden(n_u, n_h))
model.add(decoder(n_h, n_d, n_y, time_steps_x, time_steps_y))

model.build("softmax")


if mode == "tr":
    model.train(seq, targets)
    model.save("encdec_new.pkl")
else:
    model.load("encdec_new.pkl")

i = 20
plt.close("all")
fig = plt.figure()