Python numpy.float16() Examples
The following are 30 code examples for showing how to use numpy.float16(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
You may check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: profiler_ndarray.py License: Apache License 2.0 | 6 votes |
def test_ndarray_elementwise(): np.random.seed(0) nrepeat = 10 maxdim = 4 all_type = [np.float32, np.float64, np.float16, np.uint8, np.int32] real_type = [np.float32, np.float64, np.float16] for repeat in range(nrepeat): for dim in range(1, maxdim): check_with_uniform(lambda x, y: x + y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x - y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x * y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x / y, 2, dim, type_list=real_type) check_with_uniform(lambda x, y: x / y, 2, dim, rmin=1, type_list=all_type) check_with_uniform(mx.nd.sqrt, 1, dim, np.sqrt, rmin=0) check_with_uniform(mx.nd.square, 1, dim, np.square, rmin=0) check_with_uniform(lambda x: mx.nd.norm(x).asscalar(), 1, dim, np.linalg.norm)
Example 2
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: optimizer.py License: Apache License 2.0 | 6 votes |
def create_state(self, index, weight): momentum = None weight_master_copy = None if self.multi_precision and weight.dtype == numpy.float16: weight_master_copy = array(weight, ctx=weight.context, dtype=numpy.float32) if self.momentum != 0.0: momentum = zeros(weight.shape, weight.context, dtype=numpy.float32, stype=weight.stype) return (momentum, weight_master_copy) if weight.dtype == numpy.float16 and not self.multi_precision: warnings.warn("Accumulating with float16 in optimizer can lead to " "poor accuracy or slow convergence. " "Consider using multi_precision=True option of the " "SGD optimizer") if self.momentum != 0.0: momentum = zeros(weight.shape, weight.context, dtype=weight.dtype, stype=weight.stype) return momentum
Example 3
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_optimizer.py License: Apache License 2.0 | 6 votes |
def create_state(self, index, weight): """Create additional optimizer state: momentum Parameters ---------- weight : NDArray The weight data """ momentum = None weight_master_copy = None do_multi_precision = self.multi_precision and weight.dtype == np.float16 if do_multi_precision: if self.momentum != 0.0: momentum = mx.nd.zeros(weight.shape, weight.context, dtype=np.float32) weight_master_copy = array(weight, ctx=weight.context, dtype=np.float32) return (momentum, weight_master_copy) else: if self.momentum != 0.0: momentum = mx.nd.zeros(weight.shape, weight.context, dtype=weight.dtype) return momentum
Example 4
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_operator_gpu.py License: Apache License 2.0 | 6 votes |
def test_pooling_with_type2(): ctx_list = [{'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float64}}, {'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float32}}, {'ctx': mx.gpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float16}}, {'ctx': mx.cpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float64}}, {'ctx': mx.cpu(0), 'pool_data': (10, 2, 10, 10), 'type_dict': {'pool_data': np.float32}}] sym = mx.sym.Pooling(name='pool', kernel=(3,3), stride=(2,2), pool_type='max') check_consistency(sym, ctx_list, rand_type=np.float16) sym = mx.sym.Pooling(name='pool', kernel=(3,3), pad=(1,1), pool_type='avg') check_consistency(sym, ctx_list) sym = mx.sym.Pooling(name='pool', kernel=(5,5), pad=(2,2), pool_type='max') check_consistency(sym, ctx_list, rand_type=np.float16) sym = mx.sym.Pooling(name='pool', kernel=(3,3), pad=(1,1), pool_type='sum') check_consistency(sym, ctx_list)
Example 5
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_operator_gpu.py License: Apache License 2.0 | 6 votes |
def test_elementwisesum_with_type(): dev_types = [[mx.gpu(0), [np.float64, np.float32, np.float16]], [mx.cpu(0), [np.float64, np.float32]] ] for num_args in range(1, 6): ews_arg_shape = {} for i in range(num_args): ews_arg_shape['ews_arg'+str(i)] = (2, 10) sym = mx.sym.ElementWiseSum(name='ews', num_args=num_args) ctx_list = [] for dev, types in dev_types: for dtype in types: ews_arg_dtype = {'type_dict':{}} for i in range(num_args): ews_arg_dtype['type_dict']['ews_arg'+str(i)] = dtype ctx_elem = {'ctx': dev} ctx_elem.update(ews_arg_shape) ctx_elem.update(ews_arg_dtype) ctx_list.append(ctx_elem) check_consistency(sym, ctx_list)
Example 6
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_operator_gpu.py License: Apache License 2.0 | 6 votes |
def test_embedding_with_type(): def test_embedding_helper(data_types, weight_types, low_pad, high_pad): NVD = [[20, 10, 20], [200, 10, 300]] for N, V, D in NVD: sym = mx.sym.Embedding(name='embedding', input_dim=V, output_dim=D) ctx_list = [] for data_type in data_types: for weight_type in weight_types: ctx_list.append({'ctx': mx.gpu(0), 'embedding_data': (N,), 'type_dict': {'embedding_data': data_type, 'embedding_weight': weight_type}}) ctx_list.append({'ctx': mx.cpu(0), 'embedding_data': (N,), 'type_dict': {'embedding_data': data_type, 'embedding_weight': weight_type}}) arg_params = {'embedding_data': np.random.randint(low=-low_pad, high=V+high_pad, size=(N,))} check_consistency(sym, ctx_list, grad_req={'embedding_data': 'null','embedding_weight': 'write'}, arg_params=arg_params) data_types = [np.float16, np.float32, np.float64, np.int32] weight_types = [np.float16, np.float32, np.float64] test_embedding_helper(data_types, weight_types, 5, 5) data_types = [np.uint8] weight_types = [np.float16, np.float32, np.float64] test_embedding_helper(data_types, weight_types, 0, 5)
Example 7
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_operator_gpu.py License: Apache License 2.0 | 6 votes |
def test_psroipooling_with_type(): arg_params = { 'psroipool_rois': np.array([[0, 10, 22, 161, 173], [0, 20, 15, 154, 160]])} # plain psroipooling sym = mx.sym.contrib.PSROIPooling(spatial_scale=0.0625, output_dim=2, pooled_size=3, name='psroipool') ctx_list = [{'ctx': mx.gpu(0), 'psroipool_data': (1, 18, 14, 14), 'psroipool_rois': (2, 5), 'type_dict': {'psroipool_data': np.float64, 'psroipool_rois': np.float64}}, {'ctx': mx.gpu(0), 'psroipool_data': (1, 18, 14, 14), 'psroipool_rois': (2, 5), 'type_dict': {'psroipool_data': np.float32, 'psroipool_rois': np.float32}}, {'ctx': mx.gpu(0), 'psroipool_data': (1, 18, 14, 14), 'psroipool_rois': (2, 5), 'type_dict': {'psroipool_data': np.float16, 'psroipool_rois': np.float16}}, ] check_consistency(sym, ctx_list, grad_req={'psroipool_data': 'write', 'psroipool_rois': 'null'}, arg_params=arg_params)
Example 8
Project: pruning_yolov3 Author: zbyuan File: datasets.py License: GNU General Public License v3.0 | 6 votes |
def __next__(self): self.count += 1 img0 = self.imgs.copy() if cv2.waitKey(1) == ord('q'): # q to quit cv2.destroyAllWindows() raise StopIteration # Letterbox img = [letterbox(x, new_shape=self.img_size, interp=cv2.INTER_LINEAR)[0] for x in img0] # Stack img = np.stack(img, 0) # Normalize RGB img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB img = np.ascontiguousarray(img, dtype=np.float16 if self.half else np.float32) # uint8 to fp16/fp32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 return self.sources, img, img0, None
Example 9
Project: ConvLab Author: ConvLab File: replay.py License: MIT License | 6 votes |
def add_experience(self, state, action, reward, next_state, done): '''Implementation for update() to add experience to memory, expanding the memory size if necessary''' # Move head pointer. Wrap around if necessary self.head = (self.head + 1) % self.max_size self.states[self.head] = state.astype(np.float16) self.actions[self.head] = action self.rewards[self.head] = reward self.next_states[self.head] = next_state # self.ns_buffer.append(next_state.astype(np.float16)) self.dones[self.head] = done # Actually occupied size of memory if self.size < self.max_size: self.size += 1 self.seen_size += 1 # set to_train using memory counters head, seen_size instead of tick since clock will step by num_envs when on venv; to_train will be set to 0 after training step algorithm = self.body.agent.algorithm algorithm.to_train = algorithm.to_train or (self.seen_size > algorithm.training_start_step and self.head % algorithm.training_frequency == 0)
Example 10
Project: recruit Author: Frank-qlu File: test_half.py License: Apache License 2.0 | 6 votes |
def setup(self): # An array of all possible float16 values self.all_f16 = np.arange(0x10000, dtype=uint16) self.all_f16.dtype = float16 self.all_f32 = np.array(self.all_f16, dtype=float32) self.all_f64 = np.array(self.all_f16, dtype=float64) # An array of all non-NaN float16 values, in sorted order self.nonan_f16 = np.concatenate( (np.arange(0xfc00, 0x7fff, -1, dtype=uint16), np.arange(0x0000, 0x7c01, 1, dtype=uint16))) self.nonan_f16.dtype = float16 self.nonan_f32 = np.array(self.nonan_f16, dtype=float32) self.nonan_f64 = np.array(self.nonan_f16, dtype=float64) # An array of all finite float16 values, in sorted order self.finite_f16 = self.nonan_f16[1:-1] self.finite_f32 = self.nonan_f32[1:-1] self.finite_f64 = self.nonan_f64[1:-1]
Example 11
Project: recruit Author: Frank-qlu File: test_half.py License: Apache License 2.0 | 6 votes |
def test_half_conversion_denormal_round_even(self, float_t, uint_t, bits): # Test specifically that all bits are considered when deciding # whether round to even should occur (i.e. no bits are lost at the # end. Compare also gh-12721. The most bits can get lost for the # smallest denormal: smallest_value = np.uint16(1).view(np.float16).astype(float_t) assert smallest_value == 2**-24 # Will be rounded to zero based on round to even rule: rounded_to_zero = smallest_value / float_t(2) assert rounded_to_zero.astype(np.float16) == 0 # The significand will be all 0 for the float_t, test that we do not # lose the lower ones of these: for i in range(bits): # slightly increasing the value should make it round up: larger_pattern = rounded_to_zero.view(uint_t) | uint_t(1 << i) larger_value = larger_pattern.view(float_t) assert larger_value.astype(np.float16) == smallest_value
Example 12
Project: recruit Author: Frank-qlu File: test_half.py License: Apache License 2.0 | 6 votes |
def test_half_values(self): """Confirms a small number of known half values""" a = np.array([1.0, -1.0, 2.0, -2.0, 0.0999755859375, 0.333251953125, # 1/10, 1/3 65504, -65504, # Maximum magnitude 2.0**(-14), -2.0**(-14), # Minimum normal 2.0**(-24), -2.0**(-24), # Minimum subnormal 0, -1/1e1000, # Signed zeros np.inf, -np.inf]) b = np.array([0x3c00, 0xbc00, 0x4000, 0xc000, 0x2e66, 0x3555, 0x7bff, 0xfbff, 0x0400, 0x8400, 0x0001, 0x8001, 0x0000, 0x8000, 0x7c00, 0xfc00], dtype=uint16) b.dtype = float16 assert_equal(a, b)
Example 13
Project: vergeml Author: mme File: env.py License: MIT License | 5 votes |
def _convert(self, vals): res = {} for k, v in vals.items(): if isinstance(v, (np.int, np.int8, np.int16, np.int32, np.int64)): v = int(v) elif isinstance(v, (np.float, np.float16, np.float32, np.float64)): v = float(v) elif isinstance(v, Labels): v = list(v) elif isinstance(v, np.ndarray): v = v.tolist() elif isinstance(v, dict): v = self._convert(v) res[k] = v return res
Example 14
Project: vergeml Author: mme File: env.py License: MIT License | 5 votes |
def _toscalar(v): if isinstance(v, (np.float16, np.float32, np.float64, np.uint8, np.uint16, np.uint32, np.uint64, np.int8, np.int16, np.int32, np.int64)): return np.asscalar(v) else: return v
Example 15
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: vgg.py License: Apache License 2.0 | 5 votes |
def get_symbol(num_classes, num_layers=11, batch_norm=False, dtype='float32', **kwargs): """ Parameters ---------- num_classes : int, default 1000 Number of classification classes. num_layers : int Number of layers for the variant of densenet. Options are 11, 13, 16, 19. batch_norm : bool, default False Use batch normalization. dtype: str, float32 or float16 Data precision. """ vgg_spec = {11: ([1, 1, 2, 2, 2], [64, 128, 256, 512, 512]), 13: ([2, 2, 2, 2, 2], [64, 128, 256, 512, 512]), 16: ([2, 2, 3, 3, 3], [64, 128, 256, 512, 512]), 19: ([2, 2, 4, 4, 4], [64, 128, 256, 512, 512])} if num_layers not in vgg_spec: raise ValueError("Invalide num_layers {}. Possible choices are 11,13,16,19.".format(num_layers)) layers, filters = vgg_spec[num_layers] data = mx.sym.Variable(name="data") if dtype == 'float16': data = mx.sym.Cast(data=data, dtype=np.float16) feature = get_feature(data, layers, filters, batch_norm) classifier = get_classifier(feature, num_classes) if dtype == 'float16': classifier = mx.sym.Cast(data=classifier, dtype=np.float32) symbol = mx.sym.SoftmaxOutput(data=classifier, name='softmax') return symbol
Example 16
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: profiler_ndarray.py License: Apache License 2.0 | 5 votes |
def check_with_uniform(uf, arg_shapes, dim=None, npuf=None, rmin=-10, type_list=[np.float32]): """check function consistency with uniform random numbers""" if isinstance(arg_shapes, int): assert dim shape = tuple(np.random.randint(1, int(1000**(1.0/dim)), size=dim)) arg_shapes = [shape] * arg_shapes for dtype in type_list: ndarray_arg = [] numpy_arg = [] for s in arg_shapes: npy = np.random.uniform(rmin, 10, s).astype(dtype) narr = mx.nd.array(npy, dtype=dtype) ndarray_arg.append(narr) numpy_arg.append(npy) out1 = uf(*ndarray_arg) if npuf is None: out2 = uf(*numpy_arg).astype(dtype) else: out2 = npuf(*numpy_arg).astype(dtype) assert out1.shape == out2.shape if isinstance(out1, mx.nd.NDArray): out1 = out1.asnumpy() if dtype == np.float16: assert reldiff(out1, out2) < 2e-3 else: assert reldiff(out1, out2) < 1e-6
Example 17
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: image_classification.py License: Apache License 2.0 | 5 votes |
def main(): if opt.builtin_profiler > 0: profiler.set_config(profile_all=True, aggregate_stats=True) profiler.set_state('run') if opt.mode == 'symbolic': data = mx.sym.var('data') if opt.dtype == 'float16': data = mx.sym.Cast(data=data, dtype=np.float16) out = net(data) if opt.dtype == 'float16': out = mx.sym.Cast(data=out, dtype=np.float32) softmax = mx.sym.SoftmaxOutput(out, name='softmax') mod = mx.mod.Module(softmax, context=context) train_data, val_data = get_data_iters(dataset, batch_size, opt) mod.fit(train_data, eval_data=val_data, num_epoch=opt.epochs, kvstore=kv, batch_end_callback = mx.callback.Speedometer(batch_size, max(1, opt.log_interval)), epoch_end_callback = mx.callback.do_checkpoint('image-classifier-%s'% opt.model), optimizer = 'sgd', optimizer_params = {'learning_rate': opt.lr, 'wd': opt.wd, 'momentum': opt.momentum, 'multi_precision': True}, initializer = mx.init.Xavier(magnitude=2)) mod.save_parameters('image-classifier-%s-%d-final.params'%(opt.model, opt.epochs)) else: if opt.mode == 'hybrid': net.hybridize() train(opt, context) if opt.builtin_profiler > 0: profiler.set_state('stop') print(profiler.dumps())
Example 18
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: train_resnet.py License: Apache License 2.0 | 5 votes |
def main(): data = mx.sym.var('data') if opt.dtype == 'float16': data = mx.sym.Cast(data=data, dtype=np.float16) net.cast(np.float16) out = net(data) if opt.dtype == 'float16': out = mx.sym.Cast(data=out, dtype=np.float32) softmax = mx.sym.SoftmaxOutput(out, name='softmax') # We need to pass the data_iterator to Module so that when the number of workers # changes, the iterator is updated with new batch size and partition size. mod = mx.mod.Module(softmax, context=context, data_iterator=data_iterator_fn) if opt.use_pretrained: arg_params = {} for x in net.collect_params().values(): x.reset_ctx(mx.cpu()) arg_params[x.name] = x.data() else: arg_params = None mod.fit(train_data, arg_params=arg_params, eval_data = val_data, num_epoch=opt.num_epochs, kvstore=kv, batch_end_callback = ElasticSpeedometer(kv, batch_size, max(1, opt.log_interval)), epoch_end_callback = mx.callback.do_checkpoint('imagenet-%s'% opt.model, period=save_frequency), optimizer = optimizer, optimizer_params=optimizer_params, initializer=initializer)
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: ndarray.py License: Apache License 2.0 | 5 votes |
def full(shape, val, ctx=None, dtype=mx_real_t, out=None): """Returns a new array of given shape and type, filled with the given value `val`. Parameters -------- shape : int or tuple of int The shape of the new array. val : scalar Fill value. ctx : Context, optional Device context (default is the current default context). dtype : `str` or `numpy.dtype`, optional The data type of the returned `NDArray`. The default datatype is `float32`. out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray `NDArray` filled with `val`, with the given shape, ctx, and dtype. Examples -------- >>> mx.nd.full(1, 2.0).asnumpy() array([ 2.], dtype=float32) >>> mx.nd.full((1, 2), 2.0, mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.full((1, 2), 2.0, dtype='float16').asnumpy() array([[ 2., 2.]], dtype=float16) """ out = empty(shape, ctx, dtype) if out is None else out out[:] = val return out
Example 20
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: ndarray.py License: Apache License 2.0 | 5 votes |
def zeros(shape, ctx=None, dtype=None, **kwargs): """Returns a new array filled with all zeros, with the given shape and type. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A created array Examples -------- >>> mx.nd.zeros(1).asnumpy() array([ 0.], dtype=float32) >>> mx.nd.zeros((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy() array([[ 0., 0.]], dtype=float16) """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._zeros(shape=shape, ctx=ctx, dtype=dtype, **kwargs) # pylint: enable= no-member, protected-access
Example 21
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: optimizer.py License: Apache License 2.0 | 5 votes |
def create_state_multi_precision(self, index, weight): """Creates auxiliary state for a given weight, including FP32 high precision copy if original weight is FP16. This method is provided to perform automatic mixed precision training for optimizers that do not support it themselves. Parameters ---------- index : int An unique index to identify the weight. weight : NDArray The weight. Returns ------- state : any obj The state associated with the weight. """ weight_master_copy = None if self.multi_precision and weight.dtype == numpy.float16: weight_master_copy = weight.astype(numpy.float32) return (weight_master_copy,) + (self.create_state(index, weight_master_copy),) if weight.dtype == numpy.float16 and not self.multi_precision: warnings.warn("Accumulating with float16 in optimizer can lead to " "poor accuracy or slow convergence. " "Consider using multi_precision=True option of the " "optimizer") return self.create_state(index, weight)
Example 22
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: optimizer.py License: Apache License 2.0 | 5 votes |
def update_multi_precision(self, index, weight, grad, state): """Updates the given parameter using the corresponding gradient and state. Mixed precision version. Parameters ---------- index : int The unique index of the parameter into the individual learning rates and weight decays. Learning rates and weight decay may be set via `set_lr_mult()` and `set_wd_mult()`, respectively. weight : NDArray The parameter to be updated. grad : NDArray The gradient of the objective with respect to this parameter. state : any obj The state returned by `create_state()`. """ if self.multi_precision and weight.dtype == numpy.float16: # Wrapper for mixed precision weight_master_copy = state[0] original_state = state[1] grad32 = grad.astype(numpy.float32) self.update(index, weight_master_copy, grad32, original_state) cast(weight_master_copy, dtype=weight.dtype, out=weight) else: self.update(index, weight, grad, state)
Example 23
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: optimizer.py License: Apache License 2.0 | 5 votes |
def create_state_multi_precision(self, index, weight): weight_master_copy = None if self.multi_precision and weight.dtype == numpy.float16: weight_master_copy = weight.astype(numpy.float32) return (self.create_state(index, weight_master_copy), weight_master_copy) if weight.dtype == numpy.float16 and not self.multi_precision: warnings.warn("Accumulating with float16 in optimizer can lead to " "poor accuracy or slow convergence. " "Consider using multi_precision=True option of the " "SGD optimizer") return self.create_state(index, weight)
Example 24
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_optimizer.py License: Apache License 2.0 | 5 votes |
def test_sgd(): opt1 = PySGD opt2 = mx.optimizer.SGD shape = (3, 4, 5) mom_options = [{}, {'momentum': 0.9}] cg_options = [{}, {'clip_gradient': 0.4}, {'clip_gradient': 0.5}] rg_options = [{}, {'rescale_grad': 0.14}, {'rescale_grad': 0.8}] wd_options = [{}, {'wd': 0.03}, {'wd': 0.05}, {'wd': 0.07}] mp_options = [{}, {'multi_precision': False}, {'multi_precision': True}] for dtype in [np.float16, np.float32, np.float64]: for mom_option in mom_options: for cg_option in cg_options: for rg_option in rg_options: for wd_option in wd_options: for mp_option in mp_options: kwarg = {} kwarg.update(mom_option) kwarg.update(cg_option) kwarg.update(rg_option) kwarg.update(wd_option) kwarg.update(mp_option) if (dtype == np.float16 and ('multi_precision' not in kwarg or not kwarg['multi_precision'])): continue if dtype == np.float16: compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype, rtol=1e-3) else: compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype) # test operator fallback on cpu if dtype != np.float16: compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape[:2], dtype, w_stype='csr', g_stype='csr')
Example 25
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_optimizer.py License: Apache License 2.0 | 5 votes |
def test_nag(): opt1 = PyNAG opt2 = mx.optimizer.NAG shape = (3, 4, 5) mom_options = [{}, {'momentum': 0.9}] cg_options = [{}, {'clip_gradient': 0.4}, {'clip_gradient': 0.5}] rg_options = [{}, {'rescale_grad': 0.14}, {'rescale_grad': 0.8}] wd_options = [{}, {'wd': 0.03}, {'wd': 0.05}, {'wd': 0.07}] mp_options = [{}, {'multi_precision': False}, {'multi_precision': True}] for dtype in [np.float16, np.float32, np.float64]: for mom_option in mom_options: for cg_option in cg_options: for rg_option in rg_options: for wd_option in wd_options: for mp_option in mp_options: kwarg = {} kwarg.update(mom_option) kwarg.update(cg_option) kwarg.update(rg_option) kwarg.update(wd_option) kwarg.update(mp_option) if (dtype == np.float16 and ('multi_precision' not in kwarg or not kwarg['multi_precision'])): continue compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype) # FTML
Example 26
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_optimizer.py License: Apache License 2.0 | 5 votes |
def test_adam(): opt1 = PyAdam opt2 = mx.optimizer.Adam shape = (3, 4, 5) cg_options = [{}, {'clip_gradient': 0.4}, {'clip_gradient': 0.5}] rg_options = [{}, {'rescale_grad': 0.14}, {'rescale_grad': 0.8}] wd_options = [{}, {'wd': 0.03}, {'wd': 0.05}, {'wd': 0.07}] mp_options = [{}, {'multi_precision': False}, {'multi_precision': True}] for dtype in [np.float16, np.float32, np.float64]: for cg_option in cg_options: for rg_option in rg_options: for wd_option in wd_options: for mp_option in mp_options: kwarg = {} kwarg.update(cg_option) kwarg.update(rg_option) kwarg.update(wd_option) kwarg.update(mp_option) if (dtype == np.float16 and ('multi_precision' not in kwarg or not kwarg['multi_precision'])): continue # atol 2e-5 needed to pass with seed 1248389097 compare_optimizer(opt1(lazy_update=False, **kwarg), opt2(**kwarg), shape, dtype, rtol=1e-4, atol=2e-5) # atol 2e-5 needed to pass with seed 781809840 compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype, w_stype='row_sparse', g_stype='row_sparse', rtol=1e-4, atol=2e-5) compare_optimizer(opt1(lazy_update=False, **kwarg), opt2(lazy_update=False, **kwarg), shape, dtype, w_stype='row_sparse', g_stype='row_sparse', rtol=1e-4, atol=2e-5) compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype, w_stype='default', g_stype='row_sparse', rtol=1e-4, atol=2e-5) compare_optimizer(opt1(lazy_update=False, **kwarg), opt2(lazy_update=False, **kwarg), shape, dtype, w_stype='default', g_stype='row_sparse', rtol=1e-4, atol=2e-5) # Signum
Example 27
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_optimizer.py License: Apache License 2.0 | 5 votes |
def test_rms(): opt1 = PyRMSProp opt2 = mx.optimizer.RMSProp shape = (3, 4, 5) cg_options = [{}, {'clip_gradient': 0.4}, {'clip_gradient': 0.5}] cw_options = [{}, {'clip_weights': 0.01}] center_options = [{}, {'centered': False}, {'centered': True}] rg_options = [{}, {'rescale_grad': 0.14}, {'rescale_grad': 0.8}] wd_options = [{}, {'wd': 0.03}, {'wd': 0.05}, {'wd': 0.07}] mp_options = [{}, {'multi_precision': False}, {'multi_precision': True}] for dtype in [np.float16, np.float32]: # Reduce foating point compare tolerance to avoid flaky test failure. rtol, atol = (1e-1, 1e-1) if dtype is np.float16 else (1e-2, 1e-2) for cw_option in cw_options: for cg_option in cg_options: for center_option in center_options: for rg_option in rg_options: for wd_option in wd_options: for mp_option in mp_options: kwarg = {} kwarg.update(cw_option) kwarg.update(cg_option) kwarg.update(center_option) kwarg.update(rg_option) kwarg.update(wd_option) kwarg.update(mp_option) if (dtype == np.float16 and ('multi_precision' not in kwarg or not kwarg['multi_precision'])): continue compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype, rtol=rtol, atol=atol) if (default_context() == mx.cpu()): compare_optimizer(opt1(**kwarg), opt2(**kwarg), shape, dtype, g_stype='row_sparse', rtol=rtol, atol=atol)
Example 28
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_ndarray.py License: Apache License 2.0 | 5 votes |
def check_with_uniform(uf, arg_shapes, dim=None, npuf=None, rmin=-10, type_list=[np.float32]): """check function consistency with uniform random numbers""" if isinstance(arg_shapes, int): assert dim shape = tuple(np.random.randint(1, int(1000**(1.0/dim)), size=dim)) arg_shapes = [shape] * arg_shapes for dtype in type_list: ndarray_arg = [] numpy_arg = [] for s in arg_shapes: npy = np.random.uniform(rmin, 10, s).astype(dtype) narr = mx.nd.array(npy, dtype=dtype) ndarray_arg.append(narr) numpy_arg.append(npy) out1 = uf(*ndarray_arg) if npuf is None: out2 = uf(*numpy_arg).astype(dtype) else: out2 = npuf(*numpy_arg).astype(dtype) assert out1.shape == out2.shape if isinstance(out1, mx.nd.NDArray): out1 = out1.asnumpy() if dtype == np.float16: assert_almost_equal(out1, out2, rtol=2e-3, atol=1e-5) else: assert_almost_equal(out1, out2, atol=1e-5)
Example 29
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_ndarray.py License: Apache License 2.0 | 5 votes |
def test_ndarray_elementwise(): nrepeat = 10 maxdim = 4 all_type = [np.float32, np.float64, np.float16, np.uint8, np.int8, np.int32, np.int64] real_type = [np.float32, np.float64, np.float16] for repeat in range(nrepeat): for dim in range(1, maxdim): check_with_uniform(lambda x, y: x + y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x - y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x * y, 2, dim, type_list=all_type) check_with_uniform(lambda x, y: x / y, 2, dim, type_list=real_type) check_with_uniform(lambda x, y: x / y, 2, dim, rmin=1, type_list=all_type) check_with_uniform(mx.nd.sqrt, 1, dim, np.sqrt, rmin=0) check_with_uniform(mx.nd.square, 1, dim, np.square, rmin=0) check_with_uniform(lambda x: mx.nd.norm(x).asscalar(), 1, dim, np.linalg.norm)
Example 30
Project: dynamic-training-with-apache-mxnet-on-aws Author: awslabs File: test_symbol.py License: Apache License 2.0 | 5 votes |
def test_symbol_infer_type(): data = mx.symbol.Variable('data') f32data = mx.symbol.Cast(data=data, dtype='float32') fc1 = mx.symbol.FullyConnected(data = f32data, name='fc1', num_hidden=128) mlp = mx.symbol.SoftmaxOutput(data = fc1, name = 'softmax') arg, out, aux = mlp.infer_type(data=np.float16) assert arg == [np.float16, np.float32, np.float32, np.float32] assert out == [np.float32] assert aux == []