本文整理汇总了Python中mxnet.ndarray.zeros函数的典型用法代码示例。如果您正苦于以下问题:Python zeros函数的具体用法?Python zeros怎么用?Python zeros使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了zeros函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_boston_housing_DistilledSGLD
def run_boston_housing_DistilledSGLD():
X, Y, X_test, Y_test, X_mean, X_std, Y_mean, Y_std = load_boston_housing()
print(X.shape, Y.shape, X_test.shape, Y_test.shape)
minibatch_size = 1
teacher_noise_precision = 1.25
teacher_net = get_boston_housing_sym(True, teacher_noise_precision)
student_net = get_boston_housing_sym(False)
data_shape = (minibatch_size,) + X.shape[1::]
teacher_data_inputs = {'data': nd.zeros(data_shape, ctx=dev()),
'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev())}
student_data_inputs = {'data': nd.zeros(data_shape, ctx=dev())}
# 'softmax_label': nd.zeros((minibatch_size, 10), ctx=dev())}
teacher_initializer = BiasXavier(factor_type="in", magnitude=1)
student_initializer = BiasXavier(factor_type="in", magnitude=1)
student_grad_f = lambda student_outputs, teacher_pred: \
regression_student_grad(student_outputs, teacher_pred, teacher_noise_precision)
student_exe, student_params, _ = \
DistilledSGLD(teacher_sym=teacher_net, student_sym=student_net,
teacher_data_inputs=teacher_data_inputs,
student_data_inputs=student_data_inputs,
X=X, Y=Y, X_test=X_test, Y_test=Y_test,
X_mean=X_mean, X_std=X_std, Y_mean=Y_mean, Y_std=Y_std,
total_iter_num=5000000,
teacher_initializer=teacher_initializer,
student_initializer=student_initializer,
teacher_learning_rate=2E-7, student_learning_rate=1E-2,
student_optimizing_algorithm='sgd',
teacher_lr_scheduler=mx.lr_scheduler.FactorScheduler(80000, 0.5, 1E-7),
student_lr_scheduler=mx.lr_scheduler.FactorScheduler(step=5000, factor=0.8,
stop_factor_lr=1E-6),
student_grad_f=student_grad_f,
teacher_prior_precision=2.5, student_prior_precision=0.001,
perturb_deviation=0.05, minibatch_size=minibatch_size, task='boston',
dev=dev())
示例2: run_toy_DistilledSGLD
def run_toy_DistilledSGLD(gpu_id=None):
X, Y, X_test, Y_test = load_toy()
minibatch_size = 1
teacher_noise_precision = 1.0
teacher_net = get_toy_sym(True, teacher_noise_precision)
student_net = get_toy_sym(False)
data_shape = (minibatch_size,) + X.shape[1::]
teacher_data_inputs = {'data': nd.zeros(data_shape, ctx=dev(gpu_id)),
'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev(gpu_id))}
student_data_inputs = {'data': nd.zeros(data_shape, ctx=dev(gpu_id))}
teacher_initializer = mx.init.Uniform(0.07)
student_initializer = mx.init.Uniform(0.07)
student_grad_f = lambda student_outputs, teacher_pred: \
regression_student_grad(student_outputs, teacher_pred, teacher_noise_precision)
student_exe, student_params, _ = \
DistilledSGLD(teacher_sym=teacher_net, student_sym=student_net,
teacher_data_inputs=teacher_data_inputs,
student_data_inputs=student_data_inputs,
X=X, Y=Y, X_test=X_test, Y_test=Y_test, total_iter_num=80000,
teacher_initializer=teacher_initializer,
student_initializer=student_initializer,
teacher_learning_rate=1E-4, student_learning_rate=0.01,
# teacher_lr_scheduler=mx.lr_scheduler.FactorScheduler(100000, 0.5),
student_lr_scheduler=mx.lr_scheduler.FactorScheduler(8000, 0.8),
student_grad_f=student_grad_f,
teacher_prior_precision=0.1, student_prior_precision=0.001,
perturb_deviation=0.1, minibatch_size=minibatch_size, task='regression',
dev=dev(gpu_id))
示例3: _get_or_reshape
def _get_or_reshape(name, shared_data_arrays, arg_shape, arg_type, context, logger):
"""Internal helper to get a memory block or re-use by re-shaping"""
if name in shared_data_arrays:
arg_arr = shared_data_arrays[name]
if np.prod(arg_arr.shape) >= np.prod(arg_shape):
# nice, we can directly re-use this data blob
assert arg_arr.dtype == arg_type
arg_arr = arg_arr.reshape(arg_shape)
else:
logger.warning(('bucketing: data "%s" has a shape %s' % (name, arg_shape)) +
(', which is larger than already allocated ') +
('shape %s' % (arg_arr.shape,)) +
('. Need to re-allocate. Consider putting ') +
('default_bucket_key to') +
(' be the bucket taking the largest input for better ') +
('memory sharing.'))
arg_arr = nd.zeros(arg_shape, context, dtype=arg_type)
# replace existing shared array because the new one is bigger
shared_data_arrays[name] = arg_arr
else:
arg_arr = nd.zeros(arg_shape, context, dtype=arg_type)
shared_data_arrays[name] = arg_arr
return arg_arr
示例4: main
def main(args):
ctx = mx.gpu(args.gpu)
args.ctx_num = 1
prop = face_image.load_property(args.data)
image_size = prop.image_size
print('image_size', image_size)
vec = args.model.split(',')
prefix = vec[0]
epoch = int(vec[1])
print('loading',prefix, epoch)
sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch)
arg_params, aux_params = ch_dev(arg_params, aux_params, ctx)
all_layers = sym.get_internals()
sym = all_layers['fc1_output']
#model = mx.mod.Module.load(prefix, epoch, context = ctx)
model = mx.mod.Module(symbol=sym, context=ctx, label_names = None)
#model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))], label_shapes=[('softmax_label', (args.batch_size,))])
model.bind(data_shapes=[('data', (args.batch_size, 3, image_size[0], image_size[1]))])
model.set_params(arg_params, aux_params)
path_imgrec = os.path.join(args.data, 'train.rec')
path_imgidx = os.path.join(args.data, 'train.idx')
imgrec = mx.recordio.MXIndexedRecordIO(path_imgidx, path_imgrec, 'r') # pylint: disable=redefined-variable-type
s = imgrec.read_idx(0)
header, _ = mx.recordio.unpack(s)
assert header.flag>0
print('header0 label', header.label)
header0 = (int(header.label[0]), int(header.label[1]))
#assert(header.flag==1)
imgidx = range(1, int(header.label[0]))
stat = []
count = 0
data = nd.zeros( (1 ,3, image_size[0], image_size[1]) )
label = nd.zeros( (1,) )
for idx in imgidx:
if len(stat)%100==0:
print('processing', len(stat))
s = imgrec.read_idx(idx)
header, img = mx.recordio.unpack(s)
img = mx.image.imdecode(img)
img = nd.transpose(img, axes=(2, 0, 1))
data[0][:] = img
#input_blob = np.expand_dims(img.asnumpy(), axis=0)
#arg_params["data"] = mx.nd.array(input_blob, ctx)
#arg_params["softmax_label"] = mx.nd.empty((1,), ctx)
time_now = datetime.datetime.now()
#exe = sym.bind(ctx, arg_params ,args_grad=None, grad_req="null", aux_states=aux_params)
#exe.forward(is_train=False)
#_embedding = exe.outputs[0].asnumpy().flatten()
#db = mx.io.DataBatch(data=(data,), label=(label,))
db = mx.io.DataBatch(data=(data,))
model.forward(db, is_train=False)
net_out = model.get_outputs()[0].asnumpy()
time_now2 = datetime.datetime.now()
diff = time_now2 - time_now
stat.append(diff.total_seconds())
if len(stat)==args.param1:
break
stat = stat[10:]
print('avg infer time', np.mean(stat))
示例5: train
def train(input_variable, target_variable, encoder, decoder, teacher_forcing_ratio,
encoder_optimizer, decoder_optimizer, criterion, max_length, ctx):
with autograd.record():
loss = F.zeros((1,), ctx=ctx)
encoder_hidden = encoder.initHidden(ctx)
input_length = input_variable.shape[0]
target_length = target_variable.shape[0]
encoder_outputs, encoder_hidden = encoder(
input_variable.expand_dims(0), encoder_hidden)
if input_length < max_length:
encoder_outputs = F.concat(encoder_outputs.flatten(),
F.zeros((max_length - input_length, encoder.hidden_size), ctx=ctx), dim=0)
else:
encoder_outputs = encoder_outputs.flatten()
decoder_input = F.array([SOS_token], ctx=ctx)
decoder_hidden = encoder_hidden
use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False
if use_teacher_forcing:
# Teacher forcing: Feed the target as the next input
for di in range(target_length):
decoder_output, decoder_hidden, decoder_attention = decoder(
decoder_input, decoder_hidden, encoder_outputs)
loss = F.add(loss, criterion(decoder_output, target_variable[di]))
print criterion(decoder_output, target_variable[di])
decoder_input = target_variable[di] # Teacher forcing
else:
# Without teacher forcing: use its own predictions as the next input
for di in range(target_length):
decoder_output, decoder_hidden, decoder_attention = decoder(
decoder_input, decoder_hidden, encoder_outputs)
topi = decoder_output.argmax(axis=1)
decoder_input = F.array([topi.asscalar()], ctx=ctx)
loss = F.add(loss, criterion(decoder_output, target_variable[di]))
if topi.asscalar() == EOS_token:
break
loss.backward()
encoder_optimizer.step(1)
decoder_optimizer.step(1)
return loss.asscalar()/target_length
示例6: weights_init
def weights_init(layers):
for layer in layers:
classname = layer.__class__.__name__
if hasattr(layer, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
layer.weight.set_data(nd.random.normal(0.0,0.02,shape=layer.weight.data().shape))
if hasattr(layer, 'bias') and layer.bias is not None:
layer.bias.set_data(nd.zeros(layer.bias.data().shape))
elif classname.find('BatchNorm') != -1:
layer.gamma.set_data(nd.random.normal(1.0, 0.02,shape=layer.gamma.data().shape))
layer.beta.set_data(nd.zeros(layer.bias.data().shape))
示例7: get_params
def get_params():
W_xh = nd.random_normal(scale=std, shape=(input_dim, hidden_dim), ctx=ctx)
W_hh = nd.random_normal(scale=std, shape=(hidden_dim, hidden_dim), ctx=ctx)
b_h = nd.zeros(hidden_dim, ctx=ctx)
W_hy = nd.random_normal(scale=std, shape=(hidden_dim, output_dim), ctx=ctx)
b_y = nd.zeros(output_dim, ctx=ctx)
params = [W_xh, W_hh, b_h, W_hy, b_y]
for param in params:
param.attach_grad()
return params
示例8: run_toy_HMC
def run_toy_HMC(gpu_id=None):
X, Y, X_test, Y_test = load_toy()
minibatch_size = Y.shape[0]
noise_precision = 1 / 9.0
net = get_toy_sym(True, noise_precision)
data_shape = (minibatch_size,) + X.shape[1::]
data_inputs = {'data': nd.zeros(data_shape, ctx=dev(gpu_id)),
'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev(gpu_id))}
initializer = mx.init.Uniform(0.07)
sample_pool = HMC(net, data_inputs=data_inputs, X=X, Y=Y, X_test=X_test, Y_test=Y_test,
sample_num=300000, initializer=initializer, prior_precision=1.0,
learning_rate=1E-3, L=10, dev=dev(gpu_id))
示例9: get_parameters
def get_parameters():
W_xh = nd.random_normal(scale=config.std, shape=(config.input_dim, config.hidden_dim))
W_hh = nd.random_normal(scale=config.std, shape=(config.hidden_dim, config.hidden_dim))
b_h = nd.zeros(config.hidden_dim)
W_hy = nd.random_normal(scale=config.std, shape=(config.hidden_dim, config.output_dim))
b_y = nd.zeros(config.output_dim)
parameters = [W_xh, W_hh, b_h, W_hy, b_y]
for parameter in parameters:
parameter.attach_grad()
return parameters
示例10: run_mnist_SGD
def run_mnist_SGD(training_num=50000, gpu_id=None):
X, Y, X_test, Y_test = load_mnist(training_num)
minibatch_size = 100
net = get_mnist_sym()
data_shape = (minibatch_size,) + X.shape[1::]
data_inputs = {'data': nd.zeros(data_shape, ctx=dev(gpu_id)),
'softmax_label': nd.zeros((minibatch_size,), ctx=dev(gpu_id))}
initializer = mx.init.Xavier(factor_type="in", magnitude=2.34)
exe, exe_params, _ = SGD(sym=net, dev=dev(gpu_id), data_inputs=data_inputs, X=X, Y=Y,
X_test=X_test, Y_test=Y_test,
total_iter_num=1000000,
initializer=initializer,
lr=5E-6, prior_precision=1.0, minibatch_size=100)
示例11: reset_c2c
def reset_c2c(self):
self.select_triplets()
for identity,v in self.id2range.iteritems():
_list = range(*v)
for idx in _list:
s = imgrec.read_idx(idx)
ocontents.append(s)
embeddings = None
#print(len(ocontents))
ba = 0
while True:
bb = min(ba+args.batch_size, len(ocontents))
if ba>=bb:
break
_batch_size = bb-ba
_batch_size2 = max(_batch_size, args.ctx_num)
data = nd.zeros( (_batch_size2,3, image_size[0], image_size[1]) )
label = nd.zeros( (_batch_size2,) )
count = bb-ba
ii=0
for i in xrange(ba, bb):
header, img = mx.recordio.unpack(ocontents[i])
img = mx.image.imdecode(img)
img = nd.transpose(img, axes=(2, 0, 1))
data[ii][:] = img
label[ii][:] = header.label
ii+=1
while ii<_batch_size2:
data[ii][:] = data[0][:]
label[ii][:] = label[0][:]
ii+=1
db = mx.io.DataBatch(data=(data,), label=(label,))
self.mx_model.forward(db, is_train=False)
net_out = self.mx_model.get_outputs()
net_out = net_out[0].asnumpy()
model.forward(db, is_train=False)
net_out = model.get_outputs()
net_out = net_out[0].asnumpy()
if embeddings is None:
embeddings = np.zeros( (len(ocontents), net_out.shape[1]))
embeddings[ba:bb,:] = net_out[0:_batch_size,:]
ba = bb
embeddings = sklearn.preprocessing.normalize(embeddings)
embedding = np.mean(embeddings, axis=0, keepdims=True)
embedding = sklearn.preprocessing.normalize(embedding)
sims = np.dot(embeddings, embedding).flatten()
assert len(sims)==len(_list)
for i in xrange(len(_list)):
_idx = _list[i]
self.idx2cos[_idx] = sims[i]
示例12: run_mnist_SGLD
def run_mnist_SGLD(training_num=50000):
X, Y, X_test, Y_test = load_mnist(training_num)
minibatch_size = 100
net = get_mnist_sym()
data_shape = (minibatch_size,) + X.shape[1::]
data_inputs = {'data': nd.zeros(data_shape, ctx=dev()),
'softmax_label': nd.zeros((minibatch_size,), ctx=dev())}
initializer = mx.init.Xavier(factor_type="in", magnitude=2.34)
exe, sample_pool = SGLD(sym=net, dev=dev(), data_inputs=data_inputs, X=X, Y=Y,
X_test=X_test, Y_test=Y_test,
total_iter_num=1000000,
initializer=initializer,
learning_rate=4E-6, prior_precision=1.0, minibatch_size=100,
thin_interval=100, burn_in_iter_num=1000)
示例13: run_boston_housing_SGLD
def run_boston_housing_SGLD():
X, Y, X_test, Y_test = load_boston_housing()
minibatch_size = 1
teacher_noise_precision = 1.25
net = get_boston_housing_sym(True, teacher_noise_precision)
data_shape = (minibatch_size,) + X.shape[1::]
data_inputs = {'data': nd.zeros(data_shape, ctx=dev()),
'teacher_output_label': nd.zeros((minibatch_size, 1), ctx=dev())}
initializer = BiasXavier(factor_type="in", magnitude=2.34)
exe, sample_pool = SGLD(sym=net, dev=dev(), data_inputs=data_inputs, X=X, Y=Y,
X_test=X_test, Y_test=Y_test,
total_iter_num=1000000,
initializer=initializer,
learning_rate=5E-10, prior_precision=1.0, minibatch_size=minibatch_size,
thin_interval=100, burn_in_iter_num=1000, task='boston')
示例14: orthonormal_VanillaLSTMBuilder
def orthonormal_VanillaLSTMBuilder(lstm_layers, input_dims, lstm_hiddens, dropout_x=0., dropout_h=0., debug=False):
"""Build a standard LSTM cell, with variational dropout,
with weights initialized to be orthonormal (https://arxiv.org/abs/1312.6120)
Parameters
----------
lstm_layers : int
Currently only support one layer
input_dims : int
word vector dimensions
lstm_hiddens : int
hidden size
dropout_x : float
dropout on inputs, not used in this implementation, see `biLSTM` below
dropout_h : float
dropout on hidden states
debug : bool
set to True to skip orthonormal initialization
Returns
-------
lstm_cell : VariationalDropoutCell
A LSTM cell
"""
assert lstm_layers == 1, 'only accept one layer lstm'
W = orthonormal_initializer(lstm_hiddens, lstm_hiddens + input_dims, debug)
W_h, W_x = W[:, :lstm_hiddens], W[:, lstm_hiddens:]
b = nd.zeros((4 * lstm_hiddens,))
b[lstm_hiddens:2 * lstm_hiddens] = -1.0
lstm_cell = rnn.LSTMCell(input_size=input_dims, hidden_size=lstm_hiddens,
i2h_weight_initializer=mx.init.Constant(np.concatenate([W_x] * 4, 0)),
h2h_weight_initializer=mx.init.Constant(np.concatenate([W_h] * 4, 0)),
h2h_bias_initializer=mx.init.Constant(b))
wrapper = VariationalDropoutCell(lstm_cell, drop_states=dropout_h)
return wrapper
示例15: init_params
def init_params():
w = nd.random_normal(scale=1, shape=(num_inputs, 1))
b = nd.zeros(shape=(1,))
params = [w, b]
for param in params:
param.attach_grad()#自动求导 需要创建它们的梯度
return params