def __init__(self, n, h, in_size, in_channels, embed_size, block_size):
super().__init__(
l0=L.Convolution2D(in_channels, n, 3, stride=1, pad=1),
ln=L.Linear(None, h))
self.n_blocks = int(log2(in_size / embed_size)) + 1
self.block_size = block_size
for i in range(self.n_blocks):
n_in = (i + 1) * n
n_out = (i + 2) * n if i < self.n_blocks - 1 else n_in
for j in range(block_size - 1):
self.add_link('c{}'.format(i * block_size + j),
L.Convolution2D(n_in, n_in, 3, stride=1, pad=1))
self.add_link('c{}'.format(i * block_size + block_size - 1),
L.Convolution2D(n_in, n_out, 3, stride=1, pad=1))
python类log2()的实例源码
def get_entropy(question, animals):
""" Finds the entropy of the answers of a question for a set of animals """
response_set = {answer:0.000001 for answer in ANSWERS}
all_responses = query_all_responses(question=question, animals=animals)
for animal in animals:
responses = all_responses.get(animal.name, NO_RESPONSE)
if responses is not None:
responses = sorted(
responses.items(),
key=lambda resp: -resp[1]
)
(first, _) = responses[0]
(last, _) = responses[-1]
response_set[first] += animal.prob
response_set[last] += (1-animal.prob)
else:
print("COULDN'T FIND ANIMAL \"{}\"".format(animal.name))
total = sum(response_set.values())
probs = [count/total for count in response_set.values()]
entropy = sum([-(p)*log2(p) for p in probs])
return entropy
def create_for_user(cls, pkcs12, user, cn=None, days=None):
if not cn:
cn = get_full_name(user)
ec = EthicsCommission.objects.get(uuid=settings.ETHICS_COMMISSION_UUID)
subject = '/CN={}/O={}/emailAddress={}'.format(cn, ec.name[:64], user.email)
passphrase_len = math.ceil(
PASSPHRASE_ENTROPY / math.log2(len(PASSPHRASE_CHARS)))
passphrase = ''.join(
SystemRandom().choice(PASSPHRASE_CHARS)
for i in range(passphrase_len)
)
from ecs.pki import openssl
data = openssl.make_cert(subject, pkcs12, passphrase=passphrase,
days=days)
cert = cls.objects.create(user=user, cn=cn, **data)
return (cert, passphrase)
def PKCS1(message : int, size_block : int) -> int:
"""
PKCS1 padding function
the format of this padding is :
0x02 | 0x00 | [0xFF...0xFF] | 0x00 | [message]
"""
# compute the length in bytes of the message
length = math.ceil(math.ceil(math.log2(message-1)) / 8)
template = "0200"
# generate a template 0xFFFFF.....FF of size_block bytes
for i in range(size_block-2):
template = template + 'FF'
template = int(template,16)
# Add the 00 of the end of the padding to the template
for i in range(length+1) :
template = template ^ (0xFF << i*8)
# add the padding to the original message
message = message | template
return message
def testLog2(self):
self.assertRaises(TypeError, math.log2)
# Check some integer values
self.assertEqual(math.log2(1), 0.0)
self.assertEqual(math.log2(2), 1.0)
self.assertEqual(math.log2(4), 2.0)
# Large integer values
#fixme brython (javascript chokes on these large values)
#self.assertEqual(math.log2(2**1023), 1023.0)
#self.assertEqual(math.log2(2**1024), 1024.0)
#self.assertEqual(math.log2(2**2000), 2000.0)
self.assertRaises(ValueError, math.log2, -1.5)
self.assertRaises(ValueError, math.log2, NINF)
self.assertTrue(math.isnan(math.log2(NAN)))
#@unittest.skip("brython, skip for now")
def set_number_features_evaluated_split(self, row):
"""
Sets the number of considered features at each split depending on the
max_split_features parameter.
:param row: A single row of the features of shape (nb_features)
"""
if isinstance(self.max_split_features, int):
self.considered_features = self.max_split_features if \
self.max_split_features <= len(row) else len(row)
elif isinstance(self.max_split_features, str):
if self.max_split_features in ['auto','sqrt']:
self.considered_features = int(sqrt(len(row)))
elif self.max_split_features == 'log2':
self.considered_features = int(log2(len(row)))
else:
self.considered_features = len(row)
def testLog2(self):
self.assertRaises(TypeError, math.log2)
# Check some integer values
self.assertEqual(math.log2(1), 0.0)
self.assertEqual(math.log2(2), 1.0)
self.assertEqual(math.log2(4), 2.0)
# Large integer values
self.assertEqual(math.log2(2**1023), 1023.0)
self.assertEqual(math.log2(2**1024), 1024.0)
self.assertEqual(math.log2(2**2000), 2000.0)
self.assertRaises(ValueError, math.log2, -1.5)
self.assertRaises(ValueError, math.log2, NINF)
self.assertTrue(math.isnan(math.log2(NAN)))
def testLog2(self):
self.assertRaises(TypeError, math.log2)
# Check some integer values
self.assertEqual(math.log2(1), 0.0)
self.assertEqual(math.log2(2), 1.0)
self.assertEqual(math.log2(4), 2.0)
# Large integer values
self.assertEqual(math.log2(2**1023), 1023.0)
self.assertEqual(math.log2(2**1024), 1024.0)
self.assertEqual(math.log2(2**2000), 2000.0)
self.assertRaises(ValueError, math.log2, -1.5)
self.assertRaises(ValueError, math.log2, NINF)
self.assertTrue(math.isnan(math.log2(NAN)))
def dynamic_timeout_formular(min_votes, votes_fraction):
"""
Formula used for dynamic timeout
:param min_votes:
:param votes_fraction:
:return:
"""
if votes_fraction >= 1:
return 1 / votes_fraction # Reduce timeout if more people than necessary voted
result = 1
result += (1 - votes_fraction) * math.log2(min_votes) # Linear part makes timeout rise
result += min_votes * (
min_votes ** ((1 - votes_fraction) ** 3) - 1) # Exponential part to punish really low vote counts
result += (40 - 40 ** (votes_fraction)) / min_votes ** 2 # Punish missing votes harder if min_votes is low
return result
def sampled_entropy(sample_size, success_size):
'''
Estimate the change in entropy given a sample of passwords from a set.
Rationalle: Assume that we can produce passwords with a known entropy.
However, not all of these passwords are at least 24 characters long. Rather
than try to calculate the exact change in entropy, we estimate it by
generating {sample_size} passwords, and seeing that {success_size} meet our
constraints. We then assume that the ratio of these numbers is proportional
to the overall ratio of possible_passwords to allowed_passwords. This
allows us to do the following caluclation:
original_entropy = log2(permutation_space)
new_entropy = log2(permutation_space * ratio)
= log2(permutation_space) + log2(ratio)
= log2(permutation_space) + log2(success_size / sample_size)
= log2(permutation_space) + log2(success_size) - log2(sample_size)
= original_entropy + log2(success_size) - log2(sample_size)
'''
return log2(success_size) - log2(sample_size)
def Waifu2xResize(src, width, height, targetWidth, targetHeight):
resize_factor = 2 ** math.ceil(max(math.log2(targetHeight / height), math.log2(targetWidth / targetWidth)))
logging.debug("w: {} h: {} tw: {} th: {} rf: {}".format(width, height, targetWidth, targetHeight, resize_factor))
tmp = tempfile.mkstemp(suffix=".png", prefix="pmsync_cover_")
try:
subprocess.run(["waifu2x-converter-cpp",
"--scale_ratio", str(resize_factor),
"-m", "scale",
"-i", src,
"-o", tmp[1]],
check=True,
stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
except subprocess.CalledProcessError as e:
logging.debug("=== CalledProcessError ===")
logging.debug("cmd: {}".format(e.cmd))
logging.debug("output: {}".format(e.stdout.decode()))
logging.debug("stderr: {}".format(e.stderr.decode()))
logging.debug("=== CalledProcessError ===")
os.remove(tmp[1])
return PILResize(tmp[1], width*resize_factor, height*resize_factor, targetWidth, targetHeight)
def getClass(self, string):
maxProb = float('-inf')
maxClass = ''
for i in self.classCount:
logProb = log2(self.classCount[i])-log2(sum(self.classCount.values()))
# print(i)
for word in string.split():
if word in self.wordsFreq:
wordLogProb = log2(self.wordsFreq[word][i]+1)-log2(len(self.classFreq[i])+len(self.wordsFreq)+1)
else:
wordLogProb = -log2(len(self.classFreq[i])+len(self.wordsFreq)+1)
logProb += wordLogProb
# print('\t',word, wordProb)
# print(prob,'\n****************')
if logProb > maxProb:
maxProb = logProb
maxClass = i
# print(maxProb)
return maxClass
def calculateEntropy(self, corpus=None):
corpus_size = len(corpus) if corpus is not None else len(self.corpus)
return corpus_size, sum([1 / corpus_size * log(1 / corpus_size) for n in range(corpus_size)]) * -1
def WriteFloat(self, v):
if v < 0:
neg = (1 << 31)
v *= -1
else:
neg = 0
if v == 0:
e = 0
m = 0
else:
e = int(floor(log2(v/0x1000000))+1 + 150)
if e < 0 or e > 0xff:
print("float off range, assuming zero", file=sys.stderr)
e = 0
m = 0
elif e == 0:
e = e << 23
m = int(v / 2**(-150)) >> 1
else:
e = e << 23
m = int(v/2**(floor(log2(v/0x1000000))+1))
if m < 0x800000 or m >= 0x1000000:
raise ValueError('float dump: bad m value')
m &= 0x7fffff
self.writeDword(neg | e | m)
def log2(x: Real) -> Real:
"""
Return the logarithm of x in base 2.
"""
return _math.log2(x)
def get_metric_range(self, pre_dist):
num_classes = len(pre_dist)
if num_classes < 2:
num_classes = 2
return math.log2(num_classes)
def _impl(self):
self.DATA_WIDTH = int(self.DATA_WIDTH)
vldAll = mask(self.DATA_WIDTH // 8)
dout = self.dataOut
DATA_LEN = len(self.DATA)
wordIndex_w = int(math.log2(DATA_LEN) + 1)
wordIndex = self._reg("wordIndex", Bits(wordIndex_w), defVal=0)
Switch(wordIndex)\
.addCases([(i, dout.data(d))
for i, d in enumerate(self.DATA)])\
.Default(dout.data(None))
dout.last(wordIndex._eq(DATA_LEN - 1))
If(wordIndex < DATA_LEN,
dout.strb(vldAll),
dout.valid(1)
).Else(
dout.strb(None),
dout.valid(0)
)
If(self.dataRd(),
self.nextWordIndexLogic(wordIndex)
)
def __init__(self, ranges, eps=0.001):
'''
Class for individual in population. Random solution will be initialized
by default.
NOTE: The decrete precisions for different components in varants may be
adjusted automatically (possible precision loss) if eps and ranges
are not appropriate.
:param ranges: value ranges for all entries in solution.
:type ranges: list of range tuples. e.g. [(0, 1), (-1, 1)]
:param eps: decrete precisions for binary encoding, default is 0.001.
:type eps: float or float list with the same length with ranges.
'''
super(self.__class__, self).__init__(ranges, eps)
# Lengths for all binary sequence in chromsome and adjusted decrete precisions.
self.lengths = []
for i, ((a, b), eps) in enumerate(zip(self.ranges, self.eps)):
length = int(log2((b - a)/eps))
precision = (b - a)/(2**length)
self.lengths.append(length)
self.precisions[i] = precision
# The start and end indices for each gene segment for entries in solution.
self.gene_indices = self._get_gene_indices()
# Initialize individual randomly.
self.init()
def calculateIDF(self):
# ???????????????
if len(self.wordset) == 0:
self.buildWordSet()
if len(self.words_location_record) == 0:
self.buildWordLocationRecord()
# ?? idf
for word in self.wordset:
self.words_idf[word] = math.log2((self.D + .5)/(self.words_location_record[word] + .5))
def _entropy(temp, prob):
if prob == 0 or prob == 0.5 or temp == 0:
return prob
if prob < 0.5:
return 1.0 - _original(temp, 1.0 - prob)
coldness = 100.0 - temp
a = math.sqrt(coldness)
c = (10 - a) / 100
f = (c + 1) * prob
return -f * math.log2(f)
def size(value, suffixes=None):
"""
>>> size(1024)
'1 KB'
:param size:
:param suffixes:
:return:
"""
suffixes = suffixes or [
'bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
order = int(log2(value) / 10) if value else 0
return '{:.4g} {}'.format(value / (1 << (order * 10)), suffixes[order])
def __init__(self, n, out_size, out_channels, embed_size, block_size):
super().__init__(
l0=L.Linear(None, n * embed_size * embed_size),
ln=L.Convolution2D(n, out_channels, 3, stride=1, pad=1))
self.embed_shape = (n, embed_size, embed_size)
self.n_blocks = int(log2(out_size / embed_size)) + 1
self.block_size = block_size
for i in range(self.n_blocks * block_size):
self.add_link('c{}'.format(i),
L.Convolution2D(n, n, 3, stride=1, pad=1))
def entropy_term(x):
"""Helper function for entropy_single: calculates one term in the sum."""
if x==0: return 0.0
else: return -x*math.log2(x)
def kl_term(x,y):
"""Helper function for kl: calculates one term in the sum."""
if x>0 and y>0: return x*math.log2(x/y)
elif x==0: return 0.0
else: return math.inf
def _calc_entropy(self, data_set):
"""
compute the entropy of the data_set
:param data_set: the data_set you want to calculator entropy
:return: x, x is a numerical value represent the entropy
"""
class_list = [example[-1] for example in data_set]
unique_class = set(class_list)
record_num = len(data_set)
entropy = 0.0
for item in unique_class:
item_data = self._find_record_with_value(-1, data_set, item)
probability = len(item_data) / record_num
entropy -= probability * math.log2(probability)
return entropy
def decode(message : int, pad_option : str) -> str :
size_block = math.ceil(math.ceil(math.log2(message))/8)*8
message = unpadding(message, size_block, pad_option)
message = binascii.unhexlify(hex(message)[2:])
return str(message)[2:-1]
def unpad_PKCS1(message : int, size_block : int) -> int:
"""
PKCS1 unpadding function
"""
pts = 0xff << (size_block - 24)
while (pts & message != 0):
pts = pts >> 8
a = ~pts & ((1 << size_block) - 1)
message = message & (~pts & ((1 << math.ceil(math.log2(pts))) - 1) )
return message
def obj_IA_sum_rate(H, p, var_noise, K):
y = 0.0
for i in range(K):
s = var_noise
for j in range(K):
if j!=i:
s = s+H[i,j]**2*p[j]
y = y+math.log2(1+H[i,i]**2*p[i]/s)
return y
# Functions for WMMSE algorithm
def WMMSE_sum_rate(p_int, H, Pmax, var_noise):
K = np.size(p_int)
vnew = 0
b = np.sqrt(p_int)
f = np.zeros(K)
w = np.zeros(K)
for i in range(K):
f[i] = H[i, i] * b[i] / (np.square(H[i, :]) @ np.square(b) + var_noise)
w[i] = 1 / (1 - f[i] * b[i] * H[i, i])
vnew = vnew + math.log2(w[i])
VV = np.zeros(100)
for iter in range(100):
vold = vnew
for i in range(K):
btmp = w[i] * f[i] * H[i, i] / sum(w * np.square(f) * np.square(H[:, i]))
b[i] = min(btmp, np.sqrt(Pmax)) + max(btmp, 0) - btmp
vnew = 0
for i in range(K):
f[i] = H[i, i] * b[i] / ((np.square(H[i, :])) @ (np.square(b)) + var_noise)
w[i] = 1 / (1 - f[i] * b[i] * H[i, i])
vnew = vnew + math.log2(w[i])
VV[iter] = vnew
if vnew - vold <= 1e-3:
break
p_opt = np.square(b)
return p_opt
# Functions for performance evaluation
def testLog2Exact(self):
#fixme brython.
# Check that we get exact equality for log2 of powers of 2.
actual = [math.log2(math.ldexp(1.0, n)) for n in range(-1074, 1024)]
expected = [float(n) for n in range(-1074, 1024)]
self.assertEqual(actual, expected)