本文整理汇总了Python中theano.compile.nanguardmode.NanGuardMode方法的典型用法代码示例。如果您正苦于以下问题:Python nanguardmode.NanGuardMode方法的具体用法?Python nanguardmode.NanGuardMode怎么用?Python nanguardmode.NanGuardMode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类theano.compile.nanguardmode
的用法示例。
在下文中一共展示了nanguardmode.NanGuardMode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_NanGuardMode
# 需要导入模块: from theano.compile import nanguardmode [as 别名]
# 或者: from theano.compile.nanguardmode import NanGuardMode [as 别名]
def test_NanGuardMode():
"""
Tests if NanGuardMode is working by feeding in numpy.inf and numpy.nans
intentionally. A working implementation should be able to capture all
the abnormalties.
"""
x = T.matrix()
w = theano.shared(numpy.random.randn(5, 7).astype(theano.config.floatX))
y = T.dot(x, w)
fun = theano.function(
[x], y,
mode=NanGuardMode(nan_is_error=True, inf_is_error=True)
)
a = numpy.random.randn(3, 5).astype(theano.config.floatX)
infa = numpy.tile(
(numpy.asarray(100.) ** 1000000).astype(theano.config.floatX), (3, 5))
nana = numpy.tile(
numpy.asarray(numpy.nan).astype(theano.config.floatX), (3, 5))
biga = numpy.tile(
numpy.asarray(1e20).astype(theano.config.floatX), (3, 5))
fun(a) # normal values
# Temporarily silence logger
_logger = logging.getLogger("theano.compile.nanguardmode")
try:
_logger.propagate = False
assert_raises(AssertionError, fun, infa) # INFs
assert_raises(AssertionError, fun, nana) # NANs
assert_raises(AssertionError, fun, biga) # big values
finally:
_logger.propagate = True
示例2: _get_theano_monitor_mode
# 需要导入模块: from theano.compile import nanguardmode [as 别名]
# 或者: from theano.compile.nanguardmode import NanGuardMode [as 别名]
def _get_theano_monitor_mode(self):
mode = None
if self.__DETECT_EXCEPTIONS__ == 'cpu':
mode = theano.compile.MonitorMode(
post_func=detect_floating_point_exceptions)
elif self.__DETECT_EXCEPTIONS__ == 'theano':
mode = nanguardmode.NanGuardMode(
nan_is_error=True,
inf_is_error=True,
big_is_error=False)
logging.debug('Injection mode: %s.', repr(mode))
return mode
示例3: compile_train
# 需要导入模块: from theano.compile import nanguardmode [as 别名]
# 或者: from theano.compile.nanguardmode import NanGuardMode [as 别名]
def compile_train(self):
# questions (theano variables)
inputs = T.imatrix() # padded input word sequence (for training)
target = T.imatrix() # padded target word sequence (for training)
# encoding & decoding
if not self.attend:
code = self.encoder.build_encoder(inputs, None)
logPxz, logPPL = self.decoder.build_decoder(target, code)
else:
code, _, c_mask, _ = self.encoder.build_encoder(inputs, None, return_sequence=True, return_embed=True)
logPxz, logPPL = self.decoder.build_decoder(target, code, c_mask)
# responding loss
loss_rec = T.mean(-logPxz)
loss_ppl = T.exp(T.mean(-logPPL))
loss = loss_rec
updates = self.optimizer.get_updates(self.params, loss)
logger.info("compiling the compuational graph ::training function::")
train_inputs = [inputs, target]
self.train_ = theano.function(train_inputs,
[loss_rec, loss_ppl],
updates=updates,
name='train_fun')
# mode=NanGuardMode(nan_is_error=True, inf_is_error=True, big_is_error=True))
logger.info("training functions compile done.")
# # add monitoring:
# self.monitor['context'] = context
# self._monitoring()
#
# # compiling monitoring
# self.compile_monitoring(train_inputs)
示例4: compile_train
# 需要导入模块: from theano.compile import nanguardmode [as 别名]
# 或者: from theano.compile.nanguardmode import NanGuardMode [as 别名]
def compile_train(self):
# questions (theano variables)
inputs = T.imatrix() # padded input word sequence (for training)
target = T.imatrix() # padded target word sequence (for training)
cc_matrix = T.tensor3()
# encoding & decoding
code, _, c_mask, _ = self.encoder.build_encoder(inputs, None, return_sequence=True, return_embed=True)
# code: (nb_samples, max_len, contxt_dim)
if 'explicit_loc' in self.config:
if self.config['explicit_loc']:
print 'use explicit location!!'
max_len = code.shape[1]
expLoc = T.eye(max_len, self.config['encode_max_len'], dtype='float32')[None, :, :]
expLoc = T.repeat(expLoc, code.shape[0], axis=0)
code = T.concatenate([code, expLoc], axis=2)
logPxz, logPPL = self.decoder.build_decoder(target, cc_matrix,
code, c_mask)
# responding loss
loss_rec = T.mean(-logPxz)
loss_ppl = T.exp(T.mean(-logPPL))
loss = loss_rec
updates = self.optimizer.get_updates(self.params, loss)
logger.info("compiling the compuational graph ::training function::")
train_inputs = [inputs, target, cc_matrix]
self.train_ = theano.function(train_inputs,
[loss_rec, loss_ppl],
updates=updates,
name='train_fun')
self.train_guard = theano.function(train_inputs,
[loss_rec, loss_ppl],
updates=updates,
name='train_fun',
mode=NanGuardMode(nan_is_error=True, inf_is_error=True, big_is_error=True))
logger.info("training functions compile done.")
# # add monitoring:
# self.monitor['context'] = context
# self._monitoring()
#
# # compiling monitoring
# self.compile_monitoring(train_inputs)