コード例 #1
0
    def adadelta(allparams,
                 nat_stepsize,
                 num_epochs,
                 seq_len,
                 num_seqs=None,
                 rho=0.95,
                 epsilon=1e-6,
                 num_samples=1,
                 permute=True):
        natparams, params = allparams[:1], allparams[1:]
        sum_gsq = zeros_like(params)  # accumulated sq. grads
        sum_usq = zeros_like(params)  # accumulated sq. updates
        accumulate = lambda a, b: add(scale(rho, a), scale(1 - rho, b))

        for epoch in xrange(num_epochs):
            vals = []
            batches, num_batches = split_into_batches(data, seq_len, num_seqs)
            for y in batches:
                val, grad = scale(
                    1. / num_datapoints,
                    val_and_grad(y, num_batches, num_samples, *allparams))
                natgrad, grad = grad[:1], grad[1:]
                sum_gsq = accumulate(sum_gsq, square(grad))
                diag_scaling = div(sqrt(add_scalar(epsilon, sum_usq)),
                                   sqrt(add_scalar(epsilon, sum_gsq)))
                update = mul(diag_scaling, grad)
                sum_usq = accumulate(sum_usq, square(update))

                natparams = add(natparams, scale(nat_stepsize, natgrad))
                params = add(params, update)
                allparams = concat(natparams, params)
                vals.append(val)

                if callback: callback(epoch, vals, natgrad, allparams)
        return allparams
コード例 #2
0
    def adam(allparams,
             nat_stepsize,
             stepsize,
             num_epochs,
             seq_len,
             num_seqs=None,
             b1=0.9,
             b2=0.999,
             eps=1e-8,
             num_samples=1):
        natparams, params = allparams[:1], allparams[1:]
        m = zeros_like(params)
        v = zeros_like(params)
        i = 0
        accumulate = lambda rho, a, b: add(scale(1 - rho, a), scale(rho, b))

        for epoch in xrange(num_epochs):
            vals = []
            batches, num_batches = split_into_batches(data, seq_len, num_seqs)
            for y in batches:
                val, grad = scale(
                    1. / num_datapoints,
                    val_and_grad(y, num_batches, num_samples, *allparams))
                natgrad, grad = grad[:1], grad[1:]

                m = accumulate(b1, grad, m)  # first moment estimate
                v = accumulate(b2, square(grad), v)  # second moment estimate
                mhat = scale(1. / (1 - b1**(i + 1)), m)  # bias correction
                vhat = scale(1. / (1 - b2**(i + 1)), v)
                update = scale(stepsize, div(mhat, add_scalar(eps,
                                                              sqrt(vhat))))

                natparams = add(natparams, scale(nat_stepsize, natgrad))
                params = add(params, update)
                allparams = concat(natparams, params)
                vals.append(val)
                i += 1

                if callback: callback(epoch, vals, natgrad, allparams)

        return allparams
コード例 #3
0
def adam(gradfun, allparams, num_iters, step_size, b1=0.9, b2=0.999, eps=1e-8):
    natparams, params = allparams[:1], allparams[1:]
    m = zeros_like(params)
    v = zeros_like(params)
    i = 0
    accumulate = lambda rho, a, b: add(scale(1-rho, a), scale(rho, b))
 
    for i in xrange(num_iters):
        grad = gradfun(allparams, i)
        natgrad, grad = grad[:1], grad[1:]
 
        m = accumulate(b1, grad, m)          # first moment estimate
        v = accumulate(b2, square(grad), v)  # second moment estimate
        mhat = scale(1./(1 - b1**(i+1)), m)  # bias correction
        vhat = scale(1./(1 - b2**(i+1)), v)
        update = scale(step_size, div(mhat, add_scalar(eps, sqrt(vhat))))
 
        natparams = sub(natparams, scale(step_size, natgrad))
        params = sub(params, update)
        allparams = concat(natparams, params)
 
    return allparams
コード例 #4
0
    def verify(self):
        hash = self.checksum()
        '''
		try:
			dao.TransactionDAO.read_by_hash(binascii.hexlify(hash).decode())
			# the transaction was already spent
			# this is not enough, we should also check if this transaction was already mined inside a block
			return False
		except:
			pass
		'''

        valid = False
        # we need to scan the blockchain in order to understand
        # if this transaction was already included in a block

        # check if inputs can be spent!
        inputs_value = 0

        for input in self.inputs:
            # recover the utxo refered by the input
            # output = input.utxo.outputs[input.vout]
            utxo = dao.TransactionDAO.read_by_hash(
                binascii.hexlify(input.utxo).decode())
            output = utxo.outputs[input.vout]

            # how many time utxo was mined into a block?
            tx_utxo_count = len(
                list(dao.BlockDAO.tx_in_blocks(utxo.checksum())))
            tx_this_count = len(list(dao.BlockDAO.tx_in_blocks(hash)))

            # print('%s -> [count] %d, [unlock] %d' % (binascii.hexlify(utxo.checksum()).decode(), tx_utxo_count, tx_this_count))
            if (tx_utxo_count - tx_this_count) < 1:
                print('The UTXO [%s] cannot be spent anymore' %
                      binascii.hexlify(utxo.checksum()).decode())
                break

            # input pubkey to address
            sha256 = hashlib.new('sha256')
            ripemd160 = hashlib.new('ripemd160')

            sha256.update(input.pubkey)
            ripemd160.update(sha256.digest())

            if output.address != base58.b58encode_check(b'\x00' +
                                                        ripemd160.digest()):
                # print('You cannot unlock this output with this public key')
                break

            # the unlocking address is the same as the locking one
            # now we have to check the signature
            # decompress the public key
            curve = SECP256k1.curve
            signY = input.pubkey[0] - 2
            x = int.from_bytes(input.pubkey[1:], byteorder='big')
            yy = (pow(x, 3, curve.p()) + curve.a() * x + curve.b()) % curve.p()

            try:
                y = sqrt(yy, curve.p())
            except:
                # invalid x probably
                break

            if y % 2 != signY:
                y = curve.p() - y

            key = x.to_bytes(32, byteorder='big') + y.to_bytes(32,
                                                               byteorder='big')
            vk = VerifyingKey.from_string(key, SECP256k1)

            try:
                vk.verify(input.signature, utxo.checksum())
            except BadSignatureError:
                break

            inputs_value += output.value
        else:
            #print('Every input can be unlocked')
            valid = True

        # signature is valid
        if valid:
            outputs_value = 0

            for output in self.outputs:
                outputs_value += output.value

            # print(inputs_value, outputs_value)

            # ignore coinbase TXs for this check
            if len(self.inputs) > 0 and inputs_value < outputs_value:
                valid = False

        return valid