if pos < 0: filename = model + '.theta0' else: filename = model[0:pos] + '.theta0' + model[pos:] with Writer(filename) as theta0_writer: pickle.dump(theta0, theta0_writer) theta0_saving_time = timer.toc() print >> stderr, 'optimizing...' callback = ThetaSaver(model, every) func = compute_cost_and_grad args = (instances, total_internal_node, word_vectors, embsize, lambda_reg) theta_opt = None try: theta_opt = lbfgs.optimize(func, theta0, maxiter, verbose, checking_grad, args, callback=callback) except GridentCheckingFailedError: send_terminate_signal() print >> stderr, 'Gradient checking failed, exit' exit(-1) send_terminate_signal() opt_time = timer.toc() timer.tic() # pickle form print >> stderr, 'saving parameters to %s' % model with Writer(model) as model_pickler: pickle.dump(theta_opt, model_pickler) # pure text form with Writer(model+'.txt') as writer:
pickle.dump(trg_theta0, theta0_writer) theta0_saving_time = timer.toc() # 每隔every步就存储一次模型参数 src_callback = ThetaSaver( model + '_source', every ) trg_callback = ThetaSaver( model + '_target', every ) func = compute_cost_and_grad src_args = ( src_instances, src_total_internal_node, src_word_vectors, src_embsize, lambda_reg, lambda_reg_L ) trg_args = ( trg_instances, trg_total_internal_node, trg_word_vectors, trg_embsize, lambda_reg, lambda_reg_L ) src_theta_opt = None trg_theta_opt = None print >> stderr, 'optimizing...' try: # 开始优化 src_theta_opt = lbfgs.optimize( func, src_theta0, maxiter, verbose, checking_grad, src_args, callback = src_callback ) except GridentCheckingFailedError: print >> stderr, 'Gradient checking failed, exit' exit(-1) try: # 开始优化 trg_theta_opt = lbfgs.optimize( func, trg_theta0, maxiter, verbose, checking_grad, trg_args, callback = trg_callback ) except GridentCheckingFailedError: print >> stderr, 'Gradient checking failed, exit' exit(-1) send_terminate_signal()
with Writer(filename) as theta0_writer: pickle.dump(theta0, theta0_writer) theta0_saving_time = timer.toc() print >> stderr, 'optimizing...' callback = ThetaSaver(model, every) func = compute_cost_and_grad args = (instances, total_internal_node, word_vectors, embsize, lambda_reg) theta_opt = None try: theta_opt = lbfgs.optimize(func, theta0, maxiter, verbose, checking_grad, args, callback=callback) except GridentCheckingFailedError: send_terminate_signal() print >> stderr, 'Gradient checking failed, exit' exit(-1) send_terminate_signal() opt_time = timer.toc() timer.tic() # pickle form print >> stderr, 'saving parameters to %s' % model with Writer(model) as model_pickler:
theta0_la_saving_time = timer.toc() # 每隔every步就存储一次模型参数 callback_la = ThetaSaver( model_la, every ) theta_opt_la = None func = compute_cost_and_grad_la args_la = ( src_instances, src_total_internal_node, src_word_vectors, src_embsize, trg_instances, trg_total_internal_node, trg_word_vectors, trg_embsize, lambda_reg_L, lambda_reg_rec,lambda_reg_sem, alpha, bad_src_instances, bad_trg_instances ) print >> stderr, 'Start Supervised Optimizing...' try: # Optimizing with label theta_opt_la = lbfgs.optimize( func, theta0_la, maxiter_la, verbose, checking_grad, args_la, callback = callback_la ) except GridentCheckingFailedError: print >> stderr, 'Gradient checking failed, exit' exit(-1) send_terminate_signal() opt_la_time = timer.toc() timer.tic() # pickle form print >> stderr, 'saving parameters to %s' % model_la with Writer( model_la ) as model_pickler: pickle.dump( theta_opt_la, model_pickler) # pure text form with Writer(model_la+'.txt') as writer: [writer.write('%20.8f\n' % v) for v in theta_opt_la]
filename = model + '.theta0' else: filename = model[0:pos] + '.theta0' + model[pos:] with Writer(filename) as theta0_writer: pickle.dump(theta0, theta0_writer) theta0_saving_time = timer.toc() print >> stderr, 'optimizing...' callback = ThetaSaver(model, every) func = preTrain args = (instances, total_internal_node, word_vectors, embsize, lambda_reg) theta_opt = None try: theta_opt = lbfgs.optimize(func, theta0[0:4 * embsize * embsize + 3 * embsize], maxiter, verbose, checking_grad, args, callback=callback) except GridentCheckingFailedError: send_terminate_signal() print >> stderr, 'Gradient checking failed, exit' exit(-1) send_terminate_signal() opt_time = timer.toc() timer.tic() print >> stderr, 'Prepare training data...' instances, instances_of_Unlabel, _ = prepare_data(word_vectors, instances_files, instances_file_of_Unlabel) func = compute_cost_and_grad args = (instances, instances_of_Unlabel, word_vectors, embsize, total_labeled_internal_node, lambda_rec, lambda_reg, lambda_reo, lambda_unlabel, instances_of_News, is_Test)