示例#1
0
    def add_to_hamiltonian(self, left_block_op='id', left_site_op='id', 
    		           right_site_op='id', right_block_op='id',
			   param=1.0):
	"""Adds a term to the hamiltonian.

	You use this function to add a term to the Hamiltonian of the
	system. This is just a convenience function. 

	Parameters
	----------
	left_block_op : a string (optional).
	    The name of an operator in the left block of the system.
	left_site_op : a string (optional).
	    The name of an operator in the left site of the system.
	right_site_op : a string (optional).
	    The name of an operator in the right site of the system.
	right_block_op : a string (optional).
	    The name of an operator in the right block of the system.
	param : a double/complex (optional).
	    A parameter which multiplies the term.

	Raises
	------
	DMRGException 
	    if any of the operators are not in the corresponding
	    site/block.

	Examples
	--------
        >>> from dmrg101.core.sites import SpinOneHalfSite
        >>> from dmrg101.core.system import System
        >>> # build a system with four spins one-half.
        >>> spin_one_half_site = SpinOneHalfSite()
        >>> ising_fm_in_field = System(spin_one_half_site)
        >>> # use four strings to name each operator in term
        >>> ising_fm_in_field.add_to_hamiltonian('s_z', 's_z', 'id', 'id')
        >>> ising_fm_in_field.add_to_hamiltonian('id', 's_z', 's_z', 'id')
        >>> ising_fm_in_field.add_to_hamiltonian('id', 'id', 's_z', 's_z')
        >>> # use argument names to save extra typing for some terms
        >>> h = 0.1
        >>> ising_fm_in_field.add_to_hamiltonian(left_block_op='s_z', param=-h)
        >>> ising_fm_in_field.add_to_hamiltonian(left_site_op='s_z', param=-h)
        >>> ising_fm_in_field.add_to_hamiltonian(right_site_op='s_z', param=-h)
        >>> ising_fm_in_field.add_to_hamiltonian(right_block_op='s_z', param=-h)
	"""
	left_side_op = make_tensor(self.left_block.operators[left_block_op],
		                   self.left_site.operators[left_site_op])
	right_side_op = make_tensor(self.right_block.operators[right_block_op],
		                    self.right_site.operators[right_site_op])
	self.h.add(left_side_op, right_side_op, param)
示例#2
0
    def add_to_operators_to_update(self, name, block_op='id', site_op='id'):
	"""Adds a term to the hamiltonian.

	You use this function to add an operator to the list of operators
	that you need to update. You need to update an operator if it is
	going to be part of a term in the Hamiltonian in any later step in
	the current sweep.

	Parameters
	----------
	name : a string.
	    The name of the operator you are including in the list to
	    update.
	left_block_op : a string (optional).
	    The name of an operator in the left block of the system.
	left_site_op : a string (optional).
	    The name of an operator in the left site of the system.

	Raises
	------
	DMRGException 
	    if any of the operators are not in the corresponding
	    site/block.

	Examples
	--------
        >>> from dmrg101.core.sites import SpinOneHalfSite
        >>> from dmrg101.core.system import System
        >>> # build a system with four spins one-half.
        >>> spin_one_half_site = SpinOneHalfSite()
        >>> ising_fm_in_field = System(spin_one_half_site)
        >>> # some stuff here..., but the only operator that you need to
	>>> # update is 's_z' for the last site of the block.
        >>> ising_fm_in_field.add_to_operators_to_update(site_op='s_z')
	>>> print ising_fm_in_field.operators_to_add_to_block.keys()
	('s_z')
	"""
	tmp = make_tensor(self.growing_block.operators[block_op],
		          self.growing_site.operators[site_op])
	self.operators_to_add_to_block[name] = tmp
示例#3
0
    def add_to_block_hamiltonian(self, tmp_matrix_for_bh, block_op='id', 
		                 site_op='id', param=1.0):
	"""Adds a term to the hamiltonian.

	You use this function to add a term to the Hamiltonian of the
	system. This is just a convenience function. 

	Parameters
	----------
	tmp_matrix_for_bh : a numpy array of ndim = 2.
	    An auxiliary matrix to keep track of the result.
	left_block_op : a string (optional).
	    The name of an operator in the left block of the system.
	left_site_op : a string (optional).
	    The name of an operator in the left site of the system.
	param : a double/complex (optional).
	    A parameter which multiplies the term.

	Raises
	------
	DMRGException 
	    if any of the operators are not in the corresponding
	    site/block.

	Examples
	--------
        >>> from dmrg101.core.sites import SpinOneHalfSite
        >>> from dmrg101.core.system import System
        >>> # build a system with four spins one-half.
        >>> spin_one_half_site = SpinOneHalfSite()
        >>> ising_fm_in_field = System(spin_one_half_site)
        >>> # add the previous block Hamiltonian...
        >>> ising_fm_in_field.add_to_block_hamiltonian(block_op = 'bh')
	>>> # ... and then add the term coming from eating the current site.
        >>> ising_fm_in_field.add_to_block_hamiltonian('s_z', 's_z')
	"""
	tmp = make_tensor(self.growing_block.operators[block_op],
		          self.growing_site.operators[site_op])
	tmp_matrix_for_bh += param * tmp
示例#4
0
            # TODO: Refine dev loss calculation
            avg_dev_loss = _forward_all(dev_tensor, model, sess)
            logger.info('Epoch: {}; Train loss: {}; Dev loss: {};'.format(
                epoch, avg_loss, avg_dev_loss))

            if epoch % 2 == 0:
                dev_eval = evaluate(dev_tensor, candidates_tensor, sess, model)
                logger.info('Evaluation: {}'.format(dev_eval))
                accuracy = dev_eval[2]
                if accuracy >= prev_best_accuracy:
                    logger.debug('Saving checkpoint')
                    prev_best_accuracy = accuracy
                    saver.save(sess, save_dir)


if __name__ == '__main__':
    args = _parse_args()
    vocab = load_vocab(args.vocab)
    train_tensor = make_tensor(args.train, vocab)
    dev_tensor = make_tensor(args.dev, vocab)
    candidates_tensor = make_tensor(args.candidates, vocab)
    config = {
        'batch_size': 32,
        'epochs': 15,
        'negative_cand': args.negative_cand,
        'save_dir': args.save_dir,
        'lr': args.learning_rate
    }
    model = Model(len(vocab), emb_dim=args.emb_dim, margin=args.margin)
    main(train_tensor, dev_tensor, candidates_tensor, model, config)
                    'Inf') or score == -float('Inf') or score == float('NaN'):
                print(score, ind, scores[ind])
                raise ValueError
            if score >= test_score and not np.array_equal(
                    candidate_responses[ind], true_response):
                return False
    return True


def _parse_args():
    parser = argparse.ArgumentParser()

    parser.add_argument('--test', help='Path to test filename')
    parser.add_argument('--vocab', default='data/vocab.tsv')
    parser.add_argument('--candidates', default='data/candidates.tsv')
    parser.add_argument('--checkpoint_dir')
    parser.add_argument('--emb_dim', type=int, default=32)

    args = parser.parse_args()

    return args


if __name__ == '__main__':
    args = _parse_args()
    vocab = load_vocab(args.vocab)
    test_tensor = make_tensor(args.test, vocab)
    candidates_tensor = make_tensor(args.candidates, vocab)
    model = Model(len(vocab), args.emb_dim)
    main(test_tensor, candidates_tensor, model, args.checkpoint_dir)