Here are the examples of the python api numpy.tanh taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
93 Examples
4
Example 1
def direct_tilbert(x,h=1,period=None):
fx = fft(x)
n = len(fx)
if period is None:
period = 2*pi
w = fftfreq(n)*h*2*pi/period*n
w[0] = 1
w = 1j/tanh(w)
w[0] = 0j
return ifft(w*fx)
3
Example 2
Project: conceptors Source File: net.py
def offline_recall(self,
C,
x):
"""
Activation offline recall using conceptor C and an activation state x
@param C: conceptor matrix
@param x: activation state
"""
r=np.tanh(self.W.dot(x)+self.D.dot(x)+self.bias);
return C.dot(r), r;
3
Example 3
def __call__(self, step, history, na=numpy.newaxis):
g_ij = history.es_weights
x_i, x_j = history.query(step)
if self.dynamic:
_ = (self.P * x_j[:,0] - x_j[:,1,self.sliceT])[:,na]
else:
_ = self.P * x_j - self.theta[self.sliceT,na]
A_j = self.H * (self.Q + numpy.tanh(self.G * _))
if self.dynamic:
c_0 = (g_ij[:,0] * A_j[:,0]).sum(axis=0)
c_1 = numpy.diag(A_j[:,0,:,0])[:, na]
if self.globalT:
c_1[:] = c_1.mean()
return numpy.array([c_0, c_1])
else: # static threshold
return (g_ij.transpose((2, 1, 0, 3)) * A_j).sum(axis=0)
3
Example 4
Project: kameleon-mcmc Source File: HypercubeKernelUnitTest.py
def test_kernel_X_Y_one_point_different(self):
gamma = .2
k = HypercubeKernel(gamma)
X = asarray([[1]], dtype=numpy.bool8)
Y = asarray([[0]], dtype=numpy.bool8)
K = k.kernel(X, Y)
self.assertEqual(K[0, 0], tanh(gamma))
3
Example 5
Project: pyomo Source File: FuncDesignerExt.py
def tanh(inp):
if isinstance(inp, ooarray) and inp.dtype == object:
return ooarray([tanh(elem) for elem in inp])
if not isinstance(inp, oofun):
return np.tanh(inp)
# TODO: move it outside of tanh definition
def interval(arg_inf, arg_sup):
raise 'interval for tanh is unimplemented yet'
r = oofun(np.tanh, inp, d = lambda x: FDmisc.Diag(1.0/np.cosh(x) ** 2), vectorized = True, interval = interval)
return r
3
Example 6
def fprop(self, inputs):
"""Forward propagate input through this module."""
outputs = super(AveragePoolingFeatureMap, self).fprop(inputs)
outputs *= self.weights[0]
outputs += self.biases[0]
outputs *= self.inner
np.tanh(outputs, outputs)
outputs *= self.outer
return outputs
3
Example 7
def fprop(self, inputs):
"""Forward propagate input through this module."""
out = self._out_array
if out is not inputs:
out[...] = inputs
if self.bias is not None:
out += self.bias
out *= self.inner
np.tanh(out, out)
out *= self.outer
return out
3
Example 8
Project: CommPy Source File: ldpc.py
def sum_product_update(cnode_idx, cnode_adj_list, cnode_deg_list, cnode_msgs,
vnode_msgs, cnode_vnode_map, max_cnode_deg, max_vnode_deg):
start_idx = cnode_idx*max_cnode_deg
offset = cnode_deg_list[cnode_idx]
vnode_list = cnode_adj_list[start_idx:start_idx+offset]
vnode_list_msgs_tanh = np.tanh(vnode_msgs[vnode_list*max_vnode_deg +
cnode_vnode_map[start_idx:start_idx+offset]]/2.0)
msg_prod = np.prod(vnode_list_msgs_tanh)
# Compute messages on outgoing edges using the incoming message product
cnode_msgs[start_idx:start_idx+offset]= 2.0*np.arctanh(msg_prod/vnode_list_msgs_tanh)
3
Example 9
Project: python-neural-network Source File: activation_functions.py
def tanh_function( signal, derivative=False ):
# Calculate activation signal
signal = np.tanh( signal )
if derivative:
# Return the partial derivation of the activation function
return 1-np.power(signal,2)
else:
# Return the activation signal
return signal
3
Example 10
Project: numdifftools Source File: example_functions.py
def derivative_tanh(n):
def dtanh(x):
return 1. / cosh(x) ** 2
def ddtanh(x):
return -2 * sinh(x) / cosh(x) ** 3
def dddtanh(x):
y = cosh(x)
return 4 * (tanh(x) / y)**2 - 2. / y**4
def ddddtanh(x):
y = tanh(x)
return 8 * y * (y**2 - 1) * (3 * y**2 - 2)
return (tanh, dtanh, ddtanh, dddtanh, ddddtanh, None)[min(n, 5)]
3
Example 11
def fun(self, x, *args):
self.nfev += 1
t = 0.1 * arange(16)
y = (53.81 * 1.27 ** t * tanh(3.012 * t + sin(2.13 * t))
* cos(exp(0.507) * t))
return sum((x[0] * (x[1] ** t) * tanh(x[2] * t + sin(x[3] * t))
* cos(t * exp(x[4])) - y) ** 2.0)
3
Example 12
def f(self, y):
"""
Transform y with f using parameter vector psi
psi = [[a,b,c]]
:math:`f = (y * d) + \\sum_{terms} a * tanh(b *(y + c))`
"""
d = self.d
mpsi = self.psi
z = d * y.copy()
for i in range(len(mpsi)):
a, b, c = mpsi[i]
z += a * np.tanh(b * (y + c))
return z
3
Example 13
def tanh(X):
"""Compute the hyperbolic tan function inplace.
Parameters
----------
X : {array-like, sparse matrix}, shape (n_samples, n_features)
The input data.
Returns
-------
X_new : {array-like, sparse matrix}, shape (n_samples, n_features)
The transformed data.
"""
return np.tanh(X, out=X)
3
Example 14
Project: qutip Source File: pulsegen.py
def _apply_bounds(self, pulse):
"""
Scaling the amplitudes using the tanh function if there are bounds
"""
if self._bound_scale_cond == self._BSC_ALL:
pulse = np.tanh(pulse)*self._bound_scale + self._bound_mean
return pulse
elif self._bound_scale_cond == self._BSC_GT_MEAN:
scale_where = pulse > self._bound_mean
pulse[scale_where] = (np.tanh(pulse[scale_where])*self._bound_scale
+ self._bound_mean)
return pulse
elif self._bound_scale_cond == self._BSC_LT_MEAN:
scale_where = pulse < self._bound_mean
pulse[scale_where] = (np.tanh(pulse[scale_where])*self._bound_scale
+ self._bound_mean)
return pulse
else:
return pulse
3
Example 15
def __call__(self, t):
phi = np.tanh(self.st * (t - self.tR))
d0 = self.aR * (1 + phi) + self.lo
d1 = self.aR * self.st * (1 - phi**2)
d2 = 2 * self.aR * self.st**2 * phi * (1 - phi**2)
d3 = -2 * self.aR * self.st**3 * (1 - phi**2) * (1 - 3 * phi**2)
d4 = 8 * self.aR * self.st**4 * phi * (3 * phi**4 - 5 * phi**2 + 2)
d5 = -8 * self.aR * self.st**5 * (15 * phi**6 - 30 * phi**4 + 17 * phi**2 - 2)
r = np.array([ [d0, d1, d2, d3, d4],
[d0, d1, d2, d3, d4],
[d0, d1, d2, d3, d4],
[d0, d1, d2, d3, d4] ])
return r
3
Example 16
def fprop(self, inputs):
"""Forward propagate input through this module."""
super(ConvolutionalFeatureMap, self).fprop(inputs)
out = self._out_array[self.active_slice]
out *= self.inner
np.tanh(out, out)
out *= self.outer
return out
3
Example 17
def value(self, x):
try:
returnValue = numpy.tanh(x)
return ConvertInfAndNanToLargeNumber(returnValue)
except:
return 1.0E300 * numpy.ones_like(x)
3
Example 18
@Operation.factory
def Tanh(a):
"""
Tanh op.
"""
return np.tanh(a),
3
Example 19
Project: pyflux Source File: family.py
@staticmethod
def transform_define(transform):
"""
This function links the user's choice of transformation with the associated numpy function
"""
if transform == 'tanh':
return np.tanh
elif transform == 'exp':
return np.exp
elif transform == 'logit':
return ilogit
elif transform is None:
return np.array
else:
return None
3
Example 20
Project: Kayak Source File: test_TanH.py
def test_tanh_values():
npr.seed(1)
for ii in xrange(NUM_TRIALS):
np_X = npr.randn(6,5)
X = kayak.Parameter(np_X)
Y = kayak.TanH(X)
assert np.all(close_float(np.tanh(np_X), Y.value))
3
Example 21
def tanh(mat, target = None):
"""
Apply the logistic sigmoid to each element of the matrix mat.
"""
if not target:
target = mat
target.resize(mat.shape)
target.numpy_array[:] = np.tanh(mat.numpy_array)
return target
3
Example 22
Project: scipy Source File: fftpack_pseudo_diffs.py
def direct_tilbert(x, h=1, period=None):
fx = fft(x)
n = len(fx)
if period is None:
period = 2*pi
w = fftfreq(n)*h*2*pi/period*n
w[0] = 1
w = 1j/tanh(w)
w[0] = 0j
return ifft(w*fx)
3
Example 23
Project: Azimuth Source File: corrstats.py
def rz_ci(r, n, conf_level = 0.95):
zr_se = pow(1/(n - 3), .5)
moe = norm.ppf(1 - (1 - conf_level)/float(2)) * zr_se
zu = atanh(r) + moe
zl = atanh(r) - moe
return tanh((zl, zu))
3
Example 24
Project: scikit-learn Source File: fastica_.py
def _logcosh(x, fun_args=None):
alpha = fun_args.get('alpha', 1.0) # comment it out?
x *= alpha
gx = np.tanh(x, x) # apply the tanh inplace
g_x = np.empty(x.shape[0])
# XXX compute in chunks to avoid extra allocation
for i, gx_i in enumerate(gx): # please don't vectorize.
g_x[i] = (alpha * (1 - gx_i ** 2)).mean()
return gx, g_x
3
Example 25
Project: scipy Source File: test_pseudo_diffs.py
def direct_itilbert(x,h=1,period=None):
fx = fft(x)
n = len(fx)
if period is None:
period = 2*pi
w = fftfreq(n)*h*2*pi/period*n
w = -1j*tanh(w)
return ifft(w*fx)
2
Example 26
def dfun(self, state_variables, coupling, local_coupling=0.0):
"""
.. math::
\\dot{V} &= - g_L \\, (V - V_L) - g_K\\, Z \\, (V - V_K) -
g_{Ca} \\, m_{\\infty} \\, (V - 1) + I - \\alpha_{inh}\\,Z \\\\
\\dot{W} &= \\frac{\\phi \\, (w_{\\infty} - W)}{\\tau_w} \\\\
\\dot{Z}(t) &= b ( c \\, I_{ext} + \\alpha_{exc} \\,V ) \\\\
m_{\\infty} &= 0.5 \\, \\left(1 + \\tanh\\left(\\frac{V - V_1}{V_2}\\right)\\right) \\\\
w_{\\infty} &= 0.5 \\, \\left(1 + \\tanh\\left(\\frac{V - V_3}{V_4}\\right)\\right)\\\\
tau_{w} &= \\left[ \\cosh\\left(\\frac{V - V_3}{2 \\,V_4}\\right) \\right]^{-1} \\\\
\\alpha_{exc} &= a_{exc} \\,\\left(1 + \\tanh\\left(\\frac{V - V_5}{V_6}\\right)\\right)\\\\
\\alpha_{inh} &= a_{inh} \\,\\left(1 + \\tanh\\left(\\frac{V - V_7}{V_6}\\right)\\right)
See Eqs (1)-(8) in [Larteretal_1999]_
"""
##--------------------- As in Larter 1999 ----------------------------##
V = state_variables[0, :]
W = state_variables[1, :]
Z = state_variables[2, :]
c_0 = coupling[0, :]
M_inf = 0.5 * (1 + numpy.tanh((V - self.V1) / self.V2))
W_inf = 0.5 * (1 + numpy.tanh((V - self.V3) / self.V4))
tau_Winv = numpy.cosh((V - self.V3) / (2 * self.V4))
alpha_exc = self.a_exc * (1 + numpy.tanh((V - self.V5) / self.V6))
alpha_inh = self.a_inh * (1 + numpy.tanh((V - self.V7) / self.V6))
#import pdb; pdb.set_trace()
dV = (local_coupling * V - alpha_inh * Z -
self.gL * (V - self.VL) -
self.gCa * M_inf * (V - 1) -
self.gK * W * (V - self.VK + c_0) + self.Iext)
dW = self.phi * tau_Winv * (W_inf - W)
dZ = self.b * ((self.c * self.Iext) + (alpha_exc * V))
derivative = numpy.array([dV, dW, dZ])
return derivative
2
Example 27
def dfun(self, state_variables, coupling, local_coupling=0.0):
"""
.. math::
\\dot{V} &= - (g_{Ca} + (1 - C) \\, r_{NMDA} \\, a_{ee} Q_V^i +
C \\, r_{NMDA} \\, a_{ee} \\langle Q_V \\rangle) \\, m_{Ca} \\,(V - V_{Ca})
- g_K\\, W\\, (V - V_K) - g_L\\, (V - V_L)
- (g_{Na} m_{Na} + (1 - C) \\, a_{ee} Q_V^i +
C \\, a_{ee} \\langle Q_V \\rangle) \\, (V - V_{Na})
- a_{ie}\\, Z \\, Q_Z^i + a_{ne} \\, I_{\\delta}
\\dot{W} &= \\frac{\\phi \\, (m_K - W)}{\\tau_K} \\\\
\\dot{Z} &= b \\, (a_{ni} \\, I_{\\delta} + a_{ei} \\, V \\, Q_V)\\\\
m_{ion}(X) &= 0.5 \\, (1 + tanh(\\frac{V-T_{ion}}{\\delta_{ion}})
See Equations (7), (3), (6) and (2) respectively in [Breaksetal_2003]_.
Pag: 705-706
NOTE: Equation (8) has an error the sign before the term :math:`a_{ie}\\, Z \\, Q_Z^i`
should be a minus (-) and not a plus (+).
"""
V = state_variables[0, :]
W = state_variables[1, :]
Z = state_variables[2, :]
c_0 = coupling[0, :]
lc_0 = local_coupling
# relationship between membrane voltage and channel conductance
m_Ca = 0.5 * (1 + numpy.tanh((V - self.TCa) / self.d_Ca))
m_Na = 0.5 * (1 + numpy.tanh((V - self.TNa) / self.d_Na))
m_K = 0.5 * (1 + numpy.tanh((V - self.TK ) / self.d_K))
# voltage to firing rate
QV = 0.5 * self.QV_max * (1 + numpy.tanh((V - self.VT) / self.d_V))
QZ = 0.5 * self.QZ_max * (1 + numpy.tanh((Z - self.ZT) / self.d_Z))
dV = (- (self.gCa + (1.0 - self.C) * self.rNMDA * self.aee * QV + self.C * self.rNMDA * self.aee * c_0) * m_Ca * (V - self.VCa) - self.gK * W * (V - self.VK) - self.gL * (V - self.VL) - (self.gNa * m_Na + (1.0 - self.C) * self.aee * QV + self.C * self.aee * c_0) * (V - self.VNa) - self.aei * Z * QZ + self.ane * self.Iext)
dW = (self.phi * (m_K - W) / self.tau_K)
dZ = (self.b * (self.ani * self.Iext + self.aei * V * QV))
derivative = numpy.array([dV, dW, dZ])
return derivative
2
Example 28
Project: chainer Source File: test_slstm.py
def check_forward(self, c_prev1_data, c_prev2_data, x1_data, x2_data):
c_prev1 = chainer.Variable(c_prev1_data)
c_prev2 = chainer.Variable(c_prev2_data)
x1 = chainer.Variable(x1_data)
x2 = chainer.Variable(x2_data)
c, h = functions.slstm(c_prev1, c_prev2, x1, x2)
self.assertEqual(c.data.dtype, self.dtype)
self.assertEqual(h.data.dtype, self.dtype)
# Compute expected out
a1_in = self.x1[:, [0, 4]]
i1_in = self.x1[:, [1, 5]]
f1_in = self.x1[:, [2, 6]]
o1_in = self.x1[:, [3, 7]]
a2_in = self.x2[:, [0, 4]]
i2_in = self.x2[:, [1, 5]]
f2_in = self.x2[:, [2, 6]]
o2_in = self.x2[:, [3, 7]]
c_expect = _sigmoid(i1_in) * numpy.tanh(a1_in) + \
_sigmoid(i2_in) * numpy.tanh(a2_in) + \
_sigmoid(f1_in) * self.c_prev1 + \
_sigmoid(f2_in) * self.c_prev2
h_expect = _sigmoid(o1_in + o2_in) * numpy.tanh(c_expect)
testing.assert_allclose(
c_expect, c.data, **self.check_forward_options)
testing.assert_allclose(
h_expect, h.data, **self.check_forward_options)
0
Example 29
def _pmf(self, k, a):
return tanh(a/2.0) * exp(-a * abs(k))
0
Example 30
def sigmoid(x):
return numpy.tanh(x * 0.5) * 0.5 + 0.5
0
Example 31
def fgrad_y(self, y, psi, return_precalc = False):
"""
gradient of f w.r.t to y ([N x 1])
:returns: Nx1 vector of derivatives, unless return_precalc is true, then it also returns the precomputed stuff
"""
mpsi = psi.copy()
d = psi[-1]
mpsi = mpsi[:self.num_parameters-1].reshape(self.n_terms, 3)
# vectorized version
S = (mpsi[:,1]*(y[:,:,None] + mpsi[:,2])).T
R = np.tanh(S)
D = 1-R**2
GRAD = (d + (mpsi[:,0:1][:,:,None]*mpsi[:,1:2][:,:,None]*D).sum(axis=0)).T
if return_precalc:
return GRAD, S, R, D
return GRAD
0
Example 32
Project: scipy Source File: _discrete_distns.py
def _entropy(self, a):
return a / sinh(a) - log(tanh(a/2.0))
0
Example 33
Project: chainer Source File: slstm.py
def backward(self, inputs, grad_outputs):
xp = cuda.get_array_module(*inputs)
c_prev1, c_prev2, x1, x2 = inputs
gc, gh = grad_outputs
gx1 = xp.empty_like(x1)
gx2 = xp.empty_like(x2)
ga1, gi1, gf1, go1 = _extract_gates(gx1)
ga2, gi2, gf2, go2 = _extract_gates(gx2)
# Consider the case that either gradient is not given
if gc is None:
gc = 0
if gh is None:
gh = 0
if xp is numpy:
co = numpy.tanh(self.c)
# multiply f later
gc_prev = gh * self.o * _grad_tanh(co) + gc
ga1[:] = gc_prev * self.i1 * _grad_tanh(self.a1)
gi1[:] = gc_prev * self.a1 * _grad_sigmoid(self.i1)
gf1[:] = gc_prev * c_prev1 * _grad_sigmoid(self.f1)
go1[:] = gh * co * _grad_sigmoid(self.o)
ga2[:] = gc_prev * self.i2 * _grad_tanh(self.a2)
gi2[:] = gc_prev * self.a2 * _grad_sigmoid(self.i2)
gf2[:] = gc_prev * c_prev2 * _grad_sigmoid(self.f2)
go2[:] = gh * co * _grad_sigmoid(self.o)
# multiply f here
gc_prev1 = gc_prev * self.f1
gc_prev2 = gc_prev * self.f2
else:
a1, i1, f1, o1 = _extract_gates(x1)
a2, i2, f2, o2 = _extract_gates(x2)
gc_prev1 = xp.empty_like(c_prev1)
gc_prev2 = xp.empty_like(c_prev2)
cuda.elementwise(
'''T c_prev1, T a1, T i1, T f1, T o1,
T c_prev2, T a2, T i2, T f2, T o2,
T c, T gc, T gh''',
'''T gc_prev1, T ga1, T gi1, T gf1, T go1,
T gc_prev2, T ga2, T gi2, T gf2, T go2''',
'''
COMMON_ROUTINE;
T co = tanh(c);
T temp = gh * ao * grad_tanh(co) + gc;
ga1 = temp * ai1 * grad_tanh(aa1);
gi1 = temp * aa1 * grad_sigmoid(ai1);
gf1 = temp * c_prev1 * grad_sigmoid(af1);
go1 = gh * co * grad_sigmoid(ao);
gc_prev1 = temp * af1;
ga2 = temp * ai2 * grad_tanh(aa2);
gi2 = temp * aa2 * grad_sigmoid(ai2);
gf2 = temp * c_prev2 * grad_sigmoid(af2);
go2 = gh * co * grad_sigmoid(ao);
gc_prev2 = temp * af2;
''',
'lstm_bwd', preamble=_preamble)(
c_prev1, a1, i1, f1, o1,
c_prev2, a2, i2, f2, o2,
self.c, gc, gh,
gc_prev1, ga1, gi1, gf1, go1,
gc_prev2, ga2, gi2, gf2, go2)
return gc_prev1, gc_prev2, gx1, gx2
0
Example 34
Project: chainer Source File: test_n_step_lstm.py
def check_forward(
self, h_data, c_data, xs_data, ws_data, bs_data, volatile):
h = chainer.Variable(h_data, volatile=volatile)
c = chainer.Variable(c_data, volatile=volatile)
xs = [chainer.Variable(x, volatile=volatile) for x in xs_data]
ws = [[chainer.Variable(w, volatile=volatile) for w in ws]
for ws in ws_data]
bs = [[chainer.Variable(b, volatile=volatile) for b in bs]
for bs in bs_data]
hy, cy, ys = functions.n_step_lstm(
self.n_layers, self.dropout, h, c, ws, bs, xs,
use_cudnn=self.use_cudnn)
e_hy = self.hx.copy()
e_cy = self.cx.copy()
for ind in range(self.length):
x = self.xs[ind]
batch = x.shape[0]
for layer in range(self.n_layers):
w = self.ws[layer]
b = self.bs[layer]
h_prev = e_hy[layer, :batch]
c_prev = e_cy[layer, :batch]
i = sigmoid(x.dot(w[0].T) + h_prev.dot(w[4].T) + b[0] + b[4])
f = sigmoid(x.dot(w[1].T) + h_prev.dot(w[5].T) + b[1] + b[5])
c_bar = numpy.tanh(
x.dot(w[2].T) + h_prev.dot(w[6].T) + b[2] + b[6])
o = sigmoid(x.dot(w[3].T) + h_prev.dot(w[7].T) + b[3] + b[7])
e_c = (f * c_prev + i * c_bar)
e_h = o * numpy.tanh(e_c)
e_hy[layer, :batch] = e_h
e_cy[layer, :batch] = e_c
x = e_h
testing.assert_allclose(
ys[ind].data, x, rtol=1e-4, atol=1e-4)
testing.assert_allclose(hy.data, e_hy, rtol=1e-4, atol=1e-4)
testing.assert_allclose(cy.data, e_cy, rtol=1e-4, atol=1e-4)
0
Example 35
Project: qutip Source File: test_heom_solver.py
def test_pure_dephasing(self):
"""
HSolverDL: Compare with pure-dephasing analytical
assert that the analytical result and HEOM produce the
same time dephasing evoltion.
"""
resid_tol = 1e-4
def spectral_density(omega, lam_c, omega_c):
return 2.0*lam_c*omega*omega_c / (omega_c**2 + omega**2)
def integrand(omega, lam_c, omega_c, Temp, t):
J = spectral_density(omega, lam_c, omega_c)
return (-4.0*J*(1.0 - cos(omega*t)) /
(tanh(omega/(2.0*Temp))*omega**2))
cut_freq = 0.05
coup_strength = 0.025
temperature = 1.0/0.95
tlist = np.linspace(0, 10, 21)
# Calculate the analytical results by numerical integration
lam_c = coup_strength/pi
PEG_DL = [0.5*np.exp(quad(integrand, 0, np.inf,
args=(lam_c, cut_freq, temperature, t))[0])
for t in tlist]
H_sys = Qobj(np.zeros((2, 2)))
Q = sigmaz()
initial_state = 0.5*Qobj(np.ones((2, 2)))
P12p = basis(2,0)*basis(2,1).dag()
integ_options = Options(nsteps=15000, store_states=True)
test_desc = "renorm, bnd_cut_approx, and stats"
hsolver = HSolverDL(H_sys, Q, coup_strength, temperature,
20, 2, cut_freq,
renorm=True, bnd_cut_approx=True,
options=integ_options, stats=True)
result = hsolver.run(initial_state, tlist)
P12_result1 = expect(result.states, P12p)
resid = abs(real(P12_result1 - PEG_DL))
max_resid = max(resid)
assert_(max_resid < resid_tol, "Max residual {} outside tolerence {}, "
"for hsolve with {}".format(max_resid, resid_tol, test_desc))
resid_tol = 1e-3
test_desc = "renorm"
hsolver.configure(H_sys, Q, coup_strength, temperature,
20, 2, cut_freq,
renorm=True, bnd_cut_approx=False,
options=integ_options, stats=False)
assert_(hsolver.stats == None, "Failed to unset stats")
result = hsolver.run(initial_state, tlist)
P12_result1 = expect(result.states, P12p)
resid = abs(real(P12_result1 - PEG_DL))
max_resid = max(resid)
assert_(max_resid < resid_tol, "Max residual {} outside tolerence {}, "
"for hsolve with {}".format(max_resid, resid_tol, test_desc))
resid_tol = 1e-4
test_desc = "bnd_cut_approx"
hsolver.configure(H_sys, Q, coup_strength, temperature,
20, 2, cut_freq,
renorm=False, bnd_cut_approx=True,
options=integ_options, stats=False)
assert_(hsolver.stats == None, "Failed to unset stats")
result = hsolver.run(initial_state, tlist)
P12_result1 = expect(result.states, P12p)
resid = abs(real(P12_result1 - PEG_DL))
max_resid = max(resid)
assert_(max_resid < resid_tol, "Max residual {} outside tolerence {}, "
"for hsolve with {}".format(max_resid, resid_tol, test_desc))
0
Example 36
def sigmoid(self, x):
half = x.dtype.type(0.5)
return numpy.tanh(x * half) * half + half
0
Example 37
Project: chainer Source File: lstm.py
def backward(self, inputs, grad_outputs):
xp = cuda.get_array_module(*inputs)
c_prev, x = inputs
batch = len(x)
gc, gh = grad_outputs
gx = xp.empty_like(x)
ga, gi, gf, go = _extract_gates(gx)
# Consider the case that either gradient is not given
if gc is None:
gc_update = 0
gc_rest = 0
else:
gc_update = gc[:batch]
gc_rest = gc[batch:]
if gh is None:
gh = 0
if xp is numpy:
co = numpy.tanh(self.c)
gc_prev = numpy.empty_like(c_prev)
# multiply f later
gc_prev[:batch] = gh * self.o * _grad_tanh(co) + gc_update
gc = gc_prev[:batch]
ga[:] = gc * self.i * _grad_tanh(self.a)
gi[:] = gc * self.a * _grad_sigmoid(self.i)
gf[:] = gc * c_prev[:batch] * _grad_sigmoid(self.f)
go[:] = gh * co * _grad_sigmoid(self.o)
gc_prev[:batch] *= self.f # multiply f here
gc_prev[batch:] = gc_rest
else:
a, i, f, o = _extract_gates(x)
gc_prev = xp.empty_like(c_prev)
cuda.elementwise(
'T c_prev, T c, T gc, T gh, T a, T i_, T f, T o',
'T gc_prev, T ga, T gi, T gf, T go',
'''
COMMON_ROUTINE;
T co = tanh(c);
T temp = gh * ao * grad_tanh(co) + gc;
ga = temp * ai * grad_tanh(aa);
gi = temp * aa * grad_sigmoid(ai);
gf = temp * c_prev * grad_sigmoid(af);
go = gh * co * grad_sigmoid(ao);
gc_prev = temp * af;
''',
'lstm_bwd', preamble=_preamble)(
c_prev[:batch], self.c, gc_update, gh, a, i, f, o,
gc_prev[:batch], ga, gi, gf, go)
gc_prev[batch:] = gc_rest
return gc_prev, gx
0
Example 38
Project: chainer Source File: tanh.py
def forward_cpu(self, x):
self.y = utils.force_array(numpy.tanh(x[0]))
return self.y,
0
Example 39
Project: chainer Source File: test_n_step_lstm.py
def check_forward(self, h_data, c_data, xs_data):
h = chainer.Variable(h_data)
c = chainer.Variable(c_data)
xs = [chainer.Variable(x) for x in xs_data]
hy, cy, ys = self.rnn(h, c, xs)
self.assertEqual(hy.data.shape, h.data.shape)
self.assertEqual(cy.data.shape, c.data.shape)
self.assertEqual(len(xs), len(ys))
for x, y in zip(xs, ys):
self.assertEqual(len(x.data), len(y.data))
self.assertEqual(y.data.shape[1], self.out_size)
self.rnn.to_cpu()
for batch, seq in enumerate(self.xs):
for layer in range(self.n_layer):
p = self.rnn[layer]
h_prev = self.h[layer, batch]
c_prev = self.c[layer, batch]
hs = []
for x in seq:
i = sigmoid(x.dot(p.w0.data.T) + h_prev.dot(p.w4.data.T) +
p.b0.data + p.b4.data)
f = sigmoid(x.dot(p.w1.data.T) + h_prev.dot(p.w5.data.T) +
p.b1.data + p.b5.data)
c_bar = numpy.tanh(
x.dot(p.w2.data.T) + h_prev.dot(p.w6.data.T) +
p.b2.data + p.b6.data)
o = sigmoid(x.dot(p.w3.data.T) + h_prev.dot(p.w7.data.T) +
p.b3.data + p.b7.data)
e_c = (f * c_prev + i * c_bar)
e_h = o * numpy.tanh(e_c)
h_prev = e_h
c_prev = e_c
hs.append(e_h)
seq = hs
testing.assert_allclose(hy.data[layer, batch], h_prev)
testing.assert_allclose(cy.data[layer, batch], c_prev)
for y, ey in zip(ys[batch].data, seq):
testing.assert_allclose(y, ey)
0
Example 40
def analyze(problem, X, Y, num_resamples=10,
conf_level=0.95, print_to_console=False):
"""Perform Delta Moment-Independent Analysis on model outputs.
Returns a dictionary with keys 'delta', 'delta_conf', 'S1', and 'S1_conf',
where each entry is a list of size D (the number of parameters) containing
the indices in the same order as the parameter file.
Parameters
----------
problem : dict
The problem definition
X: numpy.matrix
A NumPy matrix containing the model inputs
Y : numpy.array
A NumPy array containing the model outputs
num_resamples : int
The number of resamples when computing confidence intervals (default 10)
conf_level : float
The confidence interval level (default 0.95)
print_to_console : bool
Print results directly to console (default False)
References
----------
.. [1] Borgonovo, E. (2007). "A new uncertainty importance measure."
Reliability Engineering & System Safety, 92(6):771-784,
doi:10.1016/j.ress.2006.04.015.
.. [2] Plischke, E., E. Borgonovo, and C. L. Smith (2013). "Global
sensitivity measures from given data." European Journal of
Operational Research, 226(3):536-550, doi:10.1016/j.ejor.2012.11.047.
Examples
--------
>>> X = latin.sample(problem, 1000)
>>> Y = Ishigami.evaluate(X)
>>> Si = delta.analyze(problem, X, Y, print_to_console=True)
"""
D = problem['num_vars']
N = Y.size
if not 0 < conf_level < 1:
raise RuntimeError("Confidence level must be between 0-1.")
# equal frequency partition
M = min(np.ceil(N ** (2 / (7 + np.tanh((1500 - N) / 500)))), 48)
m = np.linspace(0, N, M + 1)
Ygrid = np.linspace(np.min(Y), np.max(Y), 100)
keys = ('delta', 'delta_conf', 'S1', 'S1_conf')
S = dict((k, np.zeros(D)) for k in keys)
if print_to_console:
print("Parameter %s %s %s %s" % keys)
for i in range(D):
S['delta'][i], S['delta_conf'][i] = bias_reduced_delta(
Y, Ygrid, X[:, i], m, num_resamples, conf_level)
S['S1'][i] = sobol_first(Y, X[:, i], m)
S['S1_conf'][i] = sobol_first_conf(
Y, X[:, i], m, num_resamples, conf_level)
if print_to_console:
print("%s %f %f %f %f" % (problem['names'][i], S['delta'][
i], S['delta_conf'][i], S['S1'][i], S['S1_conf'][i]))
return S
0
Example 41
Project: pyunicorn Source File: climate_network.py
def _calculate_non_local_adjacency(self, similarity_measure, threshold,
a=20, d_min=0.05):
"""
Return the adjacency matrix with suppressed spatially local links.
Physically trivial links between geographically close nodes
are removed.
For large a, :math:`d_min` corresponds to the minimum distance for
which links are allowed to exist.
**Example:**
>>> net = ClimateNetwork.SmallTestNetwork()
>>> net._calculate_non_local_adjacency(
... similarity_measure=net.similarity_measure(),
... threshold=0.5, a=30, d_min=0.20)
array([[0, 0, 0, 1, 1, 1], [0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0]], dtype=int8)
:type similarity_measure: 2D Numpy array [index, index]
:arg similarity_measure: The similarity measure for all pairs of
nodes.
:type threshold: number (float)
:arg threshold: The threshold of similarity measure, above which
two nodes are linked in the network.
:type a: number (float)
:arg a: The steepness parameter of the distance weighting function
in the transition region from not including any links
(weight=0) to including all links (weight=1).
:type d_min: number (float)
:arg d_min: The parameter controlling the minimum distance, above
which links can be included in the network
(unit radians).
:rtype: 2D Numpy array (int8) [index, index]
:return: the network's adjacency matrix.
"""
if self.silence_level <= 1:
print "Extracting network adjacency matrix removing local \
connections..."
# This function provides a smooth transition of distance weight
# centered around distance d_min.
# Other sigmoidal type functions could be used as well.
f = lambda x, a, b: 0.5 * (np.tanh(a * (x - b)) + 1)
weighted_similarity = similarity_measure * \
f(self.grid.angular_distance(), a, d_min)
return self._calculate_threshold_adjacency(weighted_similarity,
threshold)
0
Example 42
def sigmoid_kernel(X, Y=None, gamma=None, coef0=1):
"""
Compute the sigmoid kernel between X and Y::
K(X, Y) = tanh(gamma <X, Y> + coef0)
Read more in the :ref:`User Guide <sigmoid_kernel>`.
Parameters
----------
X : ndarray of shape (n_samples_1, n_features)
Y : ndarray of shape (n_samples_2, n_features)
gamma : float, default None
If None, defaults to 1.0 / n_samples_1
coef0 : int, default 1
Returns
-------
Gram matrix : array of shape (n_samples_1, n_samples_2)
"""
X, Y = check_pairwise_arrays(X, Y)
if gamma is None:
gamma = 1.0 / X.shape[1]
K = safe_sparse_dot(X, Y.T, dense_output=True)
K *= gamma
K += coef0
np.tanh(K, K) # compute tanh in-place
return K
0
Example 43
Project: chainer Source File: test_lstm.py
def check_forward(self, c_prev_data, x_data):
c_prev = chainer.Variable(c_prev_data)
x = chainer.Variable(x_data)
c, h = functions.lstm(c_prev, x)
self.assertEqual(c.data.dtype, self.dtype)
self.assertEqual(h.data.dtype, self.dtype)
batch = len(x_data)
# Compute expected out
a_in = self.x[:, [0, 4]]
i_in = self.x[:, [1, 5]]
f_in = self.x[:, [2, 6]]
o_in = self.x[:, [3, 7]]
c_expect = _sigmoid(i_in) * numpy.tanh(a_in) + \
_sigmoid(f_in) * self.c_prev[:batch]
h_expect = _sigmoid(o_in) * numpy.tanh(c_expect)
testing.assert_allclose(
c_expect, c.data[:batch], **self.check_forward_options)
testing.assert_allclose(
h_expect, h.data, **self.check_forward_options)
testing.assert_allclose(
c_prev_data[batch:], c.data[batch:], **self.check_forward_options)
0
Example 44
def f(self,y,psi):
"""
transform y with f using parameter vector psi
psi = [[a,b,c]]
::math::`f = \\sum_{terms} a * tanh(b*(y+c))`
"""
#1. check that number of params is consistent
assert psi.shape[0] == self.n_terms, 'inconsistent parameter dimensions'
assert psi.shape[1] == 3, 'inconsistent parameter dimensions'
#2. exponentiate the a and b (positive!)
mpsi = psi.copy()
#3. transform data
z = y.copy()
for i in range(len(mpsi)):
a,b,c = mpsi[i]
z += a*np.tanh(b*(y+c))
return z
0
Example 45
Project: scipy Source File: pseudo_diffs.py
def tilbert(x, h, period=None, _cache=_cache):
"""
Return h-Tilbert transform of a periodic sequence x.
If x_j and y_j are Fourier coefficients of periodic functions x
and y, respectively, then::
y_j = sqrt(-1)*coth(j*h*2*pi/period) * x_j
y_0 = 0
Parameters
----------
x : array_like
The input array to transform.
h : float
Defines the parameter of the Tilbert transform.
period : float, optional
The assumed period of the sequence. Default period is ``2*pi``.
Returns
-------
tilbert : ndarray
The result of the transform.
Notes
-----
If ``sum(x, axis=0) == 0`` and ``n = len(x)`` is odd then
``tilbert(itilbert(x)) == x``.
If ``2 * pi * h / period`` is approximately 10 or larger, then
numerically ``tilbert == hilbert``
(theoretically oo-Tilbert == Hilbert).
For even ``len(x)``, the Nyquist mode of ``x`` is taken zero.
"""
tmp = asarray(x)
if iscomplexobj(tmp):
return tilbert(tmp.real, h, period) + \
1j * tilbert(tmp.imag, h, period)
if period is not None:
h = h * 2 * pi / period
n = len(x)
omega = _cache.get((n, h))
if omega is None:
if len(_cache) > 20:
while _cache:
_cache.popitem()
def kernel(k, h=h):
if k:
return 1.0/tanh(h*k)
return 0
omega = convolve.init_convolution_kernel(n, kernel, d=1)
_cache[(n,h)] = omega
overwrite_x = _datacopied(tmp, x)
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
0
Example 46
def _sigmoid(x):
half = x.dtype.type(0.5)
return numpy.tanh(x * half) * half + half
0
Example 47
Project: chainer Source File: sigmoid.py
def forward_cpu(self, x):
half = x[0].dtype.type(0.5)
self.y = utils.force_array(numpy.tanh(x[0] * half) * half + half)
return self.y,
0
Example 48
def fgrad_y(self, y, psi, return_precalc = False):
"""
gradient of f w.r.t to y ([N x 1])
returns: Nx1 vector of derivatives, unless return_precalc is true,
then it also returns the precomputed stuff
"""
mpsi = psi.copy()
# vectorized version
# S = (mpsi[:,1]*(y + mpsi[:,2])).T
S = (mpsi[:,1]*(y[:,:,None] + mpsi[:,2])).T
R = np.tanh(S)
D = 1-R**2
# GRAD = (1+(mpsi[:,0:1]*mpsi[:,1:2]*D).sum(axis=0))[:,np.newaxis]
GRAD = (1+(mpsi[:,0:1][:,:,None]*mpsi[:,1:2][:,:,None]*D).sum(axis=0)).T
if return_precalc:
# return GRAD,S.sum(axis=1),R.sum(axis=1),D.sum(axis=1)
return GRAD, S, R, D
return GRAD
0
Example 49
Project: scipy Source File: pseudo_diffs.py
def itilbert(x,h,period=None, _cache=_cache):
"""
Return inverse h-Tilbert transform of a periodic sequence x.
If ``x_j`` and ``y_j`` are Fourier coefficients of periodic functions x
and y, respectively, then::
y_j = -sqrt(-1)*tanh(j*h*2*pi/period) * x_j
y_0 = 0
For more details, see `tilbert`.
"""
tmp = asarray(x)
if iscomplexobj(tmp):
return itilbert(tmp.real,h,period) + \
1j*itilbert(tmp.imag,h,period)
if period is not None:
h = h*2*pi/period
n = len(x)
omega = _cache.get((n,h))
if omega is None:
if len(_cache) > 20:
while _cache:
_cache.popitem()
def kernel(k,h=h):
if k:
return -tanh(h*k)
return 0
omega = convolve.init_convolution_kernel(n,kernel,d=1)
_cache[(n,h)] = omega
overwrite_x = _datacopied(tmp, x)
return convolve.convolve(tmp,omega,swap_real_imag=1,overwrite_x=overwrite_x)
0
Example 50
def f(self,y,psi):
"""
Transform y with f using parameter vector psi
psi = [[a,b,c]]
:math:`f = \\sum_{terms} a * tanh(b*(y+c))`
"""
#1. check that number of params is consistent
# assert psi.shape[0] == self.n_terms, 'inconsistent parameter dimensions'
# assert psi.shape[1] == 4, 'inconsistent parameter dimensions'
mpsi = psi.copy()
d = psi[-1]
mpsi = mpsi[:self.num_parameters-1].reshape(self.n_terms, 3)
#3. transform data
z = d*y.copy()
for i in range(len(mpsi)):
a,b,c = mpsi[i]
z += a*np.tanh(b*(y+c))
return z