def sigmoid(x):
return math.tanh(x)
# derivative of our sigmoid function
python类tanh()的实例源码
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))
# check that tanh(-0.) == -0. on IEEE 754 systems
if float.__getformat__("double").startswith("IEEE"):
self.assertEqual(math.tanh(-0.), -0.)
self.assertEqual(math.copysign(1., math.tanh(-0.)),
math.copysign(1., -0.))
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))
# check that tanh(-0.) == -0. on IEEE 754 systems
if float.__getformat__("double").startswith("IEEE"):
self.assertEqual(math.tanh(-0.), -0.)
self.assertEqual(math.copysign(1., math.tanh(-0.)),
math.copysign(1., -0.))
def __init__(self, opt={}):
self.out_sx = opt['in_sx']
self.out_sy = opt['in_sy']
self.out_depth = opt['in_depth']
self.layer_type = 'tanh'
def forward(self, V, is_training):
self.in_act = V
V2 = V.cloneAndZero()
V2.w = map(tanh, V.w)
self.out_act = V2
return self.out_act
def dsigmoid(y):
return y * (1.0 - y)
#using tanh instead of logistic sigmoid
def tanh(x):
return math.tanh(x)
#derivative of tanh
def feedForward(self, inputs):
'''
Feed Forward Propagation
'''
if len(inputs) != self.input - 1:
raise ValueError("Wrong Number of Inputs!")
#input activations
for i in range(self.input - 1): #-1 because of bias term
self.ai[i] = inputs[i]
#hidden layer activations
for i in range(self.hidden):
sum_ = 0.0
for j in range(self.input):
sum_ += self.ai[j] * self.wi[j][i]
self.ah[i] = tanh(sum_) #assigning the activation
#output activations
for k in range(self.output):
sum_ = 0.0
for i in range(self.hidden):
sum_ += self.ah[i] * self.wi[i][k]
self.ao[k] = sigmoid(sum_) #assigning the activation
return self.ao[:]
def tanh(a):
"""Elementwise hyperbolic tangent."""
if not isSparseVector(a):
raise TypeError("Argument must be a SparseVector")
rv = SparseVector(a.n, {})
for k in a.values.keys():
rv.values[k] = math.tanh(a.values[k])
return rv
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))
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 sigmod(x):
'''
the sigmod function,tanh is a little better than the standard 1/(1+e^-x)
:param x: the self variable
:return: the value after tanh
'''
return math.tanh(x)
def activation(x):
#return expit(x)
##return 1.7159 * math.tanh(2/3*x)
#print(x)
return np.tanh(x)#list(map(math.tanh, x))
#return np.multiply(x > 0, x)
def dactivation(x):
#v = expit(x)
#return v*(1-v)
#return 1 - math.tanh(x)**2
return 1 - np.tanh(x)**2#list(map(lambda y: 1 - math.tanh(y)**2, x))
#return np.float64(x > 0)
def feedforward(self, words, urls):
for j in range(len(self.hidden_nodes)):
sum = 0.0
for i in range(len(self.words)):
sum += self.li[i]*self.w_ih[i][j]
self.lh[j] = tanh(sum)
for j in range(len(self.urls)):
sum = 0.0
for i in range(len(self.hidden_nodes)):
sum += self.lh[i]*self.w_ho[i][j]
self.lo[j] = tanh(sum)
return self.lo
def tanh(node): return merge([node], math.tanh)
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))
# check that tanh(-0.) == -0. on IEEE 754 systems
if float.__getformat__("double").startswith("IEEE"):
self.assertEqual(math.tanh(-0.), -0.)
self.assertEqual(math.copysign(1., math.tanh(-0.)),
math.copysign(1., -0.))
def tan(x):
z = _make_complex(x)
z = tanh(complex(-z.imag, z.real))
return complex(z.imag, -z.real)
def tanh(x):
_tanh_special = [
[-1, None, complex(-1, -0.0), -1, None, -1, -1],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[nan+nanj, None, complex(-0.0, -0.0), complex(-0.0, 0.0), None, nan+nanj, nan+nanj],
[nan+nanj, None, complex(0.0, -0.0), 0.0, None, nan+nanj, nan+nanj],
[nan+nanj, None, None, None, None, nan+nanj, nan+nanj],
[1, None, complex(1, -0.0), 1, None, 1, 1],
[nan+nanj, nan+nanj, complex(float("nan"), -0.0), nan, nan+nanj, nan+nanj, nan+nanj]
]
z = _make_complex(x)
if not isfinite(z):
if math.isinf(z.imag) and math.isfinite(z.real):
raise ValueError
if math.isinf(z.real) and math.isfinite(z.imag) and z.imag != 0:
if z.real > 0:
return complex(1, math.copysign(0.0, math.sin(z.imag)
* math.cos(z.imag)))
return complex(-1, math.copysign(0.0, math.sin(z.imag)
* math.cos(z.imag)))
return _tanh_special[_special_type(z.real)][_special_type(z.imag)]
if abs(z.real) > _LOG_LARGE_DOUBLE:
return complex(
math.copysign(1, z.real),
4*math.sin(z.imag)*math.cos(z.imag)*math.exp(-2*abs(z.real))
)
tanh_x = math.tanh(z.real)
tan_y = math.tan(z.imag)
cx = 1/math.cosh(z.real)
denom = 1 + tanh_x * tanh_x * tan_y * tan_y
return complex(tanh_x * (1 + tan_y*tan_y)/denom,
((tan_y / denom) * cx) * cx)
def testTanh(self):
self.assertRaises(TypeError, math.tanh)
self.ftest('tanh(0)', math.tanh(0), 0)
self.ftest('tanh(1)+tanh(-1)', math.tanh(1)+math.tanh(-1), 0)
self.ftest('tanh(inf)', math.tanh(INF), 1)
self.ftest('tanh(-inf)', math.tanh(NINF), -1)
self.assertTrue(math.isnan(math.tanh(NAN)))