def feedforward(self):
#??????????
for i in range(len(self.wordids)):
self.ai[i]=1.0
#??????????
for j in range(len(self.hiddenids)):
sumi = 0.0
for i in range(len(self.wordids)):
sumi=sumi+self.ai[i]*self.wi[i][j]
self.ah[j]=tanh(sumi)
#??????????
for k in range(len(self.urlids)):
sumj = 0.0
for j in range(len(self.hiddenids)):
sumj = sumj + self.ah[j]*self.wo[j][k]
self.ao[k]=tanh(sumj)
return self.ao[:]
python类tanh()的实例源码
def update(self, inputs):
# input activations
for i in range(self.input_size - 1):
self.ai[i] = inputs[i]
# hidden activations
for j in range(self.hidden_size):
sum = 0.0
for i in range(self.input_size):
sum = sum + self.ai[i] * self.Wi[i][j]
self.ah[j] = math.tanh(sum)
# output activations
for k in range(self.output_size):
sum = 0.0
for j in range(self.hidden_size):
sum = sum + self.ah[j] * self.Wo[j][k]
self.ao[k] = sigmoid(sum)
return self.ao[:]
def feedforward(self):
for i in xrange(len(self.wordids)):
self.ai[i] = 1.0
for j in xrange(len(self.hiddenids)):
sumit = 0.0
for i in xrange(len(self.wordids)):
sumit += self.ai[i] * self.wi[i][j]
self.ah[j] = tanh(sumit)
for k in xrange(len(self.urlids)):
sumit = 0.0
for j in xrange(len(self.hiddenids)):
sumit += self.ah[j] * self.wo[j][k]
self.ao[k] = tanh(sumit)
return self.ao[:]
def test_tanh():
from keras.activations import tanh as t
test_values = get_standard_values()
x = T.vector()
exp = t(x)
f = theano.function([x], exp)
result = f(test_values)
expected = [math.tanh(v) for v in test_values]
print(result)
print(expected)
list_assert_equal(result, expected)
def similarity(user_id_a,user_id_b,sim_type=0):
user_a_tuple_list = userdict[user_id_a].get_items()
user_b_tuple_list = userdict[user_id_b].get_items()
common_items=0
sim = 0.0
for t1 in user_a_tuple_list:
for t2 in user_b_tuple_list:
if (t1[0] == t2[0]):
common_items += 1
sim += math.pow(t1[1]-t2[1],2)
if common_items>0:
sim = math.sqrt(sim/common_items)
sim = 1.0 - math.tanh(sim)
if sim_type==1:
max_common = min(len(user_a_tuple_list),len(user_b_tuple_list))
sim = sim * common_items / max_common
print "User Similarity between",names[user_id_a],"and",names[user_id_b],"is", sim
return sim #If no common items, returns zero
#3.1
# load movielens data
def feedforward(self):
# the only inputs are the query words
for i in range(len(self.wordids)):
self.ai[i] = 1.0
# hidden activations
for j in range(len(self.hiddenids)):
sum = 0.0
for i in range(len(self.wordids)):
sum = sum + self.ai[i] * self.wi[i][j]
self.ah[j] = tanh(sum)
# output activations
for k in range(len(self.urlids)):
sum = 0.0
for j in range(len(self.hiddenids)):
sum = sum + self.ah[j] * self.wo[j][k]
self.ao[k] = tanh(sum)
return self.ao[:]
def feed_forward(self):
for i in range(len(self.word_ids)):
self.ai[i]=1.0
for j in range(len(self.hidden_ids)):
sum=0.0
for i in range(len(self.word_ids)):
sum=sum+self.ai[i]*self.wi[i][j]
self.ah[j]=tanh(sum)
for k in range(len(self.url_ids)):
sum=0.0
for j in range(len(self.hidden_ids)):
sum=sum+self.ah[j]*self.wo[j][k]
self.ao[k]=tanh(sum)
return self.ao[:]
def get(self):
self.x += self.config.get('dx', 0.1)
val = eval(self.config.get('function', 'sin(x)'), {
'sin': math.sin,
'sinh': math.sinh,
'cos': math.cos,
'cosh': math.cosh,
'tan': math.tan,
'tanh': math.tanh,
'asin': math.asin,
'acos': math.acos,
'atan': math.atan,
'asinh': math.asinh,
'acosh': math.acosh,
'atanh': math.atanh,
'log': math.log,
'abs': abs,
'e': math.e,
'pi': math.pi,
'x': self.x
})
return self.createEvent('ok', 'Sine wave', val)
def feedforward(self):
# the only inputs are the query words
for i in range(len(self.wordids)):
self.ai[i] = 1.0
# hidden activations
for j in range(len(self.hiddenids)):
sum = 0.0
for i in range(len(self.wordids)):
sum = sum + self.ai[i] * self.wi[i][j]
self.ah[j] = tanh(sum)
# output activations
for k in range(len(self.urlids)):
sum = 0.0
for j in range(len(self.hiddenids)):
sum = sum + self.ah[j] * self.wo[j][k]
self.ao[k] = tanh(sum)
return self.ao[:]
def obFixer(observation_space,observation): # fixes observation ranges, uses hyperbolic tangent for infinite values
newObservation = []
if observation_space.__class__ == gym.spaces.box.Box:
for space in range(observation_space.shape[0]):
high = observation_space.high[space]
low = observation_space.low[space]
if high == numpy.finfo(numpy.float32).max or high == float('Inf'):
newObservation.append(math.tanh(observation[space]))
else:
dif = high - low
percent = (observation[space]+abs(low))/dif
newObservation.append(((percent * 2)-1).tolist())
if observation_space.__class__ == gym.spaces.discrete.Discrete:
c = 0
for neuron in range(observation_space.n):
if observation == neuron:
newObservation.append(1)
else:
newObservation.append(0)
return newObservation
def set_candidate_inlinks(self):
'''
Determine inlinks feature values.
'''
if not [f for f in self.features if f.startswith('candidate_inlinks')]:
return
for link_type in ['inlinks', 'inlinks_newspapers']:
link_count = self.document.get(link_type)
if link_count:
setattr(self, 'candidate_' + link_type,
math.tanh(link_count * 0.001))
if not hasattr(self.cand_list, 'sum_' + link_type):
self.cand_list.set_sum_inlinks()
link_sum = getattr(self.cand_list, 'sum_' + link_type)
if link_sum:
link_count_rel = link_count / float(link_sum)
setattr(self, 'candidate_' + link_type + '_rel',
link_count_rel)
def set_solr_properties(self):
'''
Determine Solr iteration, position and score.
'''
if not [f for f in self.features if f.startswith('match_str_solr')]:
return
# Solr iteration
self.match_str_solr_query_0 = 1 if self.query_id == 0 else 0
self.match_str_solr_query_1 = 1 if self.query_id == 1 else 0
self.match_str_solr_query_2 = 1 if self.query_id == 2 else 0
self.match_str_solr_query_3 = 1 if self.query_id == 3 else 0
self.match_str_solr_substitution = 1 if self.query_iteration == 1 else 0
# Solr position (relative to other remaining candidates)
pos = self.cand_list.filtered_candidates.index(self)
self.match_str_solr_position = 1.0 - math.tanh(pos * 0.25)
# Solr score (relative to other remaining candidates)
if not hasattr(self.cand_list, 'max_score'):
self.cand_list.set_max_score()
if self.cand_list.max_score:
self.match_str_solr_score = (self.document.get('score') /
float(self.cand_list.max_score))
def set_entity_match(self):
'''
Match other entities appearing in the article with DBpedia abstract.
'''
if not 'match_txt_entities' in self.features:
return
if not hasattr(self.cluster, 'entity_parts'):
self.cluster.get_entity_parts()
if not hasattr(self.cluster, 'context_entity_parts'):
self.cluster.get_context_entity_parts()
if not self.cluster.context_entity_parts:
return
if not hasattr(self, 'abstract_bow'):
self.tokenize_abstract()
bow = [t for t in self.abstract_bow if len(t) > 4]
entity_match = len(set(self.cluster.context_entity_parts) & set(bow))
self.match_txt_entities = math.tanh(entity_match * 0.25)
def test_tanh():
from keras.activations import tanh as t
test_values = get_standard_values()
x = T.vector()
exp = t(x)
f = theano.function([x], exp)
result = f(test_values)
expected = [math.tanh(v) for v in test_values]
print(result)
print(expected)
list_assert_equal(result, expected)
def reference_value(self, x):
return np.vectorize(true_tanh)(x)
outfall_rockdesign.py 文件源码
项目:Outfall-Rock-Protection-Design-Tool
作者: FranciscoChaves90
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def __init__(self,tp,d):
self.L0 = g*pow(tp,2)/(2*pi)
while True:
self.L = self.L0 #inititate self.L
self.LIteration = 0 #inititate self.LIteration
while abs(self.L-self.LIteration)>0.0001:
self.LIteration = self.L
self.L = self.L0 * math.tanh(2*math.pi*d/self.LIteration)
break
def test_trig_hyperb_basic():
for x in (list(range(100)) + list(range(-100,0))):
t = x / 4.1
assert cos(mpf(t)).ae(math.cos(t))
assert sin(mpf(t)).ae(math.sin(t))
assert tan(mpf(t)).ae(math.tan(t))
assert cosh(mpf(t)).ae(math.cosh(t))
assert sinh(mpf(t)).ae(math.sinh(t))
assert tanh(mpf(t)).ae(math.tanh(t))
assert sin(1+1j).ae(cmath.sin(1+1j))
assert sin(-4-3.6j).ae(cmath.sin(-4-3.6j))
assert cos(1+1j).ae(cmath.cos(1+1j))
assert cos(-4-3.6j).ae(cmath.cos(-4-3.6j))
def test_invhyperb_inaccuracy():
mp.dps = 15
assert (asinh(1e-5)*10**5).ae(0.99999999998333333)
assert (asinh(1e-10)*10**10).ae(1)
assert (asinh(1e-50)*10**50).ae(1)
assert (asinh(-1e-5)*10**5).ae(-0.99999999998333333)
assert (asinh(-1e-10)*10**10).ae(-1)
assert (asinh(-1e-50)*10**50).ae(-1)
assert asinh(10**20).ae(46.744849040440862)
assert asinh(-10**20).ae(-46.744849040440862)
assert (tanh(1e-10)*10**10).ae(1)
assert (tanh(-1e-10)*10**10).ae(-1)
assert (atanh(1e-10)*10**10).ae(1)
assert (atanh(-1e-10)*10**10).ae(-1)
def test_complex_functions():
for x in (list(range(10)) + list(range(-10,0))):
for y in (list(range(10)) + list(range(-10,0))):
z = complex(x, y)/4.3 + 0.01j
assert exp(mpc(z)).ae(cmath.exp(z))
assert log(mpc(z)).ae(cmath.log(z))
assert cos(mpc(z)).ae(cmath.cos(z))
assert sin(mpc(z)).ae(cmath.sin(z))
assert tan(mpc(z)).ae(cmath.tan(z))
assert sinh(mpc(z)).ae(cmath.sinh(z))
assert cosh(mpc(z)).ae(cmath.cosh(z))
assert tanh(mpc(z)).ae(cmath.tanh(z))
def test_tanh():
mp.dps = 15
assert tanh(0) == 0
assert tanh(inf) == 1
assert tanh(-inf) == -1
assert isnan(tanh(nan))
assert tanh(mpc('inf', '0')) == 1
def testTanhSign(self):
# check that tanh(-0.) == -0. on IEEE 754 systems
self.assertEqual(math.tanh(-0.), -0.)
self.assertEqual(math.copysign(1., math.tanh(-0.)),
math.copysign(1., -0.))
def test_trig_hyperb_basic():
for x in (list(range(100)) + list(range(-100,0))):
t = x / 4.1
assert cos(mpf(t)).ae(math.cos(t))
assert sin(mpf(t)).ae(math.sin(t))
assert tan(mpf(t)).ae(math.tan(t))
assert cosh(mpf(t)).ae(math.cosh(t))
assert sinh(mpf(t)).ae(math.sinh(t))
assert tanh(mpf(t)).ae(math.tanh(t))
assert sin(1+1j).ae(cmath.sin(1+1j))
assert sin(-4-3.6j).ae(cmath.sin(-4-3.6j))
assert cos(1+1j).ae(cmath.cos(1+1j))
assert cos(-4-3.6j).ae(cmath.cos(-4-3.6j))
def test_invhyperb_inaccuracy():
mp.dps = 15
assert (asinh(1e-5)*10**5).ae(0.99999999998333333)
assert (asinh(1e-10)*10**10).ae(1)
assert (asinh(1e-50)*10**50).ae(1)
assert (asinh(-1e-5)*10**5).ae(-0.99999999998333333)
assert (asinh(-1e-10)*10**10).ae(-1)
assert (asinh(-1e-50)*10**50).ae(-1)
assert asinh(10**20).ae(46.744849040440862)
assert asinh(-10**20).ae(-46.744849040440862)
assert (tanh(1e-10)*10**10).ae(1)
assert (tanh(-1e-10)*10**10).ae(-1)
assert (atanh(1e-10)*10**10).ae(1)
assert (atanh(-1e-10)*10**10).ae(-1)
def test_complex_functions():
for x in (list(range(10)) + list(range(-10,0))):
for y in (list(range(10)) + list(range(-10,0))):
z = complex(x, y)/4.3 + 0.01j
assert exp(mpc(z)).ae(cmath.exp(z))
assert log(mpc(z)).ae(cmath.log(z))
assert cos(mpc(z)).ae(cmath.cos(z))
assert sin(mpc(z)).ae(cmath.sin(z))
assert tan(mpc(z)).ae(cmath.tan(z))
assert sinh(mpc(z)).ae(cmath.sinh(z))
assert cosh(mpc(z)).ae(cmath.cosh(z))
assert tanh(mpc(z)).ae(cmath.tanh(z))
def test_tanh():
mp.dps = 15
assert tanh(0) == 0
assert tanh(inf) == 1
assert tanh(-inf) == -1
assert isnan(tanh(nan))
assert tanh(mpc('inf', '0')) == 1
def test_trig_hyperb_basic():
for x in (list(range(100)) + list(range(-100,0))):
t = x / 4.1
assert cos(mpf(t)).ae(math.cos(t))
assert sin(mpf(t)).ae(math.sin(t))
assert tan(mpf(t)).ae(math.tan(t))
assert cosh(mpf(t)).ae(math.cosh(t))
assert sinh(mpf(t)).ae(math.sinh(t))
assert tanh(mpf(t)).ae(math.tanh(t))
assert sin(1+1j).ae(cmath.sin(1+1j))
assert sin(-4-3.6j).ae(cmath.sin(-4-3.6j))
assert cos(1+1j).ae(cmath.cos(1+1j))
assert cos(-4-3.6j).ae(cmath.cos(-4-3.6j))
def test_invhyperb_inaccuracy():
mp.dps = 15
assert (asinh(1e-5)*10**5).ae(0.99999999998333333)
assert (asinh(1e-10)*10**10).ae(1)
assert (asinh(1e-50)*10**50).ae(1)
assert (asinh(-1e-5)*10**5).ae(-0.99999999998333333)
assert (asinh(-1e-10)*10**10).ae(-1)
assert (asinh(-1e-50)*10**50).ae(-1)
assert asinh(10**20).ae(46.744849040440862)
assert asinh(-10**20).ae(-46.744849040440862)
assert (tanh(1e-10)*10**10).ae(1)
assert (tanh(-1e-10)*10**10).ae(-1)
assert (atanh(1e-10)*10**10).ae(1)
assert (atanh(-1e-10)*10**10).ae(-1)
def test_complex_functions():
for x in (list(range(10)) + list(range(-10,0))):
for y in (list(range(10)) + list(range(-10,0))):
z = complex(x, y)/4.3 + 0.01j
assert exp(mpc(z)).ae(cmath.exp(z))
assert log(mpc(z)).ae(cmath.log(z))
assert cos(mpc(z)).ae(cmath.cos(z))
assert sin(mpc(z)).ae(cmath.sin(z))
assert tan(mpc(z)).ae(cmath.tan(z))
assert sinh(mpc(z)).ae(cmath.sinh(z))
assert cosh(mpc(z)).ae(cmath.cosh(z))
assert tanh(mpc(z)).ae(cmath.tanh(z))
def test_tanh():
mp.dps = 15
assert tanh(0) == 0
assert tanh(inf) == 1
assert tanh(-inf) == -1
assert isnan(tanh(nan))
assert tanh(mpc('inf', '0')) == 1
def makeMatrix(I, J, fill=0.0):
m = []
for i in range(I):
m.append([fill]*J)
return m
# our sigmoid function, tanh is a little nicer than the standard 1/(1+e^-x)