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 plt.close('all') fig = plt.figure() ax3 = plt.subplot(111) plt.plot(model.errors)
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) plt.plot(seq[:,i]) plt.grid() ax1.set_title('input')
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 plt.close('all') fig = plt.figure()
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) plt.plot(seq[i]) plt.grid() ax1.set_title('input') ax2 = plt.subplot(312)
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) plt.plot(seq[i]) plt.grid() ax1.set_title("input")