本文整理汇总了Python中utils.allclose_with_out函数的典型用法代码示例。如果您正苦于以下问题:Python allclose_with_out函数的具体用法?Python allclose_with_out怎么用?Python allclose_with_out使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了allclose_with_out函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_wgan_cost
def test_wgan_cost(backend_default):
"""
Set up a Wasserstein GANCost transform and make sure cost and errors are getting
computed correctly.
"""
be = backend_default
cost = GANCost(func="wasserstein")
y_data = be.iobuf(5).fill(1.)
y_noise = be.iobuf(5).fill(2.)
output = be.iobuf(1)
expected = be.iobuf(1)
delta = be.iobuf(5)
# fprop for discriminator cost
output[:] = cost(y_data, y_noise)
expected[:] = be.sum(y_data - y_noise, axis=0)
tensors_allclose(output, expected)
# bprop for wasserstein cost
delta[:] = cost.bprop_data(y_data)
assert allclose_with_out(delta.get(), 1.)
delta[:] = cost.bprop_noise(y_noise)
assert allclose_with_out(delta.get(), -1.)
delta[:] = cost.bprop_generator(y_noise)
assert allclose_with_out(delta.get(), 1.)
示例2: test_modified_gan_cost
def test_modified_gan_cost(backend_default):
"""
Set up a modified GANCost transform and make sure cost and errors are getting
computed correctly.
"""
be = backend_default
cost = GANCost(cost_type="dis", func="modified")
y_data = be.iobuf(5).fill(1.)
y_noise = be.iobuf(5).fill(2.)
output = be.iobuf(1)
expected = be.iobuf(1)
delta = be.iobuf(5)
# fprop for discriminator cost
output[:] = cost(y_data, y_noise)
expected[:] = -be.sum(be.safelog(y_data) + be.safelog(1-y_noise), axis=0)
tensors_allclose(output, expected)
# bprop for modified cost
delta[:] = cost.bprop_data(y_data)
assert allclose_with_out(delta.get(), -1. / 1)
delta[:] = cost.bprop_noise(y_noise)
assert allclose_with_out(delta.get(), 1. - 2.)
delta[:] = cost.bprop_generator(y_noise)
assert allclose_with_out(delta.get(), -1. / 2.)
示例3: test_biSum
def test_biSum(backend_default, fargs, deltas_buffer):
seq_len, input_size, hidden_size, batch_size = fargs
input_size *= 2
in_shape = (input_size, seq_len)
NervanaObject.be.bsz = batch_size
bisum = BiSum()
bisum.configure(in_shape)
bisum.prev_layer = True
bisum.allocate()
bisum.allocate_deltas(deltas_buffer)
deltas_buffer.allocate_buffers()
bisum.set_deltas(deltas_buffer)
# inputs
inp_np = np.random.random((input_size, seq_len * batch_size))
inp_be = bisum.be.array(inp_np)
# outputs
out_be = bisum.fprop(inp_be)
del_be = bisum.bprop(out_be)
out_ref = bisum.be.empty_like(out_be)
out_ref[:] = inp_be[:input_size // 2] + inp_be[input_size // 2:]
assert out_be.shape[0] * 2 == inp_be.shape[0]
assert allclose_with_out(out_be.get(), out_ref.get(), rtol=0.0, atol=1.0e-5)
assert allclose_with_out(del_be[:input_size // 2].get(), out_be.get(), rtol=0.0, atol=1.0e-5)
assert allclose_with_out(del_be[input_size // 2:].get(), out_be.get(), rtol=0.0, atol=1.0e-5)
示例4: test_model_get_outputs_rnn
def test_model_get_outputs_rnn(backend_default, data):
dataset = PTB(50, path=data)
dataiter = dataset.train_iter
# weight initialization
init = Constant(0.08)
# model initialization
layers = [
Recurrent(150, init, activation=Logistic()),
Affine(len(dataiter.vocab), init, bias=init, activation=Rectlin())
]
model = Model(layers=layers)
output = model.get_outputs(dataiter)
assert output.shape == (dataiter.ndata, dataiter.seq_length, dataiter.nclass)
# since the init are all constant and model is un-trained:
# along the feature dim, the values should be all the same
assert allclose_with_out(output[0, 0], output[0, 0, 0], rtol=0, atol=1e-4)
assert allclose_with_out(output[0, 1], output[0, 1, 0], rtol=0, atol=1e-4)
# along the time dim, the values should be increasing:
assert np.alltrue(output[0, 2] > output[0, 1])
assert np.alltrue(output[0, 1] > output[0, 0])
示例5: compare_helper
def compare_helper(op, inA, inB, ng, nc, dtype):
numpy_result = math_helper(np, op, inA, inB, dtype=np.float32).astype(dtype)
nervanaGPU_result = math_helper(ng, op, inA, inB, dtype=dtype).get()
allclose_with_out(numpy_result, nervanaGPU_result, rtol=0, atol=1e-5)
nervanaCPU_result = math_helper(nc, op, inA, inB, dtype=dtype).get()
allclose_with_out(numpy_result, nervanaCPU_result, rtol=0, atol=1e-5)
示例6: test_bibn
def test_bibn(backend_default, fargs):
seq_len, input_size, hidden_size, batch_size = fargs
in_shape = (input_size, seq_len)
NervanaObject.be.bsz = batch_size
hidden_size = min(10, hidden_size)
# setup the bi-directional rnn
init_glorot = GlorotUniform()
birnn = BiBNRNN(hidden_size, activation=Rectlinclip(slope=0), init=init_glorot)
birnn.configure(in_shape)
birnn.prev_layer = True
birnn.allocate()
birnn.set_deltas([birnn.be.iobuf(birnn.in_shape)])
# test fprop
# set the ff buffer
inp_np = np.random.random(birnn.h_ff_buffer.shape)
inp_be = birnn.be.array(inp_np)
birnn.h_ff_buffer[:] = inp_np
# compare the bn output with calling the backend bn
xsum = birnn.be.zeros_like(birnn.xmean)
xvar = birnn.be.zeros_like(birnn.xvar)
gmean = birnn.be.zeros_like(birnn.gmean)
gvar = birnn.be.zeros_like(birnn.gvar)
gamma = birnn.be.ones(birnn.gamma.shape)
beta = birnn.be.zeros_like(birnn.beta)
grad_gamma = birnn.be.zeros_like(gamma)
grad_beta = birnn.be.zeros_like(beta)
out_ref = birnn.be.zeros_like(birnn.h_ff_buffer)
xsum[:] = birnn.be.sum(birnn.h_ff_buffer, axis=1)
birnn.be.compound_fprop_bn(
birnn.h_ff_buffer, xsum, xvar, gmean, gvar,
gamma, beta, out_ref, birnn.eps, birnn.rho,
accumbeta=0, relu=False)
# call the bibnrnn layer fprop_bn
out_bn = birnn._fprop_bn(birnn.h_ff_buffer, inference=False)
assert allclose_with_out(out_bn.get(), out_ref.get(), rtol=0.0, atol=1.0e-5)
# test bprop
err_np = np.random.random(birnn.h_ff_buffer.shape)
err_be = birnn.be.array(err_np)
err_out_ref = birnn.be.empty_like(err_be)
birnn.be.compound_bprop_bn(err_out_ref, grad_gamma, grad_beta,
err_be,
inp_be, xsum, xvar, gamma,
birnn.eps)
err_out_bn = birnn._bprop_bn(err_be, out_bn)
assert allclose_with_out(err_out_bn.get(), err_out_ref.get(), rtol=0.0, atol=2.5e-5)
示例7: test_all_rand
def test_all_rand(backend_default, allrand_args, deltas_buffer):
# test with random weights and random inputs
dtypeu = np.float32
w_rng, rngmax = allrand_args
inp_rng = [0.0, rngmax]
nin = 1024
nout = 2048
batch_size = 16
NervanaObject.be.bsz = batch_size
init_unif = Uniform(low=w_rng[0], high=w_rng[1])
layer = Linear(nout=nout, init=init_unif)
inp = np.random.random((nin, batch_size))
inp *= inp_rng[1] - inp_rng[0]
inp += inp_rng[0]
inp = inp.astype(dtypeu)
layer.configure(nin)
layer.prev_layer = True # Hack to force delta buffer allocation
layer.allocate()
layer.allocate_deltas(deltas_buffer)
deltas_buffer.allocate_buffers()
layer.set_deltas(deltas_buffer)
out = layer.fprop(layer.be.array(inp)).get()
w = layer.W.get()
# the expected output using numpy
out_exp = np.dot(w, inp)
# for larger layers need to estimate numerical precision
atol = 2 * est_mm_prec(w, inp, ntrials=1)
assert allclose_with_out(out_exp, out, atol=atol, rtol=0.0), \
'%e %e' % (np.max(np.abs(out - out_exp)), atol)
err = np.random.random((nout, batch_size))
err = err * (inp_rng[1] - inp_rng[0]) + inp_rng[0]
err = err.astype(dtypeu)
deltas = layer.bprop(layer.be.array(err)).get()
dw = layer.dW.get()
deltas_exp = np.dot(w.T, err)
atol = 2 * est_mm_prec(w.T, err, ntrials=1)
assert allclose_with_out(deltas_exp, deltas, atol=atol, rtol=0.0), \
'%e %e' % (np.max(np.abs(deltas_exp - deltas)), atol)
dw_exp = np.dot(err, inp.T)
atol = 2 * est_mm_prec(err, inp.T, ntrials=1)
assert allclose_with_out(dw_exp, dw, atol=atol, rtol=0.0), \
'%e %e' % (np.max(np.abs(dw_exp - dw)), atol)
return
示例8: test_recurrent_mean
def test_recurrent_mean(backend_default, refgruargs, deltas_buffer):
seq_len, nin, batch_size = refgruargs
NervanaObject.be.bsz = batch_size
in_shape = (nin, seq_len)
layer = RecurrentMean()
layer.configure(in_shape)
layer.prev_layer = True
layer.allocate()
layer.allocate_deltas(deltas_buffer)
deltas_buffer.allocate_buffers()
layer.set_deltas(deltas_buffer)
# zeros
inp = layer.be.zeros((nin, seq_len * batch_size))
out = layer.fprop(inp)
err = layer.bprop(out).get()
assert np.all(out.get() == np.zeros((nin, batch_size)))
assert np.all(err == inp.get())
# ones
inp = layer.be.ones((nin, seq_len * batch_size))
out = layer.fprop(inp)
err = layer.bprop(out).get()
assert np.all(out.get() == np.ones((nin, batch_size)))
assert np.all(err == 1. / seq_len * inp.get())
# random
rinp = np.random.random((nin, batch_size))
inp = np.repeat(rinp, repeats=seq_len, axis=1)
inp_g = layer.be.array(inp)
out = layer.fprop(inp_g)
err = layer.bprop(out)
assert allclose_with_out(out.get(), rinp)
assert allclose_with_out(err.get(), 1. / seq_len * inp)
# full random
inp = np.random.random((nin, seq_len * batch_size))
inp_g = layer.be.array(inp)
out = layer.fprop(inp_g)
err = layer.bprop(out)
out_comp = np.zeros(out.shape)
err_comp = np.zeros(inp.shape)
for i in range(seq_len):
out_comp[:] = out_comp + inp[:, i * batch_size:(i + 1) * batch_size]
err_comp[:, i * batch_size:(i + 1) * batch_size] = out.get() / float(seq_len)
out_comp[:] /= float(seq_len)
assert allclose_with_out(out_comp, out.get())
assert allclose_with_out(err_comp, err.get())
示例9: test_schedule
def test_schedule(backend_default):
"""
Test constant rate, fixed step and various modes of programmable steps.
"""
lr_init = 0.1
# default scheduler has a constant learning rate
sch = Schedule()
for epoch in range(10):
lr = sch.get_learning_rate(learning_rate=lr_init, epoch=epoch)
assert lr == lr_init
# test a uniform step schedule
step_config = 2
change = 0.5
sch = Schedule(step_config=step_config, change=change)
for epoch in range(10):
lr = sch.get_learning_rate(learning_rate=lr_init, epoch=epoch)
# test a repeated call for the same epoch
lr2 = sch.get_learning_rate(learning_rate=lr_init, epoch=epoch)
# print epoch, lr, lr2
assert allclose_with_out(lr, lr_init * change**(np.floor(epoch // step_config)))
assert allclose_with_out(lr2, lr_init * change**(np.floor(epoch // step_config)))
# test a list step schedule
sch = Schedule(step_config=[2, 3], change=.1)
assert allclose_with_out(.1, sch.get_learning_rate(learning_rate=.1, epoch=0))
assert allclose_with_out(.1, sch.get_learning_rate(learning_rate=.1, epoch=1))
assert allclose_with_out(.01, sch.get_learning_rate(learning_rate=.1, epoch=2))
# test a repeated call for the same epoch
assert allclose_with_out(.01, sch.get_learning_rate(learning_rate=.1, epoch=2))
assert allclose_with_out(.001, sch.get_learning_rate(learning_rate=.1, epoch=3))
assert allclose_with_out(.001, sch.get_learning_rate(learning_rate=.1, epoch=4))
示例10: test_biLSTM_fprop
def test_biLSTM_fprop(backend_default, fargs):
# basic sanity check with 0 weights random inputs
seq_len, input_size, hidden_size, batch_size = fargs
in_shape = (input_size, seq_len)
out_shape = (hidden_size, seq_len)
NervanaObject.be.bsz = batch_size
# setup the bi-directional rnn
init_glorot = GlorotUniform()
bilstm = BiLSTM(hidden_size, gate_activation=Logistic(), init=init_glorot,
activation=Tanh(), reset_cells=True)
bilstm.configure(in_shape)
bilstm.prev_layer = True
bilstm.allocate()
# same weight
nout = hidden_size
bilstm.W_input_b[:] = bilstm.W_input_f
bilstm.W_recur_b[:] = bilstm.W_recur_f
bilstm.b_b[:] = bilstm.b_f
bilstm.dW[:] = 0
# inputs - random and flipped left-to-right inputs
lr = np.random.random((input_size, seq_len * batch_size))
lr_rev = list(reversed(get_steps(lr.copy(), in_shape)))
rl = con(lr_rev, axis=1)
inp_lr = bilstm.be.array(lr)
inp_rl = bilstm.be.array(rl)
# outputs
out_lr = bilstm.fprop(inp_lr).get().copy()
bilstm.h_buffer[:] = 0
out_rl = bilstm.fprop(inp_rl).get().copy()
# views
out_lr_f_s = get_steps(out_lr[:nout], out_shape)
out_lr_b_s = get_steps(out_lr[nout:], out_shape)
out_rl_f_s = get_steps(out_rl[:nout], out_shape)
out_rl_b_s = get_steps(out_rl[nout:], out_shape)
# asserts
for x_f, x_b, y_f, y_b in zip(out_lr_f_s, out_lr_b_s,
reversed(out_rl_f_s), reversed(out_rl_b_s)):
assert allclose_with_out(x_f, y_b, rtol=0.0, atol=1.0e-5)
assert allclose_with_out(x_b, y_f, rtol=0.0, atol=1.0e-5)
示例11: test_recurrent_last
def test_recurrent_last(backend_default, refgruargs, deltas_buffer):
seq_len, nin, batch_size = refgruargs
NervanaObject.be.bsz = batch_size
in_shape = (nin, seq_len)
layer = RecurrentLast()
layer.configure(in_shape)
layer.prev_layer = True
layer.allocate()
layer.allocate_deltas(deltas_buffer)
deltas_buffer.allocate_buffers()
layer.set_deltas(deltas_buffer)
# zeros
inp = layer.be.zeros((nin, seq_len * batch_size))
out = layer.fprop(inp)
err = layer.bprop(out).get()
assert np.all(out.get() == np.zeros((nin, batch_size)))
assert np.all(err == inp.get())
# ones
inp = layer.be.ones((nin, seq_len * batch_size))
out = layer.fprop(inp)
err = layer.bprop(out).get()
assert np.all(out.get() == np.ones((nin, batch_size)))
assert np.all(err[:, -batch_size:] == inp.get()[:, -batch_size:])
assert np.all(
err[:, :-batch_size] == np.zeros((nin, (seq_len - 1) * batch_size)))
# random
rinp = np.random.random((nin, batch_size))
inp = np.repeat(rinp, repeats=seq_len, axis=1)
inp_g = layer.be.array(inp)
out = layer.fprop(inp_g)
err = layer.bprop(out)
assert allclose_with_out(out.get(), rinp)
assert allclose_with_out(err[:, -batch_size:].get(), rinp)
# full random
inp = np.random.random((nin, seq_len * batch_size))
inp_g = layer.be.array(inp)
out = layer.fprop(inp_g)
err = layer.bprop(out)
out_comp = np.zeros(out.shape)
err_comp = np.zeros(inp.shape)
out_comp[:] = inp[:, -batch_size:]
err_comp[:, -batch_size:] = out.get()
示例12: test_hdf5meansubtract
def test_hdf5meansubtract(backend_default, meansubhdf):
NervanaObject.be.bsz = 128
bsz = 128
datit = HDF5Iterator(meansubhdf[0])
datit.allocate()
typ = meansubhdf[1]
mn = datit.mean.get()
assert typ in ['chan_mean', 'full_mean']
cnt_image = 0
max_len = datit.ndata
MAX_CNT = max_len*datit.inp.shape[1]
for x, t in datit:
x_ = x.get().flatten()
x_exp = (np.arange(len(x_)) + cnt_image) % MAX_CNT
x_exp = x_exp.reshape((-1, np.prod(datit.lshape))).T
if typ == 'chan_mean':
x_exp = x_exp.reshape((datit.lshape[0], -1)) - mn
elif typ == 'full_mean':
x_exp = x_exp.reshape((-1, bsz)) - mn
x_exp = x_exp.flatten()
assert allclose_with_out(x_, x_exp, atol=0.0, rtol=1.0e-7)
cnt_image += len(x_)
datit.cleanup()
示例13: test_linear_ones
def test_linear_ones(backend_default, basic_linargs, deltas_buffer):
# basic sanity check with all ones on the inputs
# and weights, check that each row in output
# is the sum of the weights for that output
# this check will confirm that the correct number
# of operations is being run
nin, nout, batch_size = basic_linargs
NervanaObject.be.bsz = batch_size
dtypeu = np.float32
init_unif = Uniform(low=1.0, high=1.0)
layer = Linear(nout=nout, init=init_unif)
inp = layer.be.array(dtypeu(np.ones((nin, batch_size))))
layer.configure(nin)
layer.prev_layer = True # Hack to force delta buffer allocation
layer.allocate()
layer.allocate_deltas(deltas_buffer)
deltas_buffer.allocate_buffers()
layer.set_deltas(deltas_buffer)
out = layer.fprop(inp).get()
w = layer.W.get()
sums = np.sum(w, 1).reshape((nout, 1)) * np.ones((1, batch_size))
# for larger layers need to estimate numerical precision
# atol = est_mm_prec(w, inp.get())
assert allclose_with_out(sums, out, atol=0.0, rtol=0.0), \
'%e' % np.max(np.abs(out - sums))
return
示例14: test_padding
def test_padding(backend_default, poolargs):
fshape, nifm, padding, stride, in_sz, batch_size = poolargs
NervanaObject.be.bsz = batch_size
# basic sanity check with random inputs
inshape = (nifm, in_sz, in_sz)
insize = np.prod(inshape)
neon_layer = Pooling(fshape=fshape, strides=stride, padding=padding)
inp = neon_layer.be.array(np.random.random((insize, batch_size)))
inp.lshape = inshape
neon_layer.configure(inshape)
neon_layer.prev_layer = True
neon_layer.allocate()
neon_layer.set_deltas([neon_layer.be.iobuf(inshape)])
out = neon_layer.fprop(inp).get()
ncheck = [0, batch_size // 2, batch_size - 1]
(out_exp, check_inds) = ref_pooling(inp, inp.lshape,
(fshape, fshape),
padding,
(stride, stride),
neon_layer.be,
ncheck=ncheck)
out_shape = list(out_exp.shape[0:3])
out_shape.append(batch_size)
outa = out.reshape(out_shape)
assert allclose_with_out(out_exp, outa[:, :, :, check_inds], atol=0.0, rtol=0.0)
示例15: test_softmax_big_inputs
def test_softmax_big_inputs(backend_default):
np.random.seed(1)
be = backend_default
assert be.bsz >= 128, 'This tests needs large batch size'
act = Softmax()
Nout = 1000 # 1000 input and output units to softmax
# random inputs
x_ = np.random.random((Nout, be.bsz))
x = be.iobuf(Nout)
# init input to softmax
x[:] = x_
# numpy softmax
mx = np.max(x_, axis=0)
ex = np.exp(x_ - mx)
y_ = ex/np.sum(ex, axis=0)
# in-place softmax on device
x[:] = act(x)
assert allclose_with_out(y_, x.get(), atol=0.0, rtol=1.0e-5)