本文整理汇总了Python中neon.backends.gen_backend函数的典型用法代码示例。如果您正苦于以下问题:Python gen_backend函数的具体用法?Python gen_backend怎么用?Python gen_backend使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gen_backend函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_cpu_randomstate
def test_cpu_randomstate():
# run 1
be = gen_backend(backend='cpu', rng_seed=100)
a = be.empty((3, 3))
be.make_binary_mask(a, keepthresh=be.rng.rand())
x0 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
x1 = a.get()
# run 2, using reset
be.rng_reset()
be.make_binary_mask(a, keepthresh=be.rng.rand())
y0 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
y1 = a.get()
del(be)
# run 3, using a new backend
be = gen_backend(backend='cpu', rng_seed=100)
a = be.empty((3, 3))
be.make_binary_mask(a, keepthresh=be.rng.rand())
z0 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
z1 = a.get()
# check equality
assert tensors_allclose([x0, x1], [y0, y1], rtol=0., atol=0.)
assert tensors_allclose([x0, x1], [z0, z1], rtol=0., atol=0.)
del(be)
示例2: __init__
def __init__(self, model, ngpu, options,
data_options=None, time_options=None):
self.model = model
#self.model.set_batch_size(data_options['batch_size'])
self.ngpu = ngpu
self.gpu_mode = True if ngpu >= 1 else False
self.time_options = time_options
self.data_options = data_options
if self.gpu_mode:
try:
self.be = gen_backend(backend='nervanagpu',
batch_size=data_options['batch_size'])
print("Backgrand: nervanagpu")
except:
self.be = gen_backend(backend='gpu',
batch_size=data_options['batch_size'])
print("Backgrand: gpu")
else:
self.be = gen_backend(backend='mkl',
batch_size=data_options['batch_size'])
self.loss = L.GeneralizedCost(costfunc=TF.CrossEntropyMulti())
B = self.data_options['batch_size']
self.model.bsz(B)
C, W, H = self.data_options['image_shape']
self.model.initialize(((C, H, W), B), self.loss)
示例3: test_gpu_randomstate
def test_gpu_randomstate():
# run 1
be = gen_backend(backend='gpu', rng_seed=100)
a = be.empty((3, 3))
a[:] = be.rand() # gpu rand
x0 = a.get()
x1 = be.rng.rand(3, 3) # host rand
a[:] = be.rand() # gpu rand
x2 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
x3 = a.get()
assert len(be.context_rand_state_map) == 1 and len(be.context_rand_state_alive) == 1
for ctx in be.context_rand_state_alive:
assert be.context_rand_state_alive[ctx] is True
# run 2, using reset
be.rng_reset()
for ctx in be.context_rand_state_alive:
assert be.context_rand_state_alive[ctx] is False
a[:] = be.rand()
y0 = a.get()
y1 = be.rng.rand(3, 3)
a[:] = be.rand()
y2 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
y3 = a.get()
assert len(be.context_rand_state_map) == 1 and len(be.context_rand_state_alive) == 1
for ctx in be.context_rand_state_alive:
assert be.context_rand_state_alive[ctx] is True
del(be)
# run 3, using a new backend
be = gen_backend(backend='gpu', rng_seed=100)
a = be.empty((3, 3))
a[:] = be.rand() # gpu rand
z0 = a.get()
z1 = be.rng.rand(3, 3) # host rand
a[:] = be.rand() # gpu rand
z2 = a.get()
be.make_binary_mask(a, keepthresh=be.rng.rand())
z3 = a.get()
# check equality
assert_tensors_allclose([x0, x1, x2, x3], [y0, y1, y2, y3], rtol=0., atol=0.)
assert_tensors_allclose([x0, x1, x2, x3], [z0, z1, z2, z3], rtol=0., atol=0.)
del(be)
示例4: __init__
def __init__(self, num_actions, args):
# remember parameters
self.num_actions = num_actions
self.batch_size = args.batch_size
self.discount_rate = args.discount_rate
self.history_length = args.history_length
self.screen_dim = (args.screen_height, args.screen_width)
self.clip_error = args.clip_error
self.min_reward = args.min_reward
self.max_reward = args.max_reward
self.batch_norm = args.batch_norm
# create Neon backend
self.be = gen_backend(backend = args.backend,
batch_size = args.batch_size,
rng_seed = args.random_seed,
device_id = args.device_id,
datatype = np.dtype(args.datatype).type,
stochastic_round = args.stochastic_round)
# prepare tensors once and reuse them
self.input_shape = (self.history_length,) + self.screen_dim + (self.batch_size,)
self.input = self.be.empty(self.input_shape)
self.input.lshape = self.input_shape # HACK: needed for convolutional networks
self.targets = self.be.empty((self.num_actions, self.batch_size))
# create model
layers = self._createLayers(num_actions)
self.model = Model(layers = layers)
self.cost = GeneralizedCost(costfunc = SumSquared())
# Bug fix
for l in self.model.layers.layers:
l.parallelism = 'Disabled'
self.model.initialize(self.input_shape[:-1], self.cost)
if args.optimizer == 'rmsprop':
self.optimizer = RMSProp(learning_rate = args.learning_rate,
decay_rate = args.decay_rate,
stochastic_round = args.stochastic_round)
elif args.optimizer == 'adam':
self.optimizer = Adam(learning_rate = args.learning_rate,
stochastic_round = args.stochastic_round)
elif args.optimizer == 'adadelta':
self.optimizer = Adadelta(decay = args.decay_rate,
stochastic_round = args.stochastic_round)
else:
assert false, "Unknown optimizer"
# create target model
self.train_iterations = 0
if args.target_steps:
self.target_model = Model(layers = self._createLayers(num_actions))
# Bug fix
for l in self.target_model.layers.layers:
l.parallelism = 'Disabled'
self.target_model.initialize(self.input_shape[:-1])
self.save_weights_prefix = args.save_weights_prefix
else:
self.target_model = self.model
self.callback = None
示例5: sanity_check
def sanity_check(conf_file, result, **be_args):
experiment = deserialize(os.path.join(dir, conf_file))
backend = gen_backend(model=experiment.model, **be_args)
experiment.initialize(backend)
res = experiment.run()
print(float(res['test']['MisclassRate_TOP_1']))
assert float(res['test']['MisclassRate_TOP_1']) == result
示例6: backend
def backend(request):
"""
Fixture to setup the backend before running a
test. Also registers the teardown function to clean
up the backend after a test is done. This had module
scope, so this will be run once for each test in a
given test file (module).
This fixture is parameterized to run both the cpu and
gpu backends for every test
"""
be = gen_backend(backend=request.param, rng_seed=0)
NervanaObject.be = be
be.bsz = 128 # hardwires here to 128
# add a cleanup call - will run after all
# test in module are done
def cleanup():
be = request.getfuncargvalue("backend")
del be
request.addfinalizer(cleanup)
# tests using this fixture can
# access the backend object from
# backend or use the NervanaObject.be global
return be
示例7: backend_default
def backend_default(request):
'''
Fixture to setup the backend before running a
test. Also registers the teardown function to clean
up the backend after a test is done. This had module
scope, so this will be run once for each test in a
given test file (module).
This fixture is parameterized to run both the cpu and
gpu backends for every test
'''
be = gen_backend(backend=request.param,
default_dtype=np.float32,
batch_size=128,
rng_seed=0)
# add a cleanup call - will run after all
# test in module are done
def cleanup():
be = request.getfuncargvalue('backend_default')
del be
request.addfinalizer(cleanup)
# tests using this fixture can
# access the backend object from
# backend or use the NervanaObject.be global
return be
示例8: __init__
def __init__(self, env, args, rng, name = "DQNNeon"):
""" Initializes a network based on the Neon framework.
Args:
env (AtariEnv): The envirnoment in which the agent actuates.
args (argparse.Namespace): All settings either with a default value or set via command line arguments.
rng (mtrand.RandomState): initialized Mersenne Twister pseudo-random number generator.
name (str): The name of the network object.
Note:
This function should always call the base class first to initialize
the common values for the networks.
"""
_logger.info("Initializing new object of type " + str(type(self).__name__))
super(DQNNeon, self).__init__(env, args, rng, name)
self.input_shape = (self.sequence_length,) + self.frame_dims + (self.batch_size,)
self.dummy_batch = np.zeros((self.batch_size, self.sequence_length) + self.frame_dims, dtype=np.uint8)
self.batch_norm = args.batch_norm
self.be = gen_backend(
backend = args.backend,
batch_size = args.batch_size,
rng_seed = args.random_seed,
device_id = args.device_id,
datatype = np.dtype(args.datatype).type,
stochastic_round = args.stochastic_round)
# prepare tensors once and reuse them
self.input = self.be.empty(self.input_shape)
self.input.lshape = self.input_shape # HACK: needed for convolutional networks
self.targets = self.be.empty((self.output_shape, self.batch_size))
# create model
layers = self._create_layer()
self.model = Model(layers = layers)
self.cost_func = GeneralizedCost(costfunc = SumSquared())
# Bug fix
for l in self.model.layers.layers:
l.parallelism = 'Disabled'
self.model.initialize(self.input_shape[:-1], self.cost_func)
self._set_optimizer()
if not self.args.load_weights == None:
self.load_weights(self.args.load_weights)
# create target model
if self.target_update_frequency:
layers = self._create_layer()
self.target_model = Model(layers)
# Bug fix
for l in self.target_model.layers.layers:
l.parallelism = 'Disabled'
self.target_model.initialize(self.input_shape[:-1])
else:
self.target_model = self.model
self.callback = None
_logger.debug("%s" % self)
示例9: serialize_check
def serialize_check(conf_file, result, tol, res_string, **be_args):
experiment = deserialize(conf_file)
backend = gen_backend(model=experiment.model, **be_args)
experiment.initialize(backend)
res = experiment.run()
print float(res[res_string]['MisclassPercentage_TOP_1']), result,
assert abs(
float(res[res_string]['MisclassPercentage_TOP_1']) - result) < tol
示例10: speed_check
def speed_check(conf_file, num_epochs, **be_args):
experiment = deserialize(os.path.join(dir, conf_file))
experiment.model.num_epochs = num_epochs
backend = gen_backend(model=experiment.model, **be_args)
experiment.initialize(backend)
start = time.time()
experiment.run()
return (time.time() - start)
示例11: test_loader_exception_iter
def test_loader_exception_iter():
# NOTE: manifest needs to stay in scope until DataLoader has read it.
manifest = random_manifest(10, 2)
config = generic_config(manifest.name)
dl = DataLoader(config, gen_backend(backend='cpu'))
assert len(list(iter(dl))) == 4
示例12: run
def run():
model = create_model(nin=784)
backend = gen_backend(rng_seed=0)
dataset = MNIST(repo_path='~/data/')
experiment = FitPredictErrorExperiment(model=model,
backend=backend,
dataset=dataset)
experiment.run()
示例13: __init__
def __init__(self, batch_size, its, layer_def, W, I, gradO):
gen_backend(backend='gpu', batch_size=batch_size,
datatype=np.float32, device_id=0)
assert layer_def['iH'] == layer_def['iW']
assert layer_def['kH'] == layer_def['kW']
assert layer_def['dH'] == layer_def['dW']
assert layer_def['padH'] == layer_def['padW']
input_filters = layer_def['Ci']
output_filters = layer_def['Co']
image_size = layer_def['iW']
filter_size = layer_def['kH']
padding = layer_def['padH']
stride = layer_def['dH']
self.I = I
self.W = W
self.gradO = gradO
I_cuda = gpuarray.to_gpu(I)
gradO_cuda = gpuarray.to_gpu(gradO)
W_cuda = gpuarray.to_gpu(W)
conv = Convolution((filter_size, filter_size, output_filters), strides=stride, padding=padding, init=init)
conv.configure((input_filters, image_size, image_size))
conv.allocate()
# conv.allocate_deltas()
conv.W = W_cuda
self.conv = conv
deltas = np.zeros(I.shape, dtype=np.float32)
deltas_cuda = gpuarray.to_gpu(deltas)
conv.deltas = deltas_cuda
# self.O = O
# self.gradW = gradW
# self.gradI = gradI
self.I_cuda = I_cuda
self.O_cuda = conv.outputs
self.gradO_cuda = gradO_cuda
self.gradW_cuda = conv.dW
self.gradI_cuda = conv.deltas
示例14: compare_helper
def compare_helper(op, inA, inB, dtype):
numpy_result = math_helper(np, op, inA, inB, dtype=np.float32)
if np.dtype(dtype).kind == 'i' or np.dtype(dtype).kind == 'u':
numpy_result = np.around(numpy_result)
numpy_result = numpy_result.clip(np.iinfo(dtype).min, np.iinfo(dtype).max)
numpy_result = numpy_result.astype(dtype)
if dtype in (np.float32, np.float16):
gpu = gen_backend(backend='gpu', default_dtype=dtype)
nervanaGPU_result = math_helper(gpu, op, inA, inB, dtype=dtype)
nervanaGPU_result = nervanaGPU_result.get()
np.allclose(numpy_result, nervanaGPU_result, rtol=0, atol=1e-5)
cpu = gen_backend(backend='cpu', default_dtype=dtype)
nervanaCPU_result = math_helper(cpu, op, inA, inB, dtype=dtype)
nervanaCPU_result = nervanaCPU_result.get()
np.allclose(numpy_result, nervanaCPU_result, rtol=0, atol=1e-5)
示例15: test_loader_exception_next
def test_loader_exception_next():
# NOTE: manifest needs to stay in scope until DataLoader has read it.
manifest = random_manifest(10, 2)
config = generic_config(manifest.name)
dl = DataLoader(config, gen_backend(backend='cpu'))
dl.next()
with pytest.raises(LoaderRuntimeError):
dl.next()