本文整理汇总了Python中quagga.connector.Connector.tanh方法的典型用法代码示例。如果您正苦于以下问题:Python Connector.tanh方法的具体用法?Python Connector.tanh怎么用?Python Connector.tanh使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类quagga.connector.Connector
的用法示例。
在下文中一共展示了Connector.tanh方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: LstmBlock
# 需要导入模块: from quagga.connector import Connector [as 别名]
# 或者: from quagga.connector.Connector import tanh [as 别名]
class LstmBlock(object):
"""
A long short-term memory (LSTM) block.
Parameters
----------
W
R
b
grad_clipping
x
mask
prev_c
prev_h
device_id : int
Defines the device's id on which the computation will take place
Returns
-------
"""
def __init__(self, W, R, b, grad_clipping, x, mask, prev_c, prev_h, device_id=None):
self.f_context = Context(device_id)
device_id = self.f_context.device_id
if W.bpropagable:
self.W, self.dL_dW = W.register_usage(device_id, device_id)
self.W_b_context = Context(device_id)
else:
self.W = W.register_usage(device_id)
if R.bpropagable:
self.R, self.dL_dR = R.register_usage(device_id, device_id)
self.R_b_context = Context(device_id)
else:
self.R = R.register_usage(device_id)
if b.bpropagable:
self.b, self.dL_db = b.register_usage(device_id, device_id)
self.b_b_context = Context(device_id)
else:
self.b = b.register_usage(device_id)
self.grad_clipping = grad_clipping
if x.bpropagable:
self.x, self.dL_dx = x.register_usage(device_id, device_id)
self.x_b_context = Context(device_id)
else:
self.x = x.register_usage(device_id)
if mask:
self.mask = mask.register_usage(device_id)
if prev_c.bpropagable:
self.prev_c, self.dL_dprev_c = prev_c.register_usage(device_id, device_id)
self.prev_c_b_context = Context(device_id)
else:
self.prev_c = prev_c.register_usage(device_id)
if prev_h.bpropagable:
self.prev_h, self.dL_dprev_h = prev_h.register_usage(device_id, device_id)
self.prev_h_b_context = Context(device_id)
else:
self.prev_h = prev_h.register_usage(device_id)
self.learning = W.bpropagable or R.bpropagable or x.bpropagable or \
prev_c.bpropagable or prev_h.bpropagable
if self.learning:
self.b_context = Context(device_id)
dim = self.R.nrows
batch_size = self.x.nrows
self.zifo = Matrix.empty(batch_size, 4 * dim, device_id=device_id)
self.z = self.zifo[:, 0*dim:1*dim]
self.i = self.zifo[:, 1*dim:2*dim]
self.f = self.zifo[:, 2*dim:3*dim]
self.o = self.zifo[:, 3*dim:4*dim]
self.c = Matrix.empty_like(self.prev_c, device_id)
self.c = Connector(self.c, device_id if self.learning else None)
self.tanh_c = Matrix.empty_like(self.c, device_id)
self.h = Matrix.empty_like(self.c, device_id)
self.h = Connector(self.h, device_id if self.learning else None)
if self.learning:
self._dzifo_dpre_zifo = Matrix.empty_like(self.zifo)
self.dz_dpre_z = self._dzifo_dpre_zifo[:, 0*dim:1*dim]
self.di_dpre_i = self._dzifo_dpre_zifo[:, 1*dim:2*dim]
self.df_dpre_f = self._dzifo_dpre_zifo[:, 2*dim:3*dim]
self.do_dpre_o = self._dzifo_dpre_zifo[:, 3*dim:4*dim]
self.dL_dpre_zifo = self._dzifo_dpre_zifo
self.dL_dpre_z = self.dz_dpre_z
self.dL_dpre_i = self.di_dpre_i
self.dL_dpre_f = self.df_dpre_f
self.dL_dpre_o = self.do_dpre_o
self._dtanh_c_dc = Matrix.empty_like(self.c)
@property
def dzifo_dpre_zifo(self):
if self.learning:
return self._dzifo_dpre_zifo
@property
def dtanh_c_dc(self):
if self.learning:
return self._dtanh_c_dc
def fprop(self):
#.........这里部分代码省略.........