id int32 0 252k | repo stringlengths 7 55 | path stringlengths 4 127 | func_name stringlengths 1 88 | original_string stringlengths 75 19.8k | language stringclasses 1 value | code stringlengths 75 19.8k | code_tokens list | docstring stringlengths 3 17.3k | docstring_tokens list | sha stringlengths 40 40 | url stringlengths 87 242 |
|---|---|---|---|---|---|---|---|---|---|---|---|
27,000 | Microsoft/nni | examples/trials/kaggle-tgs-salt/predict.py | do_tta_predict | def do_tta_predict(args, model, ckp_path, tta_num=4):
'''
return 18000x128x128 np array
'''
model.eval()
preds = []
meta = None
# i is tta index, 0: no change, 1: horizon flip, 2: vertical flip, 3: do both
for flip_index in range(tta_num):
print('flip_index:', flip_index)
test_loader = get_test_loader(args.batch_size, index=flip_index, dev_mode=False, pad_mode=args.pad_mode)
meta = test_loader.meta
outputs = None
with torch.no_grad():
for i, img in enumerate(test_loader):
add_depth_channel(img, args.pad_mode)
img = img.cuda()
output, _ = model(img)
output = torch.sigmoid(output)
if outputs is None:
outputs = output.squeeze()
else:
outputs = torch.cat([outputs, output.squeeze()], 0)
print('{} / {}'.format(args.batch_size*(i+1), test_loader.num), end='\r')
outputs = outputs.cpu().numpy()
# flip back masks
if flip_index == 1:
outputs = np.flip(outputs, 2)
elif flip_index == 2:
outputs = np.flip(outputs, 1)
elif flip_index == 3:
outputs = np.flip(outputs, 2)
outputs = np.flip(outputs, 1)
#print(outputs.shape)
preds.append(outputs)
parent_dir = ckp_path+'_out'
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
np_file = os.path.join(parent_dir, 'pred.npy')
model_pred_result = np.mean(preds, 0)
np.save(np_file, model_pred_result)
return model_pred_result, meta | python | def do_tta_predict(args, model, ckp_path, tta_num=4):
'''
return 18000x128x128 np array
'''
model.eval()
preds = []
meta = None
# i is tta index, 0: no change, 1: horizon flip, 2: vertical flip, 3: do both
for flip_index in range(tta_num):
print('flip_index:', flip_index)
test_loader = get_test_loader(args.batch_size, index=flip_index, dev_mode=False, pad_mode=args.pad_mode)
meta = test_loader.meta
outputs = None
with torch.no_grad():
for i, img in enumerate(test_loader):
add_depth_channel(img, args.pad_mode)
img = img.cuda()
output, _ = model(img)
output = torch.sigmoid(output)
if outputs is None:
outputs = output.squeeze()
else:
outputs = torch.cat([outputs, output.squeeze()], 0)
print('{} / {}'.format(args.batch_size*(i+1), test_loader.num), end='\r')
outputs = outputs.cpu().numpy()
# flip back masks
if flip_index == 1:
outputs = np.flip(outputs, 2)
elif flip_index == 2:
outputs = np.flip(outputs, 1)
elif flip_index == 3:
outputs = np.flip(outputs, 2)
outputs = np.flip(outputs, 1)
#print(outputs.shape)
preds.append(outputs)
parent_dir = ckp_path+'_out'
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
np_file = os.path.join(parent_dir, 'pred.npy')
model_pred_result = np.mean(preds, 0)
np.save(np_file, model_pred_result)
return model_pred_result, meta | [
"def",
"do_tta_predict",
"(",
"args",
",",
"model",
",",
"ckp_path",
",",
"tta_num",
"=",
"4",
")",
":",
"model",
".",
"eval",
"(",
")",
"preds",
"=",
"[",
"]",
"meta",
"=",
"None",
"# i is tta index, 0: no change, 1: horizon flip, 2: vertical flip, 3: do both",
"for",
"flip_index",
"in",
"range",
"(",
"tta_num",
")",
":",
"print",
"(",
"'flip_index:'",
",",
"flip_index",
")",
"test_loader",
"=",
"get_test_loader",
"(",
"args",
".",
"batch_size",
",",
"index",
"=",
"flip_index",
",",
"dev_mode",
"=",
"False",
",",
"pad_mode",
"=",
"args",
".",
"pad_mode",
")",
"meta",
"=",
"test_loader",
".",
"meta",
"outputs",
"=",
"None",
"with",
"torch",
".",
"no_grad",
"(",
")",
":",
"for",
"i",
",",
"img",
"in",
"enumerate",
"(",
"test_loader",
")",
":",
"add_depth_channel",
"(",
"img",
",",
"args",
".",
"pad_mode",
")",
"img",
"=",
"img",
".",
"cuda",
"(",
")",
"output",
",",
"_",
"=",
"model",
"(",
"img",
")",
"output",
"=",
"torch",
".",
"sigmoid",
"(",
"output",
")",
"if",
"outputs",
"is",
"None",
":",
"outputs",
"=",
"output",
".",
"squeeze",
"(",
")",
"else",
":",
"outputs",
"=",
"torch",
".",
"cat",
"(",
"[",
"outputs",
",",
"output",
".",
"squeeze",
"(",
")",
"]",
",",
"0",
")",
"print",
"(",
"'{} / {}'",
".",
"format",
"(",
"args",
".",
"batch_size",
"*",
"(",
"i",
"+",
"1",
")",
",",
"test_loader",
".",
"num",
")",
",",
"end",
"=",
"'\\r'",
")",
"outputs",
"=",
"outputs",
".",
"cpu",
"(",
")",
".",
"numpy",
"(",
")",
"# flip back masks",
"if",
"flip_index",
"==",
"1",
":",
"outputs",
"=",
"np",
".",
"flip",
"(",
"outputs",
",",
"2",
")",
"elif",
"flip_index",
"==",
"2",
":",
"outputs",
"=",
"np",
".",
"flip",
"(",
"outputs",
",",
"1",
")",
"elif",
"flip_index",
"==",
"3",
":",
"outputs",
"=",
"np",
".",
"flip",
"(",
"outputs",
",",
"2",
")",
"outputs",
"=",
"np",
".",
"flip",
"(",
"outputs",
",",
"1",
")",
"#print(outputs.shape)",
"preds",
".",
"append",
"(",
"outputs",
")",
"parent_dir",
"=",
"ckp_path",
"+",
"'_out'",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"parent_dir",
")",
":",
"os",
".",
"makedirs",
"(",
"parent_dir",
")",
"np_file",
"=",
"os",
".",
"path",
".",
"join",
"(",
"parent_dir",
",",
"'pred.npy'",
")",
"model_pred_result",
"=",
"np",
".",
"mean",
"(",
"preds",
",",
"0",
")",
"np",
".",
"save",
"(",
"np_file",
",",
"model_pred_result",
")",
"return",
"model_pred_result",
",",
"meta"
] | return 18000x128x128 np array | [
"return",
"18000x128x128",
"np",
"array"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/kaggle-tgs-salt/predict.py#L37-L83 |
27,001 | Microsoft/nni | examples/trials/mnist-distributed-pytorch/dist_mnist.py | average_gradients | def average_gradients(model):
""" Gradient averaging. """
size = float(dist.get_world_size())
for param in model.parameters():
dist.all_reduce(param.grad.data, op=dist.reduce_op.SUM, group=0)
param.grad.data /= size | python | def average_gradients(model):
""" Gradient averaging. """
size = float(dist.get_world_size())
for param in model.parameters():
dist.all_reduce(param.grad.data, op=dist.reduce_op.SUM, group=0)
param.grad.data /= size | [
"def",
"average_gradients",
"(",
"model",
")",
":",
"size",
"=",
"float",
"(",
"dist",
".",
"get_world_size",
"(",
")",
")",
"for",
"param",
"in",
"model",
".",
"parameters",
"(",
")",
":",
"dist",
".",
"all_reduce",
"(",
"param",
".",
"grad",
".",
"data",
",",
"op",
"=",
"dist",
".",
"reduce_op",
".",
"SUM",
",",
"group",
"=",
"0",
")",
"param",
".",
"grad",
".",
"data",
"/=",
"size"
] | Gradient averaging. | [
"Gradient",
"averaging",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-distributed-pytorch/dist_mnist.py#L113-L118 |
27,002 | Microsoft/nni | examples/trials/mnist-distributed-pytorch/dist_mnist.py | run | def run(params):
""" Distributed Synchronous SGD Example """
rank = dist.get_rank()
torch.manual_seed(1234)
train_set, bsz = partition_dataset()
model = Net()
model = model
optimizer = optim.SGD(model.parameters(), lr=params['learning_rate'], momentum=params['momentum'])
num_batches = ceil(len(train_set.dataset) / float(bsz))
total_loss = 0.0
for epoch in range(3):
epoch_loss = 0.0
for data, target in train_set:
data, target = Variable(data), Variable(target)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
epoch_loss += loss.item()
loss.backward()
average_gradients(model)
optimizer.step()
#logger.debug('Rank: ', rank, ', epoch: ', epoch, ': ', epoch_loss / num_batches)
if rank == 0:
nni.report_intermediate_result(epoch_loss / num_batches)
total_loss += (epoch_loss / num_batches)
total_loss /= 3
logger.debug('Final loss: {}'.format(total_loss))
if rank == 0:
nni.report_final_result(total_loss) | python | def run(params):
""" Distributed Synchronous SGD Example """
rank = dist.get_rank()
torch.manual_seed(1234)
train_set, bsz = partition_dataset()
model = Net()
model = model
optimizer = optim.SGD(model.parameters(), lr=params['learning_rate'], momentum=params['momentum'])
num_batches = ceil(len(train_set.dataset) / float(bsz))
total_loss = 0.0
for epoch in range(3):
epoch_loss = 0.0
for data, target in train_set:
data, target = Variable(data), Variable(target)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
epoch_loss += loss.item()
loss.backward()
average_gradients(model)
optimizer.step()
#logger.debug('Rank: ', rank, ', epoch: ', epoch, ': ', epoch_loss / num_batches)
if rank == 0:
nni.report_intermediate_result(epoch_loss / num_batches)
total_loss += (epoch_loss / num_batches)
total_loss /= 3
logger.debug('Final loss: {}'.format(total_loss))
if rank == 0:
nni.report_final_result(total_loss) | [
"def",
"run",
"(",
"params",
")",
":",
"rank",
"=",
"dist",
".",
"get_rank",
"(",
")",
"torch",
".",
"manual_seed",
"(",
"1234",
")",
"train_set",
",",
"bsz",
"=",
"partition_dataset",
"(",
")",
"model",
"=",
"Net",
"(",
")",
"model",
"=",
"model",
"optimizer",
"=",
"optim",
".",
"SGD",
"(",
"model",
".",
"parameters",
"(",
")",
",",
"lr",
"=",
"params",
"[",
"'learning_rate'",
"]",
",",
"momentum",
"=",
"params",
"[",
"'momentum'",
"]",
")",
"num_batches",
"=",
"ceil",
"(",
"len",
"(",
"train_set",
".",
"dataset",
")",
"/",
"float",
"(",
"bsz",
")",
")",
"total_loss",
"=",
"0.0",
"for",
"epoch",
"in",
"range",
"(",
"3",
")",
":",
"epoch_loss",
"=",
"0.0",
"for",
"data",
",",
"target",
"in",
"train_set",
":",
"data",
",",
"target",
"=",
"Variable",
"(",
"data",
")",
",",
"Variable",
"(",
"target",
")",
"optimizer",
".",
"zero_grad",
"(",
")",
"output",
"=",
"model",
"(",
"data",
")",
"loss",
"=",
"F",
".",
"nll_loss",
"(",
"output",
",",
"target",
")",
"epoch_loss",
"+=",
"loss",
".",
"item",
"(",
")",
"loss",
".",
"backward",
"(",
")",
"average_gradients",
"(",
"model",
")",
"optimizer",
".",
"step",
"(",
")",
"#logger.debug('Rank: ', rank, ', epoch: ', epoch, ': ', epoch_loss / num_batches)",
"if",
"rank",
"==",
"0",
":",
"nni",
".",
"report_intermediate_result",
"(",
"epoch_loss",
"/",
"num_batches",
")",
"total_loss",
"+=",
"(",
"epoch_loss",
"/",
"num_batches",
")",
"total_loss",
"/=",
"3",
"logger",
".",
"debug",
"(",
"'Final loss: {}'",
".",
"format",
"(",
"total_loss",
")",
")",
"if",
"rank",
"==",
"0",
":",
"nni",
".",
"report_final_result",
"(",
"total_loss",
")"
] | Distributed Synchronous SGD Example | [
"Distributed",
"Synchronous",
"SGD",
"Example"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-distributed-pytorch/dist_mnist.py#L121-L150 |
27,003 | Microsoft/nni | examples/trials/ga_squad/graph.py | Layer.set_size | def set_size(self, graph_id, size):
'''
Set size.
'''
if self.graph_type == LayerType.attention.value:
if self.input[0] == graph_id:
self.size = size
if self.graph_type == LayerType.rnn.value:
self.size = size
if self.graph_type == LayerType.self_attention.value:
self.size = size
if self.graph_type == LayerType.output.value:
if self.size != size:
return False
return True | python | def set_size(self, graph_id, size):
'''
Set size.
'''
if self.graph_type == LayerType.attention.value:
if self.input[0] == graph_id:
self.size = size
if self.graph_type == LayerType.rnn.value:
self.size = size
if self.graph_type == LayerType.self_attention.value:
self.size = size
if self.graph_type == LayerType.output.value:
if self.size != size:
return False
return True | [
"def",
"set_size",
"(",
"self",
",",
"graph_id",
",",
"size",
")",
":",
"if",
"self",
".",
"graph_type",
"==",
"LayerType",
".",
"attention",
".",
"value",
":",
"if",
"self",
".",
"input",
"[",
"0",
"]",
"==",
"graph_id",
":",
"self",
".",
"size",
"=",
"size",
"if",
"self",
".",
"graph_type",
"==",
"LayerType",
".",
"rnn",
".",
"value",
":",
"self",
".",
"size",
"=",
"size",
"if",
"self",
".",
"graph_type",
"==",
"LayerType",
".",
"self_attention",
".",
"value",
":",
"self",
".",
"size",
"=",
"size",
"if",
"self",
".",
"graph_type",
"==",
"LayerType",
".",
"output",
".",
"value",
":",
"if",
"self",
".",
"size",
"!=",
"size",
":",
"return",
"False",
"return",
"True"
] | Set size. | [
"Set",
"size",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/graph.py#L69-L83 |
27,004 | Microsoft/nni | examples/trials/ga_squad/graph.py | Graph.is_topology | def is_topology(self, layers=None):
'''
valid the topology
'''
if layers is None:
layers = self.layers
layers_nodle = []
result = []
for i, layer in enumerate(layers):
if layer.is_delete is False:
layers_nodle.append(i)
while True:
flag_break = True
layers_toremove = []
for layer1 in layers_nodle:
flag_arrive = True
for layer2 in layers[layer1].input:
if layer2 in layers_nodle:
flag_arrive = False
if flag_arrive is True:
for layer2 in layers[layer1].output:
# Size is error
if layers[layer2].set_size(layer1, layers[layer1].size) is False:
return False
layers_toremove.append(layer1)
result.append(layer1)
flag_break = False
for layer in layers_toremove:
layers_nodle.remove(layer)
result.append('|')
if flag_break:
break
# There is loop in graph || some layers can't to arrive
if layers_nodle:
return False
return result | python | def is_topology(self, layers=None):
'''
valid the topology
'''
if layers is None:
layers = self.layers
layers_nodle = []
result = []
for i, layer in enumerate(layers):
if layer.is_delete is False:
layers_nodle.append(i)
while True:
flag_break = True
layers_toremove = []
for layer1 in layers_nodle:
flag_arrive = True
for layer2 in layers[layer1].input:
if layer2 in layers_nodle:
flag_arrive = False
if flag_arrive is True:
for layer2 in layers[layer1].output:
# Size is error
if layers[layer2].set_size(layer1, layers[layer1].size) is False:
return False
layers_toremove.append(layer1)
result.append(layer1)
flag_break = False
for layer in layers_toremove:
layers_nodle.remove(layer)
result.append('|')
if flag_break:
break
# There is loop in graph || some layers can't to arrive
if layers_nodle:
return False
return result | [
"def",
"is_topology",
"(",
"self",
",",
"layers",
"=",
"None",
")",
":",
"if",
"layers",
"is",
"None",
":",
"layers",
"=",
"self",
".",
"layers",
"layers_nodle",
"=",
"[",
"]",
"result",
"=",
"[",
"]",
"for",
"i",
",",
"layer",
"in",
"enumerate",
"(",
"layers",
")",
":",
"if",
"layer",
".",
"is_delete",
"is",
"False",
":",
"layers_nodle",
".",
"append",
"(",
"i",
")",
"while",
"True",
":",
"flag_break",
"=",
"True",
"layers_toremove",
"=",
"[",
"]",
"for",
"layer1",
"in",
"layers_nodle",
":",
"flag_arrive",
"=",
"True",
"for",
"layer2",
"in",
"layers",
"[",
"layer1",
"]",
".",
"input",
":",
"if",
"layer2",
"in",
"layers_nodle",
":",
"flag_arrive",
"=",
"False",
"if",
"flag_arrive",
"is",
"True",
":",
"for",
"layer2",
"in",
"layers",
"[",
"layer1",
"]",
".",
"output",
":",
"# Size is error",
"if",
"layers",
"[",
"layer2",
"]",
".",
"set_size",
"(",
"layer1",
",",
"layers",
"[",
"layer1",
"]",
".",
"size",
")",
"is",
"False",
":",
"return",
"False",
"layers_toremove",
".",
"append",
"(",
"layer1",
")",
"result",
".",
"append",
"(",
"layer1",
")",
"flag_break",
"=",
"False",
"for",
"layer",
"in",
"layers_toremove",
":",
"layers_nodle",
".",
"remove",
"(",
"layer",
")",
"result",
".",
"append",
"(",
"'|'",
")",
"if",
"flag_break",
":",
"break",
"# There is loop in graph || some layers can't to arrive",
"if",
"layers_nodle",
":",
"return",
"False",
"return",
"result"
] | valid the topology | [
"valid",
"the",
"topology"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/graph.py#L133-L168 |
27,005 | Microsoft/nni | examples/trials/ga_squad/graph.py | Graph.is_legal | def is_legal(self, layers=None):
'''
Judge whether is legal for layers
'''
if layers is None:
layers = self.layers
for layer in layers:
if layer.is_delete is False:
if len(layer.input) != layer.input_size:
return False
if len(layer.output) < layer.output_size:
return False
# layer_num <= max_layer_num
if self.layer_num(layers) > self.max_layer_num:
return False
# There is loop in graph || some layers can't to arrive
if self.is_topology(layers) is False:
return False
return True | python | def is_legal(self, layers=None):
'''
Judge whether is legal for layers
'''
if layers is None:
layers = self.layers
for layer in layers:
if layer.is_delete is False:
if len(layer.input) != layer.input_size:
return False
if len(layer.output) < layer.output_size:
return False
# layer_num <= max_layer_num
if self.layer_num(layers) > self.max_layer_num:
return False
# There is loop in graph || some layers can't to arrive
if self.is_topology(layers) is False:
return False
return True | [
"def",
"is_legal",
"(",
"self",
",",
"layers",
"=",
"None",
")",
":",
"if",
"layers",
"is",
"None",
":",
"layers",
"=",
"self",
".",
"layers",
"for",
"layer",
"in",
"layers",
":",
"if",
"layer",
".",
"is_delete",
"is",
"False",
":",
"if",
"len",
"(",
"layer",
".",
"input",
")",
"!=",
"layer",
".",
"input_size",
":",
"return",
"False",
"if",
"len",
"(",
"layer",
".",
"output",
")",
"<",
"layer",
".",
"output_size",
":",
"return",
"False",
"# layer_num <= max_layer_num",
"if",
"self",
".",
"layer_num",
"(",
"layers",
")",
">",
"self",
".",
"max_layer_num",
":",
"return",
"False",
"# There is loop in graph || some layers can't to arrive",
"if",
"self",
".",
"is_topology",
"(",
"layers",
")",
"is",
"False",
":",
"return",
"False",
"return",
"True"
] | Judge whether is legal for layers | [
"Judge",
"whether",
"is",
"legal",
"for",
"layers"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/graph.py#L183-L205 |
27,006 | Microsoft/nni | examples/trials/kaggle-tgs-salt/lovasz_losses.py | lovasz_grad | def lovasz_grad(gt_sorted):
"""
Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper
"""
p = len(gt_sorted)
gts = gt_sorted.sum()
intersection = gts - gt_sorted.float().cumsum(0)
union = gts + (1 - gt_sorted).float().cumsum(0)
jaccard = 1. - intersection / union
if p > 1: # cover 1-pixel case
jaccard[1:p] = jaccard[1:p] - jaccard[0:-1]
return jaccard | python | def lovasz_grad(gt_sorted):
"""
Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper
"""
p = len(gt_sorted)
gts = gt_sorted.sum()
intersection = gts - gt_sorted.float().cumsum(0)
union = gts + (1 - gt_sorted).float().cumsum(0)
jaccard = 1. - intersection / union
if p > 1: # cover 1-pixel case
jaccard[1:p] = jaccard[1:p] - jaccard[0:-1]
return jaccard | [
"def",
"lovasz_grad",
"(",
"gt_sorted",
")",
":",
"p",
"=",
"len",
"(",
"gt_sorted",
")",
"gts",
"=",
"gt_sorted",
".",
"sum",
"(",
")",
"intersection",
"=",
"gts",
"-",
"gt_sorted",
".",
"float",
"(",
")",
".",
"cumsum",
"(",
"0",
")",
"union",
"=",
"gts",
"+",
"(",
"1",
"-",
"gt_sorted",
")",
".",
"float",
"(",
")",
".",
"cumsum",
"(",
"0",
")",
"jaccard",
"=",
"1.",
"-",
"intersection",
"/",
"union",
"if",
"p",
">",
"1",
":",
"# cover 1-pixel case",
"jaccard",
"[",
"1",
":",
"p",
"]",
"=",
"jaccard",
"[",
"1",
":",
"p",
"]",
"-",
"jaccard",
"[",
"0",
":",
"-",
"1",
"]",
"return",
"jaccard"
] | Computes gradient of the Lovasz extension w.r.t sorted errors
See Alg. 1 in paper | [
"Computes",
"gradient",
"of",
"the",
"Lovasz",
"extension",
"w",
".",
"r",
".",
"t",
"sorted",
"errors",
"See",
"Alg",
".",
"1",
"in",
"paper"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/kaggle-tgs-salt/lovasz_losses.py#L36-L48 |
27,007 | Microsoft/nni | examples/trials/kaggle-tgs-salt/lovasz_losses.py | flatten_probas | def flatten_probas(probas, labels, ignore=None):
"""
Flattens predictions in the batch
"""
B, C, H, W = probas.size()
probas = probas.permute(0, 2, 3, 1).contiguous().view(-1, C) # B * H * W, C = P, C
labels = labels.view(-1)
if ignore is None:
return probas, labels
valid = (labels != ignore)
vprobas = probas[valid.nonzero().squeeze()]
vlabels = labels[valid]
return vprobas, vlabels | python | def flatten_probas(probas, labels, ignore=None):
"""
Flattens predictions in the batch
"""
B, C, H, W = probas.size()
probas = probas.permute(0, 2, 3, 1).contiguous().view(-1, C) # B * H * W, C = P, C
labels = labels.view(-1)
if ignore is None:
return probas, labels
valid = (labels != ignore)
vprobas = probas[valid.nonzero().squeeze()]
vlabels = labels[valid]
return vprobas, vlabels | [
"def",
"flatten_probas",
"(",
"probas",
",",
"labels",
",",
"ignore",
"=",
"None",
")",
":",
"B",
",",
"C",
",",
"H",
",",
"W",
"=",
"probas",
".",
"size",
"(",
")",
"probas",
"=",
"probas",
".",
"permute",
"(",
"0",
",",
"2",
",",
"3",
",",
"1",
")",
".",
"contiguous",
"(",
")",
".",
"view",
"(",
"-",
"1",
",",
"C",
")",
"# B * H * W, C = P, C",
"labels",
"=",
"labels",
".",
"view",
"(",
"-",
"1",
")",
"if",
"ignore",
"is",
"None",
":",
"return",
"probas",
",",
"labels",
"valid",
"=",
"(",
"labels",
"!=",
"ignore",
")",
"vprobas",
"=",
"probas",
"[",
"valid",
".",
"nonzero",
"(",
")",
".",
"squeeze",
"(",
")",
"]",
"vlabels",
"=",
"labels",
"[",
"valid",
"]",
"return",
"vprobas",
",",
"vlabels"
] | Flattens predictions in the batch | [
"Flattens",
"predictions",
"in",
"the",
"batch"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/kaggle-tgs-salt/lovasz_losses.py#L211-L223 |
27,008 | Microsoft/nni | examples/trials/kaggle-tgs-salt/lovasz_losses.py | xloss | def xloss(logits, labels, ignore=None):
"""
Cross entropy loss
"""
return F.cross_entropy(logits, Variable(labels), ignore_index=255) | python | def xloss(logits, labels, ignore=None):
"""
Cross entropy loss
"""
return F.cross_entropy(logits, Variable(labels), ignore_index=255) | [
"def",
"xloss",
"(",
"logits",
",",
"labels",
",",
"ignore",
"=",
"None",
")",
":",
"return",
"F",
".",
"cross_entropy",
"(",
"logits",
",",
"Variable",
"(",
"labels",
")",
",",
"ignore_index",
"=",
"255",
")"
] | Cross entropy loss | [
"Cross",
"entropy",
"loss"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/kaggle-tgs-salt/lovasz_losses.py#L225-L229 |
27,009 | Microsoft/nni | examples/trials/kaggle-tgs-salt/lovasz_losses.py | mean | def mean(l, ignore_nan=False, empty=0):
"""
nanmean compatible with generators.
"""
l = iter(l)
if ignore_nan:
l = ifilterfalse(np.isnan, l)
try:
n = 1
acc = next(l)
except StopIteration:
if empty == 'raise':
raise ValueError('Empty mean')
return empty
for n, v in enumerate(l, 2):
acc += v
if n == 1:
return acc
return acc / n | python | def mean(l, ignore_nan=False, empty=0):
"""
nanmean compatible with generators.
"""
l = iter(l)
if ignore_nan:
l = ifilterfalse(np.isnan, l)
try:
n = 1
acc = next(l)
except StopIteration:
if empty == 'raise':
raise ValueError('Empty mean')
return empty
for n, v in enumerate(l, 2):
acc += v
if n == 1:
return acc
return acc / n | [
"def",
"mean",
"(",
"l",
",",
"ignore_nan",
"=",
"False",
",",
"empty",
"=",
"0",
")",
":",
"l",
"=",
"iter",
"(",
"l",
")",
"if",
"ignore_nan",
":",
"l",
"=",
"ifilterfalse",
"(",
"np",
".",
"isnan",
",",
"l",
")",
"try",
":",
"n",
"=",
"1",
"acc",
"=",
"next",
"(",
"l",
")",
"except",
"StopIteration",
":",
"if",
"empty",
"==",
"'raise'",
":",
"raise",
"ValueError",
"(",
"'Empty mean'",
")",
"return",
"empty",
"for",
"n",
",",
"v",
"in",
"enumerate",
"(",
"l",
",",
"2",
")",
":",
"acc",
"+=",
"v",
"if",
"n",
"==",
"1",
":",
"return",
"acc",
"return",
"acc",
"/",
"n"
] | nanmean compatible with generators. | [
"nanmean",
"compatible",
"with",
"generators",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/kaggle-tgs-salt/lovasz_losses.py#L234-L252 |
27,010 | Microsoft/nni | tools/nni_trial_tool/trial_keeper.py | main_loop | def main_loop(args):
'''main loop logic for trial keeper'''
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
stdout_file = open(STDOUT_FULL_PATH, 'a+')
stderr_file = open(STDERR_FULL_PATH, 'a+')
trial_keeper_syslogger = RemoteLogger(args.nnimanager_ip, args.nnimanager_port, 'trial_keeper', StdOutputType.Stdout, args.log_collection)
# redirect trial keeper's stdout and stderr to syslog
trial_syslogger_stdout = RemoteLogger(args.nnimanager_ip, args.nnimanager_port, 'trial', StdOutputType.Stdout, args.log_collection)
sys.stdout = sys.stderr = trial_keeper_syslogger
# backward compatibility
hdfs_host = None
hdfs_output_dir = None
if args.hdfs_host:
hdfs_host = args.hdfs_host
elif args.pai_hdfs_host:
hdfs_host = args.pai_hdfs_host
if args.hdfs_output_dir:
hdfs_output_dir = args.hdfs_output_dir
elif args.pai_hdfs_output_dir:
hdfs_output_dir = args.pai_hdfs_output_dir
if hdfs_host is not None and args.nni_hdfs_exp_dir is not None:
try:
if args.webhdfs_path:
hdfs_client = HdfsClient(hosts='{0}:80'.format(hdfs_host), user_name=args.pai_user_name, webhdfs_path=args.webhdfs_path, timeout=5)
else:
# backward compatibility
hdfs_client = HdfsClient(hosts='{0}:{1}'.format(hdfs_host, '50070'), user_name=args.pai_user_name, timeout=5)
except Exception as e:
nni_log(LogType.Error, 'Create HDFS client error: ' + str(e))
raise e
copyHdfsDirectoryToLocal(args.nni_hdfs_exp_dir, os.getcwd(), hdfs_client)
# Notice: We don't appoint env, which means subprocess wil inherit current environment and that is expected behavior
log_pipe_stdout = trial_syslogger_stdout.get_pipelog_reader()
process = Popen(args.trial_command, shell = True, stdout = log_pipe_stdout, stderr = log_pipe_stdout)
nni_log(LogType.Info, 'Trial keeper spawns a subprocess (pid {0}) to run command: {1}'.format(process.pid, shlex.split(args.trial_command)))
while True:
retCode = process.poll()
# child worker process exits and all stdout data is read
if retCode is not None and log_pipe_stdout.set_process_exit() and log_pipe_stdout.is_read_completed == True:
nni_log(LogType.Info, 'subprocess terminated. Exit code is {}. Quit'.format(retCode))
if hdfs_output_dir is not None:
# Copy local directory to hdfs for OpenPAI
nni_local_output_dir = os.environ['NNI_OUTPUT_DIR']
try:
if copyDirectoryToHdfs(nni_local_output_dir, hdfs_output_dir, hdfs_client):
nni_log(LogType.Info, 'copy directory from {0} to {1} success!'.format(nni_local_output_dir, hdfs_output_dir))
else:
nni_log(LogType.Info, 'copy directory from {0} to {1} failed!'.format(nni_local_output_dir, hdfs_output_dir))
except Exception as e:
nni_log(LogType.Error, 'HDFS copy directory got exception: ' + str(e))
raise e
## Exit as the retCode of subprocess(trial)
exit(retCode)
break
time.sleep(2) | python | def main_loop(args):
'''main loop logic for trial keeper'''
if not os.path.exists(LOG_DIR):
os.makedirs(LOG_DIR)
stdout_file = open(STDOUT_FULL_PATH, 'a+')
stderr_file = open(STDERR_FULL_PATH, 'a+')
trial_keeper_syslogger = RemoteLogger(args.nnimanager_ip, args.nnimanager_port, 'trial_keeper', StdOutputType.Stdout, args.log_collection)
# redirect trial keeper's stdout and stderr to syslog
trial_syslogger_stdout = RemoteLogger(args.nnimanager_ip, args.nnimanager_port, 'trial', StdOutputType.Stdout, args.log_collection)
sys.stdout = sys.stderr = trial_keeper_syslogger
# backward compatibility
hdfs_host = None
hdfs_output_dir = None
if args.hdfs_host:
hdfs_host = args.hdfs_host
elif args.pai_hdfs_host:
hdfs_host = args.pai_hdfs_host
if args.hdfs_output_dir:
hdfs_output_dir = args.hdfs_output_dir
elif args.pai_hdfs_output_dir:
hdfs_output_dir = args.pai_hdfs_output_dir
if hdfs_host is not None and args.nni_hdfs_exp_dir is not None:
try:
if args.webhdfs_path:
hdfs_client = HdfsClient(hosts='{0}:80'.format(hdfs_host), user_name=args.pai_user_name, webhdfs_path=args.webhdfs_path, timeout=5)
else:
# backward compatibility
hdfs_client = HdfsClient(hosts='{0}:{1}'.format(hdfs_host, '50070'), user_name=args.pai_user_name, timeout=5)
except Exception as e:
nni_log(LogType.Error, 'Create HDFS client error: ' + str(e))
raise e
copyHdfsDirectoryToLocal(args.nni_hdfs_exp_dir, os.getcwd(), hdfs_client)
# Notice: We don't appoint env, which means subprocess wil inherit current environment and that is expected behavior
log_pipe_stdout = trial_syslogger_stdout.get_pipelog_reader()
process = Popen(args.trial_command, shell = True, stdout = log_pipe_stdout, stderr = log_pipe_stdout)
nni_log(LogType.Info, 'Trial keeper spawns a subprocess (pid {0}) to run command: {1}'.format(process.pid, shlex.split(args.trial_command)))
while True:
retCode = process.poll()
# child worker process exits and all stdout data is read
if retCode is not None and log_pipe_stdout.set_process_exit() and log_pipe_stdout.is_read_completed == True:
nni_log(LogType.Info, 'subprocess terminated. Exit code is {}. Quit'.format(retCode))
if hdfs_output_dir is not None:
# Copy local directory to hdfs for OpenPAI
nni_local_output_dir = os.environ['NNI_OUTPUT_DIR']
try:
if copyDirectoryToHdfs(nni_local_output_dir, hdfs_output_dir, hdfs_client):
nni_log(LogType.Info, 'copy directory from {0} to {1} success!'.format(nni_local_output_dir, hdfs_output_dir))
else:
nni_log(LogType.Info, 'copy directory from {0} to {1} failed!'.format(nni_local_output_dir, hdfs_output_dir))
except Exception as e:
nni_log(LogType.Error, 'HDFS copy directory got exception: ' + str(e))
raise e
## Exit as the retCode of subprocess(trial)
exit(retCode)
break
time.sleep(2) | [
"def",
"main_loop",
"(",
"args",
")",
":",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"LOG_DIR",
")",
":",
"os",
".",
"makedirs",
"(",
"LOG_DIR",
")",
"stdout_file",
"=",
"open",
"(",
"STDOUT_FULL_PATH",
",",
"'a+'",
")",
"stderr_file",
"=",
"open",
"(",
"STDERR_FULL_PATH",
",",
"'a+'",
")",
"trial_keeper_syslogger",
"=",
"RemoteLogger",
"(",
"args",
".",
"nnimanager_ip",
",",
"args",
".",
"nnimanager_port",
",",
"'trial_keeper'",
",",
"StdOutputType",
".",
"Stdout",
",",
"args",
".",
"log_collection",
")",
"# redirect trial keeper's stdout and stderr to syslog",
"trial_syslogger_stdout",
"=",
"RemoteLogger",
"(",
"args",
".",
"nnimanager_ip",
",",
"args",
".",
"nnimanager_port",
",",
"'trial'",
",",
"StdOutputType",
".",
"Stdout",
",",
"args",
".",
"log_collection",
")",
"sys",
".",
"stdout",
"=",
"sys",
".",
"stderr",
"=",
"trial_keeper_syslogger",
"# backward compatibility",
"hdfs_host",
"=",
"None",
"hdfs_output_dir",
"=",
"None",
"if",
"args",
".",
"hdfs_host",
":",
"hdfs_host",
"=",
"args",
".",
"hdfs_host",
"elif",
"args",
".",
"pai_hdfs_host",
":",
"hdfs_host",
"=",
"args",
".",
"pai_hdfs_host",
"if",
"args",
".",
"hdfs_output_dir",
":",
"hdfs_output_dir",
"=",
"args",
".",
"hdfs_output_dir",
"elif",
"args",
".",
"pai_hdfs_output_dir",
":",
"hdfs_output_dir",
"=",
"args",
".",
"pai_hdfs_output_dir",
"if",
"hdfs_host",
"is",
"not",
"None",
"and",
"args",
".",
"nni_hdfs_exp_dir",
"is",
"not",
"None",
":",
"try",
":",
"if",
"args",
".",
"webhdfs_path",
":",
"hdfs_client",
"=",
"HdfsClient",
"(",
"hosts",
"=",
"'{0}:80'",
".",
"format",
"(",
"hdfs_host",
")",
",",
"user_name",
"=",
"args",
".",
"pai_user_name",
",",
"webhdfs_path",
"=",
"args",
".",
"webhdfs_path",
",",
"timeout",
"=",
"5",
")",
"else",
":",
"# backward compatibility",
"hdfs_client",
"=",
"HdfsClient",
"(",
"hosts",
"=",
"'{0}:{1}'",
".",
"format",
"(",
"hdfs_host",
",",
"'50070'",
")",
",",
"user_name",
"=",
"args",
".",
"pai_user_name",
",",
"timeout",
"=",
"5",
")",
"except",
"Exception",
"as",
"e",
":",
"nni_log",
"(",
"LogType",
".",
"Error",
",",
"'Create HDFS client error: '",
"+",
"str",
"(",
"e",
")",
")",
"raise",
"e",
"copyHdfsDirectoryToLocal",
"(",
"args",
".",
"nni_hdfs_exp_dir",
",",
"os",
".",
"getcwd",
"(",
")",
",",
"hdfs_client",
")",
"# Notice: We don't appoint env, which means subprocess wil inherit current environment and that is expected behavior",
"log_pipe_stdout",
"=",
"trial_syslogger_stdout",
".",
"get_pipelog_reader",
"(",
")",
"process",
"=",
"Popen",
"(",
"args",
".",
"trial_command",
",",
"shell",
"=",
"True",
",",
"stdout",
"=",
"log_pipe_stdout",
",",
"stderr",
"=",
"log_pipe_stdout",
")",
"nni_log",
"(",
"LogType",
".",
"Info",
",",
"'Trial keeper spawns a subprocess (pid {0}) to run command: {1}'",
".",
"format",
"(",
"process",
".",
"pid",
",",
"shlex",
".",
"split",
"(",
"args",
".",
"trial_command",
")",
")",
")",
"while",
"True",
":",
"retCode",
"=",
"process",
".",
"poll",
"(",
")",
"# child worker process exits and all stdout data is read",
"if",
"retCode",
"is",
"not",
"None",
"and",
"log_pipe_stdout",
".",
"set_process_exit",
"(",
")",
"and",
"log_pipe_stdout",
".",
"is_read_completed",
"==",
"True",
":",
"nni_log",
"(",
"LogType",
".",
"Info",
",",
"'subprocess terminated. Exit code is {}. Quit'",
".",
"format",
"(",
"retCode",
")",
")",
"if",
"hdfs_output_dir",
"is",
"not",
"None",
":",
"# Copy local directory to hdfs for OpenPAI",
"nni_local_output_dir",
"=",
"os",
".",
"environ",
"[",
"'NNI_OUTPUT_DIR'",
"]",
"try",
":",
"if",
"copyDirectoryToHdfs",
"(",
"nni_local_output_dir",
",",
"hdfs_output_dir",
",",
"hdfs_client",
")",
":",
"nni_log",
"(",
"LogType",
".",
"Info",
",",
"'copy directory from {0} to {1} success!'",
".",
"format",
"(",
"nni_local_output_dir",
",",
"hdfs_output_dir",
")",
")",
"else",
":",
"nni_log",
"(",
"LogType",
".",
"Info",
",",
"'copy directory from {0} to {1} failed!'",
".",
"format",
"(",
"nni_local_output_dir",
",",
"hdfs_output_dir",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"nni_log",
"(",
"LogType",
".",
"Error",
",",
"'HDFS copy directory got exception: '",
"+",
"str",
"(",
"e",
")",
")",
"raise",
"e",
"## Exit as the retCode of subprocess(trial)",
"exit",
"(",
"retCode",
")",
"break",
"time",
".",
"sleep",
"(",
"2",
")"
] | main loop logic for trial keeper | [
"main",
"loop",
"logic",
"for",
"trial",
"keeper"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_trial_tool/trial_keeper.py#L43-L105 |
27,011 | Microsoft/nni | examples/trials/ga_squad/trial.py | load_embedding | def load_embedding(path):
'''
return embedding for a specific file by given file path.
'''
EMBEDDING_DIM = 300
embedding_dict = {}
with open(path, 'r', encoding='utf-8') as file:
pairs = [line.strip('\r\n').split() for line in file.readlines()]
for pair in pairs:
if len(pair) == EMBEDDING_DIM + 1:
embedding_dict[pair[0]] = [float(x) for x in pair[1:]]
logger.debug('embedding_dict size: %d', len(embedding_dict))
return embedding_dict | python | def load_embedding(path):
'''
return embedding for a specific file by given file path.
'''
EMBEDDING_DIM = 300
embedding_dict = {}
with open(path, 'r', encoding='utf-8') as file:
pairs = [line.strip('\r\n').split() for line in file.readlines()]
for pair in pairs:
if len(pair) == EMBEDDING_DIM + 1:
embedding_dict[pair[0]] = [float(x) for x in pair[1:]]
logger.debug('embedding_dict size: %d', len(embedding_dict))
return embedding_dict | [
"def",
"load_embedding",
"(",
"path",
")",
":",
"EMBEDDING_DIM",
"=",
"300",
"embedding_dict",
"=",
"{",
"}",
"with",
"open",
"(",
"path",
",",
"'r'",
",",
"encoding",
"=",
"'utf-8'",
")",
"as",
"file",
":",
"pairs",
"=",
"[",
"line",
".",
"strip",
"(",
"'\\r\\n'",
")",
".",
"split",
"(",
")",
"for",
"line",
"in",
"file",
".",
"readlines",
"(",
")",
"]",
"for",
"pair",
"in",
"pairs",
":",
"if",
"len",
"(",
"pair",
")",
"==",
"EMBEDDING_DIM",
"+",
"1",
":",
"embedding_dict",
"[",
"pair",
"[",
"0",
"]",
"]",
"=",
"[",
"float",
"(",
"x",
")",
"for",
"x",
"in",
"pair",
"[",
"1",
":",
"]",
"]",
"logger",
".",
"debug",
"(",
"'embedding_dict size: %d'",
",",
"len",
"(",
"embedding_dict",
")",
")",
"return",
"embedding_dict"
] | return embedding for a specific file by given file path. | [
"return",
"embedding",
"for",
"a",
"specific",
"file",
"by",
"given",
"file",
"path",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/trial.py#L87-L99 |
27,012 | Microsoft/nni | examples/trials/ga_squad/trial.py | generate_predict_json | def generate_predict_json(position1_result, position2_result, ids, passage_tokens):
'''
Generate json by prediction.
'''
predict_len = len(position1_result)
logger.debug('total prediction num is %s', str(predict_len))
answers = {}
for i in range(predict_len):
sample_id = ids[i]
passage, tokens = passage_tokens[i]
kbest = find_best_answer_span(
position1_result[i], position2_result[i], len(tokens), 23)
_, start, end = kbest[0]
answer = passage[tokens[start]['char_begin']:tokens[end]['char_end']]
answers[sample_id] = answer
logger.debug('generate predict done.')
return answers | python | def generate_predict_json(position1_result, position2_result, ids, passage_tokens):
'''
Generate json by prediction.
'''
predict_len = len(position1_result)
logger.debug('total prediction num is %s', str(predict_len))
answers = {}
for i in range(predict_len):
sample_id = ids[i]
passage, tokens = passage_tokens[i]
kbest = find_best_answer_span(
position1_result[i], position2_result[i], len(tokens), 23)
_, start, end = kbest[0]
answer = passage[tokens[start]['char_begin']:tokens[end]['char_end']]
answers[sample_id] = answer
logger.debug('generate predict done.')
return answers | [
"def",
"generate_predict_json",
"(",
"position1_result",
",",
"position2_result",
",",
"ids",
",",
"passage_tokens",
")",
":",
"predict_len",
"=",
"len",
"(",
"position1_result",
")",
"logger",
".",
"debug",
"(",
"'total prediction num is %s'",
",",
"str",
"(",
"predict_len",
")",
")",
"answers",
"=",
"{",
"}",
"for",
"i",
"in",
"range",
"(",
"predict_len",
")",
":",
"sample_id",
"=",
"ids",
"[",
"i",
"]",
"passage",
",",
"tokens",
"=",
"passage_tokens",
"[",
"i",
"]",
"kbest",
"=",
"find_best_answer_span",
"(",
"position1_result",
"[",
"i",
"]",
",",
"position2_result",
"[",
"i",
"]",
",",
"len",
"(",
"tokens",
")",
",",
"23",
")",
"_",
",",
"start",
",",
"end",
"=",
"kbest",
"[",
"0",
"]",
"answer",
"=",
"passage",
"[",
"tokens",
"[",
"start",
"]",
"[",
"'char_begin'",
"]",
":",
"tokens",
"[",
"end",
"]",
"[",
"'char_end'",
"]",
"]",
"answers",
"[",
"sample_id",
"]",
"=",
"answer",
"logger",
".",
"debug",
"(",
"'generate predict done.'",
")",
"return",
"answers"
] | Generate json by prediction. | [
"Generate",
"json",
"by",
"prediction",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/trial.py#L252-L269 |
27,013 | Microsoft/nni | examples/trials/ga_squad/evaluate.py | f1_score | def f1_score(prediction, ground_truth):
'''
Calculate the f1 score.
'''
prediction_tokens = normalize_answer(prediction).split()
ground_truth_tokens = normalize_answer(ground_truth).split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return 0
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1_result = (2 * precision * recall) / (precision + recall)
return f1_result | python | def f1_score(prediction, ground_truth):
'''
Calculate the f1 score.
'''
prediction_tokens = normalize_answer(prediction).split()
ground_truth_tokens = normalize_answer(ground_truth).split()
common = Counter(prediction_tokens) & Counter(ground_truth_tokens)
num_same = sum(common.values())
if num_same == 0:
return 0
precision = 1.0 * num_same / len(prediction_tokens)
recall = 1.0 * num_same / len(ground_truth_tokens)
f1_result = (2 * precision * recall) / (precision + recall)
return f1_result | [
"def",
"f1_score",
"(",
"prediction",
",",
"ground_truth",
")",
":",
"prediction_tokens",
"=",
"normalize_answer",
"(",
"prediction",
")",
".",
"split",
"(",
")",
"ground_truth_tokens",
"=",
"normalize_answer",
"(",
"ground_truth",
")",
".",
"split",
"(",
")",
"common",
"=",
"Counter",
"(",
"prediction_tokens",
")",
"&",
"Counter",
"(",
"ground_truth_tokens",
")",
"num_same",
"=",
"sum",
"(",
"common",
".",
"values",
"(",
")",
")",
"if",
"num_same",
"==",
"0",
":",
"return",
"0",
"precision",
"=",
"1.0",
"*",
"num_same",
"/",
"len",
"(",
"prediction_tokens",
")",
"recall",
"=",
"1.0",
"*",
"num_same",
"/",
"len",
"(",
"ground_truth_tokens",
")",
"f1_result",
"=",
"(",
"2",
"*",
"precision",
"*",
"recall",
")",
"/",
"(",
"precision",
"+",
"recall",
")",
"return",
"f1_result"
] | Calculate the f1 score. | [
"Calculate",
"the",
"f1",
"score",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/evaluate.py#L63-L76 |
27,014 | Microsoft/nni | examples/trials/ga_squad/evaluate.py | _evaluate | def _evaluate(dataset, predictions):
'''
Evaluate function.
'''
f1_result = exact_match = total = 0
count = 0
for article in dataset:
for paragraph in article['paragraphs']:
for qa_pair in paragraph['qas']:
total += 1
if qa_pair['id'] not in predictions:
count += 1
continue
ground_truths = list(map(lambda x: x['text'], qa_pair['answers']))
prediction = predictions[qa_pair['id']]
exact_match += metric_max_over_ground_truths(
exact_match_score, prediction, ground_truths)
f1_result += metric_max_over_ground_truths(
f1_score, prediction, ground_truths)
print('total', total, 'exact_match', exact_match, 'unanswer_question ', count)
exact_match = 100.0 * exact_match / total
f1_result = 100.0 * f1_result / total
return {'exact_match': exact_match, 'f1': f1_result} | python | def _evaluate(dataset, predictions):
'''
Evaluate function.
'''
f1_result = exact_match = total = 0
count = 0
for article in dataset:
for paragraph in article['paragraphs']:
for qa_pair in paragraph['qas']:
total += 1
if qa_pair['id'] not in predictions:
count += 1
continue
ground_truths = list(map(lambda x: x['text'], qa_pair['answers']))
prediction = predictions[qa_pair['id']]
exact_match += metric_max_over_ground_truths(
exact_match_score, prediction, ground_truths)
f1_result += metric_max_over_ground_truths(
f1_score, prediction, ground_truths)
print('total', total, 'exact_match', exact_match, 'unanswer_question ', count)
exact_match = 100.0 * exact_match / total
f1_result = 100.0 * f1_result / total
return {'exact_match': exact_match, 'f1': f1_result} | [
"def",
"_evaluate",
"(",
"dataset",
",",
"predictions",
")",
":",
"f1_result",
"=",
"exact_match",
"=",
"total",
"=",
"0",
"count",
"=",
"0",
"for",
"article",
"in",
"dataset",
":",
"for",
"paragraph",
"in",
"article",
"[",
"'paragraphs'",
"]",
":",
"for",
"qa_pair",
"in",
"paragraph",
"[",
"'qas'",
"]",
":",
"total",
"+=",
"1",
"if",
"qa_pair",
"[",
"'id'",
"]",
"not",
"in",
"predictions",
":",
"count",
"+=",
"1",
"continue",
"ground_truths",
"=",
"list",
"(",
"map",
"(",
"lambda",
"x",
":",
"x",
"[",
"'text'",
"]",
",",
"qa_pair",
"[",
"'answers'",
"]",
")",
")",
"prediction",
"=",
"predictions",
"[",
"qa_pair",
"[",
"'id'",
"]",
"]",
"exact_match",
"+=",
"metric_max_over_ground_truths",
"(",
"exact_match_score",
",",
"prediction",
",",
"ground_truths",
")",
"f1_result",
"+=",
"metric_max_over_ground_truths",
"(",
"f1_score",
",",
"prediction",
",",
"ground_truths",
")",
"print",
"(",
"'total'",
",",
"total",
",",
"'exact_match'",
",",
"exact_match",
",",
"'unanswer_question '",
",",
"count",
")",
"exact_match",
"=",
"100.0",
"*",
"exact_match",
"/",
"total",
"f1_result",
"=",
"100.0",
"*",
"f1_result",
"/",
"total",
"return",
"{",
"'exact_match'",
":",
"exact_match",
",",
"'f1'",
":",
"f1_result",
"}"
] | Evaluate function. | [
"Evaluate",
"function",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/ga_squad/evaluate.py#L94-L116 |
27,015 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | json2space | def json2space(in_x, name=ROOT):
"""
Change json to search space in hyperopt.
Parameters
----------
in_x : dict/list/str/int/float
The part of json.
name : str
name could be ROOT, TYPE, VALUE or INDEX.
"""
out_y = copy.deepcopy(in_x)
if isinstance(in_x, dict):
if TYPE in in_x.keys():
_type = in_x[TYPE]
name = name + '-' + _type
_value = json2space(in_x[VALUE], name=name)
if _type == 'choice':
out_y = eval('hp.hp.'+_type)(name, _value)
else:
if _type in ['loguniform', 'qloguniform']:
_value[:2] = np.log(_value[:2])
out_y = eval('hp.hp.' + _type)(name, *_value)
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2space(in_x[key], name+'[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2space(x_i, name+'[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y | python | def json2space(in_x, name=ROOT):
"""
Change json to search space in hyperopt.
Parameters
----------
in_x : dict/list/str/int/float
The part of json.
name : str
name could be ROOT, TYPE, VALUE or INDEX.
"""
out_y = copy.deepcopy(in_x)
if isinstance(in_x, dict):
if TYPE in in_x.keys():
_type = in_x[TYPE]
name = name + '-' + _type
_value = json2space(in_x[VALUE], name=name)
if _type == 'choice':
out_y = eval('hp.hp.'+_type)(name, _value)
else:
if _type in ['loguniform', 'qloguniform']:
_value[:2] = np.log(_value[:2])
out_y = eval('hp.hp.' + _type)(name, *_value)
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2space(in_x[key], name+'[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2space(x_i, name+'[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y | [
"def",
"json2space",
"(",
"in_x",
",",
"name",
"=",
"ROOT",
")",
":",
"out_y",
"=",
"copy",
".",
"deepcopy",
"(",
"in_x",
")",
"if",
"isinstance",
"(",
"in_x",
",",
"dict",
")",
":",
"if",
"TYPE",
"in",
"in_x",
".",
"keys",
"(",
")",
":",
"_type",
"=",
"in_x",
"[",
"TYPE",
"]",
"name",
"=",
"name",
"+",
"'-'",
"+",
"_type",
"_value",
"=",
"json2space",
"(",
"in_x",
"[",
"VALUE",
"]",
",",
"name",
"=",
"name",
")",
"if",
"_type",
"==",
"'choice'",
":",
"out_y",
"=",
"eval",
"(",
"'hp.hp.'",
"+",
"_type",
")",
"(",
"name",
",",
"_value",
")",
"else",
":",
"if",
"_type",
"in",
"[",
"'loguniform'",
",",
"'qloguniform'",
"]",
":",
"_value",
"[",
":",
"2",
"]",
"=",
"np",
".",
"log",
"(",
"_value",
"[",
":",
"2",
"]",
")",
"out_y",
"=",
"eval",
"(",
"'hp.hp.'",
"+",
"_type",
")",
"(",
"name",
",",
"*",
"_value",
")",
"else",
":",
"out_y",
"=",
"dict",
"(",
")",
"for",
"key",
"in",
"in_x",
".",
"keys",
"(",
")",
":",
"out_y",
"[",
"key",
"]",
"=",
"json2space",
"(",
"in_x",
"[",
"key",
"]",
",",
"name",
"+",
"'[%s]'",
"%",
"str",
"(",
"key",
")",
")",
"elif",
"isinstance",
"(",
"in_x",
",",
"list",
")",
":",
"out_y",
"=",
"list",
"(",
")",
"for",
"i",
",",
"x_i",
"in",
"enumerate",
"(",
"in_x",
")",
":",
"out_y",
".",
"append",
"(",
"json2space",
"(",
"x_i",
",",
"name",
"+",
"'[%d]'",
"%",
"i",
")",
")",
"else",
":",
"logger",
".",
"info",
"(",
"'in_x is not a dict or a list in json2space fuinction %s'",
",",
"str",
"(",
"in_x",
")",
")",
"return",
"out_y"
] | Change json to search space in hyperopt.
Parameters
----------
in_x : dict/list/str/int/float
The part of json.
name : str
name could be ROOT, TYPE, VALUE or INDEX. | [
"Change",
"json",
"to",
"search",
"space",
"in",
"hyperopt",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L52-L85 |
27,016 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | json2parameter | def json2parameter(in_x, parameter, name=ROOT):
"""
Change json to parameters.
"""
out_y = copy.deepcopy(in_x)
if isinstance(in_x, dict):
if TYPE in in_x.keys():
_type = in_x[TYPE]
name = name + '-' + _type
if _type == 'choice':
_index = parameter[name]
out_y = {
INDEX: _index,
VALUE: json2parameter(in_x[VALUE][_index], parameter, name=name+'[%d]' % _index)
}
else:
out_y = parameter[name]
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2parameter(
in_x[key], parameter, name + '[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2parameter(x_i, parameter, name + '[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y | python | def json2parameter(in_x, parameter, name=ROOT):
"""
Change json to parameters.
"""
out_y = copy.deepcopy(in_x)
if isinstance(in_x, dict):
if TYPE in in_x.keys():
_type = in_x[TYPE]
name = name + '-' + _type
if _type == 'choice':
_index = parameter[name]
out_y = {
INDEX: _index,
VALUE: json2parameter(in_x[VALUE][_index], parameter, name=name+'[%d]' % _index)
}
else:
out_y = parameter[name]
else:
out_y = dict()
for key in in_x.keys():
out_y[key] = json2parameter(
in_x[key], parameter, name + '[%s]' % str(key))
elif isinstance(in_x, list):
out_y = list()
for i, x_i in enumerate(in_x):
out_y.append(json2parameter(x_i, parameter, name + '[%d]' % i))
else:
logger.info('in_x is not a dict or a list in json2space fuinction %s', str(in_x))
return out_y | [
"def",
"json2parameter",
"(",
"in_x",
",",
"parameter",
",",
"name",
"=",
"ROOT",
")",
":",
"out_y",
"=",
"copy",
".",
"deepcopy",
"(",
"in_x",
")",
"if",
"isinstance",
"(",
"in_x",
",",
"dict",
")",
":",
"if",
"TYPE",
"in",
"in_x",
".",
"keys",
"(",
")",
":",
"_type",
"=",
"in_x",
"[",
"TYPE",
"]",
"name",
"=",
"name",
"+",
"'-'",
"+",
"_type",
"if",
"_type",
"==",
"'choice'",
":",
"_index",
"=",
"parameter",
"[",
"name",
"]",
"out_y",
"=",
"{",
"INDEX",
":",
"_index",
",",
"VALUE",
":",
"json2parameter",
"(",
"in_x",
"[",
"VALUE",
"]",
"[",
"_index",
"]",
",",
"parameter",
",",
"name",
"=",
"name",
"+",
"'[%d]'",
"%",
"_index",
")",
"}",
"else",
":",
"out_y",
"=",
"parameter",
"[",
"name",
"]",
"else",
":",
"out_y",
"=",
"dict",
"(",
")",
"for",
"key",
"in",
"in_x",
".",
"keys",
"(",
")",
":",
"out_y",
"[",
"key",
"]",
"=",
"json2parameter",
"(",
"in_x",
"[",
"key",
"]",
",",
"parameter",
",",
"name",
"+",
"'[%s]'",
"%",
"str",
"(",
"key",
")",
")",
"elif",
"isinstance",
"(",
"in_x",
",",
"list",
")",
":",
"out_y",
"=",
"list",
"(",
")",
"for",
"i",
",",
"x_i",
"in",
"enumerate",
"(",
"in_x",
")",
":",
"out_y",
".",
"append",
"(",
"json2parameter",
"(",
"x_i",
",",
"parameter",
",",
"name",
"+",
"'[%d]'",
"%",
"i",
")",
")",
"else",
":",
"logger",
".",
"info",
"(",
"'in_x is not a dict or a list in json2space fuinction %s'",
",",
"str",
"(",
"in_x",
")",
")",
"return",
"out_y"
] | Change json to parameters. | [
"Change",
"json",
"to",
"parameters",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L88-L116 |
27,017 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | _split_index | def _split_index(params):
"""
Delete index infromation from params
"""
if isinstance(params, list):
return [params[0], _split_index(params[1])]
elif isinstance(params, dict):
if INDEX in params.keys():
return _split_index(params[VALUE])
result = dict()
for key in params:
result[key] = _split_index(params[key])
return result
else:
return params | python | def _split_index(params):
"""
Delete index infromation from params
"""
if isinstance(params, list):
return [params[0], _split_index(params[1])]
elif isinstance(params, dict):
if INDEX in params.keys():
return _split_index(params[VALUE])
result = dict()
for key in params:
result[key] = _split_index(params[key])
return result
else:
return params | [
"def",
"_split_index",
"(",
"params",
")",
":",
"if",
"isinstance",
"(",
"params",
",",
"list",
")",
":",
"return",
"[",
"params",
"[",
"0",
"]",
",",
"_split_index",
"(",
"params",
"[",
"1",
"]",
")",
"]",
"elif",
"isinstance",
"(",
"params",
",",
"dict",
")",
":",
"if",
"INDEX",
"in",
"params",
".",
"keys",
"(",
")",
":",
"return",
"_split_index",
"(",
"params",
"[",
"VALUE",
"]",
")",
"result",
"=",
"dict",
"(",
")",
"for",
"key",
"in",
"params",
":",
"result",
"[",
"key",
"]",
"=",
"_split_index",
"(",
"params",
"[",
"key",
"]",
")",
"return",
"result",
"else",
":",
"return",
"params"
] | Delete index infromation from params | [
"Delete",
"index",
"infromation",
"from",
"params"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L171-L185 |
27,018 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | HyperoptTuner.update_search_space | def update_search_space(self, search_space):
"""
Update search space definition in tuner by search_space in parameters.
Will called when first setup experiemnt or update search space in WebUI.
Parameters
----------
search_space : dict
"""
self.json = search_space
search_space_instance = json2space(self.json)
rstate = np.random.RandomState()
trials = hp.Trials()
domain = hp.Domain(None, search_space_instance,
pass_expr_memo_ctrl=None)
algorithm = self._choose_tuner(self.algorithm_name)
self.rval = hp.FMinIter(algorithm, domain, trials,
max_evals=-1, rstate=rstate, verbose=0)
self.rval.catch_eval_exceptions = False | python | def update_search_space(self, search_space):
"""
Update search space definition in tuner by search_space in parameters.
Will called when first setup experiemnt or update search space in WebUI.
Parameters
----------
search_space : dict
"""
self.json = search_space
search_space_instance = json2space(self.json)
rstate = np.random.RandomState()
trials = hp.Trials()
domain = hp.Domain(None, search_space_instance,
pass_expr_memo_ctrl=None)
algorithm = self._choose_tuner(self.algorithm_name)
self.rval = hp.FMinIter(algorithm, domain, trials,
max_evals=-1, rstate=rstate, verbose=0)
self.rval.catch_eval_exceptions = False | [
"def",
"update_search_space",
"(",
"self",
",",
"search_space",
")",
":",
"self",
".",
"json",
"=",
"search_space",
"search_space_instance",
"=",
"json2space",
"(",
"self",
".",
"json",
")",
"rstate",
"=",
"np",
".",
"random",
".",
"RandomState",
"(",
")",
"trials",
"=",
"hp",
".",
"Trials",
"(",
")",
"domain",
"=",
"hp",
".",
"Domain",
"(",
"None",
",",
"search_space_instance",
",",
"pass_expr_memo_ctrl",
"=",
"None",
")",
"algorithm",
"=",
"self",
".",
"_choose_tuner",
"(",
"self",
".",
"algorithm_name",
")",
"self",
".",
"rval",
"=",
"hp",
".",
"FMinIter",
"(",
"algorithm",
",",
"domain",
",",
"trials",
",",
"max_evals",
"=",
"-",
"1",
",",
"rstate",
"=",
"rstate",
",",
"verbose",
"=",
"0",
")",
"self",
".",
"rval",
".",
"catch_eval_exceptions",
"=",
"False"
] | Update search space definition in tuner by search_space in parameters.
Will called when first setup experiemnt or update search space in WebUI.
Parameters
----------
search_space : dict | [
"Update",
"search",
"space",
"definition",
"in",
"tuner",
"by",
"search_space",
"in",
"parameters",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L223-L242 |
27,019 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | HyperoptTuner.receive_trial_result | def receive_trial_result(self, parameter_id, parameters, value):
"""
Record an observation of the objective function
Parameters
----------
parameter_id : int
parameters : dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial.
"""
reward = extract_scalar_reward(value)
# restore the paramsters contains '_index'
if parameter_id not in self.total_data:
raise RuntimeError('Received parameter_id not in total_data.')
params = self.total_data[parameter_id]
if self.optimize_mode is OptimizeMode.Maximize:
reward = -reward
rval = self.rval
domain = rval.domain
trials = rval.trials
new_id = len(trials)
rval_specs = [None]
rval_results = [domain.new_result()]
rval_miscs = [dict(tid=new_id, cmd=domain.cmd, workdir=domain.workdir)]
vals = params
idxs = dict()
out_y = dict()
json2vals(self.json, vals, out_y)
vals = out_y
for key in domain.params:
if key in [VALUE, INDEX]:
continue
if key not in vals or vals[key] is None or vals[key] == []:
idxs[key] = vals[key] = []
else:
idxs[key] = [new_id]
vals[key] = [vals[key]]
self.miscs_update_idxs_vals(rval_miscs, idxs, vals,
idxs_map={new_id: new_id},
assert_all_vals_used=False)
trial = trials.new_trial_docs([new_id], rval_specs, rval_results, rval_miscs)[0]
trial['result'] = {'loss': reward, 'status': 'ok'}
trial['state'] = hp.JOB_STATE_DONE
trials.insert_trial_docs([trial])
trials.refresh() | python | def receive_trial_result(self, parameter_id, parameters, value):
"""
Record an observation of the objective function
Parameters
----------
parameter_id : int
parameters : dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial.
"""
reward = extract_scalar_reward(value)
# restore the paramsters contains '_index'
if parameter_id not in self.total_data:
raise RuntimeError('Received parameter_id not in total_data.')
params = self.total_data[parameter_id]
if self.optimize_mode is OptimizeMode.Maximize:
reward = -reward
rval = self.rval
domain = rval.domain
trials = rval.trials
new_id = len(trials)
rval_specs = [None]
rval_results = [domain.new_result()]
rval_miscs = [dict(tid=new_id, cmd=domain.cmd, workdir=domain.workdir)]
vals = params
idxs = dict()
out_y = dict()
json2vals(self.json, vals, out_y)
vals = out_y
for key in domain.params:
if key in [VALUE, INDEX]:
continue
if key not in vals or vals[key] is None or vals[key] == []:
idxs[key] = vals[key] = []
else:
idxs[key] = [new_id]
vals[key] = [vals[key]]
self.miscs_update_idxs_vals(rval_miscs, idxs, vals,
idxs_map={new_id: new_id},
assert_all_vals_used=False)
trial = trials.new_trial_docs([new_id], rval_specs, rval_results, rval_miscs)[0]
trial['result'] = {'loss': reward, 'status': 'ok'}
trial['state'] = hp.JOB_STATE_DONE
trials.insert_trial_docs([trial])
trials.refresh() | [
"def",
"receive_trial_result",
"(",
"self",
",",
"parameter_id",
",",
"parameters",
",",
"value",
")",
":",
"reward",
"=",
"extract_scalar_reward",
"(",
"value",
")",
"# restore the paramsters contains '_index'",
"if",
"parameter_id",
"not",
"in",
"self",
".",
"total_data",
":",
"raise",
"RuntimeError",
"(",
"'Received parameter_id not in total_data.'",
")",
"params",
"=",
"self",
".",
"total_data",
"[",
"parameter_id",
"]",
"if",
"self",
".",
"optimize_mode",
"is",
"OptimizeMode",
".",
"Maximize",
":",
"reward",
"=",
"-",
"reward",
"rval",
"=",
"self",
".",
"rval",
"domain",
"=",
"rval",
".",
"domain",
"trials",
"=",
"rval",
".",
"trials",
"new_id",
"=",
"len",
"(",
"trials",
")",
"rval_specs",
"=",
"[",
"None",
"]",
"rval_results",
"=",
"[",
"domain",
".",
"new_result",
"(",
")",
"]",
"rval_miscs",
"=",
"[",
"dict",
"(",
"tid",
"=",
"new_id",
",",
"cmd",
"=",
"domain",
".",
"cmd",
",",
"workdir",
"=",
"domain",
".",
"workdir",
")",
"]",
"vals",
"=",
"params",
"idxs",
"=",
"dict",
"(",
")",
"out_y",
"=",
"dict",
"(",
")",
"json2vals",
"(",
"self",
".",
"json",
",",
"vals",
",",
"out_y",
")",
"vals",
"=",
"out_y",
"for",
"key",
"in",
"domain",
".",
"params",
":",
"if",
"key",
"in",
"[",
"VALUE",
",",
"INDEX",
"]",
":",
"continue",
"if",
"key",
"not",
"in",
"vals",
"or",
"vals",
"[",
"key",
"]",
"is",
"None",
"or",
"vals",
"[",
"key",
"]",
"==",
"[",
"]",
":",
"idxs",
"[",
"key",
"]",
"=",
"vals",
"[",
"key",
"]",
"=",
"[",
"]",
"else",
":",
"idxs",
"[",
"key",
"]",
"=",
"[",
"new_id",
"]",
"vals",
"[",
"key",
"]",
"=",
"[",
"vals",
"[",
"key",
"]",
"]",
"self",
".",
"miscs_update_idxs_vals",
"(",
"rval_miscs",
",",
"idxs",
",",
"vals",
",",
"idxs_map",
"=",
"{",
"new_id",
":",
"new_id",
"}",
",",
"assert_all_vals_used",
"=",
"False",
")",
"trial",
"=",
"trials",
".",
"new_trial_docs",
"(",
"[",
"new_id",
"]",
",",
"rval_specs",
",",
"rval_results",
",",
"rval_miscs",
")",
"[",
"0",
"]",
"trial",
"[",
"'result'",
"]",
"=",
"{",
"'loss'",
":",
"reward",
",",
"'status'",
":",
"'ok'",
"}",
"trial",
"[",
"'state'",
"]",
"=",
"hp",
".",
"JOB_STATE_DONE",
"trials",
".",
"insert_trial_docs",
"(",
"[",
"trial",
"]",
")",
"trials",
".",
"refresh",
"(",
")"
] | Record an observation of the objective function
Parameters
----------
parameter_id : int
parameters : dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial. | [
"Record",
"an",
"observation",
"of",
"the",
"objective",
"function"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L265-L319 |
27,020 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | HyperoptTuner.miscs_update_idxs_vals | def miscs_update_idxs_vals(self, miscs, idxs, vals,
assert_all_vals_used=True,
idxs_map=None):
"""
Unpack the idxs-vals format into the list of dictionaries that is
`misc`.
Parameters
----------
idxs_map : dict
idxs_map is a dictionary of id->id mappings so that the misc['idxs'] can
contain different numbers than the idxs argument.
"""
if idxs_map is None:
idxs_map = {}
assert set(idxs.keys()) == set(vals.keys())
misc_by_id = {m['tid']: m for m in miscs}
for m in miscs:
m['idxs'] = dict([(key, []) for key in idxs])
m['vals'] = dict([(key, []) for key in idxs])
for key in idxs:
assert len(idxs[key]) == len(vals[key])
for tid, val in zip(idxs[key], vals[key]):
tid = idxs_map.get(tid, tid)
if assert_all_vals_used or tid in misc_by_id:
misc_by_id[tid]['idxs'][key] = [tid]
misc_by_id[tid]['vals'][key] = [val] | python | def miscs_update_idxs_vals(self, miscs, idxs, vals,
assert_all_vals_used=True,
idxs_map=None):
"""
Unpack the idxs-vals format into the list of dictionaries that is
`misc`.
Parameters
----------
idxs_map : dict
idxs_map is a dictionary of id->id mappings so that the misc['idxs'] can
contain different numbers than the idxs argument.
"""
if idxs_map is None:
idxs_map = {}
assert set(idxs.keys()) == set(vals.keys())
misc_by_id = {m['tid']: m for m in miscs}
for m in miscs:
m['idxs'] = dict([(key, []) for key in idxs])
m['vals'] = dict([(key, []) for key in idxs])
for key in idxs:
assert len(idxs[key]) == len(vals[key])
for tid, val in zip(idxs[key], vals[key]):
tid = idxs_map.get(tid, tid)
if assert_all_vals_used or tid in misc_by_id:
misc_by_id[tid]['idxs'][key] = [tid]
misc_by_id[tid]['vals'][key] = [val] | [
"def",
"miscs_update_idxs_vals",
"(",
"self",
",",
"miscs",
",",
"idxs",
",",
"vals",
",",
"assert_all_vals_used",
"=",
"True",
",",
"idxs_map",
"=",
"None",
")",
":",
"if",
"idxs_map",
"is",
"None",
":",
"idxs_map",
"=",
"{",
"}",
"assert",
"set",
"(",
"idxs",
".",
"keys",
"(",
")",
")",
"==",
"set",
"(",
"vals",
".",
"keys",
"(",
")",
")",
"misc_by_id",
"=",
"{",
"m",
"[",
"'tid'",
"]",
":",
"m",
"for",
"m",
"in",
"miscs",
"}",
"for",
"m",
"in",
"miscs",
":",
"m",
"[",
"'idxs'",
"]",
"=",
"dict",
"(",
"[",
"(",
"key",
",",
"[",
"]",
")",
"for",
"key",
"in",
"idxs",
"]",
")",
"m",
"[",
"'vals'",
"]",
"=",
"dict",
"(",
"[",
"(",
"key",
",",
"[",
"]",
")",
"for",
"key",
"in",
"idxs",
"]",
")",
"for",
"key",
"in",
"idxs",
":",
"assert",
"len",
"(",
"idxs",
"[",
"key",
"]",
")",
"==",
"len",
"(",
"vals",
"[",
"key",
"]",
")",
"for",
"tid",
",",
"val",
"in",
"zip",
"(",
"idxs",
"[",
"key",
"]",
",",
"vals",
"[",
"key",
"]",
")",
":",
"tid",
"=",
"idxs_map",
".",
"get",
"(",
"tid",
",",
"tid",
")",
"if",
"assert_all_vals_used",
"or",
"tid",
"in",
"misc_by_id",
":",
"misc_by_id",
"[",
"tid",
"]",
"[",
"'idxs'",
"]",
"[",
"key",
"]",
"=",
"[",
"tid",
"]",
"misc_by_id",
"[",
"tid",
"]",
"[",
"'vals'",
"]",
"[",
"key",
"]",
"=",
"[",
"val",
"]"
] | Unpack the idxs-vals format into the list of dictionaries that is
`misc`.
Parameters
----------
idxs_map : dict
idxs_map is a dictionary of id->id mappings so that the misc['idxs'] can
contain different numbers than the idxs argument. | [
"Unpack",
"the",
"idxs",
"-",
"vals",
"format",
"into",
"the",
"list",
"of",
"dictionaries",
"that",
"is",
"misc",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L321-L350 |
27,021 | Microsoft/nni | src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py | HyperoptTuner.get_suggestion | def get_suggestion(self, random_search=False):
"""get suggestion from hyperopt
Parameters
----------
random_search : bool
flag to indicate random search or not (default: {False})
Returns
----------
total_params : dict
parameter suggestion
"""
rval = self.rval
trials = rval.trials
algorithm = rval.algo
new_ids = rval.trials.new_trial_ids(1)
rval.trials.refresh()
random_state = rval.rstate.randint(2**31-1)
if random_search:
new_trials = hp.rand.suggest(new_ids, rval.domain, trials, random_state)
else:
new_trials = algorithm(new_ids, rval.domain, trials, random_state)
rval.trials.refresh()
vals = new_trials[0]['misc']['vals']
parameter = dict()
for key in vals:
try:
parameter[key] = vals[key][0].item()
except (KeyError, IndexError):
parameter[key] = None
# remove '_index' from json2parameter and save params-id
total_params = json2parameter(self.json, parameter)
return total_params | python | def get_suggestion(self, random_search=False):
"""get suggestion from hyperopt
Parameters
----------
random_search : bool
flag to indicate random search or not (default: {False})
Returns
----------
total_params : dict
parameter suggestion
"""
rval = self.rval
trials = rval.trials
algorithm = rval.algo
new_ids = rval.trials.new_trial_ids(1)
rval.trials.refresh()
random_state = rval.rstate.randint(2**31-1)
if random_search:
new_trials = hp.rand.suggest(new_ids, rval.domain, trials, random_state)
else:
new_trials = algorithm(new_ids, rval.domain, trials, random_state)
rval.trials.refresh()
vals = new_trials[0]['misc']['vals']
parameter = dict()
for key in vals:
try:
parameter[key] = vals[key][0].item()
except (KeyError, IndexError):
parameter[key] = None
# remove '_index' from json2parameter and save params-id
total_params = json2parameter(self.json, parameter)
return total_params | [
"def",
"get_suggestion",
"(",
"self",
",",
"random_search",
"=",
"False",
")",
":",
"rval",
"=",
"self",
".",
"rval",
"trials",
"=",
"rval",
".",
"trials",
"algorithm",
"=",
"rval",
".",
"algo",
"new_ids",
"=",
"rval",
".",
"trials",
".",
"new_trial_ids",
"(",
"1",
")",
"rval",
".",
"trials",
".",
"refresh",
"(",
")",
"random_state",
"=",
"rval",
".",
"rstate",
".",
"randint",
"(",
"2",
"**",
"31",
"-",
"1",
")",
"if",
"random_search",
":",
"new_trials",
"=",
"hp",
".",
"rand",
".",
"suggest",
"(",
"new_ids",
",",
"rval",
".",
"domain",
",",
"trials",
",",
"random_state",
")",
"else",
":",
"new_trials",
"=",
"algorithm",
"(",
"new_ids",
",",
"rval",
".",
"domain",
",",
"trials",
",",
"random_state",
")",
"rval",
".",
"trials",
".",
"refresh",
"(",
")",
"vals",
"=",
"new_trials",
"[",
"0",
"]",
"[",
"'misc'",
"]",
"[",
"'vals'",
"]",
"parameter",
"=",
"dict",
"(",
")",
"for",
"key",
"in",
"vals",
":",
"try",
":",
"parameter",
"[",
"key",
"]",
"=",
"vals",
"[",
"key",
"]",
"[",
"0",
"]",
".",
"item",
"(",
")",
"except",
"(",
"KeyError",
",",
"IndexError",
")",
":",
"parameter",
"[",
"key",
"]",
"=",
"None",
"# remove '_index' from json2parameter and save params-id",
"total_params",
"=",
"json2parameter",
"(",
"self",
".",
"json",
",",
"parameter",
")",
"return",
"total_params"
] | get suggestion from hyperopt
Parameters
----------
random_search : bool
flag to indicate random search or not (default: {False})
Returns
----------
total_params : dict
parameter suggestion | [
"get",
"suggestion",
"from",
"hyperopt"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperopt_tuner/hyperopt_tuner.py#L352-L387 |
27,022 | Microsoft/nni | src/sdk/pynni/nni/metis_tuner/lib_acquisition_function.py | next_hyperparameter_lowest_mu | def next_hyperparameter_lowest_mu(fun_prediction,
fun_prediction_args,
x_bounds, x_types,
minimize_starting_points,
minimize_constraints_fun=None):
'''
"Lowest Mu" acquisition function
'''
best_x = None
best_acquisition_value = None
x_bounds_minmax = [[i[0], i[-1]] for i in x_bounds]
x_bounds_minmax = numpy.array(x_bounds_minmax)
for starting_point in numpy.array(minimize_starting_points):
res = minimize(fun=_lowest_mu,
x0=starting_point.reshape(1, -1),
bounds=x_bounds_minmax,
method="L-BFGS-B",
args=(fun_prediction, fun_prediction_args, \
x_bounds, x_types, minimize_constraints_fun))
if (best_acquisition_value is None) or (res.fun < best_acquisition_value):
res.x = numpy.ndarray.tolist(res.x)
res.x = lib_data.match_val_type(res.x, x_bounds, x_types)
if (minimize_constraints_fun is None) or (minimize_constraints_fun(res.x) is True):
best_acquisition_value = res.fun
best_x = res.x
outputs = None
if best_x is not None:
mu, sigma = fun_prediction(best_x, *fun_prediction_args)
outputs = {'hyperparameter': best_x, 'expected_mu': mu,
'expected_sigma': sigma, 'acquisition_func': "lm"}
return outputs | python | def next_hyperparameter_lowest_mu(fun_prediction,
fun_prediction_args,
x_bounds, x_types,
minimize_starting_points,
minimize_constraints_fun=None):
'''
"Lowest Mu" acquisition function
'''
best_x = None
best_acquisition_value = None
x_bounds_minmax = [[i[0], i[-1]] for i in x_bounds]
x_bounds_minmax = numpy.array(x_bounds_minmax)
for starting_point in numpy.array(minimize_starting_points):
res = minimize(fun=_lowest_mu,
x0=starting_point.reshape(1, -1),
bounds=x_bounds_minmax,
method="L-BFGS-B",
args=(fun_prediction, fun_prediction_args, \
x_bounds, x_types, minimize_constraints_fun))
if (best_acquisition_value is None) or (res.fun < best_acquisition_value):
res.x = numpy.ndarray.tolist(res.x)
res.x = lib_data.match_val_type(res.x, x_bounds, x_types)
if (minimize_constraints_fun is None) or (minimize_constraints_fun(res.x) is True):
best_acquisition_value = res.fun
best_x = res.x
outputs = None
if best_x is not None:
mu, sigma = fun_prediction(best_x, *fun_prediction_args)
outputs = {'hyperparameter': best_x, 'expected_mu': mu,
'expected_sigma': sigma, 'acquisition_func': "lm"}
return outputs | [
"def",
"next_hyperparameter_lowest_mu",
"(",
"fun_prediction",
",",
"fun_prediction_args",
",",
"x_bounds",
",",
"x_types",
",",
"minimize_starting_points",
",",
"minimize_constraints_fun",
"=",
"None",
")",
":",
"best_x",
"=",
"None",
"best_acquisition_value",
"=",
"None",
"x_bounds_minmax",
"=",
"[",
"[",
"i",
"[",
"0",
"]",
",",
"i",
"[",
"-",
"1",
"]",
"]",
"for",
"i",
"in",
"x_bounds",
"]",
"x_bounds_minmax",
"=",
"numpy",
".",
"array",
"(",
"x_bounds_minmax",
")",
"for",
"starting_point",
"in",
"numpy",
".",
"array",
"(",
"minimize_starting_points",
")",
":",
"res",
"=",
"minimize",
"(",
"fun",
"=",
"_lowest_mu",
",",
"x0",
"=",
"starting_point",
".",
"reshape",
"(",
"1",
",",
"-",
"1",
")",
",",
"bounds",
"=",
"x_bounds_minmax",
",",
"method",
"=",
"\"L-BFGS-B\"",
",",
"args",
"=",
"(",
"fun_prediction",
",",
"fun_prediction_args",
",",
"x_bounds",
",",
"x_types",
",",
"minimize_constraints_fun",
")",
")",
"if",
"(",
"best_acquisition_value",
"is",
"None",
")",
"or",
"(",
"res",
".",
"fun",
"<",
"best_acquisition_value",
")",
":",
"res",
".",
"x",
"=",
"numpy",
".",
"ndarray",
".",
"tolist",
"(",
"res",
".",
"x",
")",
"res",
".",
"x",
"=",
"lib_data",
".",
"match_val_type",
"(",
"res",
".",
"x",
",",
"x_bounds",
",",
"x_types",
")",
"if",
"(",
"minimize_constraints_fun",
"is",
"None",
")",
"or",
"(",
"minimize_constraints_fun",
"(",
"res",
".",
"x",
")",
"is",
"True",
")",
":",
"best_acquisition_value",
"=",
"res",
".",
"fun",
"best_x",
"=",
"res",
".",
"x",
"outputs",
"=",
"None",
"if",
"best_x",
"is",
"not",
"None",
":",
"mu",
",",
"sigma",
"=",
"fun_prediction",
"(",
"best_x",
",",
"*",
"fun_prediction_args",
")",
"outputs",
"=",
"{",
"'hyperparameter'",
":",
"best_x",
",",
"'expected_mu'",
":",
"mu",
",",
"'expected_sigma'",
":",
"sigma",
",",
"'acquisition_func'",
":",
"\"lm\"",
"}",
"return",
"outputs"
] | "Lowest Mu" acquisition function | [
"Lowest",
"Mu",
"acquisition",
"function"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/metis_tuner/lib_acquisition_function.py#L154-L187 |
27,023 | Microsoft/nni | src/sdk/pynni/nni/metis_tuner/lib_acquisition_function.py | _lowest_mu | def _lowest_mu(x, fun_prediction, fun_prediction_args,
x_bounds, x_types, minimize_constraints_fun):
'''
Calculate the lowest mu
'''
# This is only for step-wise optimization
x = lib_data.match_val_type(x, x_bounds, x_types)
mu = sys.maxsize
if (minimize_constraints_fun is None) or (minimize_constraints_fun(x) is True):
mu, _ = fun_prediction(x, *fun_prediction_args)
return mu | python | def _lowest_mu(x, fun_prediction, fun_prediction_args,
x_bounds, x_types, minimize_constraints_fun):
'''
Calculate the lowest mu
'''
# This is only for step-wise optimization
x = lib_data.match_val_type(x, x_bounds, x_types)
mu = sys.maxsize
if (minimize_constraints_fun is None) or (minimize_constraints_fun(x) is True):
mu, _ = fun_prediction(x, *fun_prediction_args)
return mu | [
"def",
"_lowest_mu",
"(",
"x",
",",
"fun_prediction",
",",
"fun_prediction_args",
",",
"x_bounds",
",",
"x_types",
",",
"minimize_constraints_fun",
")",
":",
"# This is only for step-wise optimization",
"x",
"=",
"lib_data",
".",
"match_val_type",
"(",
"x",
",",
"x_bounds",
",",
"x_types",
")",
"mu",
"=",
"sys",
".",
"maxsize",
"if",
"(",
"minimize_constraints_fun",
"is",
"None",
")",
"or",
"(",
"minimize_constraints_fun",
"(",
"x",
")",
"is",
"True",
")",
":",
"mu",
",",
"_",
"=",
"fun_prediction",
"(",
"x",
",",
"*",
"fun_prediction_args",
")",
"return",
"mu"
] | Calculate the lowest mu | [
"Calculate",
"the",
"lowest",
"mu"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/metis_tuner/lib_acquisition_function.py#L190-L201 |
27,024 | Microsoft/nni | examples/trials/weight_sharing/ga_squad/train_model.py | GAG.build_char_states | def build_char_states(self, char_embed, is_training, reuse, char_ids, char_lengths):
"""Build char embedding network for the QA model."""
max_char_length = self.cfg.max_char_length
inputs = dropout(tf.nn.embedding_lookup(char_embed, char_ids),
self.cfg.dropout, is_training)
inputs = tf.reshape(
inputs, shape=[max_char_length, -1, self.cfg.char_embed_dim])
char_lengths = tf.reshape(char_lengths, shape=[-1])
with tf.variable_scope('char_encoding', reuse=reuse):
cell_fw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
cell_bw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
_, (left_right, right_left) = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell_fw,
cell_bw=cell_bw,
sequence_length=char_lengths,
inputs=inputs,
time_major=True,
dtype=tf.float32
)
left_right = tf.reshape(left_right, shape=[-1, self.cfg.char_embed_dim])
right_left = tf.reshape(right_left, shape=[-1, self.cfg.char_embed_dim])
states = tf.concat([left_right, right_left], axis=1)
out_shape = tf.shape(char_ids)[1:3]
out_shape = tf.concat([out_shape, tf.constant(
value=[self.cfg.char_embed_dim * 2], dtype=tf.int32)], axis=0)
return tf.reshape(states, shape=out_shape) | python | def build_char_states(self, char_embed, is_training, reuse, char_ids, char_lengths):
"""Build char embedding network for the QA model."""
max_char_length = self.cfg.max_char_length
inputs = dropout(tf.nn.embedding_lookup(char_embed, char_ids),
self.cfg.dropout, is_training)
inputs = tf.reshape(
inputs, shape=[max_char_length, -1, self.cfg.char_embed_dim])
char_lengths = tf.reshape(char_lengths, shape=[-1])
with tf.variable_scope('char_encoding', reuse=reuse):
cell_fw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
cell_bw = XGRUCell(hidden_dim=self.cfg.char_embed_dim)
_, (left_right, right_left) = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell_fw,
cell_bw=cell_bw,
sequence_length=char_lengths,
inputs=inputs,
time_major=True,
dtype=tf.float32
)
left_right = tf.reshape(left_right, shape=[-1, self.cfg.char_embed_dim])
right_left = tf.reshape(right_left, shape=[-1, self.cfg.char_embed_dim])
states = tf.concat([left_right, right_left], axis=1)
out_shape = tf.shape(char_ids)[1:3]
out_shape = tf.concat([out_shape, tf.constant(
value=[self.cfg.char_embed_dim * 2], dtype=tf.int32)], axis=0)
return tf.reshape(states, shape=out_shape) | [
"def",
"build_char_states",
"(",
"self",
",",
"char_embed",
",",
"is_training",
",",
"reuse",
",",
"char_ids",
",",
"char_lengths",
")",
":",
"max_char_length",
"=",
"self",
".",
"cfg",
".",
"max_char_length",
"inputs",
"=",
"dropout",
"(",
"tf",
".",
"nn",
".",
"embedding_lookup",
"(",
"char_embed",
",",
"char_ids",
")",
",",
"self",
".",
"cfg",
".",
"dropout",
",",
"is_training",
")",
"inputs",
"=",
"tf",
".",
"reshape",
"(",
"inputs",
",",
"shape",
"=",
"[",
"max_char_length",
",",
"-",
"1",
",",
"self",
".",
"cfg",
".",
"char_embed_dim",
"]",
")",
"char_lengths",
"=",
"tf",
".",
"reshape",
"(",
"char_lengths",
",",
"shape",
"=",
"[",
"-",
"1",
"]",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"'char_encoding'",
",",
"reuse",
"=",
"reuse",
")",
":",
"cell_fw",
"=",
"XGRUCell",
"(",
"hidden_dim",
"=",
"self",
".",
"cfg",
".",
"char_embed_dim",
")",
"cell_bw",
"=",
"XGRUCell",
"(",
"hidden_dim",
"=",
"self",
".",
"cfg",
".",
"char_embed_dim",
")",
"_",
",",
"(",
"left_right",
",",
"right_left",
")",
"=",
"tf",
".",
"nn",
".",
"bidirectional_dynamic_rnn",
"(",
"cell_fw",
"=",
"cell_fw",
",",
"cell_bw",
"=",
"cell_bw",
",",
"sequence_length",
"=",
"char_lengths",
",",
"inputs",
"=",
"inputs",
",",
"time_major",
"=",
"True",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
"left_right",
"=",
"tf",
".",
"reshape",
"(",
"left_right",
",",
"shape",
"=",
"[",
"-",
"1",
",",
"self",
".",
"cfg",
".",
"char_embed_dim",
"]",
")",
"right_left",
"=",
"tf",
".",
"reshape",
"(",
"right_left",
",",
"shape",
"=",
"[",
"-",
"1",
",",
"self",
".",
"cfg",
".",
"char_embed_dim",
"]",
")",
"states",
"=",
"tf",
".",
"concat",
"(",
"[",
"left_right",
",",
"right_left",
"]",
",",
"axis",
"=",
"1",
")",
"out_shape",
"=",
"tf",
".",
"shape",
"(",
"char_ids",
")",
"[",
"1",
":",
"3",
"]",
"out_shape",
"=",
"tf",
".",
"concat",
"(",
"[",
"out_shape",
",",
"tf",
".",
"constant",
"(",
"value",
"=",
"[",
"self",
".",
"cfg",
".",
"char_embed_dim",
"*",
"2",
"]",
",",
"dtype",
"=",
"tf",
".",
"int32",
")",
"]",
",",
"axis",
"=",
"0",
")",
"return",
"tf",
".",
"reshape",
"(",
"states",
",",
"shape",
"=",
"out_shape",
")"
] | Build char embedding network for the QA model. | [
"Build",
"char",
"embedding",
"network",
"for",
"the",
"QA",
"model",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/weight_sharing/ga_squad/train_model.py#L234-L263 |
27,025 | Microsoft/nni | src/sdk/pynni/nni/msg_dispatcher.py | MsgDispatcher._handle_final_metric_data | def _handle_final_metric_data(self, data):
"""Call tuner to process final results
"""
id_ = data['parameter_id']
value = data['value']
if id_ in _customized_parameter_ids:
self.tuner.receive_customized_trial_result(id_, _trial_params[id_], value)
else:
self.tuner.receive_trial_result(id_, _trial_params[id_], value) | python | def _handle_final_metric_data(self, data):
"""Call tuner to process final results
"""
id_ = data['parameter_id']
value = data['value']
if id_ in _customized_parameter_ids:
self.tuner.receive_customized_trial_result(id_, _trial_params[id_], value)
else:
self.tuner.receive_trial_result(id_, _trial_params[id_], value) | [
"def",
"_handle_final_metric_data",
"(",
"self",
",",
"data",
")",
":",
"id_",
"=",
"data",
"[",
"'parameter_id'",
"]",
"value",
"=",
"data",
"[",
"'value'",
"]",
"if",
"id_",
"in",
"_customized_parameter_ids",
":",
"self",
".",
"tuner",
".",
"receive_customized_trial_result",
"(",
"id_",
",",
"_trial_params",
"[",
"id_",
"]",
",",
"value",
")",
"else",
":",
"self",
".",
"tuner",
".",
"receive_trial_result",
"(",
"id_",
",",
"_trial_params",
"[",
"id_",
"]",
",",
"value",
")"
] | Call tuner to process final results | [
"Call",
"tuner",
"to",
"process",
"final",
"results"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/msg_dispatcher.py#L157-L165 |
27,026 | Microsoft/nni | src/sdk/pynni/nni/msg_dispatcher.py | MsgDispatcher._handle_intermediate_metric_data | def _handle_intermediate_metric_data(self, data):
"""Call assessor to process intermediate results
"""
if data['type'] != 'PERIODICAL':
return
if self.assessor is None:
return
trial_job_id = data['trial_job_id']
if trial_job_id in _ended_trials:
return
history = _trial_history[trial_job_id]
history[data['sequence']] = data['value']
ordered_history = _sort_history(history)
if len(ordered_history) < data['sequence']: # no user-visible update since last time
return
try:
result = self.assessor.assess_trial(trial_job_id, ordered_history)
except Exception as e:
_logger.exception('Assessor error')
if isinstance(result, bool):
result = AssessResult.Good if result else AssessResult.Bad
elif not isinstance(result, AssessResult):
msg = 'Result of Assessor.assess_trial must be an object of AssessResult, not %s'
raise RuntimeError(msg % type(result))
if result is AssessResult.Bad:
_logger.debug('BAD, kill %s', trial_job_id)
send(CommandType.KillTrialJob, json_tricks.dumps(trial_job_id))
# notify tuner
_logger.debug('env var: NNI_INCLUDE_INTERMEDIATE_RESULTS: [%s]', dispatcher_env_vars.NNI_INCLUDE_INTERMEDIATE_RESULTS)
if dispatcher_env_vars.NNI_INCLUDE_INTERMEDIATE_RESULTS == 'true':
self._earlystop_notify_tuner(data)
else:
_logger.debug('GOOD') | python | def _handle_intermediate_metric_data(self, data):
"""Call assessor to process intermediate results
"""
if data['type'] != 'PERIODICAL':
return
if self.assessor is None:
return
trial_job_id = data['trial_job_id']
if trial_job_id in _ended_trials:
return
history = _trial_history[trial_job_id]
history[data['sequence']] = data['value']
ordered_history = _sort_history(history)
if len(ordered_history) < data['sequence']: # no user-visible update since last time
return
try:
result = self.assessor.assess_trial(trial_job_id, ordered_history)
except Exception as e:
_logger.exception('Assessor error')
if isinstance(result, bool):
result = AssessResult.Good if result else AssessResult.Bad
elif not isinstance(result, AssessResult):
msg = 'Result of Assessor.assess_trial must be an object of AssessResult, not %s'
raise RuntimeError(msg % type(result))
if result is AssessResult.Bad:
_logger.debug('BAD, kill %s', trial_job_id)
send(CommandType.KillTrialJob, json_tricks.dumps(trial_job_id))
# notify tuner
_logger.debug('env var: NNI_INCLUDE_INTERMEDIATE_RESULTS: [%s]', dispatcher_env_vars.NNI_INCLUDE_INTERMEDIATE_RESULTS)
if dispatcher_env_vars.NNI_INCLUDE_INTERMEDIATE_RESULTS == 'true':
self._earlystop_notify_tuner(data)
else:
_logger.debug('GOOD') | [
"def",
"_handle_intermediate_metric_data",
"(",
"self",
",",
"data",
")",
":",
"if",
"data",
"[",
"'type'",
"]",
"!=",
"'PERIODICAL'",
":",
"return",
"if",
"self",
".",
"assessor",
"is",
"None",
":",
"return",
"trial_job_id",
"=",
"data",
"[",
"'trial_job_id'",
"]",
"if",
"trial_job_id",
"in",
"_ended_trials",
":",
"return",
"history",
"=",
"_trial_history",
"[",
"trial_job_id",
"]",
"history",
"[",
"data",
"[",
"'sequence'",
"]",
"]",
"=",
"data",
"[",
"'value'",
"]",
"ordered_history",
"=",
"_sort_history",
"(",
"history",
")",
"if",
"len",
"(",
"ordered_history",
")",
"<",
"data",
"[",
"'sequence'",
"]",
":",
"# no user-visible update since last time",
"return",
"try",
":",
"result",
"=",
"self",
".",
"assessor",
".",
"assess_trial",
"(",
"trial_job_id",
",",
"ordered_history",
")",
"except",
"Exception",
"as",
"e",
":",
"_logger",
".",
"exception",
"(",
"'Assessor error'",
")",
"if",
"isinstance",
"(",
"result",
",",
"bool",
")",
":",
"result",
"=",
"AssessResult",
".",
"Good",
"if",
"result",
"else",
"AssessResult",
".",
"Bad",
"elif",
"not",
"isinstance",
"(",
"result",
",",
"AssessResult",
")",
":",
"msg",
"=",
"'Result of Assessor.assess_trial must be an object of AssessResult, not %s'",
"raise",
"RuntimeError",
"(",
"msg",
"%",
"type",
"(",
"result",
")",
")",
"if",
"result",
"is",
"AssessResult",
".",
"Bad",
":",
"_logger",
".",
"debug",
"(",
"'BAD, kill %s'",
",",
"trial_job_id",
")",
"send",
"(",
"CommandType",
".",
"KillTrialJob",
",",
"json_tricks",
".",
"dumps",
"(",
"trial_job_id",
")",
")",
"# notify tuner",
"_logger",
".",
"debug",
"(",
"'env var: NNI_INCLUDE_INTERMEDIATE_RESULTS: [%s]'",
",",
"dispatcher_env_vars",
".",
"NNI_INCLUDE_INTERMEDIATE_RESULTS",
")",
"if",
"dispatcher_env_vars",
".",
"NNI_INCLUDE_INTERMEDIATE_RESULTS",
"==",
"'true'",
":",
"self",
".",
"_earlystop_notify_tuner",
"(",
"data",
")",
"else",
":",
"_logger",
".",
"debug",
"(",
"'GOOD'",
")"
] | Call assessor to process intermediate results | [
"Call",
"assessor",
"to",
"process",
"intermediate",
"results"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/msg_dispatcher.py#L167-L204 |
27,027 | Microsoft/nni | src/sdk/pynni/nni/msg_dispatcher.py | MsgDispatcher._earlystop_notify_tuner | def _earlystop_notify_tuner(self, data):
"""Send last intermediate result as final result to tuner in case the
trial is early stopped.
"""
_logger.debug('Early stop notify tuner data: [%s]', data)
data['type'] = 'FINAL'
if multi_thread_enabled():
self._handle_final_metric_data(data)
else:
self.enqueue_command(CommandType.ReportMetricData, data) | python | def _earlystop_notify_tuner(self, data):
"""Send last intermediate result as final result to tuner in case the
trial is early stopped.
"""
_logger.debug('Early stop notify tuner data: [%s]', data)
data['type'] = 'FINAL'
if multi_thread_enabled():
self._handle_final_metric_data(data)
else:
self.enqueue_command(CommandType.ReportMetricData, data) | [
"def",
"_earlystop_notify_tuner",
"(",
"self",
",",
"data",
")",
":",
"_logger",
".",
"debug",
"(",
"'Early stop notify tuner data: [%s]'",
",",
"data",
")",
"data",
"[",
"'type'",
"]",
"=",
"'FINAL'",
"if",
"multi_thread_enabled",
"(",
")",
":",
"self",
".",
"_handle_final_metric_data",
"(",
"data",
")",
"else",
":",
"self",
".",
"enqueue_command",
"(",
"CommandType",
".",
"ReportMetricData",
",",
"data",
")"
] | Send last intermediate result as final result to tuner in case the
trial is early stopped. | [
"Send",
"last",
"intermediate",
"result",
"as",
"final",
"result",
"to",
"tuner",
"in",
"case",
"the",
"trial",
"is",
"early",
"stopped",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/msg_dispatcher.py#L206-L215 |
27,028 | Microsoft/nni | examples/trials/network_morphism/FashionMNIST/FashionMNIST_keras.py | train_eval | def train_eval():
""" train and eval the model
"""
global trainloader
global testloader
global net
(x_train, y_train) = trainloader
(x_test, y_test) = testloader
# train procedure
net.fit(
x=x_train,
y=y_train,
batch_size=args.batch_size,
validation_data=(x_test, y_test),
epochs=args.epochs,
shuffle=True,
callbacks=[
SendMetrics(),
EarlyStopping(min_delta=0.001, patience=10),
TensorBoard(log_dir=TENSORBOARD_DIR),
],
)
# trial report final acc to tuner
_, acc = net.evaluate(x_test, y_test)
logger.debug("Final result is: %.3f", acc)
nni.report_final_result(acc) | python | def train_eval():
""" train and eval the model
"""
global trainloader
global testloader
global net
(x_train, y_train) = trainloader
(x_test, y_test) = testloader
# train procedure
net.fit(
x=x_train,
y=y_train,
batch_size=args.batch_size,
validation_data=(x_test, y_test),
epochs=args.epochs,
shuffle=True,
callbacks=[
SendMetrics(),
EarlyStopping(min_delta=0.001, patience=10),
TensorBoard(log_dir=TENSORBOARD_DIR),
],
)
# trial report final acc to tuner
_, acc = net.evaluate(x_test, y_test)
logger.debug("Final result is: %.3f", acc)
nni.report_final_result(acc) | [
"def",
"train_eval",
"(",
")",
":",
"global",
"trainloader",
"global",
"testloader",
"global",
"net",
"(",
"x_train",
",",
"y_train",
")",
"=",
"trainloader",
"(",
"x_test",
",",
"y_test",
")",
"=",
"testloader",
"# train procedure",
"net",
".",
"fit",
"(",
"x",
"=",
"x_train",
",",
"y",
"=",
"y_train",
",",
"batch_size",
"=",
"args",
".",
"batch_size",
",",
"validation_data",
"=",
"(",
"x_test",
",",
"y_test",
")",
",",
"epochs",
"=",
"args",
".",
"epochs",
",",
"shuffle",
"=",
"True",
",",
"callbacks",
"=",
"[",
"SendMetrics",
"(",
")",
",",
"EarlyStopping",
"(",
"min_delta",
"=",
"0.001",
",",
"patience",
"=",
"10",
")",
",",
"TensorBoard",
"(",
"log_dir",
"=",
"TENSORBOARD_DIR",
")",
",",
"]",
",",
")",
"# trial report final acc to tuner",
"_",
",",
"acc",
"=",
"net",
".",
"evaluate",
"(",
"x_test",
",",
"y_test",
")",
"logger",
".",
"debug",
"(",
"\"Final result is: %.3f\"",
",",
"acc",
")",
"nni",
".",
"report_final_result",
"(",
"acc",
")"
] | train and eval the model | [
"train",
"and",
"eval",
"the",
"model"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/network_morphism/FashionMNIST/FashionMNIST_keras.py#L159-L188 |
27,029 | Microsoft/nni | src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py | Bracket.get_n_r | def get_n_r(self):
"""return the values of n and r for the next round"""
return math.floor(self.n / self.eta**self.i + _epsilon), math.floor(self.r * self.eta**self.i + _epsilon) | python | def get_n_r(self):
"""return the values of n and r for the next round"""
return math.floor(self.n / self.eta**self.i + _epsilon), math.floor(self.r * self.eta**self.i + _epsilon) | [
"def",
"get_n_r",
"(",
"self",
")",
":",
"return",
"math",
".",
"floor",
"(",
"self",
".",
"n",
"/",
"self",
".",
"eta",
"**",
"self",
".",
"i",
"+",
"_epsilon",
")",
",",
"math",
".",
"floor",
"(",
"self",
".",
"r",
"*",
"self",
".",
"eta",
"**",
"self",
".",
"i",
"+",
"_epsilon",
")"
] | return the values of n and r for the next round | [
"return",
"the",
"values",
"of",
"n",
"and",
"r",
"for",
"the",
"next",
"round"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py#L159-L161 |
27,030 | Microsoft/nni | src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py | Bracket.increase_i | def increase_i(self):
"""i means the ith round. Increase i by 1"""
self.i += 1
if self.i > self.bracket_id:
self.no_more_trial = True | python | def increase_i(self):
"""i means the ith round. Increase i by 1"""
self.i += 1
if self.i > self.bracket_id:
self.no_more_trial = True | [
"def",
"increase_i",
"(",
"self",
")",
":",
"self",
".",
"i",
"+=",
"1",
"if",
"self",
".",
"i",
">",
"self",
".",
"bracket_id",
":",
"self",
".",
"no_more_trial",
"=",
"True"
] | i means the ith round. Increase i by 1 | [
"i",
"means",
"the",
"ith",
"round",
".",
"Increase",
"i",
"by",
"1"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py#L163-L167 |
27,031 | Microsoft/nni | src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py | Bracket.get_hyperparameter_configurations | def get_hyperparameter_configurations(self, num, r, searchspace_json, random_state): # pylint: disable=invalid-name
"""Randomly generate num hyperparameter configurations from search space
Parameters
----------
num: int
the number of hyperparameter configurations
Returns
-------
list
a list of hyperparameter configurations. Format: [[key1, value1], [key2, value2], ...]
"""
global _KEY # pylint: disable=global-statement
assert self.i == 0
hyperparameter_configs = dict()
for _ in range(num):
params_id = create_bracket_parameter_id(self.bracket_id, self.i)
params = json2paramater(searchspace_json, random_state)
params[_KEY] = r
hyperparameter_configs[params_id] = params
self._record_hyper_configs(hyperparameter_configs)
return [[key, value] for key, value in hyperparameter_configs.items()] | python | def get_hyperparameter_configurations(self, num, r, searchspace_json, random_state): # pylint: disable=invalid-name
"""Randomly generate num hyperparameter configurations from search space
Parameters
----------
num: int
the number of hyperparameter configurations
Returns
-------
list
a list of hyperparameter configurations. Format: [[key1, value1], [key2, value2], ...]
"""
global _KEY # pylint: disable=global-statement
assert self.i == 0
hyperparameter_configs = dict()
for _ in range(num):
params_id = create_bracket_parameter_id(self.bracket_id, self.i)
params = json2paramater(searchspace_json, random_state)
params[_KEY] = r
hyperparameter_configs[params_id] = params
self._record_hyper_configs(hyperparameter_configs)
return [[key, value] for key, value in hyperparameter_configs.items()] | [
"def",
"get_hyperparameter_configurations",
"(",
"self",
",",
"num",
",",
"r",
",",
"searchspace_json",
",",
"random_state",
")",
":",
"# pylint: disable=invalid-name",
"global",
"_KEY",
"# pylint: disable=global-statement",
"assert",
"self",
".",
"i",
"==",
"0",
"hyperparameter_configs",
"=",
"dict",
"(",
")",
"for",
"_",
"in",
"range",
"(",
"num",
")",
":",
"params_id",
"=",
"create_bracket_parameter_id",
"(",
"self",
".",
"bracket_id",
",",
"self",
".",
"i",
")",
"params",
"=",
"json2paramater",
"(",
"searchspace_json",
",",
"random_state",
")",
"params",
"[",
"_KEY",
"]",
"=",
"r",
"hyperparameter_configs",
"[",
"params_id",
"]",
"=",
"params",
"self",
".",
"_record_hyper_configs",
"(",
"hyperparameter_configs",
")",
"return",
"[",
"[",
"key",
",",
"value",
"]",
"for",
"key",
",",
"value",
"in",
"hyperparameter_configs",
".",
"items",
"(",
")",
"]"
] | Randomly generate num hyperparameter configurations from search space
Parameters
----------
num: int
the number of hyperparameter configurations
Returns
-------
list
a list of hyperparameter configurations. Format: [[key1, value1], [key2, value2], ...] | [
"Randomly",
"generate",
"num",
"hyperparameter",
"configurations",
"from",
"search",
"space"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py#L231-L253 |
27,032 | Microsoft/nni | src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py | Bracket._record_hyper_configs | def _record_hyper_configs(self, hyper_configs):
"""after generating one round of hyperconfigs, this function records the generated hyperconfigs,
creates a dict to record the performance when those hyperconifgs are running, set the number of finished configs
in this round to be 0, and increase the round number.
Parameters
----------
hyper_configs: list
the generated hyperconfigs
"""
self.hyper_configs.append(hyper_configs)
self.configs_perf.append(dict())
self.num_finished_configs.append(0)
self.num_configs_to_run.append(len(hyper_configs))
self.increase_i() | python | def _record_hyper_configs(self, hyper_configs):
"""after generating one round of hyperconfigs, this function records the generated hyperconfigs,
creates a dict to record the performance when those hyperconifgs are running, set the number of finished configs
in this round to be 0, and increase the round number.
Parameters
----------
hyper_configs: list
the generated hyperconfigs
"""
self.hyper_configs.append(hyper_configs)
self.configs_perf.append(dict())
self.num_finished_configs.append(0)
self.num_configs_to_run.append(len(hyper_configs))
self.increase_i() | [
"def",
"_record_hyper_configs",
"(",
"self",
",",
"hyper_configs",
")",
":",
"self",
".",
"hyper_configs",
".",
"append",
"(",
"hyper_configs",
")",
"self",
".",
"configs_perf",
".",
"append",
"(",
"dict",
"(",
")",
")",
"self",
".",
"num_finished_configs",
".",
"append",
"(",
"0",
")",
"self",
".",
"num_configs_to_run",
".",
"append",
"(",
"len",
"(",
"hyper_configs",
")",
")",
"self",
".",
"increase_i",
"(",
")"
] | after generating one round of hyperconfigs, this function records the generated hyperconfigs,
creates a dict to record the performance when those hyperconifgs are running, set the number of finished configs
in this round to be 0, and increase the round number.
Parameters
----------
hyper_configs: list
the generated hyperconfigs | [
"after",
"generating",
"one",
"round",
"of",
"hyperconfigs",
"this",
"function",
"records",
"the",
"generated",
"hyperconfigs",
"creates",
"a",
"dict",
"to",
"record",
"the",
"performance",
"when",
"those",
"hyperconifgs",
"are",
"running",
"set",
"the",
"number",
"of",
"finished",
"configs",
"in",
"this",
"round",
"to",
"be",
"0",
"and",
"increase",
"the",
"round",
"number",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/hyperband_advisor/hyperband_advisor.py#L255-L269 |
27,033 | Microsoft/nni | tools/nni_trial_tool/url_utils.py | gen_send_stdout_url | def gen_send_stdout_url(ip, port):
'''Generate send stdout url'''
return '{0}:{1}{2}{3}/{4}/{5}'.format(BASE_URL.format(ip), port, API_ROOT_URL, STDOUT_API, NNI_EXP_ID, NNI_TRIAL_JOB_ID) | python | def gen_send_stdout_url(ip, port):
'''Generate send stdout url'''
return '{0}:{1}{2}{3}/{4}/{5}'.format(BASE_URL.format(ip), port, API_ROOT_URL, STDOUT_API, NNI_EXP_ID, NNI_TRIAL_JOB_ID) | [
"def",
"gen_send_stdout_url",
"(",
"ip",
",",
"port",
")",
":",
"return",
"'{0}:{1}{2}{3}/{4}/{5}'",
".",
"format",
"(",
"BASE_URL",
".",
"format",
"(",
"ip",
")",
",",
"port",
",",
"API_ROOT_URL",
",",
"STDOUT_API",
",",
"NNI_EXP_ID",
",",
"NNI_TRIAL_JOB_ID",
")"
] | Generate send stdout url | [
"Generate",
"send",
"stdout",
"url"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_trial_tool/url_utils.py#L23-L25 |
27,034 | Microsoft/nni | tools/nni_trial_tool/url_utils.py | gen_send_version_url | def gen_send_version_url(ip, port):
'''Generate send error url'''
return '{0}:{1}{2}{3}/{4}/{5}'.format(BASE_URL.format(ip), port, API_ROOT_URL, VERSION_API, NNI_EXP_ID, NNI_TRIAL_JOB_ID) | python | def gen_send_version_url(ip, port):
'''Generate send error url'''
return '{0}:{1}{2}{3}/{4}/{5}'.format(BASE_URL.format(ip), port, API_ROOT_URL, VERSION_API, NNI_EXP_ID, NNI_TRIAL_JOB_ID) | [
"def",
"gen_send_version_url",
"(",
"ip",
",",
"port",
")",
":",
"return",
"'{0}:{1}{2}{3}/{4}/{5}'",
".",
"format",
"(",
"BASE_URL",
".",
"format",
"(",
"ip",
")",
",",
"port",
",",
"API_ROOT_URL",
",",
"VERSION_API",
",",
"NNI_EXP_ID",
",",
"NNI_TRIAL_JOB_ID",
")"
] | Generate send error url | [
"Generate",
"send",
"error",
"url"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_trial_tool/url_utils.py#L27-L29 |
27,035 | Microsoft/nni | tools/nni_cmd/updater.py | validate_digit | def validate_digit(value, start, end):
'''validate if a digit is valid'''
if not str(value).isdigit() or int(value) < start or int(value) > end:
raise ValueError('%s must be a digit from %s to %s' % (value, start, end)) | python | def validate_digit(value, start, end):
'''validate if a digit is valid'''
if not str(value).isdigit() or int(value) < start or int(value) > end:
raise ValueError('%s must be a digit from %s to %s' % (value, start, end)) | [
"def",
"validate_digit",
"(",
"value",
",",
"start",
",",
"end",
")",
":",
"if",
"not",
"str",
"(",
"value",
")",
".",
"isdigit",
"(",
")",
"or",
"int",
"(",
"value",
")",
"<",
"start",
"or",
"int",
"(",
"value",
")",
">",
"end",
":",
"raise",
"ValueError",
"(",
"'%s must be a digit from %s to %s'",
"%",
"(",
"value",
",",
"start",
",",
"end",
")",
")"
] | validate if a digit is valid | [
"validate",
"if",
"a",
"digit",
"is",
"valid"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L32-L35 |
27,036 | Microsoft/nni | tools/nni_cmd/updater.py | validate_dispatcher | def validate_dispatcher(args):
'''validate if the dispatcher of the experiment supports importing data'''
nni_config = Config(get_config_filename(args)).get_config('experimentConfig')
if nni_config.get('tuner') and nni_config['tuner'].get('builtinTunerName'):
dispatcher_name = nni_config['tuner']['builtinTunerName']
elif nni_config.get('advisor') and nni_config['advisor'].get('builtinAdvisorName'):
dispatcher_name = nni_config['advisor']['builtinAdvisorName']
else: # otherwise it should be a customized one
return
if dispatcher_name not in TUNERS_SUPPORTING_IMPORT_DATA:
if dispatcher_name in TUNERS_NO_NEED_TO_IMPORT_DATA:
print_warning("There is no need to import data for %s" % dispatcher_name)
exit(0)
else:
print_error("%s does not support importing addtional data" % dispatcher_name)
exit(1) | python | def validate_dispatcher(args):
'''validate if the dispatcher of the experiment supports importing data'''
nni_config = Config(get_config_filename(args)).get_config('experimentConfig')
if nni_config.get('tuner') and nni_config['tuner'].get('builtinTunerName'):
dispatcher_name = nni_config['tuner']['builtinTunerName']
elif nni_config.get('advisor') and nni_config['advisor'].get('builtinAdvisorName'):
dispatcher_name = nni_config['advisor']['builtinAdvisorName']
else: # otherwise it should be a customized one
return
if dispatcher_name not in TUNERS_SUPPORTING_IMPORT_DATA:
if dispatcher_name in TUNERS_NO_NEED_TO_IMPORT_DATA:
print_warning("There is no need to import data for %s" % dispatcher_name)
exit(0)
else:
print_error("%s does not support importing addtional data" % dispatcher_name)
exit(1) | [
"def",
"validate_dispatcher",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
".",
"get_config",
"(",
"'experimentConfig'",
")",
"if",
"nni_config",
".",
"get",
"(",
"'tuner'",
")",
"and",
"nni_config",
"[",
"'tuner'",
"]",
".",
"get",
"(",
"'builtinTunerName'",
")",
":",
"dispatcher_name",
"=",
"nni_config",
"[",
"'tuner'",
"]",
"[",
"'builtinTunerName'",
"]",
"elif",
"nni_config",
".",
"get",
"(",
"'advisor'",
")",
"and",
"nni_config",
"[",
"'advisor'",
"]",
".",
"get",
"(",
"'builtinAdvisorName'",
")",
":",
"dispatcher_name",
"=",
"nni_config",
"[",
"'advisor'",
"]",
"[",
"'builtinAdvisorName'",
"]",
"else",
":",
"# otherwise it should be a customized one",
"return",
"if",
"dispatcher_name",
"not",
"in",
"TUNERS_SUPPORTING_IMPORT_DATA",
":",
"if",
"dispatcher_name",
"in",
"TUNERS_NO_NEED_TO_IMPORT_DATA",
":",
"print_warning",
"(",
"\"There is no need to import data for %s\"",
"%",
"dispatcher_name",
")",
"exit",
"(",
"0",
")",
"else",
":",
"print_error",
"(",
"\"%s does not support importing addtional data\"",
"%",
"dispatcher_name",
")",
"exit",
"(",
"1",
")"
] | validate if the dispatcher of the experiment supports importing data | [
"validate",
"if",
"the",
"dispatcher",
"of",
"the",
"experiment",
"supports",
"importing",
"data"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L42-L57 |
27,037 | Microsoft/nni | tools/nni_cmd/updater.py | load_search_space | def load_search_space(path):
'''load search space content'''
content = json.dumps(get_json_content(path))
if not content:
raise ValueError('searchSpace file should not be empty')
return content | python | def load_search_space(path):
'''load search space content'''
content = json.dumps(get_json_content(path))
if not content:
raise ValueError('searchSpace file should not be empty')
return content | [
"def",
"load_search_space",
"(",
"path",
")",
":",
"content",
"=",
"json",
".",
"dumps",
"(",
"get_json_content",
"(",
"path",
")",
")",
"if",
"not",
"content",
":",
"raise",
"ValueError",
"(",
"'searchSpace file should not be empty'",
")",
"return",
"content"
] | load search space content | [
"load",
"search",
"space",
"content"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L59-L64 |
27,038 | Microsoft/nni | tools/nni_cmd/updater.py | update_experiment_profile | def update_experiment_profile(args, key, value):
'''call restful server to update experiment profile'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_get(experiment_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
experiment_profile = json.loads(response.text)
experiment_profile['params'][key] = value
response = rest_put(experiment_url(rest_port)+get_query_type(key), json.dumps(experiment_profile), REST_TIME_OUT)
if response and check_response(response):
return response
else:
print_error('Restful server is not running...')
return None | python | def update_experiment_profile(args, key, value):
'''call restful server to update experiment profile'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_get(experiment_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
experiment_profile = json.loads(response.text)
experiment_profile['params'][key] = value
response = rest_put(experiment_url(rest_port)+get_query_type(key), json.dumps(experiment_profile), REST_TIME_OUT)
if response and check_response(response):
return response
else:
print_error('Restful server is not running...')
return None | [
"def",
"update_experiment_profile",
"(",
"args",
",",
"key",
",",
"value",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"running",
",",
"_",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"running",
":",
"response",
"=",
"rest_get",
"(",
"experiment_url",
"(",
"rest_port",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"experiment_profile",
"=",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
"experiment_profile",
"[",
"'params'",
"]",
"[",
"key",
"]",
"=",
"value",
"response",
"=",
"rest_put",
"(",
"experiment_url",
"(",
"rest_port",
")",
"+",
"get_query_type",
"(",
"key",
")",
",",
"json",
".",
"dumps",
"(",
"experiment_profile",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"return",
"response",
"else",
":",
"print_error",
"(",
"'Restful server is not running...'",
")",
"return",
"None"
] | call restful server to update experiment profile | [
"call",
"restful",
"server",
"to",
"update",
"experiment",
"profile"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L77-L92 |
27,039 | Microsoft/nni | tools/nni_cmd/updater.py | import_data | def import_data(args):
'''import additional data to the experiment'''
validate_file(args.filename)
validate_dispatcher(args)
content = load_search_space(args.filename)
args.port = get_experiment_port(args)
if args.port is not None:
if import_data_to_restful_server(args, content):
pass
else:
print_error('Import data failed!') | python | def import_data(args):
'''import additional data to the experiment'''
validate_file(args.filename)
validate_dispatcher(args)
content = load_search_space(args.filename)
args.port = get_experiment_port(args)
if args.port is not None:
if import_data_to_restful_server(args, content):
pass
else:
print_error('Import data failed!') | [
"def",
"import_data",
"(",
"args",
")",
":",
"validate_file",
"(",
"args",
".",
"filename",
")",
"validate_dispatcher",
"(",
"args",
")",
"content",
"=",
"load_search_space",
"(",
"args",
".",
"filename",
")",
"args",
".",
"port",
"=",
"get_experiment_port",
"(",
"args",
")",
"if",
"args",
".",
"port",
"is",
"not",
"None",
":",
"if",
"import_data_to_restful_server",
"(",
"args",
",",
"content",
")",
":",
"pass",
"else",
":",
"print_error",
"(",
"'Import data failed!'",
")"
] | import additional data to the experiment | [
"import",
"additional",
"data",
"to",
"the",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L131-L141 |
27,040 | Microsoft/nni | tools/nni_cmd/updater.py | import_data_to_restful_server | def import_data_to_restful_server(args, content):
'''call restful server to import data to the experiment'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_post(import_data_url(rest_port), content, REST_TIME_OUT)
if response and check_response(response):
return response
else:
print_error('Restful server is not running...')
return None | python | def import_data_to_restful_server(args, content):
'''call restful server to import data to the experiment'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_post(import_data_url(rest_port), content, REST_TIME_OUT)
if response and check_response(response):
return response
else:
print_error('Restful server is not running...')
return None | [
"def",
"import_data_to_restful_server",
"(",
"args",
",",
"content",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"running",
",",
"_",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"running",
":",
"response",
"=",
"rest_post",
"(",
"import_data_url",
"(",
"rest_port",
")",
",",
"content",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"return",
"response",
"else",
":",
"print_error",
"(",
"'Restful server is not running...'",
")",
"return",
"None"
] | call restful server to import data to the experiment | [
"call",
"restful",
"server",
"to",
"import",
"data",
"to",
"the",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/updater.py#L143-L154 |
27,041 | Microsoft/nni | tools/nni_cmd/config_schema.py | setType | def setType(key, type):
'''check key type'''
return And(type, error=SCHEMA_TYPE_ERROR % (key, type.__name__)) | python | def setType(key, type):
'''check key type'''
return And(type, error=SCHEMA_TYPE_ERROR % (key, type.__name__)) | [
"def",
"setType",
"(",
"key",
",",
"type",
")",
":",
"return",
"And",
"(",
"type",
",",
"error",
"=",
"SCHEMA_TYPE_ERROR",
"%",
"(",
"key",
",",
"type",
".",
"__name__",
")",
")"
] | check key type | [
"check",
"key",
"type"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/config_schema.py#L26-L28 |
27,042 | Microsoft/nni | tools/nni_cmd/config_schema.py | setNumberRange | def setNumberRange(key, keyType, start, end):
'''check number range'''
return And(
And(keyType, error=SCHEMA_TYPE_ERROR % (key, keyType.__name__)),
And(lambda n: start <= n <= end, error=SCHEMA_RANGE_ERROR % (key, '(%s,%s)' % (start, end))),
) | python | def setNumberRange(key, keyType, start, end):
'''check number range'''
return And(
And(keyType, error=SCHEMA_TYPE_ERROR % (key, keyType.__name__)),
And(lambda n: start <= n <= end, error=SCHEMA_RANGE_ERROR % (key, '(%s,%s)' % (start, end))),
) | [
"def",
"setNumberRange",
"(",
"key",
",",
"keyType",
",",
"start",
",",
"end",
")",
":",
"return",
"And",
"(",
"And",
"(",
"keyType",
",",
"error",
"=",
"SCHEMA_TYPE_ERROR",
"%",
"(",
"key",
",",
"keyType",
".",
"__name__",
")",
")",
",",
"And",
"(",
"lambda",
"n",
":",
"start",
"<=",
"n",
"<=",
"end",
",",
"error",
"=",
"SCHEMA_RANGE_ERROR",
"%",
"(",
"key",
",",
"'(%s,%s)'",
"%",
"(",
"start",
",",
"end",
")",
")",
")",
",",
")"
] | check number range | [
"check",
"number",
"range"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/config_schema.py#L34-L39 |
27,043 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/layers.py | keras_dropout | def keras_dropout(layer, rate):
'''keras dropout layer.
'''
from keras import layers
input_dim = len(layer.input.shape)
if input_dim == 2:
return layers.SpatialDropout1D(rate)
elif input_dim == 3:
return layers.SpatialDropout2D(rate)
elif input_dim == 4:
return layers.SpatialDropout3D(rate)
else:
return layers.Dropout(rate) | python | def keras_dropout(layer, rate):
'''keras dropout layer.
'''
from keras import layers
input_dim = len(layer.input.shape)
if input_dim == 2:
return layers.SpatialDropout1D(rate)
elif input_dim == 3:
return layers.SpatialDropout2D(rate)
elif input_dim == 4:
return layers.SpatialDropout3D(rate)
else:
return layers.Dropout(rate) | [
"def",
"keras_dropout",
"(",
"layer",
",",
"rate",
")",
":",
"from",
"keras",
"import",
"layers",
"input_dim",
"=",
"len",
"(",
"layer",
".",
"input",
".",
"shape",
")",
"if",
"input_dim",
"==",
"2",
":",
"return",
"layers",
".",
"SpatialDropout1D",
"(",
"rate",
")",
"elif",
"input_dim",
"==",
"3",
":",
"return",
"layers",
".",
"SpatialDropout2D",
"(",
"rate",
")",
"elif",
"input_dim",
"==",
"4",
":",
"return",
"layers",
".",
"SpatialDropout3D",
"(",
"rate",
")",
"else",
":",
"return",
"layers",
".",
"Dropout",
"(",
"rate",
")"
] | keras dropout layer. | [
"keras",
"dropout",
"layer",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/layers.py#L530-L544 |
27,044 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/layers.py | to_real_keras_layer | def to_real_keras_layer(layer):
''' real keras layer.
'''
from keras import layers
if is_layer(layer, "Dense"):
return layers.Dense(layer.units, input_shape=(layer.input_units,))
if is_layer(layer, "Conv"):
return layers.Conv2D(
layer.filters,
layer.kernel_size,
input_shape=layer.input.shape,
padding="same",
) # padding
if is_layer(layer, "Pooling"):
return layers.MaxPool2D(2)
if is_layer(layer, "BatchNormalization"):
return layers.BatchNormalization(input_shape=layer.input.shape)
if is_layer(layer, "Concatenate"):
return layers.Concatenate()
if is_layer(layer, "Add"):
return layers.Add()
if is_layer(layer, "Dropout"):
return keras_dropout(layer, layer.rate)
if is_layer(layer, "ReLU"):
return layers.Activation("relu")
if is_layer(layer, "Softmax"):
return layers.Activation("softmax")
if is_layer(layer, "Flatten"):
return layers.Flatten()
if is_layer(layer, "GlobalAveragePooling"):
return layers.GlobalAveragePooling2D() | python | def to_real_keras_layer(layer):
''' real keras layer.
'''
from keras import layers
if is_layer(layer, "Dense"):
return layers.Dense(layer.units, input_shape=(layer.input_units,))
if is_layer(layer, "Conv"):
return layers.Conv2D(
layer.filters,
layer.kernel_size,
input_shape=layer.input.shape,
padding="same",
) # padding
if is_layer(layer, "Pooling"):
return layers.MaxPool2D(2)
if is_layer(layer, "BatchNormalization"):
return layers.BatchNormalization(input_shape=layer.input.shape)
if is_layer(layer, "Concatenate"):
return layers.Concatenate()
if is_layer(layer, "Add"):
return layers.Add()
if is_layer(layer, "Dropout"):
return keras_dropout(layer, layer.rate)
if is_layer(layer, "ReLU"):
return layers.Activation("relu")
if is_layer(layer, "Softmax"):
return layers.Activation("softmax")
if is_layer(layer, "Flatten"):
return layers.Flatten()
if is_layer(layer, "GlobalAveragePooling"):
return layers.GlobalAveragePooling2D() | [
"def",
"to_real_keras_layer",
"(",
"layer",
")",
":",
"from",
"keras",
"import",
"layers",
"if",
"is_layer",
"(",
"layer",
",",
"\"Dense\"",
")",
":",
"return",
"layers",
".",
"Dense",
"(",
"layer",
".",
"units",
",",
"input_shape",
"=",
"(",
"layer",
".",
"input_units",
",",
")",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Conv\"",
")",
":",
"return",
"layers",
".",
"Conv2D",
"(",
"layer",
".",
"filters",
",",
"layer",
".",
"kernel_size",
",",
"input_shape",
"=",
"layer",
".",
"input",
".",
"shape",
",",
"padding",
"=",
"\"same\"",
",",
")",
"# padding",
"if",
"is_layer",
"(",
"layer",
",",
"\"Pooling\"",
")",
":",
"return",
"layers",
".",
"MaxPool2D",
"(",
"2",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"BatchNormalization\"",
")",
":",
"return",
"layers",
".",
"BatchNormalization",
"(",
"input_shape",
"=",
"layer",
".",
"input",
".",
"shape",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Concatenate\"",
")",
":",
"return",
"layers",
".",
"Concatenate",
"(",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Add\"",
")",
":",
"return",
"layers",
".",
"Add",
"(",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Dropout\"",
")",
":",
"return",
"keras_dropout",
"(",
"layer",
",",
"layer",
".",
"rate",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"ReLU\"",
")",
":",
"return",
"layers",
".",
"Activation",
"(",
"\"relu\"",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Softmax\"",
")",
":",
"return",
"layers",
".",
"Activation",
"(",
"\"softmax\"",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"Flatten\"",
")",
":",
"return",
"layers",
".",
"Flatten",
"(",
")",
"if",
"is_layer",
"(",
"layer",
",",
"\"GlobalAveragePooling\"",
")",
":",
"return",
"layers",
".",
"GlobalAveragePooling2D",
"(",
")"
] | real keras layer. | [
"real",
"keras",
"layer",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/layers.py#L547-L578 |
27,045 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/layers.py | layer_description_extractor | def layer_description_extractor(layer, node_to_id):
'''get layer description.
'''
layer_input = layer.input
layer_output = layer.output
if layer_input is not None:
if isinstance(layer_input, Iterable):
layer_input = list(map(lambda x: node_to_id[x], layer_input))
else:
layer_input = node_to_id[layer_input]
if layer_output is not None:
layer_output = node_to_id[layer_output]
if isinstance(layer, StubConv):
return (
type(layer).__name__,
layer_input,
layer_output,
layer.input_channel,
layer.filters,
layer.kernel_size,
layer.stride,
layer.padding,
)
elif isinstance(layer, (StubDense,)):
return [
type(layer).__name__,
layer_input,
layer_output,
layer.input_units,
layer.units,
]
elif isinstance(layer, (StubBatchNormalization,)):
return (type(layer).__name__, layer_input, layer_output, layer.num_features)
elif isinstance(layer, (StubDropout,)):
return (type(layer).__name__, layer_input, layer_output, layer.rate)
elif isinstance(layer, StubPooling):
return (
type(layer).__name__,
layer_input,
layer_output,
layer.kernel_size,
layer.stride,
layer.padding,
)
else:
return (type(layer).__name__, layer_input, layer_output) | python | def layer_description_extractor(layer, node_to_id):
'''get layer description.
'''
layer_input = layer.input
layer_output = layer.output
if layer_input is not None:
if isinstance(layer_input, Iterable):
layer_input = list(map(lambda x: node_to_id[x], layer_input))
else:
layer_input = node_to_id[layer_input]
if layer_output is not None:
layer_output = node_to_id[layer_output]
if isinstance(layer, StubConv):
return (
type(layer).__name__,
layer_input,
layer_output,
layer.input_channel,
layer.filters,
layer.kernel_size,
layer.stride,
layer.padding,
)
elif isinstance(layer, (StubDense,)):
return [
type(layer).__name__,
layer_input,
layer_output,
layer.input_units,
layer.units,
]
elif isinstance(layer, (StubBatchNormalization,)):
return (type(layer).__name__, layer_input, layer_output, layer.num_features)
elif isinstance(layer, (StubDropout,)):
return (type(layer).__name__, layer_input, layer_output, layer.rate)
elif isinstance(layer, StubPooling):
return (
type(layer).__name__,
layer_input,
layer_output,
layer.kernel_size,
layer.stride,
layer.padding,
)
else:
return (type(layer).__name__, layer_input, layer_output) | [
"def",
"layer_description_extractor",
"(",
"layer",
",",
"node_to_id",
")",
":",
"layer_input",
"=",
"layer",
".",
"input",
"layer_output",
"=",
"layer",
".",
"output",
"if",
"layer_input",
"is",
"not",
"None",
":",
"if",
"isinstance",
"(",
"layer_input",
",",
"Iterable",
")",
":",
"layer_input",
"=",
"list",
"(",
"map",
"(",
"lambda",
"x",
":",
"node_to_id",
"[",
"x",
"]",
",",
"layer_input",
")",
")",
"else",
":",
"layer_input",
"=",
"node_to_id",
"[",
"layer_input",
"]",
"if",
"layer_output",
"is",
"not",
"None",
":",
"layer_output",
"=",
"node_to_id",
"[",
"layer_output",
"]",
"if",
"isinstance",
"(",
"layer",
",",
"StubConv",
")",
":",
"return",
"(",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
",",
"layer",
".",
"input_channel",
",",
"layer",
".",
"filters",
",",
"layer",
".",
"kernel_size",
",",
"layer",
".",
"stride",
",",
"layer",
".",
"padding",
",",
")",
"elif",
"isinstance",
"(",
"layer",
",",
"(",
"StubDense",
",",
")",
")",
":",
"return",
"[",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
",",
"layer",
".",
"input_units",
",",
"layer",
".",
"units",
",",
"]",
"elif",
"isinstance",
"(",
"layer",
",",
"(",
"StubBatchNormalization",
",",
")",
")",
":",
"return",
"(",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
",",
"layer",
".",
"num_features",
")",
"elif",
"isinstance",
"(",
"layer",
",",
"(",
"StubDropout",
",",
")",
")",
":",
"return",
"(",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
",",
"layer",
".",
"rate",
")",
"elif",
"isinstance",
"(",
"layer",
",",
"StubPooling",
")",
":",
"return",
"(",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
",",
"layer",
".",
"kernel_size",
",",
"layer",
".",
"stride",
",",
"layer",
".",
"padding",
",",
")",
"else",
":",
"return",
"(",
"type",
"(",
"layer",
")",
".",
"__name__",
",",
"layer_input",
",",
"layer_output",
")"
] | get layer description. | [
"get",
"layer",
"description",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/layers.py#L613-L661 |
27,046 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/layers.py | layer_description_builder | def layer_description_builder(layer_information, id_to_node):
'''build layer from description.
'''
# pylint: disable=W0123
layer_type = layer_information[0]
layer_input_ids = layer_information[1]
if isinstance(layer_input_ids, Iterable):
layer_input = list(map(lambda x: id_to_node[x], layer_input_ids))
else:
layer_input = id_to_node[layer_input_ids]
layer_output = id_to_node[layer_information[2]]
if layer_type.startswith("StubConv"):
input_channel = layer_information[3]
filters = layer_information[4]
kernel_size = layer_information[5]
stride = layer_information[6]
return eval(layer_type)(
input_channel, filters, kernel_size, stride, layer_input, layer_output
)
elif layer_type.startswith("StubDense"):
input_units = layer_information[3]
units = layer_information[4]
return eval(layer_type)(input_units, units, layer_input, layer_output)
elif layer_type.startswith("StubBatchNormalization"):
num_features = layer_information[3]
return eval(layer_type)(num_features, layer_input, layer_output)
elif layer_type.startswith("StubDropout"):
rate = layer_information[3]
return eval(layer_type)(rate, layer_input, layer_output)
elif layer_type.startswith("StubPooling"):
kernel_size = layer_information[3]
stride = layer_information[4]
padding = layer_information[5]
return eval(layer_type)(kernel_size, stride, padding, layer_input, layer_output)
else:
return eval(layer_type)(layer_input, layer_output) | python | def layer_description_builder(layer_information, id_to_node):
'''build layer from description.
'''
# pylint: disable=W0123
layer_type = layer_information[0]
layer_input_ids = layer_information[1]
if isinstance(layer_input_ids, Iterable):
layer_input = list(map(lambda x: id_to_node[x], layer_input_ids))
else:
layer_input = id_to_node[layer_input_ids]
layer_output = id_to_node[layer_information[2]]
if layer_type.startswith("StubConv"):
input_channel = layer_information[3]
filters = layer_information[4]
kernel_size = layer_information[5]
stride = layer_information[6]
return eval(layer_type)(
input_channel, filters, kernel_size, stride, layer_input, layer_output
)
elif layer_type.startswith("StubDense"):
input_units = layer_information[3]
units = layer_information[4]
return eval(layer_type)(input_units, units, layer_input, layer_output)
elif layer_type.startswith("StubBatchNormalization"):
num_features = layer_information[3]
return eval(layer_type)(num_features, layer_input, layer_output)
elif layer_type.startswith("StubDropout"):
rate = layer_information[3]
return eval(layer_type)(rate, layer_input, layer_output)
elif layer_type.startswith("StubPooling"):
kernel_size = layer_information[3]
stride = layer_information[4]
padding = layer_information[5]
return eval(layer_type)(kernel_size, stride, padding, layer_input, layer_output)
else:
return eval(layer_type)(layer_input, layer_output) | [
"def",
"layer_description_builder",
"(",
"layer_information",
",",
"id_to_node",
")",
":",
"# pylint: disable=W0123",
"layer_type",
"=",
"layer_information",
"[",
"0",
"]",
"layer_input_ids",
"=",
"layer_information",
"[",
"1",
"]",
"if",
"isinstance",
"(",
"layer_input_ids",
",",
"Iterable",
")",
":",
"layer_input",
"=",
"list",
"(",
"map",
"(",
"lambda",
"x",
":",
"id_to_node",
"[",
"x",
"]",
",",
"layer_input_ids",
")",
")",
"else",
":",
"layer_input",
"=",
"id_to_node",
"[",
"layer_input_ids",
"]",
"layer_output",
"=",
"id_to_node",
"[",
"layer_information",
"[",
"2",
"]",
"]",
"if",
"layer_type",
".",
"startswith",
"(",
"\"StubConv\"",
")",
":",
"input_channel",
"=",
"layer_information",
"[",
"3",
"]",
"filters",
"=",
"layer_information",
"[",
"4",
"]",
"kernel_size",
"=",
"layer_information",
"[",
"5",
"]",
"stride",
"=",
"layer_information",
"[",
"6",
"]",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"input_channel",
",",
"filters",
",",
"kernel_size",
",",
"stride",
",",
"layer_input",
",",
"layer_output",
")",
"elif",
"layer_type",
".",
"startswith",
"(",
"\"StubDense\"",
")",
":",
"input_units",
"=",
"layer_information",
"[",
"3",
"]",
"units",
"=",
"layer_information",
"[",
"4",
"]",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"input_units",
",",
"units",
",",
"layer_input",
",",
"layer_output",
")",
"elif",
"layer_type",
".",
"startswith",
"(",
"\"StubBatchNormalization\"",
")",
":",
"num_features",
"=",
"layer_information",
"[",
"3",
"]",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"num_features",
",",
"layer_input",
",",
"layer_output",
")",
"elif",
"layer_type",
".",
"startswith",
"(",
"\"StubDropout\"",
")",
":",
"rate",
"=",
"layer_information",
"[",
"3",
"]",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"rate",
",",
"layer_input",
",",
"layer_output",
")",
"elif",
"layer_type",
".",
"startswith",
"(",
"\"StubPooling\"",
")",
":",
"kernel_size",
"=",
"layer_information",
"[",
"3",
"]",
"stride",
"=",
"layer_information",
"[",
"4",
"]",
"padding",
"=",
"layer_information",
"[",
"5",
"]",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"kernel_size",
",",
"stride",
",",
"padding",
",",
"layer_input",
",",
"layer_output",
")",
"else",
":",
"return",
"eval",
"(",
"layer_type",
")",
"(",
"layer_input",
",",
"layer_output",
")"
] | build layer from description. | [
"build",
"layer",
"from",
"description",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/layers.py#L664-L700 |
27,047 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/layers.py | layer_width | def layer_width(layer):
'''get layer width.
'''
if is_layer(layer, "Dense"):
return layer.units
if is_layer(layer, "Conv"):
return layer.filters
raise TypeError("The layer should be either Dense or Conv layer.") | python | def layer_width(layer):
'''get layer width.
'''
if is_layer(layer, "Dense"):
return layer.units
if is_layer(layer, "Conv"):
return layer.filters
raise TypeError("The layer should be either Dense or Conv layer.") | [
"def",
"layer_width",
"(",
"layer",
")",
":",
"if",
"is_layer",
"(",
"layer",
",",
"\"Dense\"",
")",
":",
"return",
"layer",
".",
"units",
"if",
"is_layer",
"(",
"layer",
",",
"\"Conv\"",
")",
":",
"return",
"layer",
".",
"filters",
"raise",
"TypeError",
"(",
"\"The layer should be either Dense or Conv layer.\"",
")"
] | get layer width. | [
"get",
"layer",
"width",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/layers.py#L703-L711 |
27,048 | Microsoft/nni | examples/trials/weight_sharing/ga_squad/rnn.py | GRU.define_params | def define_params(self):
'''
Define parameters.
'''
input_dim = self.input_dim
hidden_dim = self.hidden_dim
prefix = self.name
self.w_matrix = tf.Variable(tf.random_normal([input_dim, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'W']))
self.U = tf.Variable(tf.random_normal([hidden_dim, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'U']))
self.bias = tf.Variable(tf.random_normal([1, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'b']))
return self | python | def define_params(self):
'''
Define parameters.
'''
input_dim = self.input_dim
hidden_dim = self.hidden_dim
prefix = self.name
self.w_matrix = tf.Variable(tf.random_normal([input_dim, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'W']))
self.U = tf.Variable(tf.random_normal([hidden_dim, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'U']))
self.bias = tf.Variable(tf.random_normal([1, 3 * hidden_dim], stddev=0.1),
name='/'.join([prefix, 'b']))
return self | [
"def",
"define_params",
"(",
"self",
")",
":",
"input_dim",
"=",
"self",
".",
"input_dim",
"hidden_dim",
"=",
"self",
".",
"hidden_dim",
"prefix",
"=",
"self",
".",
"name",
"self",
".",
"w_matrix",
"=",
"tf",
".",
"Variable",
"(",
"tf",
".",
"random_normal",
"(",
"[",
"input_dim",
",",
"3",
"*",
"hidden_dim",
"]",
",",
"stddev",
"=",
"0.1",
")",
",",
"name",
"=",
"'/'",
".",
"join",
"(",
"[",
"prefix",
",",
"'W'",
"]",
")",
")",
"self",
".",
"U",
"=",
"tf",
".",
"Variable",
"(",
"tf",
".",
"random_normal",
"(",
"[",
"hidden_dim",
",",
"3",
"*",
"hidden_dim",
"]",
",",
"stddev",
"=",
"0.1",
")",
",",
"name",
"=",
"'/'",
".",
"join",
"(",
"[",
"prefix",
",",
"'U'",
"]",
")",
")",
"self",
".",
"bias",
"=",
"tf",
".",
"Variable",
"(",
"tf",
".",
"random_normal",
"(",
"[",
"1",
",",
"3",
"*",
"hidden_dim",
"]",
",",
"stddev",
"=",
"0.1",
")",
",",
"name",
"=",
"'/'",
".",
"join",
"(",
"[",
"prefix",
",",
"'b'",
"]",
")",
")",
"return",
"self"
] | Define parameters. | [
"Define",
"parameters",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/weight_sharing/ga_squad/rnn.py#L38-L51 |
27,049 | Microsoft/nni | examples/trials/weight_sharing/ga_squad/rnn.py | GRU.build | def build(self, x, h, mask=None):
'''
Build the GRU cell.
'''
xw = tf.split(tf.matmul(x, self.w_matrix) + self.bias, 3, 1)
hu = tf.split(tf.matmul(h, self.U), 3, 1)
r = tf.sigmoid(xw[0] + hu[0])
z = tf.sigmoid(xw[1] + hu[1])
h1 = tf.tanh(xw[2] + r * hu[2])
next_h = h1 * (1 - z) + h * z
if mask is not None:
next_h = next_h * mask + h * (1 - mask)
return next_h | python | def build(self, x, h, mask=None):
'''
Build the GRU cell.
'''
xw = tf.split(tf.matmul(x, self.w_matrix) + self.bias, 3, 1)
hu = tf.split(tf.matmul(h, self.U), 3, 1)
r = tf.sigmoid(xw[0] + hu[0])
z = tf.sigmoid(xw[1] + hu[1])
h1 = tf.tanh(xw[2] + r * hu[2])
next_h = h1 * (1 - z) + h * z
if mask is not None:
next_h = next_h * mask + h * (1 - mask)
return next_h | [
"def",
"build",
"(",
"self",
",",
"x",
",",
"h",
",",
"mask",
"=",
"None",
")",
":",
"xw",
"=",
"tf",
".",
"split",
"(",
"tf",
".",
"matmul",
"(",
"x",
",",
"self",
".",
"w_matrix",
")",
"+",
"self",
".",
"bias",
",",
"3",
",",
"1",
")",
"hu",
"=",
"tf",
".",
"split",
"(",
"tf",
".",
"matmul",
"(",
"h",
",",
"self",
".",
"U",
")",
",",
"3",
",",
"1",
")",
"r",
"=",
"tf",
".",
"sigmoid",
"(",
"xw",
"[",
"0",
"]",
"+",
"hu",
"[",
"0",
"]",
")",
"z",
"=",
"tf",
".",
"sigmoid",
"(",
"xw",
"[",
"1",
"]",
"+",
"hu",
"[",
"1",
"]",
")",
"h1",
"=",
"tf",
".",
"tanh",
"(",
"xw",
"[",
"2",
"]",
"+",
"r",
"*",
"hu",
"[",
"2",
"]",
")",
"next_h",
"=",
"h1",
"*",
"(",
"1",
"-",
"z",
")",
"+",
"h",
"*",
"z",
"if",
"mask",
"is",
"not",
"None",
":",
"next_h",
"=",
"next_h",
"*",
"mask",
"+",
"h",
"*",
"(",
"1",
"-",
"mask",
")",
"return",
"next_h"
] | Build the GRU cell. | [
"Build",
"the",
"GRU",
"cell",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/weight_sharing/ga_squad/rnn.py#L53-L65 |
27,050 | Microsoft/nni | examples/trials/weight_sharing/ga_squad/rnn.py | GRU.build_sequence | def build_sequence(self, xs, masks, init, is_left_to_right):
'''
Build GRU sequence.
'''
states = []
last = init
if is_left_to_right:
for i, xs_i in enumerate(xs):
h = self.build(xs_i, last, masks[i])
states.append(h)
last = h
else:
for i in range(len(xs) - 1, -1, -1):
h = self.build(xs[i], last, masks[i])
states.insert(0, h)
last = h
return states | python | def build_sequence(self, xs, masks, init, is_left_to_right):
'''
Build GRU sequence.
'''
states = []
last = init
if is_left_to_right:
for i, xs_i in enumerate(xs):
h = self.build(xs_i, last, masks[i])
states.append(h)
last = h
else:
for i in range(len(xs) - 1, -1, -1):
h = self.build(xs[i], last, masks[i])
states.insert(0, h)
last = h
return states | [
"def",
"build_sequence",
"(",
"self",
",",
"xs",
",",
"masks",
",",
"init",
",",
"is_left_to_right",
")",
":",
"states",
"=",
"[",
"]",
"last",
"=",
"init",
"if",
"is_left_to_right",
":",
"for",
"i",
",",
"xs_i",
"in",
"enumerate",
"(",
"xs",
")",
":",
"h",
"=",
"self",
".",
"build",
"(",
"xs_i",
",",
"last",
",",
"masks",
"[",
"i",
"]",
")",
"states",
".",
"append",
"(",
"h",
")",
"last",
"=",
"h",
"else",
":",
"for",
"i",
"in",
"range",
"(",
"len",
"(",
"xs",
")",
"-",
"1",
",",
"-",
"1",
",",
"-",
"1",
")",
":",
"h",
"=",
"self",
".",
"build",
"(",
"xs",
"[",
"i",
"]",
",",
"last",
",",
"masks",
"[",
"i",
"]",
")",
"states",
".",
"insert",
"(",
"0",
",",
"h",
")",
"last",
"=",
"h",
"return",
"states"
] | Build GRU sequence. | [
"Build",
"GRU",
"sequence",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/weight_sharing/ga_squad/rnn.py#L67-L83 |
27,051 | Microsoft/nni | tools/nni_annotation/examples/mnist_without_annotation.py | conv2d | def conv2d(x_input, w_matrix):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x_input, w_matrix, strides=[1, 1, 1, 1], padding='SAME') | python | def conv2d(x_input, w_matrix):
"""conv2d returns a 2d convolution layer with full stride."""
return tf.nn.conv2d(x_input, w_matrix, strides=[1, 1, 1, 1], padding='SAME') | [
"def",
"conv2d",
"(",
"x_input",
",",
"w_matrix",
")",
":",
"return",
"tf",
".",
"nn",
".",
"conv2d",
"(",
"x_input",
",",
"w_matrix",
",",
"strides",
"=",
"[",
"1",
",",
"1",
",",
"1",
",",
"1",
"]",
",",
"padding",
"=",
"'SAME'",
")"
] | conv2d returns a 2d convolution layer with full stride. | [
"conv2d",
"returns",
"a",
"2d",
"convolution",
"layer",
"with",
"full",
"stride",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_annotation/examples/mnist_without_annotation.py#L149-L151 |
27,052 | Microsoft/nni | tools/nni_annotation/examples/mnist_without_annotation.py | max_pool | def max_pool(x_input, pool_size):
"""max_pool downsamples a feature map by 2X."""
return tf.nn.max_pool(x_input, ksize=[1, pool_size, pool_size, 1],
strides=[1, pool_size, pool_size, 1], padding='SAME') | python | def max_pool(x_input, pool_size):
"""max_pool downsamples a feature map by 2X."""
return tf.nn.max_pool(x_input, ksize=[1, pool_size, pool_size, 1],
strides=[1, pool_size, pool_size, 1], padding='SAME') | [
"def",
"max_pool",
"(",
"x_input",
",",
"pool_size",
")",
":",
"return",
"tf",
".",
"nn",
".",
"max_pool",
"(",
"x_input",
",",
"ksize",
"=",
"[",
"1",
",",
"pool_size",
",",
"pool_size",
",",
"1",
"]",
",",
"strides",
"=",
"[",
"1",
",",
"pool_size",
",",
"pool_size",
",",
"1",
"]",
",",
"padding",
"=",
"'SAME'",
")"
] | max_pool downsamples a feature map by 2X. | [
"max_pool",
"downsamples",
"a",
"feature",
"map",
"by",
"2X",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_annotation/examples/mnist_without_annotation.py#L154-L157 |
27,053 | Microsoft/nni | tools/nni_annotation/examples/mnist_without_annotation.py | main | def main(params):
'''
Main function, build mnist network, run and send result to NNI.
'''
# Import data
mnist = download_mnist_retry(params['data_dir'])
print('Mnist download data done.')
logger.debug('Mnist download data done.')
# Create the model
# Build the graph for the deep net
mnist_network = MnistNetwork(channel_1_num=params['channel_1_num'],
channel_2_num=params['channel_2_num'],
pool_size=params['pool_size'])
mnist_network.build_network()
logger.debug('Mnist build network done.')
# Write log
graph_location = tempfile.mkdtemp()
logger.debug('Saving graph to: %s', graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
test_acc = 0.0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
batch_num = nni.choice(50, 250, 500, name='batch_num')
for i in range(batch_num):
batch = mnist.train.next_batch(batch_num)
dropout_rate = nni.choice(1, 5, name='dropout_rate')
mnist_network.train_step.run(feed_dict={mnist_network.images: batch[0],
mnist_network.labels: batch[1],
mnist_network.keep_prob: dropout_rate}
)
if i % 100 == 0:
test_acc = mnist_network.accuracy.eval(
feed_dict={mnist_network.images: mnist.test.images,
mnist_network.labels: mnist.test.labels,
mnist_network.keep_prob: 1.0})
nni.report_intermediate_result(test_acc)
logger.debug('test accuracy %g', test_acc)
logger.debug('Pipe send intermediate result done.')
test_acc = mnist_network.accuracy.eval(
feed_dict={mnist_network.images: mnist.test.images,
mnist_network.labels: mnist.test.labels,
mnist_network.keep_prob: 1.0})
nni.report_final_result(test_acc)
logger.debug('Final result is %g', test_acc)
logger.debug('Send final result done.') | python | def main(params):
'''
Main function, build mnist network, run and send result to NNI.
'''
# Import data
mnist = download_mnist_retry(params['data_dir'])
print('Mnist download data done.')
logger.debug('Mnist download data done.')
# Create the model
# Build the graph for the deep net
mnist_network = MnistNetwork(channel_1_num=params['channel_1_num'],
channel_2_num=params['channel_2_num'],
pool_size=params['pool_size'])
mnist_network.build_network()
logger.debug('Mnist build network done.')
# Write log
graph_location = tempfile.mkdtemp()
logger.debug('Saving graph to: %s', graph_location)
train_writer = tf.summary.FileWriter(graph_location)
train_writer.add_graph(tf.get_default_graph())
test_acc = 0.0
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
batch_num = nni.choice(50, 250, 500, name='batch_num')
for i in range(batch_num):
batch = mnist.train.next_batch(batch_num)
dropout_rate = nni.choice(1, 5, name='dropout_rate')
mnist_network.train_step.run(feed_dict={mnist_network.images: batch[0],
mnist_network.labels: batch[1],
mnist_network.keep_prob: dropout_rate}
)
if i % 100 == 0:
test_acc = mnist_network.accuracy.eval(
feed_dict={mnist_network.images: mnist.test.images,
mnist_network.labels: mnist.test.labels,
mnist_network.keep_prob: 1.0})
nni.report_intermediate_result(test_acc)
logger.debug('test accuracy %g', test_acc)
logger.debug('Pipe send intermediate result done.')
test_acc = mnist_network.accuracy.eval(
feed_dict={mnist_network.images: mnist.test.images,
mnist_network.labels: mnist.test.labels,
mnist_network.keep_prob: 1.0})
nni.report_final_result(test_acc)
logger.debug('Final result is %g', test_acc)
logger.debug('Send final result done.') | [
"def",
"main",
"(",
"params",
")",
":",
"# Import data",
"mnist",
"=",
"download_mnist_retry",
"(",
"params",
"[",
"'data_dir'",
"]",
")",
"print",
"(",
"'Mnist download data done.'",
")",
"logger",
".",
"debug",
"(",
"'Mnist download data done.'",
")",
"# Create the model",
"# Build the graph for the deep net",
"mnist_network",
"=",
"MnistNetwork",
"(",
"channel_1_num",
"=",
"params",
"[",
"'channel_1_num'",
"]",
",",
"channel_2_num",
"=",
"params",
"[",
"'channel_2_num'",
"]",
",",
"pool_size",
"=",
"params",
"[",
"'pool_size'",
"]",
")",
"mnist_network",
".",
"build_network",
"(",
")",
"logger",
".",
"debug",
"(",
"'Mnist build network done.'",
")",
"# Write log",
"graph_location",
"=",
"tempfile",
".",
"mkdtemp",
"(",
")",
"logger",
".",
"debug",
"(",
"'Saving graph to: %s'",
",",
"graph_location",
")",
"train_writer",
"=",
"tf",
".",
"summary",
".",
"FileWriter",
"(",
"graph_location",
")",
"train_writer",
".",
"add_graph",
"(",
"tf",
".",
"get_default_graph",
"(",
")",
")",
"test_acc",
"=",
"0.0",
"with",
"tf",
".",
"Session",
"(",
")",
"as",
"sess",
":",
"sess",
".",
"run",
"(",
"tf",
".",
"global_variables_initializer",
"(",
")",
")",
"batch_num",
"=",
"nni",
".",
"choice",
"(",
"50",
",",
"250",
",",
"500",
",",
"name",
"=",
"'batch_num'",
")",
"for",
"i",
"in",
"range",
"(",
"batch_num",
")",
":",
"batch",
"=",
"mnist",
".",
"train",
".",
"next_batch",
"(",
"batch_num",
")",
"dropout_rate",
"=",
"nni",
".",
"choice",
"(",
"1",
",",
"5",
",",
"name",
"=",
"'dropout_rate'",
")",
"mnist_network",
".",
"train_step",
".",
"run",
"(",
"feed_dict",
"=",
"{",
"mnist_network",
".",
"images",
":",
"batch",
"[",
"0",
"]",
",",
"mnist_network",
".",
"labels",
":",
"batch",
"[",
"1",
"]",
",",
"mnist_network",
".",
"keep_prob",
":",
"dropout_rate",
"}",
")",
"if",
"i",
"%",
"100",
"==",
"0",
":",
"test_acc",
"=",
"mnist_network",
".",
"accuracy",
".",
"eval",
"(",
"feed_dict",
"=",
"{",
"mnist_network",
".",
"images",
":",
"mnist",
".",
"test",
".",
"images",
",",
"mnist_network",
".",
"labels",
":",
"mnist",
".",
"test",
".",
"labels",
",",
"mnist_network",
".",
"keep_prob",
":",
"1.0",
"}",
")",
"nni",
".",
"report_intermediate_result",
"(",
"test_acc",
")",
"logger",
".",
"debug",
"(",
"'test accuracy %g'",
",",
"test_acc",
")",
"logger",
".",
"debug",
"(",
"'Pipe send intermediate result done.'",
")",
"test_acc",
"=",
"mnist_network",
".",
"accuracy",
".",
"eval",
"(",
"feed_dict",
"=",
"{",
"mnist_network",
".",
"images",
":",
"mnist",
".",
"test",
".",
"images",
",",
"mnist_network",
".",
"labels",
":",
"mnist",
".",
"test",
".",
"labels",
",",
"mnist_network",
".",
"keep_prob",
":",
"1.0",
"}",
")",
"nni",
".",
"report_final_result",
"(",
"test_acc",
")",
"logger",
".",
"debug",
"(",
"'Final result is %g'",
",",
"test_acc",
")",
"logger",
".",
"debug",
"(",
"'Send final result done.'",
")"
] | Main function, build mnist network, run and send result to NNI. | [
"Main",
"function",
"build",
"mnist",
"network",
"run",
"and",
"send",
"result",
"to",
"NNI",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_annotation/examples/mnist_without_annotation.py#L185-L237 |
27,054 | Microsoft/nni | tools/nni_cmd/command_utils.py | check_output_command | def check_output_command(file_path, head=None, tail=None):
'''call check_output command to read content from a file'''
if os.path.exists(file_path):
if sys.platform == 'win32':
cmds = ['powershell.exe', 'type', file_path]
if head:
cmds += ['|', 'select', '-first', str(head)]
elif tail:
cmds += ['|', 'select', '-last', str(tail)]
return check_output(cmds, shell=True).decode('utf-8')
else:
cmds = ['cat', file_path]
if head:
cmds = ['head', '-' + str(head), file_path]
elif tail:
cmds = ['tail', '-' + str(tail), file_path]
return check_output(cmds, shell=False).decode('utf-8')
else:
print_error('{0} does not exist!'.format(file_path))
exit(1) | python | def check_output_command(file_path, head=None, tail=None):
'''call check_output command to read content from a file'''
if os.path.exists(file_path):
if sys.platform == 'win32':
cmds = ['powershell.exe', 'type', file_path]
if head:
cmds += ['|', 'select', '-first', str(head)]
elif tail:
cmds += ['|', 'select', '-last', str(tail)]
return check_output(cmds, shell=True).decode('utf-8')
else:
cmds = ['cat', file_path]
if head:
cmds = ['head', '-' + str(head), file_path]
elif tail:
cmds = ['tail', '-' + str(tail), file_path]
return check_output(cmds, shell=False).decode('utf-8')
else:
print_error('{0} does not exist!'.format(file_path))
exit(1) | [
"def",
"check_output_command",
"(",
"file_path",
",",
"head",
"=",
"None",
",",
"tail",
"=",
"None",
")",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"file_path",
")",
":",
"if",
"sys",
".",
"platform",
"==",
"'win32'",
":",
"cmds",
"=",
"[",
"'powershell.exe'",
",",
"'type'",
",",
"file_path",
"]",
"if",
"head",
":",
"cmds",
"+=",
"[",
"'|'",
",",
"'select'",
",",
"'-first'",
",",
"str",
"(",
"head",
")",
"]",
"elif",
"tail",
":",
"cmds",
"+=",
"[",
"'|'",
",",
"'select'",
",",
"'-last'",
",",
"str",
"(",
"tail",
")",
"]",
"return",
"check_output",
"(",
"cmds",
",",
"shell",
"=",
"True",
")",
".",
"decode",
"(",
"'utf-8'",
")",
"else",
":",
"cmds",
"=",
"[",
"'cat'",
",",
"file_path",
"]",
"if",
"head",
":",
"cmds",
"=",
"[",
"'head'",
",",
"'-'",
"+",
"str",
"(",
"head",
")",
",",
"file_path",
"]",
"elif",
"tail",
":",
"cmds",
"=",
"[",
"'tail'",
",",
"'-'",
"+",
"str",
"(",
"tail",
")",
",",
"file_path",
"]",
"return",
"check_output",
"(",
"cmds",
",",
"shell",
"=",
"False",
")",
".",
"decode",
"(",
"'utf-8'",
")",
"else",
":",
"print_error",
"(",
"'{0} does not exist!'",
".",
"format",
"(",
"file_path",
")",
")",
"exit",
"(",
"1",
")"
] | call check_output command to read content from a file | [
"call",
"check_output",
"command",
"to",
"read",
"content",
"from",
"a",
"file"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/command_utils.py#L8-L27 |
27,055 | Microsoft/nni | tools/nni_cmd/command_utils.py | install_package_command | def install_package_command(package_name):
'''install python package from pip'''
#TODO refactor python logic
if sys.platform == "win32":
cmds = 'python -m pip install --user {0}'.format(package_name)
else:
cmds = 'python3 -m pip install --user {0}'.format(package_name)
call(cmds, shell=True) | python | def install_package_command(package_name):
'''install python package from pip'''
#TODO refactor python logic
if sys.platform == "win32":
cmds = 'python -m pip install --user {0}'.format(package_name)
else:
cmds = 'python3 -m pip install --user {0}'.format(package_name)
call(cmds, shell=True) | [
"def",
"install_package_command",
"(",
"package_name",
")",
":",
"#TODO refactor python logic",
"if",
"sys",
".",
"platform",
"==",
"\"win32\"",
":",
"cmds",
"=",
"'python -m pip install --user {0}'",
".",
"format",
"(",
"package_name",
")",
"else",
":",
"cmds",
"=",
"'python3 -m pip install --user {0}'",
".",
"format",
"(",
"package_name",
")",
"call",
"(",
"cmds",
",",
"shell",
"=",
"True",
")"
] | install python package from pip | [
"install",
"python",
"package",
"from",
"pip"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/command_utils.py#L38-L45 |
27,056 | Microsoft/nni | tools/nni_cmd/command_utils.py | install_requirements_command | def install_requirements_command(requirements_path):
'''install requirements.txt'''
cmds = 'cd ' + requirements_path + ' && {0} -m pip install --user -r requirements.txt'
#TODO refactor python logic
if sys.platform == "win32":
cmds = cmds.format('python')
else:
cmds = cmds.format('python3')
call(cmds, shell=True) | python | def install_requirements_command(requirements_path):
'''install requirements.txt'''
cmds = 'cd ' + requirements_path + ' && {0} -m pip install --user -r requirements.txt'
#TODO refactor python logic
if sys.platform == "win32":
cmds = cmds.format('python')
else:
cmds = cmds.format('python3')
call(cmds, shell=True) | [
"def",
"install_requirements_command",
"(",
"requirements_path",
")",
":",
"cmds",
"=",
"'cd '",
"+",
"requirements_path",
"+",
"' && {0} -m pip install --user -r requirements.txt'",
"#TODO refactor python logic",
"if",
"sys",
".",
"platform",
"==",
"\"win32\"",
":",
"cmds",
"=",
"cmds",
".",
"format",
"(",
"'python'",
")",
"else",
":",
"cmds",
"=",
"cmds",
".",
"format",
"(",
"'python3'",
")",
"call",
"(",
"cmds",
",",
"shell",
"=",
"True",
")"
] | install requirements.txt | [
"install",
"requirements",
".",
"txt"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/command_utils.py#L47-L55 |
27,057 | Microsoft/nni | examples/trials/mnist-advisor/mnist.py | get_params | def get_params():
''' Get parameters from command line '''
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default='/tmp/tensorflow/mnist/input_data', help="data directory")
parser.add_argument("--dropout_rate", type=float, default=0.5, help="dropout rate")
parser.add_argument("--channel_1_num", type=int, default=32)
parser.add_argument("--channel_2_num", type=int, default=64)
parser.add_argument("--conv_size", type=int, default=5)
parser.add_argument("--pool_size", type=int, default=2)
parser.add_argument("--hidden_size", type=int, default=1024)
parser.add_argument("--learning_rate", type=float, default=1e-4)
parser.add_argument("--batch_num", type=int, default=2700)
parser.add_argument("--batch_size", type=int, default=32)
args, _ = parser.parse_known_args()
return args | python | def get_params():
''' Get parameters from command line '''
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default='/tmp/tensorflow/mnist/input_data', help="data directory")
parser.add_argument("--dropout_rate", type=float, default=0.5, help="dropout rate")
parser.add_argument("--channel_1_num", type=int, default=32)
parser.add_argument("--channel_2_num", type=int, default=64)
parser.add_argument("--conv_size", type=int, default=5)
parser.add_argument("--pool_size", type=int, default=2)
parser.add_argument("--hidden_size", type=int, default=1024)
parser.add_argument("--learning_rate", type=float, default=1e-4)
parser.add_argument("--batch_num", type=int, default=2700)
parser.add_argument("--batch_size", type=int, default=32)
args, _ = parser.parse_known_args()
return args | [
"def",
"get_params",
"(",
")",
":",
"parser",
"=",
"argparse",
".",
"ArgumentParser",
"(",
")",
"parser",
".",
"add_argument",
"(",
"\"--data_dir\"",
",",
"type",
"=",
"str",
",",
"default",
"=",
"'/tmp/tensorflow/mnist/input_data'",
",",
"help",
"=",
"\"data directory\"",
")",
"parser",
".",
"add_argument",
"(",
"\"--dropout_rate\"",
",",
"type",
"=",
"float",
",",
"default",
"=",
"0.5",
",",
"help",
"=",
"\"dropout rate\"",
")",
"parser",
".",
"add_argument",
"(",
"\"--channel_1_num\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"32",
")",
"parser",
".",
"add_argument",
"(",
"\"--channel_2_num\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"64",
")",
"parser",
".",
"add_argument",
"(",
"\"--conv_size\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"5",
")",
"parser",
".",
"add_argument",
"(",
"\"--pool_size\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"2",
")",
"parser",
".",
"add_argument",
"(",
"\"--hidden_size\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"1024",
")",
"parser",
".",
"add_argument",
"(",
"\"--learning_rate\"",
",",
"type",
"=",
"float",
",",
"default",
"=",
"1e-4",
")",
"parser",
".",
"add_argument",
"(",
"\"--batch_num\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"2700",
")",
"parser",
".",
"add_argument",
"(",
"\"--batch_size\"",
",",
"type",
"=",
"int",
",",
"default",
"=",
"32",
")",
"args",
",",
"_",
"=",
"parser",
".",
"parse_known_args",
"(",
")",
"return",
"args"
] | Get parameters from command line | [
"Get",
"parameters",
"from",
"command",
"line"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-advisor/mnist.py#L211-L226 |
27,058 | Microsoft/nni | examples/trials/mnist-advisor/mnist.py | MnistNetwork.build_network | def build_network(self):
'''
Building network for mnist
'''
# Reshape to use within a convolutional neural net.
# Last dimension is for "features" - there is only one here, since images are
# grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
with tf.name_scope('reshape'):
try:
input_dim = int(math.sqrt(self.x_dim))
except:
print(
'input dim cannot be sqrt and reshape. input dim: ' + str(self.x_dim))
logger.debug(
'input dim cannot be sqrt and reshape. input dim: %s', str(self.x_dim))
raise
x_image = tf.reshape(self.images, [-1, input_dim, input_dim, 1])
# First convolutional layer - maps one grayscale image to 32 feature maps.
with tf.name_scope('conv1'):
w_conv1 = weight_variable(
[self.conv_size, self.conv_size, 1, self.channel_1_num])
b_conv1 = bias_variable([self.channel_1_num])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
# Pooling layer - downsamples by 2X.
with tf.name_scope('pool1'):
h_pool1 = max_pool(h_conv1, self.pool_size)
# Second convolutional layer -- maps 32 feature maps to 64.
with tf.name_scope('conv2'):
w_conv2 = weight_variable([self.conv_size, self.conv_size,
self.channel_1_num, self.channel_2_num])
b_conv2 = bias_variable([self.channel_2_num])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
# Second pooling layer.
with tf.name_scope('pool2'):
h_pool2 = max_pool(h_conv2, self.pool_size)
# Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image
# is down to 7x7x64 feature maps -- maps this to 1024 features.
last_dim = int(input_dim / (self.pool_size * self.pool_size))
with tf.name_scope('fc1'):
w_fc1 = weight_variable(
[last_dim * last_dim * self.channel_2_num, self.hidden_size])
b_fc1 = bias_variable([self.hidden_size])
h_pool2_flat = tf.reshape(
h_pool2, [-1, last_dim * last_dim * self.channel_2_num])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
# Dropout - controls the complexity of the model, prevents co-adaptation of features.
with tf.name_scope('dropout'):
h_fc1_drop = tf.nn.dropout(h_fc1, self.keep_prob)
# Map the 1024 features to 10 classes, one for each digit
with tf.name_scope('fc2'):
w_fc2 = weight_variable([self.hidden_size, self.y_dim])
b_fc2 = bias_variable([self.y_dim])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2
with tf.name_scope('loss'):
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=self.labels, logits=y_conv))
with tf.name_scope('adam_optimizer'):
self.train_step = tf.train.AdamOptimizer(
self.learning_rate).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(
tf.argmax(y_conv, 1), tf.argmax(self.labels, 1))
self.accuracy = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32)) | python | def build_network(self):
'''
Building network for mnist
'''
# Reshape to use within a convolutional neural net.
# Last dimension is for "features" - there is only one here, since images are
# grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.
with tf.name_scope('reshape'):
try:
input_dim = int(math.sqrt(self.x_dim))
except:
print(
'input dim cannot be sqrt and reshape. input dim: ' + str(self.x_dim))
logger.debug(
'input dim cannot be sqrt and reshape. input dim: %s', str(self.x_dim))
raise
x_image = tf.reshape(self.images, [-1, input_dim, input_dim, 1])
# First convolutional layer - maps one grayscale image to 32 feature maps.
with tf.name_scope('conv1'):
w_conv1 = weight_variable(
[self.conv_size, self.conv_size, 1, self.channel_1_num])
b_conv1 = bias_variable([self.channel_1_num])
h_conv1 = tf.nn.relu(conv2d(x_image, w_conv1) + b_conv1)
# Pooling layer - downsamples by 2X.
with tf.name_scope('pool1'):
h_pool1 = max_pool(h_conv1, self.pool_size)
# Second convolutional layer -- maps 32 feature maps to 64.
with tf.name_scope('conv2'):
w_conv2 = weight_variable([self.conv_size, self.conv_size,
self.channel_1_num, self.channel_2_num])
b_conv2 = bias_variable([self.channel_2_num])
h_conv2 = tf.nn.relu(conv2d(h_pool1, w_conv2) + b_conv2)
# Second pooling layer.
with tf.name_scope('pool2'):
h_pool2 = max_pool(h_conv2, self.pool_size)
# Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image
# is down to 7x7x64 feature maps -- maps this to 1024 features.
last_dim = int(input_dim / (self.pool_size * self.pool_size))
with tf.name_scope('fc1'):
w_fc1 = weight_variable(
[last_dim * last_dim * self.channel_2_num, self.hidden_size])
b_fc1 = bias_variable([self.hidden_size])
h_pool2_flat = tf.reshape(
h_pool2, [-1, last_dim * last_dim * self.channel_2_num])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w_fc1) + b_fc1)
# Dropout - controls the complexity of the model, prevents co-adaptation of features.
with tf.name_scope('dropout'):
h_fc1_drop = tf.nn.dropout(h_fc1, self.keep_prob)
# Map the 1024 features to 10 classes, one for each digit
with tf.name_scope('fc2'):
w_fc2 = weight_variable([self.hidden_size, self.y_dim])
b_fc2 = bias_variable([self.y_dim])
y_conv = tf.matmul(h_fc1_drop, w_fc2) + b_fc2
with tf.name_scope('loss'):
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=self.labels, logits=y_conv))
with tf.name_scope('adam_optimizer'):
self.train_step = tf.train.AdamOptimizer(
self.learning_rate).minimize(cross_entropy)
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(
tf.argmax(y_conv, 1), tf.argmax(self.labels, 1))
self.accuracy = tf.reduce_mean(
tf.cast(correct_prediction, tf.float32)) | [
"def",
"build_network",
"(",
"self",
")",
":",
"# Reshape to use within a convolutional neural net.",
"# Last dimension is for \"features\" - there is only one here, since images are",
"# grayscale -- it would be 3 for an RGB image, 4 for RGBA, etc.",
"with",
"tf",
".",
"name_scope",
"(",
"'reshape'",
")",
":",
"try",
":",
"input_dim",
"=",
"int",
"(",
"math",
".",
"sqrt",
"(",
"self",
".",
"x_dim",
")",
")",
"except",
":",
"print",
"(",
"'input dim cannot be sqrt and reshape. input dim: '",
"+",
"str",
"(",
"self",
".",
"x_dim",
")",
")",
"logger",
".",
"debug",
"(",
"'input dim cannot be sqrt and reshape. input dim: %s'",
",",
"str",
"(",
"self",
".",
"x_dim",
")",
")",
"raise",
"x_image",
"=",
"tf",
".",
"reshape",
"(",
"self",
".",
"images",
",",
"[",
"-",
"1",
",",
"input_dim",
",",
"input_dim",
",",
"1",
"]",
")",
"# First convolutional layer - maps one grayscale image to 32 feature maps.",
"with",
"tf",
".",
"name_scope",
"(",
"'conv1'",
")",
":",
"w_conv1",
"=",
"weight_variable",
"(",
"[",
"self",
".",
"conv_size",
",",
"self",
".",
"conv_size",
",",
"1",
",",
"self",
".",
"channel_1_num",
"]",
")",
"b_conv1",
"=",
"bias_variable",
"(",
"[",
"self",
".",
"channel_1_num",
"]",
")",
"h_conv1",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"conv2d",
"(",
"x_image",
",",
"w_conv1",
")",
"+",
"b_conv1",
")",
"# Pooling layer - downsamples by 2X.",
"with",
"tf",
".",
"name_scope",
"(",
"'pool1'",
")",
":",
"h_pool1",
"=",
"max_pool",
"(",
"h_conv1",
",",
"self",
".",
"pool_size",
")",
"# Second convolutional layer -- maps 32 feature maps to 64.",
"with",
"tf",
".",
"name_scope",
"(",
"'conv2'",
")",
":",
"w_conv2",
"=",
"weight_variable",
"(",
"[",
"self",
".",
"conv_size",
",",
"self",
".",
"conv_size",
",",
"self",
".",
"channel_1_num",
",",
"self",
".",
"channel_2_num",
"]",
")",
"b_conv2",
"=",
"bias_variable",
"(",
"[",
"self",
".",
"channel_2_num",
"]",
")",
"h_conv2",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"conv2d",
"(",
"h_pool1",
",",
"w_conv2",
")",
"+",
"b_conv2",
")",
"# Second pooling layer.",
"with",
"tf",
".",
"name_scope",
"(",
"'pool2'",
")",
":",
"h_pool2",
"=",
"max_pool",
"(",
"h_conv2",
",",
"self",
".",
"pool_size",
")",
"# Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image",
"# is down to 7x7x64 feature maps -- maps this to 1024 features.",
"last_dim",
"=",
"int",
"(",
"input_dim",
"/",
"(",
"self",
".",
"pool_size",
"*",
"self",
".",
"pool_size",
")",
")",
"with",
"tf",
".",
"name_scope",
"(",
"'fc1'",
")",
":",
"w_fc1",
"=",
"weight_variable",
"(",
"[",
"last_dim",
"*",
"last_dim",
"*",
"self",
".",
"channel_2_num",
",",
"self",
".",
"hidden_size",
"]",
")",
"b_fc1",
"=",
"bias_variable",
"(",
"[",
"self",
".",
"hidden_size",
"]",
")",
"h_pool2_flat",
"=",
"tf",
".",
"reshape",
"(",
"h_pool2",
",",
"[",
"-",
"1",
",",
"last_dim",
"*",
"last_dim",
"*",
"self",
".",
"channel_2_num",
"]",
")",
"h_fc1",
"=",
"tf",
".",
"nn",
".",
"relu",
"(",
"tf",
".",
"matmul",
"(",
"h_pool2_flat",
",",
"w_fc1",
")",
"+",
"b_fc1",
")",
"# Dropout - controls the complexity of the model, prevents co-adaptation of features.",
"with",
"tf",
".",
"name_scope",
"(",
"'dropout'",
")",
":",
"h_fc1_drop",
"=",
"tf",
".",
"nn",
".",
"dropout",
"(",
"h_fc1",
",",
"self",
".",
"keep_prob",
")",
"# Map the 1024 features to 10 classes, one for each digit",
"with",
"tf",
".",
"name_scope",
"(",
"'fc2'",
")",
":",
"w_fc2",
"=",
"weight_variable",
"(",
"[",
"self",
".",
"hidden_size",
",",
"self",
".",
"y_dim",
"]",
")",
"b_fc2",
"=",
"bias_variable",
"(",
"[",
"self",
".",
"y_dim",
"]",
")",
"y_conv",
"=",
"tf",
".",
"matmul",
"(",
"h_fc1_drop",
",",
"w_fc2",
")",
"+",
"b_fc2",
"with",
"tf",
".",
"name_scope",
"(",
"'loss'",
")",
":",
"cross_entropy",
"=",
"tf",
".",
"reduce_mean",
"(",
"tf",
".",
"nn",
".",
"softmax_cross_entropy_with_logits",
"(",
"labels",
"=",
"self",
".",
"labels",
",",
"logits",
"=",
"y_conv",
")",
")",
"with",
"tf",
".",
"name_scope",
"(",
"'adam_optimizer'",
")",
":",
"self",
".",
"train_step",
"=",
"tf",
".",
"train",
".",
"AdamOptimizer",
"(",
"self",
".",
"learning_rate",
")",
".",
"minimize",
"(",
"cross_entropy",
")",
"with",
"tf",
".",
"name_scope",
"(",
"'accuracy'",
")",
":",
"correct_prediction",
"=",
"tf",
".",
"equal",
"(",
"tf",
".",
"argmax",
"(",
"y_conv",
",",
"1",
")",
",",
"tf",
".",
"argmax",
"(",
"self",
".",
"labels",
",",
"1",
")",
")",
"self",
".",
"accuracy",
"=",
"tf",
".",
"reduce_mean",
"(",
"tf",
".",
"cast",
"(",
"correct_prediction",
",",
"tf",
".",
"float32",
")",
")"
] | Building network for mnist | [
"Building",
"network",
"for",
"mnist"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-advisor/mnist.py#L48-L122 |
27,059 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | get_experiment_time | def get_experiment_time(port):
'''get the startTime and endTime of an experiment'''
response = rest_get(experiment_url(port), REST_TIME_OUT)
if response and check_response(response):
content = convert_time_stamp_to_date(json.loads(response.text))
return content.get('startTime'), content.get('endTime')
return None, None | python | def get_experiment_time(port):
'''get the startTime and endTime of an experiment'''
response = rest_get(experiment_url(port), REST_TIME_OUT)
if response and check_response(response):
content = convert_time_stamp_to_date(json.loads(response.text))
return content.get('startTime'), content.get('endTime')
return None, None | [
"def",
"get_experiment_time",
"(",
"port",
")",
":",
"response",
"=",
"rest_get",
"(",
"experiment_url",
"(",
"port",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"content",
"=",
"convert_time_stamp_to_date",
"(",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
")",
"return",
"content",
".",
"get",
"(",
"'startTime'",
")",
",",
"content",
".",
"get",
"(",
"'endTime'",
")",
"return",
"None",
",",
"None"
] | get the startTime and endTime of an experiment | [
"get",
"the",
"startTime",
"and",
"endTime",
"of",
"an",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L36-L42 |
27,060 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | get_experiment_status | def get_experiment_status(port):
'''get the status of an experiment'''
result, response = check_rest_server_quick(port)
if result:
return json.loads(response.text).get('status')
return None | python | def get_experiment_status(port):
'''get the status of an experiment'''
result, response = check_rest_server_quick(port)
if result:
return json.loads(response.text).get('status')
return None | [
"def",
"get_experiment_status",
"(",
"port",
")",
":",
"result",
",",
"response",
"=",
"check_rest_server_quick",
"(",
"port",
")",
"if",
"result",
":",
"return",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
".",
"get",
"(",
"'status'",
")",
"return",
"None"
] | get the status of an experiment | [
"get",
"the",
"status",
"of",
"an",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L44-L49 |
27,061 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | update_experiment | def update_experiment():
'''Update the experiment status in config file'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
return None
for key in experiment_dict.keys():
if isinstance(experiment_dict[key], dict):
if experiment_dict[key].get('status') != 'STOPPED':
nni_config = Config(experiment_dict[key]['fileName'])
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
experiment_config.update_experiment(key, 'status', 'STOPPED')
continue
rest_port = nni_config.get_config('restServerPort')
startTime, endTime = get_experiment_time(rest_port)
if startTime:
experiment_config.update_experiment(key, 'startTime', startTime)
if endTime:
experiment_config.update_experiment(key, 'endTime', endTime)
status = get_experiment_status(rest_port)
if status:
experiment_config.update_experiment(key, 'status', status) | python | def update_experiment():
'''Update the experiment status in config file'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
return None
for key in experiment_dict.keys():
if isinstance(experiment_dict[key], dict):
if experiment_dict[key].get('status') != 'STOPPED':
nni_config = Config(experiment_dict[key]['fileName'])
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
experiment_config.update_experiment(key, 'status', 'STOPPED')
continue
rest_port = nni_config.get_config('restServerPort')
startTime, endTime = get_experiment_time(rest_port)
if startTime:
experiment_config.update_experiment(key, 'startTime', startTime)
if endTime:
experiment_config.update_experiment(key, 'endTime', endTime)
status = get_experiment_status(rest_port)
if status:
experiment_config.update_experiment(key, 'status', status) | [
"def",
"update_experiment",
"(",
")",
":",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"if",
"not",
"experiment_dict",
":",
"return",
"None",
"for",
"key",
"in",
"experiment_dict",
".",
"keys",
"(",
")",
":",
"if",
"isinstance",
"(",
"experiment_dict",
"[",
"key",
"]",
",",
"dict",
")",
":",
"if",
"experiment_dict",
"[",
"key",
"]",
".",
"get",
"(",
"'status'",
")",
"!=",
"'STOPPED'",
":",
"nni_config",
"=",
"Config",
"(",
"experiment_dict",
"[",
"key",
"]",
"[",
"'fileName'",
"]",
")",
"rest_pid",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPid'",
")",
"if",
"not",
"detect_process",
"(",
"rest_pid",
")",
":",
"experiment_config",
".",
"update_experiment",
"(",
"key",
",",
"'status'",
",",
"'STOPPED'",
")",
"continue",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"startTime",
",",
"endTime",
"=",
"get_experiment_time",
"(",
"rest_port",
")",
"if",
"startTime",
":",
"experiment_config",
".",
"update_experiment",
"(",
"key",
",",
"'startTime'",
",",
"startTime",
")",
"if",
"endTime",
":",
"experiment_config",
".",
"update_experiment",
"(",
"key",
",",
"'endTime'",
",",
"endTime",
")",
"status",
"=",
"get_experiment_status",
"(",
"rest_port",
")",
"if",
"status",
":",
"experiment_config",
".",
"update_experiment",
"(",
"key",
",",
"'status'",
",",
"status",
")"
] | Update the experiment status in config file | [
"Update",
"the",
"experiment",
"status",
"in",
"config",
"file"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L51-L73 |
27,062 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | check_experiment_id | def check_experiment_id(args):
'''check if the id is valid
'''
update_experiment()
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print_normal('There is no experiment running...')
return None
if not args.id:
running_experiment_list = []
for key in experiment_dict.keys():
if isinstance(experiment_dict[key], dict):
if experiment_dict[key].get('status') != 'STOPPED':
running_experiment_list.append(key)
elif isinstance(experiment_dict[key], list):
# if the config file is old version, remove the configuration from file
experiment_config.remove_experiment(key)
if len(running_experiment_list) > 1:
print_error('There are multiple experiments, please set the experiment id...')
experiment_information = ""
for key in running_experiment_list:
experiment_information += (EXPERIMENT_DETAIL_FORMAT % (key, experiment_dict[key]['status'], \
experiment_dict[key]['port'], experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], experiment_dict[key]['endTime']))
print(EXPERIMENT_INFORMATION_FORMAT % experiment_information)
exit(1)
elif not running_experiment_list:
print_error('There is no experiment running!')
return None
else:
return running_experiment_list[0]
if experiment_dict.get(args.id):
return args.id
else:
print_error('Id not correct!')
return None | python | def check_experiment_id(args):
'''check if the id is valid
'''
update_experiment()
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print_normal('There is no experiment running...')
return None
if not args.id:
running_experiment_list = []
for key in experiment_dict.keys():
if isinstance(experiment_dict[key], dict):
if experiment_dict[key].get('status') != 'STOPPED':
running_experiment_list.append(key)
elif isinstance(experiment_dict[key], list):
# if the config file is old version, remove the configuration from file
experiment_config.remove_experiment(key)
if len(running_experiment_list) > 1:
print_error('There are multiple experiments, please set the experiment id...')
experiment_information = ""
for key in running_experiment_list:
experiment_information += (EXPERIMENT_DETAIL_FORMAT % (key, experiment_dict[key]['status'], \
experiment_dict[key]['port'], experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], experiment_dict[key]['endTime']))
print(EXPERIMENT_INFORMATION_FORMAT % experiment_information)
exit(1)
elif not running_experiment_list:
print_error('There is no experiment running!')
return None
else:
return running_experiment_list[0]
if experiment_dict.get(args.id):
return args.id
else:
print_error('Id not correct!')
return None | [
"def",
"check_experiment_id",
"(",
"args",
")",
":",
"update_experiment",
"(",
")",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"if",
"not",
"experiment_dict",
":",
"print_normal",
"(",
"'There is no experiment running...'",
")",
"return",
"None",
"if",
"not",
"args",
".",
"id",
":",
"running_experiment_list",
"=",
"[",
"]",
"for",
"key",
"in",
"experiment_dict",
".",
"keys",
"(",
")",
":",
"if",
"isinstance",
"(",
"experiment_dict",
"[",
"key",
"]",
",",
"dict",
")",
":",
"if",
"experiment_dict",
"[",
"key",
"]",
".",
"get",
"(",
"'status'",
")",
"!=",
"'STOPPED'",
":",
"running_experiment_list",
".",
"append",
"(",
"key",
")",
"elif",
"isinstance",
"(",
"experiment_dict",
"[",
"key",
"]",
",",
"list",
")",
":",
"# if the config file is old version, remove the configuration from file",
"experiment_config",
".",
"remove_experiment",
"(",
"key",
")",
"if",
"len",
"(",
"running_experiment_list",
")",
">",
"1",
":",
"print_error",
"(",
"'There are multiple experiments, please set the experiment id...'",
")",
"experiment_information",
"=",
"\"\"",
"for",
"key",
"in",
"running_experiment_list",
":",
"experiment_information",
"+=",
"(",
"EXPERIMENT_DETAIL_FORMAT",
"%",
"(",
"key",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'status'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'port'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
".",
"get",
"(",
"'platform'",
")",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'startTime'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'endTime'",
"]",
")",
")",
"print",
"(",
"EXPERIMENT_INFORMATION_FORMAT",
"%",
"experiment_information",
")",
"exit",
"(",
"1",
")",
"elif",
"not",
"running_experiment_list",
":",
"print_error",
"(",
"'There is no experiment running!'",
")",
"return",
"None",
"else",
":",
"return",
"running_experiment_list",
"[",
"0",
"]",
"if",
"experiment_dict",
".",
"get",
"(",
"args",
".",
"id",
")",
":",
"return",
"args",
".",
"id",
"else",
":",
"print_error",
"(",
"'Id not correct!'",
")",
"return",
"None"
] | check if the id is valid | [
"check",
"if",
"the",
"id",
"is",
"valid"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L75-L110 |
27,063 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | get_config_filename | def get_config_filename(args):
'''get the file name of config file'''
experiment_id = check_experiment_id(args)
if experiment_id is None:
print_error('Please set the experiment id!')
exit(1)
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
return experiment_dict[experiment_id]['fileName'] | python | def get_config_filename(args):
'''get the file name of config file'''
experiment_id = check_experiment_id(args)
if experiment_id is None:
print_error('Please set the experiment id!')
exit(1)
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
return experiment_dict[experiment_id]['fileName'] | [
"def",
"get_config_filename",
"(",
"args",
")",
":",
"experiment_id",
"=",
"check_experiment_id",
"(",
"args",
")",
"if",
"experiment_id",
"is",
"None",
":",
"print_error",
"(",
"'Please set the experiment id!'",
")",
"exit",
"(",
"1",
")",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"return",
"experiment_dict",
"[",
"experiment_id",
"]",
"[",
"'fileName'",
"]"
] | get the file name of config file | [
"get",
"the",
"file",
"name",
"of",
"config",
"file"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L168-L176 |
27,064 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | convert_time_stamp_to_date | def convert_time_stamp_to_date(content):
'''Convert time stamp to date time format'''
start_time_stamp = content.get('startTime')
end_time_stamp = content.get('endTime')
if start_time_stamp:
start_time = datetime.datetime.utcfromtimestamp(start_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
content['startTime'] = str(start_time)
if end_time_stamp:
end_time = datetime.datetime.utcfromtimestamp(end_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
content['endTime'] = str(end_time)
return content | python | def convert_time_stamp_to_date(content):
'''Convert time stamp to date time format'''
start_time_stamp = content.get('startTime')
end_time_stamp = content.get('endTime')
if start_time_stamp:
start_time = datetime.datetime.utcfromtimestamp(start_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
content['startTime'] = str(start_time)
if end_time_stamp:
end_time = datetime.datetime.utcfromtimestamp(end_time_stamp // 1000).strftime("%Y/%m/%d %H:%M:%S")
content['endTime'] = str(end_time)
return content | [
"def",
"convert_time_stamp_to_date",
"(",
"content",
")",
":",
"start_time_stamp",
"=",
"content",
".",
"get",
"(",
"'startTime'",
")",
"end_time_stamp",
"=",
"content",
".",
"get",
"(",
"'endTime'",
")",
"if",
"start_time_stamp",
":",
"start_time",
"=",
"datetime",
".",
"datetime",
".",
"utcfromtimestamp",
"(",
"start_time_stamp",
"//",
"1000",
")",
".",
"strftime",
"(",
"\"%Y/%m/%d %H:%M:%S\"",
")",
"content",
"[",
"'startTime'",
"]",
"=",
"str",
"(",
"start_time",
")",
"if",
"end_time_stamp",
":",
"end_time",
"=",
"datetime",
".",
"datetime",
".",
"utcfromtimestamp",
"(",
"end_time_stamp",
"//",
"1000",
")",
".",
"strftime",
"(",
"\"%Y/%m/%d %H:%M:%S\"",
")",
"content",
"[",
"'endTime'",
"]",
"=",
"str",
"(",
"end_time",
")",
"return",
"content"
] | Convert time stamp to date time format | [
"Convert",
"time",
"stamp",
"to",
"date",
"time",
"format"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L188-L198 |
27,065 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | check_rest | def check_rest(args):
'''check if restful server is running'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if not running:
print_normal('Restful server is running...')
else:
print_normal('Restful server is not running...') | python | def check_rest(args):
'''check if restful server is running'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
running, _ = check_rest_server_quick(rest_port)
if not running:
print_normal('Restful server is running...')
else:
print_normal('Restful server is not running...') | [
"def",
"check_rest",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"running",
",",
"_",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"not",
"running",
":",
"print_normal",
"(",
"'Restful server is running...'",
")",
"else",
":",
"print_normal",
"(",
"'Restful server is not running...'",
")"
] | check if restful server is running | [
"check",
"if",
"restful",
"server",
"is",
"running"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L200-L208 |
27,066 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | stop_experiment | def stop_experiment(args):
'''Stop the experiment which is running'''
experiment_id_list = parse_ids(args)
if experiment_id_list:
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
for experiment_id in experiment_id_list:
print_normal('Stoping experiment %s' % experiment_id)
nni_config = Config(experiment_dict[experiment_id]['fileName'])
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if rest_pid:
kill_command(rest_pid)
tensorboard_pid_list = nni_config.get_config('tensorboardPidList')
if tensorboard_pid_list:
for tensorboard_pid in tensorboard_pid_list:
try:
kill_command(tensorboard_pid)
except Exception as exception:
print_error(exception)
nni_config.set_config('tensorboardPidList', [])
print_normal('Stop experiment success!')
experiment_config.update_experiment(experiment_id, 'status', 'STOPPED')
time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
experiment_config.update_experiment(experiment_id, 'endTime', str(time_now)) | python | def stop_experiment(args):
'''Stop the experiment which is running'''
experiment_id_list = parse_ids(args)
if experiment_id_list:
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
for experiment_id in experiment_id_list:
print_normal('Stoping experiment %s' % experiment_id)
nni_config = Config(experiment_dict[experiment_id]['fileName'])
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if rest_pid:
kill_command(rest_pid)
tensorboard_pid_list = nni_config.get_config('tensorboardPidList')
if tensorboard_pid_list:
for tensorboard_pid in tensorboard_pid_list:
try:
kill_command(tensorboard_pid)
except Exception as exception:
print_error(exception)
nni_config.set_config('tensorboardPidList', [])
print_normal('Stop experiment success!')
experiment_config.update_experiment(experiment_id, 'status', 'STOPPED')
time_now = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
experiment_config.update_experiment(experiment_id, 'endTime', str(time_now)) | [
"def",
"stop_experiment",
"(",
"args",
")",
":",
"experiment_id_list",
"=",
"parse_ids",
"(",
"args",
")",
"if",
"experiment_id_list",
":",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"for",
"experiment_id",
"in",
"experiment_id_list",
":",
"print_normal",
"(",
"'Stoping experiment %s'",
"%",
"experiment_id",
")",
"nni_config",
"=",
"Config",
"(",
"experiment_dict",
"[",
"experiment_id",
"]",
"[",
"'fileName'",
"]",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"rest_pid",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPid'",
")",
"if",
"rest_pid",
":",
"kill_command",
"(",
"rest_pid",
")",
"tensorboard_pid_list",
"=",
"nni_config",
".",
"get_config",
"(",
"'tensorboardPidList'",
")",
"if",
"tensorboard_pid_list",
":",
"for",
"tensorboard_pid",
"in",
"tensorboard_pid_list",
":",
"try",
":",
"kill_command",
"(",
"tensorboard_pid",
")",
"except",
"Exception",
"as",
"exception",
":",
"print_error",
"(",
"exception",
")",
"nni_config",
".",
"set_config",
"(",
"'tensorboardPidList'",
",",
"[",
"]",
")",
"print_normal",
"(",
"'Stop experiment success!'",
")",
"experiment_config",
".",
"update_experiment",
"(",
"experiment_id",
",",
"'status'",
",",
"'STOPPED'",
")",
"time_now",
"=",
"time",
".",
"strftime",
"(",
"'%Y-%m-%d %H:%M:%S'",
",",
"time",
".",
"localtime",
"(",
"time",
".",
"time",
"(",
")",
")",
")",
"experiment_config",
".",
"update_experiment",
"(",
"experiment_id",
",",
"'endTime'",
",",
"str",
"(",
"time_now",
")",
")"
] | Stop the experiment which is running | [
"Stop",
"the",
"experiment",
"which",
"is",
"running"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L210-L234 |
27,067 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | list_experiment | def list_experiment(args):
'''Get experiment information'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_get(experiment_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = convert_time_stamp_to_date(json.loads(response.text))
print(json.dumps(content, indent=4, sort_keys=True, separators=(',', ':')))
else:
print_error('List experiment failed...')
else:
print_error('Restful server is not running...') | python | def list_experiment(args):
'''Get experiment information'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, _ = check_rest_server_quick(rest_port)
if running:
response = rest_get(experiment_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = convert_time_stamp_to_date(json.loads(response.text))
print(json.dumps(content, indent=4, sort_keys=True, separators=(',', ':')))
else:
print_error('List experiment failed...')
else:
print_error('Restful server is not running...') | [
"def",
"list_experiment",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"rest_pid",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPid'",
")",
"if",
"not",
"detect_process",
"(",
"rest_pid",
")",
":",
"print_error",
"(",
"'Experiment is not running...'",
")",
"return",
"running",
",",
"_",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"running",
":",
"response",
"=",
"rest_get",
"(",
"experiment_url",
"(",
"rest_port",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"content",
"=",
"convert_time_stamp_to_date",
"(",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
")",
"print",
"(",
"json",
".",
"dumps",
"(",
"content",
",",
"indent",
"=",
"4",
",",
"sort_keys",
"=",
"True",
",",
"separators",
"=",
"(",
"','",
",",
"':'",
")",
")",
")",
"else",
":",
"print_error",
"(",
"'List experiment failed...'",
")",
"else",
":",
"print_error",
"(",
"'Restful server is not running...'",
")"
] | Get experiment information | [
"Get",
"experiment",
"information"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L275-L292 |
27,068 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | experiment_status | def experiment_status(args):
'''Show the status of experiment'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
result, response = check_rest_server_quick(rest_port)
if not result:
print_normal('Restful server is not running...')
else:
print(json.dumps(json.loads(response.text), indent=4, sort_keys=True, separators=(',', ':'))) | python | def experiment_status(args):
'''Show the status of experiment'''
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
result, response = check_rest_server_quick(rest_port)
if not result:
print_normal('Restful server is not running...')
else:
print(json.dumps(json.loads(response.text), indent=4, sort_keys=True, separators=(',', ':'))) | [
"def",
"experiment_status",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"result",
",",
"response",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"not",
"result",
":",
"print_normal",
"(",
"'Restful server is not running...'",
")",
"else",
":",
"print",
"(",
"json",
".",
"dumps",
"(",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
",",
"indent",
"=",
"4",
",",
"sort_keys",
"=",
"True",
",",
"separators",
"=",
"(",
"','",
",",
"':'",
")",
")",
")"
] | Show the status of experiment | [
"Show",
"the",
"status",
"of",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L294-L302 |
27,069 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | log_internal | def log_internal(args, filetype):
'''internal function to call get_log_content'''
file_name = get_config_filename(args)
if filetype == 'stdout':
file_full_path = os.path.join(NNICTL_HOME_DIR, file_name, 'stdout')
else:
file_full_path = os.path.join(NNICTL_HOME_DIR, file_name, 'stderr')
print(check_output_command(file_full_path, head=args.head, tail=args.tail)) | python | def log_internal(args, filetype):
'''internal function to call get_log_content'''
file_name = get_config_filename(args)
if filetype == 'stdout':
file_full_path = os.path.join(NNICTL_HOME_DIR, file_name, 'stdout')
else:
file_full_path = os.path.join(NNICTL_HOME_DIR, file_name, 'stderr')
print(check_output_command(file_full_path, head=args.head, tail=args.tail)) | [
"def",
"log_internal",
"(",
"args",
",",
"filetype",
")",
":",
"file_name",
"=",
"get_config_filename",
"(",
"args",
")",
"if",
"filetype",
"==",
"'stdout'",
":",
"file_full_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"NNICTL_HOME_DIR",
",",
"file_name",
",",
"'stdout'",
")",
"else",
":",
"file_full_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"NNICTL_HOME_DIR",
",",
"file_name",
",",
"'stderr'",
")",
"print",
"(",
"check_output_command",
"(",
"file_full_path",
",",
"head",
"=",
"args",
".",
"head",
",",
"tail",
"=",
"args",
".",
"tail",
")",
")"
] | internal function to call get_log_content | [
"internal",
"function",
"to",
"call",
"get_log_content"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L304-L311 |
27,070 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | log_trial | def log_trial(args):
''''get trial log path'''
trial_id_path_dict = {}
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for trial in content:
trial_id_path_dict[trial['id']] = trial['logPath']
else:
print_error('Restful server is not running...')
exit(1)
if args.id:
if args.trial_id:
if trial_id_path_dict.get(args.trial_id):
print_normal('id:' + args.trial_id + ' path:' + trial_id_path_dict[args.trial_id])
else:
print_error('trial id is not valid!')
exit(1)
else:
print_error('please specific the trial id!')
exit(1)
else:
for key in trial_id_path_dict:
print('id:' + key + ' path:' + trial_id_path_dict[key]) | python | def log_trial(args):
''''get trial log path'''
trial_id_path_dict = {}
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for trial in content:
trial_id_path_dict[trial['id']] = trial['logPath']
else:
print_error('Restful server is not running...')
exit(1)
if args.id:
if args.trial_id:
if trial_id_path_dict.get(args.trial_id):
print_normal('id:' + args.trial_id + ' path:' + trial_id_path_dict[args.trial_id])
else:
print_error('trial id is not valid!')
exit(1)
else:
print_error('please specific the trial id!')
exit(1)
else:
for key in trial_id_path_dict:
print('id:' + key + ' path:' + trial_id_path_dict[key]) | [
"def",
"log_trial",
"(",
"args",
")",
":",
"trial_id_path_dict",
"=",
"{",
"}",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"rest_pid",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPid'",
")",
"if",
"not",
"detect_process",
"(",
"rest_pid",
")",
":",
"print_error",
"(",
"'Experiment is not running...'",
")",
"return",
"running",
",",
"response",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"running",
":",
"response",
"=",
"rest_get",
"(",
"trial_jobs_url",
"(",
"rest_port",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"content",
"=",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
"for",
"trial",
"in",
"content",
":",
"trial_id_path_dict",
"[",
"trial",
"[",
"'id'",
"]",
"]",
"=",
"trial",
"[",
"'logPath'",
"]",
"else",
":",
"print_error",
"(",
"'Restful server is not running...'",
")",
"exit",
"(",
"1",
")",
"if",
"args",
".",
"id",
":",
"if",
"args",
".",
"trial_id",
":",
"if",
"trial_id_path_dict",
".",
"get",
"(",
"args",
".",
"trial_id",
")",
":",
"print_normal",
"(",
"'id:'",
"+",
"args",
".",
"trial_id",
"+",
"' path:'",
"+",
"trial_id_path_dict",
"[",
"args",
".",
"trial_id",
"]",
")",
"else",
":",
"print_error",
"(",
"'trial id is not valid!'",
")",
"exit",
"(",
"1",
")",
"else",
":",
"print_error",
"(",
"'please specific the trial id!'",
")",
"exit",
"(",
"1",
")",
"else",
":",
"for",
"key",
"in",
"trial_id_path_dict",
":",
"print",
"(",
"'id:'",
"+",
"key",
"+",
"' path:'",
"+",
"trial_id_path_dict",
"[",
"key",
"]",
")"
] | get trial log path | [
"get",
"trial",
"log",
"path"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L321-L352 |
27,071 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | webui_url | def webui_url(args):
'''show the url of web ui'''
nni_config = Config(get_config_filename(args))
print_normal('{0} {1}'.format('Web UI url:', ' '.join(nni_config.get_config('webuiUrl')))) | python | def webui_url(args):
'''show the url of web ui'''
nni_config = Config(get_config_filename(args))
print_normal('{0} {1}'.format('Web UI url:', ' '.join(nni_config.get_config('webuiUrl')))) | [
"def",
"webui_url",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"print_normal",
"(",
"'{0} {1}'",
".",
"format",
"(",
"'Web UI url:'",
",",
"' '",
".",
"join",
"(",
"nni_config",
".",
"get_config",
"(",
"'webuiUrl'",
")",
")",
")",
")"
] | show the url of web ui | [
"show",
"the",
"url",
"of",
"web",
"ui"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L359-L362 |
27,072 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | experiment_list | def experiment_list(args):
'''get the information of all experiments'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print('There is no experiment running...')
exit(1)
update_experiment()
experiment_id_list = []
if args.all and args.all == 'all':
for key in experiment_dict.keys():
experiment_id_list.append(key)
else:
for key in experiment_dict.keys():
if experiment_dict[key]['status'] != 'STOPPED':
experiment_id_list.append(key)
if not experiment_id_list:
print_warning('There is no experiment running...\nYou can use \'nnictl experiment list all\' to list all stopped experiments!')
experiment_information = ""
for key in experiment_id_list:
experiment_information += (EXPERIMENT_DETAIL_FORMAT % (key, experiment_dict[key]['status'], experiment_dict[key]['port'],\
experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], experiment_dict[key]['endTime']))
print(EXPERIMENT_INFORMATION_FORMAT % experiment_information) | python | def experiment_list(args):
'''get the information of all experiments'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print('There is no experiment running...')
exit(1)
update_experiment()
experiment_id_list = []
if args.all and args.all == 'all':
for key in experiment_dict.keys():
experiment_id_list.append(key)
else:
for key in experiment_dict.keys():
if experiment_dict[key]['status'] != 'STOPPED':
experiment_id_list.append(key)
if not experiment_id_list:
print_warning('There is no experiment running...\nYou can use \'nnictl experiment list all\' to list all stopped experiments!')
experiment_information = ""
for key in experiment_id_list:
experiment_information += (EXPERIMENT_DETAIL_FORMAT % (key, experiment_dict[key]['status'], experiment_dict[key]['port'],\
experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], experiment_dict[key]['endTime']))
print(EXPERIMENT_INFORMATION_FORMAT % experiment_information) | [
"def",
"experiment_list",
"(",
"args",
")",
":",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"if",
"not",
"experiment_dict",
":",
"print",
"(",
"'There is no experiment running...'",
")",
"exit",
"(",
"1",
")",
"update_experiment",
"(",
")",
"experiment_id_list",
"=",
"[",
"]",
"if",
"args",
".",
"all",
"and",
"args",
".",
"all",
"==",
"'all'",
":",
"for",
"key",
"in",
"experiment_dict",
".",
"keys",
"(",
")",
":",
"experiment_id_list",
".",
"append",
"(",
"key",
")",
"else",
":",
"for",
"key",
"in",
"experiment_dict",
".",
"keys",
"(",
")",
":",
"if",
"experiment_dict",
"[",
"key",
"]",
"[",
"'status'",
"]",
"!=",
"'STOPPED'",
":",
"experiment_id_list",
".",
"append",
"(",
"key",
")",
"if",
"not",
"experiment_id_list",
":",
"print_warning",
"(",
"'There is no experiment running...\\nYou can use \\'nnictl experiment list all\\' to list all stopped experiments!'",
")",
"experiment_information",
"=",
"\"\"",
"for",
"key",
"in",
"experiment_id_list",
":",
"experiment_information",
"+=",
"(",
"EXPERIMENT_DETAIL_FORMAT",
"%",
"(",
"key",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'status'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'port'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
".",
"get",
"(",
"'platform'",
")",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'startTime'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'endTime'",
"]",
")",
")",
"print",
"(",
"EXPERIMENT_INFORMATION_FORMAT",
"%",
"experiment_information",
")"
] | get the information of all experiments | [
"get",
"the",
"information",
"of",
"all",
"experiments"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L364-L387 |
27,073 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | get_time_interval | def get_time_interval(time1, time2):
'''get the interval of two times'''
try:
#convert time to timestamp
time1 = time.mktime(time.strptime(time1, '%Y/%m/%d %H:%M:%S'))
time2 = time.mktime(time.strptime(time2, '%Y/%m/%d %H:%M:%S'))
seconds = (datetime.datetime.fromtimestamp(time2) - datetime.datetime.fromtimestamp(time1)).seconds
#convert seconds to day:hour:minute:second
days = seconds / 86400
seconds %= 86400
hours = seconds / 3600
seconds %= 3600
minutes = seconds / 60
seconds %= 60
return '%dd %dh %dm %ds' % (days, hours, minutes, seconds)
except:
return 'N/A' | python | def get_time_interval(time1, time2):
'''get the interval of two times'''
try:
#convert time to timestamp
time1 = time.mktime(time.strptime(time1, '%Y/%m/%d %H:%M:%S'))
time2 = time.mktime(time.strptime(time2, '%Y/%m/%d %H:%M:%S'))
seconds = (datetime.datetime.fromtimestamp(time2) - datetime.datetime.fromtimestamp(time1)).seconds
#convert seconds to day:hour:minute:second
days = seconds / 86400
seconds %= 86400
hours = seconds / 3600
seconds %= 3600
minutes = seconds / 60
seconds %= 60
return '%dd %dh %dm %ds' % (days, hours, minutes, seconds)
except:
return 'N/A' | [
"def",
"get_time_interval",
"(",
"time1",
",",
"time2",
")",
":",
"try",
":",
"#convert time to timestamp",
"time1",
"=",
"time",
".",
"mktime",
"(",
"time",
".",
"strptime",
"(",
"time1",
",",
"'%Y/%m/%d %H:%M:%S'",
")",
")",
"time2",
"=",
"time",
".",
"mktime",
"(",
"time",
".",
"strptime",
"(",
"time2",
",",
"'%Y/%m/%d %H:%M:%S'",
")",
")",
"seconds",
"=",
"(",
"datetime",
".",
"datetime",
".",
"fromtimestamp",
"(",
"time2",
")",
"-",
"datetime",
".",
"datetime",
".",
"fromtimestamp",
"(",
"time1",
")",
")",
".",
"seconds",
"#convert seconds to day:hour:minute:second",
"days",
"=",
"seconds",
"/",
"86400",
"seconds",
"%=",
"86400",
"hours",
"=",
"seconds",
"/",
"3600",
"seconds",
"%=",
"3600",
"minutes",
"=",
"seconds",
"/",
"60",
"seconds",
"%=",
"60",
"return",
"'%dd %dh %dm %ds'",
"%",
"(",
"days",
",",
"hours",
",",
"minutes",
",",
"seconds",
")",
"except",
":",
"return",
"'N/A'"
] | get the interval of two times | [
"get",
"the",
"interval",
"of",
"two",
"times"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L389-L405 |
27,074 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | show_experiment_info | def show_experiment_info():
'''show experiment information in monitor'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print('There is no experiment running...')
exit(1)
update_experiment()
experiment_id_list = []
for key in experiment_dict.keys():
if experiment_dict[key]['status'] != 'STOPPED':
experiment_id_list.append(key)
if not experiment_id_list:
print_warning('There is no experiment running...')
return
for key in experiment_id_list:
print(EXPERIMENT_MONITOR_INFO % (key, experiment_dict[key]['status'], experiment_dict[key]['port'], \
experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], get_time_interval(experiment_dict[key]['startTime'], experiment_dict[key]['endTime'])))
print(TRIAL_MONITOR_HEAD)
running, response = check_rest_server_quick(experiment_dict[key]['port'])
if running:
response = rest_get(trial_jobs_url(experiment_dict[key]['port']), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for index, value in enumerate(content):
content[index] = convert_time_stamp_to_date(value)
print(TRIAL_MONITOR_CONTENT % (content[index].get('id'), content[index].get('startTime'), content[index].get('endTime'), content[index].get('status')))
print(TRIAL_MONITOR_TAIL) | python | def show_experiment_info():
'''show experiment information in monitor'''
experiment_config = Experiments()
experiment_dict = experiment_config.get_all_experiments()
if not experiment_dict:
print('There is no experiment running...')
exit(1)
update_experiment()
experiment_id_list = []
for key in experiment_dict.keys():
if experiment_dict[key]['status'] != 'STOPPED':
experiment_id_list.append(key)
if not experiment_id_list:
print_warning('There is no experiment running...')
return
for key in experiment_id_list:
print(EXPERIMENT_MONITOR_INFO % (key, experiment_dict[key]['status'], experiment_dict[key]['port'], \
experiment_dict[key].get('platform'), experiment_dict[key]['startTime'], get_time_interval(experiment_dict[key]['startTime'], experiment_dict[key]['endTime'])))
print(TRIAL_MONITOR_HEAD)
running, response = check_rest_server_quick(experiment_dict[key]['port'])
if running:
response = rest_get(trial_jobs_url(experiment_dict[key]['port']), REST_TIME_OUT)
if response and check_response(response):
content = json.loads(response.text)
for index, value in enumerate(content):
content[index] = convert_time_stamp_to_date(value)
print(TRIAL_MONITOR_CONTENT % (content[index].get('id'), content[index].get('startTime'), content[index].get('endTime'), content[index].get('status')))
print(TRIAL_MONITOR_TAIL) | [
"def",
"show_experiment_info",
"(",
")",
":",
"experiment_config",
"=",
"Experiments",
"(",
")",
"experiment_dict",
"=",
"experiment_config",
".",
"get_all_experiments",
"(",
")",
"if",
"not",
"experiment_dict",
":",
"print",
"(",
"'There is no experiment running...'",
")",
"exit",
"(",
"1",
")",
"update_experiment",
"(",
")",
"experiment_id_list",
"=",
"[",
"]",
"for",
"key",
"in",
"experiment_dict",
".",
"keys",
"(",
")",
":",
"if",
"experiment_dict",
"[",
"key",
"]",
"[",
"'status'",
"]",
"!=",
"'STOPPED'",
":",
"experiment_id_list",
".",
"append",
"(",
"key",
")",
"if",
"not",
"experiment_id_list",
":",
"print_warning",
"(",
"'There is no experiment running...'",
")",
"return",
"for",
"key",
"in",
"experiment_id_list",
":",
"print",
"(",
"EXPERIMENT_MONITOR_INFO",
"%",
"(",
"key",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'status'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'port'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
".",
"get",
"(",
"'platform'",
")",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'startTime'",
"]",
",",
"get_time_interval",
"(",
"experiment_dict",
"[",
"key",
"]",
"[",
"'startTime'",
"]",
",",
"experiment_dict",
"[",
"key",
"]",
"[",
"'endTime'",
"]",
")",
")",
")",
"print",
"(",
"TRIAL_MONITOR_HEAD",
")",
"running",
",",
"response",
"=",
"check_rest_server_quick",
"(",
"experiment_dict",
"[",
"key",
"]",
"[",
"'port'",
"]",
")",
"if",
"running",
":",
"response",
"=",
"rest_get",
"(",
"trial_jobs_url",
"(",
"experiment_dict",
"[",
"key",
"]",
"[",
"'port'",
"]",
")",
",",
"REST_TIME_OUT",
")",
"if",
"response",
"and",
"check_response",
"(",
"response",
")",
":",
"content",
"=",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
"for",
"index",
",",
"value",
"in",
"enumerate",
"(",
"content",
")",
":",
"content",
"[",
"index",
"]",
"=",
"convert_time_stamp_to_date",
"(",
"value",
")",
"print",
"(",
"TRIAL_MONITOR_CONTENT",
"%",
"(",
"content",
"[",
"index",
"]",
".",
"get",
"(",
"'id'",
")",
",",
"content",
"[",
"index",
"]",
".",
"get",
"(",
"'startTime'",
")",
",",
"content",
"[",
"index",
"]",
".",
"get",
"(",
"'endTime'",
")",
",",
"content",
"[",
"index",
"]",
".",
"get",
"(",
"'status'",
")",
")",
")",
"print",
"(",
"TRIAL_MONITOR_TAIL",
")"
] | show experiment information in monitor | [
"show",
"experiment",
"information",
"in",
"monitor"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L407-L434 |
27,075 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | monitor_experiment | def monitor_experiment(args):
'''monitor the experiment'''
if args.time <= 0:
print_error('please input a positive integer as time interval, the unit is second.')
exit(1)
while True:
try:
os.system('clear')
update_experiment()
show_experiment_info()
time.sleep(args.time)
except KeyboardInterrupt:
exit(0)
except Exception as exception:
print_error(exception)
exit(1) | python | def monitor_experiment(args):
'''monitor the experiment'''
if args.time <= 0:
print_error('please input a positive integer as time interval, the unit is second.')
exit(1)
while True:
try:
os.system('clear')
update_experiment()
show_experiment_info()
time.sleep(args.time)
except KeyboardInterrupt:
exit(0)
except Exception as exception:
print_error(exception)
exit(1) | [
"def",
"monitor_experiment",
"(",
"args",
")",
":",
"if",
"args",
".",
"time",
"<=",
"0",
":",
"print_error",
"(",
"'please input a positive integer as time interval, the unit is second.'",
")",
"exit",
"(",
"1",
")",
"while",
"True",
":",
"try",
":",
"os",
".",
"system",
"(",
"'clear'",
")",
"update_experiment",
"(",
")",
"show_experiment_info",
"(",
")",
"time",
".",
"sleep",
"(",
"args",
".",
"time",
")",
"except",
"KeyboardInterrupt",
":",
"exit",
"(",
"0",
")",
"except",
"Exception",
"as",
"exception",
":",
"print_error",
"(",
"exception",
")",
"exit",
"(",
"1",
")"
] | monitor the experiment | [
"monitor",
"the",
"experiment"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L436-L451 |
27,076 | Microsoft/nni | tools/nni_cmd/nnictl_utils.py | export_trials_data | def export_trials_data(args):
"""export experiment metadata to csv
"""
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), 20)
if response is not None and check_response(response):
content = json.loads(response.text)
# dframe = pd.DataFrame.from_records([parse_trial_data(t_data) for t_data in content])
# dframe.to_csv(args.csv_path, sep='\t')
records = parse_trial_data(content)
if args.type == 'json':
json_records = []
for trial in records:
value = trial.pop('reward', None)
trial_id = trial.pop('id', None)
json_records.append({'parameter': trial, 'value': value, 'id': trial_id})
with open(args.path, 'w') as file:
if args.type == 'csv':
writer = csv.DictWriter(file, set.union(*[set(r.keys()) for r in records]))
writer.writeheader()
writer.writerows(records)
else:
json.dump(json_records, file)
else:
print_error('Export failed...')
else:
print_error('Restful server is not Running') | python | def export_trials_data(args):
"""export experiment metadata to csv
"""
nni_config = Config(get_config_filename(args))
rest_port = nni_config.get_config('restServerPort')
rest_pid = nni_config.get_config('restServerPid')
if not detect_process(rest_pid):
print_error('Experiment is not running...')
return
running, response = check_rest_server_quick(rest_port)
if running:
response = rest_get(trial_jobs_url(rest_port), 20)
if response is not None and check_response(response):
content = json.loads(response.text)
# dframe = pd.DataFrame.from_records([parse_trial_data(t_data) for t_data in content])
# dframe.to_csv(args.csv_path, sep='\t')
records = parse_trial_data(content)
if args.type == 'json':
json_records = []
for trial in records:
value = trial.pop('reward', None)
trial_id = trial.pop('id', None)
json_records.append({'parameter': trial, 'value': value, 'id': trial_id})
with open(args.path, 'w') as file:
if args.type == 'csv':
writer = csv.DictWriter(file, set.union(*[set(r.keys()) for r in records]))
writer.writeheader()
writer.writerows(records)
else:
json.dump(json_records, file)
else:
print_error('Export failed...')
else:
print_error('Restful server is not Running') | [
"def",
"export_trials_data",
"(",
"args",
")",
":",
"nni_config",
"=",
"Config",
"(",
"get_config_filename",
"(",
"args",
")",
")",
"rest_port",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPort'",
")",
"rest_pid",
"=",
"nni_config",
".",
"get_config",
"(",
"'restServerPid'",
")",
"if",
"not",
"detect_process",
"(",
"rest_pid",
")",
":",
"print_error",
"(",
"'Experiment is not running...'",
")",
"return",
"running",
",",
"response",
"=",
"check_rest_server_quick",
"(",
"rest_port",
")",
"if",
"running",
":",
"response",
"=",
"rest_get",
"(",
"trial_jobs_url",
"(",
"rest_port",
")",
",",
"20",
")",
"if",
"response",
"is",
"not",
"None",
"and",
"check_response",
"(",
"response",
")",
":",
"content",
"=",
"json",
".",
"loads",
"(",
"response",
".",
"text",
")",
"# dframe = pd.DataFrame.from_records([parse_trial_data(t_data) for t_data in content])",
"# dframe.to_csv(args.csv_path, sep='\\t')",
"records",
"=",
"parse_trial_data",
"(",
"content",
")",
"if",
"args",
".",
"type",
"==",
"'json'",
":",
"json_records",
"=",
"[",
"]",
"for",
"trial",
"in",
"records",
":",
"value",
"=",
"trial",
".",
"pop",
"(",
"'reward'",
",",
"None",
")",
"trial_id",
"=",
"trial",
".",
"pop",
"(",
"'id'",
",",
"None",
")",
"json_records",
".",
"append",
"(",
"{",
"'parameter'",
":",
"trial",
",",
"'value'",
":",
"value",
",",
"'id'",
":",
"trial_id",
"}",
")",
"with",
"open",
"(",
"args",
".",
"path",
",",
"'w'",
")",
"as",
"file",
":",
"if",
"args",
".",
"type",
"==",
"'csv'",
":",
"writer",
"=",
"csv",
".",
"DictWriter",
"(",
"file",
",",
"set",
".",
"union",
"(",
"*",
"[",
"set",
"(",
"r",
".",
"keys",
"(",
")",
")",
"for",
"r",
"in",
"records",
"]",
")",
")",
"writer",
".",
"writeheader",
"(",
")",
"writer",
".",
"writerows",
"(",
"records",
")",
"else",
":",
"json",
".",
"dump",
"(",
"json_records",
",",
"file",
")",
"else",
":",
"print_error",
"(",
"'Export failed...'",
")",
"else",
":",
"print_error",
"(",
"'Restful server is not Running'",
")"
] | export experiment metadata to csv | [
"export",
"experiment",
"metadata",
"to",
"csv"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/nnictl_utils.py#L474-L507 |
27,077 | Microsoft/nni | tools/nni_cmd/ssh_utils.py | copy_remote_directory_to_local | def copy_remote_directory_to_local(sftp, remote_path, local_path):
'''copy remote directory to local machine'''
try:
os.makedirs(local_path, exist_ok=True)
files = sftp.listdir(remote_path)
for file in files:
remote_full_path = os.path.join(remote_path, file)
local_full_path = os.path.join(local_path, file)
try:
if sftp.listdir(remote_full_path):
copy_remote_directory_to_local(sftp, remote_full_path, local_full_path)
except:
sftp.get(remote_full_path, local_full_path)
except Exception:
pass | python | def copy_remote_directory_to_local(sftp, remote_path, local_path):
'''copy remote directory to local machine'''
try:
os.makedirs(local_path, exist_ok=True)
files = sftp.listdir(remote_path)
for file in files:
remote_full_path = os.path.join(remote_path, file)
local_full_path = os.path.join(local_path, file)
try:
if sftp.listdir(remote_full_path):
copy_remote_directory_to_local(sftp, remote_full_path, local_full_path)
except:
sftp.get(remote_full_path, local_full_path)
except Exception:
pass | [
"def",
"copy_remote_directory_to_local",
"(",
"sftp",
",",
"remote_path",
",",
"local_path",
")",
":",
"try",
":",
"os",
".",
"makedirs",
"(",
"local_path",
",",
"exist_ok",
"=",
"True",
")",
"files",
"=",
"sftp",
".",
"listdir",
"(",
"remote_path",
")",
"for",
"file",
"in",
"files",
":",
"remote_full_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"remote_path",
",",
"file",
")",
"local_full_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"local_path",
",",
"file",
")",
"try",
":",
"if",
"sftp",
".",
"listdir",
"(",
"remote_full_path",
")",
":",
"copy_remote_directory_to_local",
"(",
"sftp",
",",
"remote_full_path",
",",
"local_full_path",
")",
"except",
":",
"sftp",
".",
"get",
"(",
"remote_full_path",
",",
"local_full_path",
")",
"except",
"Exception",
":",
"pass"
] | copy remote directory to local machine | [
"copy",
"remote",
"directory",
"to",
"local",
"machine"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/ssh_utils.py#L33-L47 |
27,078 | Microsoft/nni | tools/nni_cmd/ssh_utils.py | create_ssh_sftp_client | def create_ssh_sftp_client(host_ip, port, username, password):
'''create ssh client'''
try:
check_environment()
import paramiko
conn = paramiko.Transport(host_ip, port)
conn.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(conn)
return sftp
except Exception as exception:
print_error('Create ssh client error %s\n' % exception) | python | def create_ssh_sftp_client(host_ip, port, username, password):
'''create ssh client'''
try:
check_environment()
import paramiko
conn = paramiko.Transport(host_ip, port)
conn.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(conn)
return sftp
except Exception as exception:
print_error('Create ssh client error %s\n' % exception) | [
"def",
"create_ssh_sftp_client",
"(",
"host_ip",
",",
"port",
",",
"username",
",",
"password",
")",
":",
"try",
":",
"check_environment",
"(",
")",
"import",
"paramiko",
"conn",
"=",
"paramiko",
".",
"Transport",
"(",
"host_ip",
",",
"port",
")",
"conn",
".",
"connect",
"(",
"username",
"=",
"username",
",",
"password",
"=",
"password",
")",
"sftp",
"=",
"paramiko",
".",
"SFTPClient",
".",
"from_transport",
"(",
"conn",
")",
"return",
"sftp",
"except",
"Exception",
"as",
"exception",
":",
"print_error",
"(",
"'Create ssh client error %s\\n'",
"%",
"exception",
")"
] | create ssh client | [
"create",
"ssh",
"client"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/ssh_utils.py#L49-L59 |
27,079 | Microsoft/nni | src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py | json2space | def json2space(x, oldy=None, name=NodeType.Root.value):
"""Change search space from json format to hyperopt format
"""
y = list()
if isinstance(x, dict):
if NodeType.Type.value in x.keys():
_type = x[NodeType.Type.value]
name = name + '-' + _type
if _type == 'choice':
if oldy != None:
_index = oldy[NodeType.Index.value]
y += json2space(x[NodeType.Value.value][_index],
oldy[NodeType.Value.value], name=name+'[%d]' % _index)
else:
y += json2space(x[NodeType.Value.value], None, name=name)
y.append(name)
else:
for key in x.keys():
y += json2space(x[key], (oldy[key] if oldy !=
None else None), name+"[%s]" % str(key))
elif isinstance(x, list):
for i, x_i in enumerate(x):
y += json2space(x_i, (oldy[i] if oldy !=
None else None), name+"[%d]" % i)
else:
pass
return y | python | def json2space(x, oldy=None, name=NodeType.Root.value):
"""Change search space from json format to hyperopt format
"""
y = list()
if isinstance(x, dict):
if NodeType.Type.value in x.keys():
_type = x[NodeType.Type.value]
name = name + '-' + _type
if _type == 'choice':
if oldy != None:
_index = oldy[NodeType.Index.value]
y += json2space(x[NodeType.Value.value][_index],
oldy[NodeType.Value.value], name=name+'[%d]' % _index)
else:
y += json2space(x[NodeType.Value.value], None, name=name)
y.append(name)
else:
for key in x.keys():
y += json2space(x[key], (oldy[key] if oldy !=
None else None), name+"[%s]" % str(key))
elif isinstance(x, list):
for i, x_i in enumerate(x):
y += json2space(x_i, (oldy[i] if oldy !=
None else None), name+"[%d]" % i)
else:
pass
return y | [
"def",
"json2space",
"(",
"x",
",",
"oldy",
"=",
"None",
",",
"name",
"=",
"NodeType",
".",
"Root",
".",
"value",
")",
":",
"y",
"=",
"list",
"(",
")",
"if",
"isinstance",
"(",
"x",
",",
"dict",
")",
":",
"if",
"NodeType",
".",
"Type",
".",
"value",
"in",
"x",
".",
"keys",
"(",
")",
":",
"_type",
"=",
"x",
"[",
"NodeType",
".",
"Type",
".",
"value",
"]",
"name",
"=",
"name",
"+",
"'-'",
"+",
"_type",
"if",
"_type",
"==",
"'choice'",
":",
"if",
"oldy",
"!=",
"None",
":",
"_index",
"=",
"oldy",
"[",
"NodeType",
".",
"Index",
".",
"value",
"]",
"y",
"+=",
"json2space",
"(",
"x",
"[",
"NodeType",
".",
"Value",
".",
"value",
"]",
"[",
"_index",
"]",
",",
"oldy",
"[",
"NodeType",
".",
"Value",
".",
"value",
"]",
",",
"name",
"=",
"name",
"+",
"'[%d]'",
"%",
"_index",
")",
"else",
":",
"y",
"+=",
"json2space",
"(",
"x",
"[",
"NodeType",
".",
"Value",
".",
"value",
"]",
",",
"None",
",",
"name",
"=",
"name",
")",
"y",
".",
"append",
"(",
"name",
")",
"else",
":",
"for",
"key",
"in",
"x",
".",
"keys",
"(",
")",
":",
"y",
"+=",
"json2space",
"(",
"x",
"[",
"key",
"]",
",",
"(",
"oldy",
"[",
"key",
"]",
"if",
"oldy",
"!=",
"None",
"else",
"None",
")",
",",
"name",
"+",
"\"[%s]\"",
"%",
"str",
"(",
"key",
")",
")",
"elif",
"isinstance",
"(",
"x",
",",
"list",
")",
":",
"for",
"i",
",",
"x_i",
"in",
"enumerate",
"(",
"x",
")",
":",
"y",
"+=",
"json2space",
"(",
"x_i",
",",
"(",
"oldy",
"[",
"i",
"]",
"if",
"oldy",
"!=",
"None",
"else",
"None",
")",
",",
"name",
"+",
"\"[%d]\"",
"%",
"i",
")",
"else",
":",
"pass",
"return",
"y"
] | Change search space from json format to hyperopt format | [
"Change",
"search",
"space",
"from",
"json",
"format",
"to",
"hyperopt",
"format"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py#L61-L87 |
27,080 | Microsoft/nni | src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py | json2paramater | def json2paramater(x, is_rand, random_state, oldy=None, Rand=False, name=NodeType.Root.value):
"""Json to pramaters.
"""
if isinstance(x, dict):
if NodeType.Type.value in x.keys():
_type = x[NodeType.Type.value]
_value = x[NodeType.Value.value]
name = name + '-' + _type
Rand |= is_rand[name]
if Rand is True:
if _type == 'choice':
_index = random_state.randint(len(_value))
y = {
NodeType.Index.value: _index,
NodeType.Value.value: json2paramater(x[NodeType.Value.value][_index],
is_rand,
random_state,
None,
Rand,
name=name+"[%d]" % _index)
}
else:
y = eval('parameter_expressions.' +
_type)(*(_value + [random_state]))
else:
y = copy.deepcopy(oldy)
else:
y = dict()
for key in x.keys():
y[key] = json2paramater(x[key], is_rand, random_state, oldy[key]
if oldy != None else None, Rand, name + "[%s]" % str(key))
elif isinstance(x, list):
y = list()
for i, x_i in enumerate(x):
y.append(json2paramater(x_i, is_rand, random_state, oldy[i]
if oldy != None else None, Rand, name + "[%d]" % i))
else:
y = copy.deepcopy(x)
return y | python | def json2paramater(x, is_rand, random_state, oldy=None, Rand=False, name=NodeType.Root.value):
"""Json to pramaters.
"""
if isinstance(x, dict):
if NodeType.Type.value in x.keys():
_type = x[NodeType.Type.value]
_value = x[NodeType.Value.value]
name = name + '-' + _type
Rand |= is_rand[name]
if Rand is True:
if _type == 'choice':
_index = random_state.randint(len(_value))
y = {
NodeType.Index.value: _index,
NodeType.Value.value: json2paramater(x[NodeType.Value.value][_index],
is_rand,
random_state,
None,
Rand,
name=name+"[%d]" % _index)
}
else:
y = eval('parameter_expressions.' +
_type)(*(_value + [random_state]))
else:
y = copy.deepcopy(oldy)
else:
y = dict()
for key in x.keys():
y[key] = json2paramater(x[key], is_rand, random_state, oldy[key]
if oldy != None else None, Rand, name + "[%s]" % str(key))
elif isinstance(x, list):
y = list()
for i, x_i in enumerate(x):
y.append(json2paramater(x_i, is_rand, random_state, oldy[i]
if oldy != None else None, Rand, name + "[%d]" % i))
else:
y = copy.deepcopy(x)
return y | [
"def",
"json2paramater",
"(",
"x",
",",
"is_rand",
",",
"random_state",
",",
"oldy",
"=",
"None",
",",
"Rand",
"=",
"False",
",",
"name",
"=",
"NodeType",
".",
"Root",
".",
"value",
")",
":",
"if",
"isinstance",
"(",
"x",
",",
"dict",
")",
":",
"if",
"NodeType",
".",
"Type",
".",
"value",
"in",
"x",
".",
"keys",
"(",
")",
":",
"_type",
"=",
"x",
"[",
"NodeType",
".",
"Type",
".",
"value",
"]",
"_value",
"=",
"x",
"[",
"NodeType",
".",
"Value",
".",
"value",
"]",
"name",
"=",
"name",
"+",
"'-'",
"+",
"_type",
"Rand",
"|=",
"is_rand",
"[",
"name",
"]",
"if",
"Rand",
"is",
"True",
":",
"if",
"_type",
"==",
"'choice'",
":",
"_index",
"=",
"random_state",
".",
"randint",
"(",
"len",
"(",
"_value",
")",
")",
"y",
"=",
"{",
"NodeType",
".",
"Index",
".",
"value",
":",
"_index",
",",
"NodeType",
".",
"Value",
".",
"value",
":",
"json2paramater",
"(",
"x",
"[",
"NodeType",
".",
"Value",
".",
"value",
"]",
"[",
"_index",
"]",
",",
"is_rand",
",",
"random_state",
",",
"None",
",",
"Rand",
",",
"name",
"=",
"name",
"+",
"\"[%d]\"",
"%",
"_index",
")",
"}",
"else",
":",
"y",
"=",
"eval",
"(",
"'parameter_expressions.'",
"+",
"_type",
")",
"(",
"*",
"(",
"_value",
"+",
"[",
"random_state",
"]",
")",
")",
"else",
":",
"y",
"=",
"copy",
".",
"deepcopy",
"(",
"oldy",
")",
"else",
":",
"y",
"=",
"dict",
"(",
")",
"for",
"key",
"in",
"x",
".",
"keys",
"(",
")",
":",
"y",
"[",
"key",
"]",
"=",
"json2paramater",
"(",
"x",
"[",
"key",
"]",
",",
"is_rand",
",",
"random_state",
",",
"oldy",
"[",
"key",
"]",
"if",
"oldy",
"!=",
"None",
"else",
"None",
",",
"Rand",
",",
"name",
"+",
"\"[%s]\"",
"%",
"str",
"(",
"key",
")",
")",
"elif",
"isinstance",
"(",
"x",
",",
"list",
")",
":",
"y",
"=",
"list",
"(",
")",
"for",
"i",
",",
"x_i",
"in",
"enumerate",
"(",
"x",
")",
":",
"y",
".",
"append",
"(",
"json2paramater",
"(",
"x_i",
",",
"is_rand",
",",
"random_state",
",",
"oldy",
"[",
"i",
"]",
"if",
"oldy",
"!=",
"None",
"else",
"None",
",",
"Rand",
",",
"name",
"+",
"\"[%d]\"",
"%",
"i",
")",
")",
"else",
":",
"y",
"=",
"copy",
".",
"deepcopy",
"(",
"x",
")",
"return",
"y"
] | Json to pramaters. | [
"Json",
"to",
"pramaters",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py#L90-L128 |
27,081 | Microsoft/nni | src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py | _split_index | def _split_index(params):
"""Delete index information from params
Parameters
----------
params : dict
Returns
-------
result : dict
"""
result = {}
for key in params:
if isinstance(params[key], dict):
value = params[key]['_value']
else:
value = params[key]
result[key] = value
return result | python | def _split_index(params):
"""Delete index information from params
Parameters
----------
params : dict
Returns
-------
result : dict
"""
result = {}
for key in params:
if isinstance(params[key], dict):
value = params[key]['_value']
else:
value = params[key]
result[key] = value
return result | [
"def",
"_split_index",
"(",
"params",
")",
":",
"result",
"=",
"{",
"}",
"for",
"key",
"in",
"params",
":",
"if",
"isinstance",
"(",
"params",
"[",
"key",
"]",
",",
"dict",
")",
":",
"value",
"=",
"params",
"[",
"key",
"]",
"[",
"'_value'",
"]",
"else",
":",
"value",
"=",
"params",
"[",
"key",
"]",
"result",
"[",
"key",
"]",
"=",
"value",
"return",
"result"
] | Delete index information from params
Parameters
----------
params : dict
Returns
-------
result : dict | [
"Delete",
"index",
"information",
"from",
"params"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py#L131-L149 |
27,082 | Microsoft/nni | src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py | EvolutionTuner.update_search_space | def update_search_space(self, search_space):
"""Update search space.
Search_space contains the information that user pre-defined.
Parameters
----------
search_space : dict
"""
self.searchspace_json = search_space
self.space = json2space(self.searchspace_json)
self.random_state = np.random.RandomState()
self.population = []
is_rand = dict()
for item in self.space:
is_rand[item] = True
for _ in range(self.population_size):
config = json2paramater(
self.searchspace_json, is_rand, self.random_state)
self.population.append(Individual(config=config)) | python | def update_search_space(self, search_space):
"""Update search space.
Search_space contains the information that user pre-defined.
Parameters
----------
search_space : dict
"""
self.searchspace_json = search_space
self.space = json2space(self.searchspace_json)
self.random_state = np.random.RandomState()
self.population = []
is_rand = dict()
for item in self.space:
is_rand[item] = True
for _ in range(self.population_size):
config = json2paramater(
self.searchspace_json, is_rand, self.random_state)
self.population.append(Individual(config=config)) | [
"def",
"update_search_space",
"(",
"self",
",",
"search_space",
")",
":",
"self",
".",
"searchspace_json",
"=",
"search_space",
"self",
".",
"space",
"=",
"json2space",
"(",
"self",
".",
"searchspace_json",
")",
"self",
".",
"random_state",
"=",
"np",
".",
"random",
".",
"RandomState",
"(",
")",
"self",
".",
"population",
"=",
"[",
"]",
"is_rand",
"=",
"dict",
"(",
")",
"for",
"item",
"in",
"self",
".",
"space",
":",
"is_rand",
"[",
"item",
"]",
"=",
"True",
"for",
"_",
"in",
"range",
"(",
"self",
".",
"population_size",
")",
":",
"config",
"=",
"json2paramater",
"(",
"self",
".",
"searchspace_json",
",",
"is_rand",
",",
"self",
".",
"random_state",
")",
"self",
".",
"population",
".",
"append",
"(",
"Individual",
"(",
"config",
"=",
"config",
")",
")"
] | Update search space.
Search_space contains the information that user pre-defined.
Parameters
----------
search_space : dict | [
"Update",
"search",
"space",
".",
"Search_space",
"contains",
"the",
"information",
"that",
"user",
"pre",
"-",
"defined",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py#L215-L234 |
27,083 | Microsoft/nni | src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py | EvolutionTuner.receive_trial_result | def receive_trial_result(self, parameter_id, parameters, value):
'''Record the result from a trial
Parameters
----------
parameters: dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial.
'''
reward = extract_scalar_reward(value)
if parameter_id not in self.total_data:
raise RuntimeError('Received parameter_id not in total_data.')
# restore the paramsters contains "_index"
params = self.total_data[parameter_id]
if self.optimize_mode == OptimizeMode.Minimize:
reward = -reward
indiv = Individual(config=params, result=reward)
self.population.append(indiv) | python | def receive_trial_result(self, parameter_id, parameters, value):
'''Record the result from a trial
Parameters
----------
parameters: dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial.
'''
reward = extract_scalar_reward(value)
if parameter_id not in self.total_data:
raise RuntimeError('Received parameter_id not in total_data.')
# restore the paramsters contains "_index"
params = self.total_data[parameter_id]
if self.optimize_mode == OptimizeMode.Minimize:
reward = -reward
indiv = Individual(config=params, result=reward)
self.population.append(indiv) | [
"def",
"receive_trial_result",
"(",
"self",
",",
"parameter_id",
",",
"parameters",
",",
"value",
")",
":",
"reward",
"=",
"extract_scalar_reward",
"(",
"value",
")",
"if",
"parameter_id",
"not",
"in",
"self",
".",
"total_data",
":",
"raise",
"RuntimeError",
"(",
"'Received parameter_id not in total_data.'",
")",
"# restore the paramsters contains \"_index\"",
"params",
"=",
"self",
".",
"total_data",
"[",
"parameter_id",
"]",
"if",
"self",
".",
"optimize_mode",
"==",
"OptimizeMode",
".",
"Minimize",
":",
"reward",
"=",
"-",
"reward",
"indiv",
"=",
"Individual",
"(",
"config",
"=",
"params",
",",
"result",
"=",
"reward",
")",
"self",
".",
"population",
".",
"append",
"(",
"indiv",
")"
] | Record the result from a trial
Parameters
----------
parameters: dict
value : dict/float
if value is dict, it should have "default" key.
value is final metrics of the trial. | [
"Record",
"the",
"result",
"from",
"a",
"trial"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/evolution_tuner/evolution_tuner.py#L280-L300 |
27,084 | Microsoft/nni | examples/trials/auto-gbdt/main.py | load_data | def load_data(train_path='./data/regression.train', test_path='./data/regression.test'):
'''
Load or create dataset
'''
print('Load data...')
df_train = pd.read_csv(train_path, header=None, sep='\t')
df_test = pd.read_csv(test_path, header=None, sep='\t')
num = len(df_train)
split_num = int(0.9 * num)
y_train = df_train[0].values
y_test = df_test[0].values
y_eval = y_train[split_num:]
y_train = y_train[:split_num]
X_train = df_train.drop(0, axis=1).values
X_test = df_test.drop(0, axis=1).values
X_eval = X_train[split_num:, :]
X_train = X_train[:split_num, :]
# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_eval, y_eval, reference=lgb_train)
return lgb_train, lgb_eval, X_test, y_test | python | def load_data(train_path='./data/regression.train', test_path='./data/regression.test'):
'''
Load or create dataset
'''
print('Load data...')
df_train = pd.read_csv(train_path, header=None, sep='\t')
df_test = pd.read_csv(test_path, header=None, sep='\t')
num = len(df_train)
split_num = int(0.9 * num)
y_train = df_train[0].values
y_test = df_test[0].values
y_eval = y_train[split_num:]
y_train = y_train[:split_num]
X_train = df_train.drop(0, axis=1).values
X_test = df_test.drop(0, axis=1).values
X_eval = X_train[split_num:, :]
X_train = X_train[:split_num, :]
# create dataset for lightgbm
lgb_train = lgb.Dataset(X_train, y_train)
lgb_eval = lgb.Dataset(X_eval, y_eval, reference=lgb_train)
return lgb_train, lgb_eval, X_test, y_test | [
"def",
"load_data",
"(",
"train_path",
"=",
"'./data/regression.train'",
",",
"test_path",
"=",
"'./data/regression.test'",
")",
":",
"print",
"(",
"'Load data...'",
")",
"df_train",
"=",
"pd",
".",
"read_csv",
"(",
"train_path",
",",
"header",
"=",
"None",
",",
"sep",
"=",
"'\\t'",
")",
"df_test",
"=",
"pd",
".",
"read_csv",
"(",
"test_path",
",",
"header",
"=",
"None",
",",
"sep",
"=",
"'\\t'",
")",
"num",
"=",
"len",
"(",
"df_train",
")",
"split_num",
"=",
"int",
"(",
"0.9",
"*",
"num",
")",
"y_train",
"=",
"df_train",
"[",
"0",
"]",
".",
"values",
"y_test",
"=",
"df_test",
"[",
"0",
"]",
".",
"values",
"y_eval",
"=",
"y_train",
"[",
"split_num",
":",
"]",
"y_train",
"=",
"y_train",
"[",
":",
"split_num",
"]",
"X_train",
"=",
"df_train",
".",
"drop",
"(",
"0",
",",
"axis",
"=",
"1",
")",
".",
"values",
"X_test",
"=",
"df_test",
".",
"drop",
"(",
"0",
",",
"axis",
"=",
"1",
")",
".",
"values",
"X_eval",
"=",
"X_train",
"[",
"split_num",
":",
",",
":",
"]",
"X_train",
"=",
"X_train",
"[",
":",
"split_num",
",",
":",
"]",
"# create dataset for lightgbm",
"lgb_train",
"=",
"lgb",
".",
"Dataset",
"(",
"X_train",
",",
"y_train",
")",
"lgb_eval",
"=",
"lgb",
".",
"Dataset",
"(",
"X_eval",
",",
"y_eval",
",",
"reference",
"=",
"lgb_train",
")",
"return",
"lgb_train",
",",
"lgb_eval",
",",
"X_test",
",",
"y_test"
] | Load or create dataset | [
"Load",
"or",
"create",
"dataset"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/auto-gbdt/main.py#L48-L72 |
27,085 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | layer_distance | def layer_distance(a, b):
"""The distance between two layers."""
# pylint: disable=unidiomatic-typecheck
if type(a) != type(b):
return 1.0
if is_layer(a, "Conv"):
att_diff = [
(a.filters, b.filters),
(a.kernel_size, b.kernel_size),
(a.stride, b.stride),
]
return attribute_difference(att_diff)
if is_layer(a, "Pooling"):
att_diff = [
(a.padding, b.padding),
(a.kernel_size, b.kernel_size),
(a.stride, b.stride),
]
return attribute_difference(att_diff)
return 0.0 | python | def layer_distance(a, b):
"""The distance between two layers."""
# pylint: disable=unidiomatic-typecheck
if type(a) != type(b):
return 1.0
if is_layer(a, "Conv"):
att_diff = [
(a.filters, b.filters),
(a.kernel_size, b.kernel_size),
(a.stride, b.stride),
]
return attribute_difference(att_diff)
if is_layer(a, "Pooling"):
att_diff = [
(a.padding, b.padding),
(a.kernel_size, b.kernel_size),
(a.stride, b.stride),
]
return attribute_difference(att_diff)
return 0.0 | [
"def",
"layer_distance",
"(",
"a",
",",
"b",
")",
":",
"# pylint: disable=unidiomatic-typecheck",
"if",
"type",
"(",
"a",
")",
"!=",
"type",
"(",
"b",
")",
":",
"return",
"1.0",
"if",
"is_layer",
"(",
"a",
",",
"\"Conv\"",
")",
":",
"att_diff",
"=",
"[",
"(",
"a",
".",
"filters",
",",
"b",
".",
"filters",
")",
",",
"(",
"a",
".",
"kernel_size",
",",
"b",
".",
"kernel_size",
")",
",",
"(",
"a",
".",
"stride",
",",
"b",
".",
"stride",
")",
",",
"]",
"return",
"attribute_difference",
"(",
"att_diff",
")",
"if",
"is_layer",
"(",
"a",
",",
"\"Pooling\"",
")",
":",
"att_diff",
"=",
"[",
"(",
"a",
".",
"padding",
",",
"b",
".",
"padding",
")",
",",
"(",
"a",
".",
"kernel_size",
",",
"b",
".",
"kernel_size",
")",
",",
"(",
"a",
".",
"stride",
",",
"b",
".",
"stride",
")",
",",
"]",
"return",
"attribute_difference",
"(",
"att_diff",
")",
"return",
"0.0"
] | The distance between two layers. | [
"The",
"distance",
"between",
"two",
"layers",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L37-L56 |
27,086 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | attribute_difference | def attribute_difference(att_diff):
''' The attribute distance.
'''
ret = 0
for a_value, b_value in att_diff:
if max(a_value, b_value) == 0:
ret += 0
else:
ret += abs(a_value - b_value) * 1.0 / max(a_value, b_value)
return ret * 1.0 / len(att_diff) | python | def attribute_difference(att_diff):
''' The attribute distance.
'''
ret = 0
for a_value, b_value in att_diff:
if max(a_value, b_value) == 0:
ret += 0
else:
ret += abs(a_value - b_value) * 1.0 / max(a_value, b_value)
return ret * 1.0 / len(att_diff) | [
"def",
"attribute_difference",
"(",
"att_diff",
")",
":",
"ret",
"=",
"0",
"for",
"a_value",
",",
"b_value",
"in",
"att_diff",
":",
"if",
"max",
"(",
"a_value",
",",
"b_value",
")",
"==",
"0",
":",
"ret",
"+=",
"0",
"else",
":",
"ret",
"+=",
"abs",
"(",
"a_value",
"-",
"b_value",
")",
"*",
"1.0",
"/",
"max",
"(",
"a_value",
",",
"b_value",
")",
"return",
"ret",
"*",
"1.0",
"/",
"len",
"(",
"att_diff",
")"
] | The attribute distance. | [
"The",
"attribute",
"distance",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L59-L69 |
27,087 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | layers_distance | def layers_distance(list_a, list_b):
"""The distance between the layers of two neural networks."""
len_a = len(list_a)
len_b = len(list_b)
f = np.zeros((len_a + 1, len_b + 1))
f[-1][-1] = 0
for i in range(-1, len_a):
f[i][-1] = i + 1
for j in range(-1, len_b):
f[-1][j] = j + 1
for i in range(len_a):
for j in range(len_b):
f[i][j] = min(
f[i][j - 1] + 1,
f[i - 1][j] + 1,
f[i - 1][j - 1] + layer_distance(list_a[i], list_b[j]),
)
return f[len_a - 1][len_b - 1] | python | def layers_distance(list_a, list_b):
"""The distance between the layers of two neural networks."""
len_a = len(list_a)
len_b = len(list_b)
f = np.zeros((len_a + 1, len_b + 1))
f[-1][-1] = 0
for i in range(-1, len_a):
f[i][-1] = i + 1
for j in range(-1, len_b):
f[-1][j] = j + 1
for i in range(len_a):
for j in range(len_b):
f[i][j] = min(
f[i][j - 1] + 1,
f[i - 1][j] + 1,
f[i - 1][j - 1] + layer_distance(list_a[i], list_b[j]),
)
return f[len_a - 1][len_b - 1] | [
"def",
"layers_distance",
"(",
"list_a",
",",
"list_b",
")",
":",
"len_a",
"=",
"len",
"(",
"list_a",
")",
"len_b",
"=",
"len",
"(",
"list_b",
")",
"f",
"=",
"np",
".",
"zeros",
"(",
"(",
"len_a",
"+",
"1",
",",
"len_b",
"+",
"1",
")",
")",
"f",
"[",
"-",
"1",
"]",
"[",
"-",
"1",
"]",
"=",
"0",
"for",
"i",
"in",
"range",
"(",
"-",
"1",
",",
"len_a",
")",
":",
"f",
"[",
"i",
"]",
"[",
"-",
"1",
"]",
"=",
"i",
"+",
"1",
"for",
"j",
"in",
"range",
"(",
"-",
"1",
",",
"len_b",
")",
":",
"f",
"[",
"-",
"1",
"]",
"[",
"j",
"]",
"=",
"j",
"+",
"1",
"for",
"i",
"in",
"range",
"(",
"len_a",
")",
":",
"for",
"j",
"in",
"range",
"(",
"len_b",
")",
":",
"f",
"[",
"i",
"]",
"[",
"j",
"]",
"=",
"min",
"(",
"f",
"[",
"i",
"]",
"[",
"j",
"-",
"1",
"]",
"+",
"1",
",",
"f",
"[",
"i",
"-",
"1",
"]",
"[",
"j",
"]",
"+",
"1",
",",
"f",
"[",
"i",
"-",
"1",
"]",
"[",
"j",
"-",
"1",
"]",
"+",
"layer_distance",
"(",
"list_a",
"[",
"i",
"]",
",",
"list_b",
"[",
"j",
"]",
")",
",",
")",
"return",
"f",
"[",
"len_a",
"-",
"1",
"]",
"[",
"len_b",
"-",
"1",
"]"
] | The distance between the layers of two neural networks. | [
"The",
"distance",
"between",
"the",
"layers",
"of",
"two",
"neural",
"networks",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L72-L89 |
27,088 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | skip_connection_distance | def skip_connection_distance(a, b):
"""The distance between two skip-connections."""
if a[2] != b[2]:
return 1.0
len_a = abs(a[1] - a[0])
len_b = abs(b[1] - b[0])
return (abs(a[0] - b[0]) + abs(len_a - len_b)) / (max(a[0], b[0]) + max(len_a, len_b)) | python | def skip_connection_distance(a, b):
"""The distance between two skip-connections."""
if a[2] != b[2]:
return 1.0
len_a = abs(a[1] - a[0])
len_b = abs(b[1] - b[0])
return (abs(a[0] - b[0]) + abs(len_a - len_b)) / (max(a[0], b[0]) + max(len_a, len_b)) | [
"def",
"skip_connection_distance",
"(",
"a",
",",
"b",
")",
":",
"if",
"a",
"[",
"2",
"]",
"!=",
"b",
"[",
"2",
"]",
":",
"return",
"1.0",
"len_a",
"=",
"abs",
"(",
"a",
"[",
"1",
"]",
"-",
"a",
"[",
"0",
"]",
")",
"len_b",
"=",
"abs",
"(",
"b",
"[",
"1",
"]",
"-",
"b",
"[",
"0",
"]",
")",
"return",
"(",
"abs",
"(",
"a",
"[",
"0",
"]",
"-",
"b",
"[",
"0",
"]",
")",
"+",
"abs",
"(",
"len_a",
"-",
"len_b",
")",
")",
"/",
"(",
"max",
"(",
"a",
"[",
"0",
"]",
",",
"b",
"[",
"0",
"]",
")",
"+",
"max",
"(",
"len_a",
",",
"len_b",
")",
")"
] | The distance between two skip-connections. | [
"The",
"distance",
"between",
"two",
"skip",
"-",
"connections",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L92-L98 |
27,089 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | skip_connections_distance | def skip_connections_distance(list_a, list_b):
"""The distance between the skip-connections of two neural networks."""
distance_matrix = np.zeros((len(list_a), len(list_b)))
for i, a in enumerate(list_a):
for j, b in enumerate(list_b):
distance_matrix[i][j] = skip_connection_distance(a, b)
return distance_matrix[linear_sum_assignment(distance_matrix)].sum() + abs(
len(list_a) - len(list_b)
) | python | def skip_connections_distance(list_a, list_b):
"""The distance between the skip-connections of two neural networks."""
distance_matrix = np.zeros((len(list_a), len(list_b)))
for i, a in enumerate(list_a):
for j, b in enumerate(list_b):
distance_matrix[i][j] = skip_connection_distance(a, b)
return distance_matrix[linear_sum_assignment(distance_matrix)].sum() + abs(
len(list_a) - len(list_b)
) | [
"def",
"skip_connections_distance",
"(",
"list_a",
",",
"list_b",
")",
":",
"distance_matrix",
"=",
"np",
".",
"zeros",
"(",
"(",
"len",
"(",
"list_a",
")",
",",
"len",
"(",
"list_b",
")",
")",
")",
"for",
"i",
",",
"a",
"in",
"enumerate",
"(",
"list_a",
")",
":",
"for",
"j",
",",
"b",
"in",
"enumerate",
"(",
"list_b",
")",
":",
"distance_matrix",
"[",
"i",
"]",
"[",
"j",
"]",
"=",
"skip_connection_distance",
"(",
"a",
",",
"b",
")",
"return",
"distance_matrix",
"[",
"linear_sum_assignment",
"(",
"distance_matrix",
")",
"]",
".",
"sum",
"(",
")",
"+",
"abs",
"(",
"len",
"(",
"list_a",
")",
"-",
"len",
"(",
"list_b",
")",
")"
] | The distance between the skip-connections of two neural networks. | [
"The",
"distance",
"between",
"the",
"skip",
"-",
"connections",
"of",
"two",
"neural",
"networks",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L101-L109 |
27,090 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | vector_distance | def vector_distance(a, b):
"""The Euclidean distance between two vectors."""
a = np.array(a)
b = np.array(b)
return np.linalg.norm(a - b) | python | def vector_distance(a, b):
"""The Euclidean distance between two vectors."""
a = np.array(a)
b = np.array(b)
return np.linalg.norm(a - b) | [
"def",
"vector_distance",
"(",
"a",
",",
"b",
")",
":",
"a",
"=",
"np",
".",
"array",
"(",
"a",
")",
"b",
"=",
"np",
".",
"array",
"(",
"b",
")",
"return",
"np",
".",
"linalg",
".",
"norm",
"(",
"a",
"-",
"b",
")"
] | The Euclidean distance between two vectors. | [
"The",
"Euclidean",
"distance",
"between",
"two",
"vectors",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L269-L273 |
27,091 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | contain | def contain(descriptors, target_descriptor):
"""Check if the target descriptor is in the descriptors."""
for descriptor in descriptors:
if edit_distance(descriptor, target_descriptor) < 1e-5:
return True
return False | python | def contain(descriptors, target_descriptor):
"""Check if the target descriptor is in the descriptors."""
for descriptor in descriptors:
if edit_distance(descriptor, target_descriptor) < 1e-5:
return True
return False | [
"def",
"contain",
"(",
"descriptors",
",",
"target_descriptor",
")",
":",
"for",
"descriptor",
"in",
"descriptors",
":",
"if",
"edit_distance",
"(",
"descriptor",
",",
"target_descriptor",
")",
"<",
"1e-5",
":",
"return",
"True",
"return",
"False"
] | Check if the target descriptor is in the descriptors. | [
"Check",
"if",
"the",
"target",
"descriptor",
"is",
"in",
"the",
"descriptors",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L449-L454 |
27,092 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | IncrementalGaussianProcess.incremental_fit | def incremental_fit(self, train_x, train_y):
""" Incrementally fit the regressor. """
if not self._first_fitted:
raise ValueError("The first_fit function needs to be called first.")
train_x, train_y = np.array(train_x), np.array(train_y)
# Incrementally compute K
up_right_k = edit_distance_matrix(self._x, train_x)
down_left_k = np.transpose(up_right_k)
down_right_k = edit_distance_matrix(train_x)
up_k = np.concatenate((self._distance_matrix, up_right_k), axis=1)
down_k = np.concatenate((down_left_k, down_right_k), axis=1)
temp_distance_matrix = np.concatenate((up_k, down_k), axis=0)
k_matrix = bourgain_embedding_matrix(temp_distance_matrix)
diagonal = np.diag_indices_from(k_matrix)
diagonal = (diagonal[0][-len(train_x) :], diagonal[1][-len(train_x) :])
k_matrix[diagonal] += self.alpha
try:
self._l_matrix = cholesky(k_matrix, lower=True) # Line 2
except LinAlgError:
return self
self._x = np.concatenate((self._x, train_x), axis=0)
self._y = np.concatenate((self._y, train_y), axis=0)
self._distance_matrix = temp_distance_matrix
self._alpha_vector = cho_solve((self._l_matrix, True), self._y) # Line 3
return self | python | def incremental_fit(self, train_x, train_y):
""" Incrementally fit the regressor. """
if not self._first_fitted:
raise ValueError("The first_fit function needs to be called first.")
train_x, train_y = np.array(train_x), np.array(train_y)
# Incrementally compute K
up_right_k = edit_distance_matrix(self._x, train_x)
down_left_k = np.transpose(up_right_k)
down_right_k = edit_distance_matrix(train_x)
up_k = np.concatenate((self._distance_matrix, up_right_k), axis=1)
down_k = np.concatenate((down_left_k, down_right_k), axis=1)
temp_distance_matrix = np.concatenate((up_k, down_k), axis=0)
k_matrix = bourgain_embedding_matrix(temp_distance_matrix)
diagonal = np.diag_indices_from(k_matrix)
diagonal = (diagonal[0][-len(train_x) :], diagonal[1][-len(train_x) :])
k_matrix[diagonal] += self.alpha
try:
self._l_matrix = cholesky(k_matrix, lower=True) # Line 2
except LinAlgError:
return self
self._x = np.concatenate((self._x, train_x), axis=0)
self._y = np.concatenate((self._y, train_y), axis=0)
self._distance_matrix = temp_distance_matrix
self._alpha_vector = cho_solve((self._l_matrix, True), self._y) # Line 3
return self | [
"def",
"incremental_fit",
"(",
"self",
",",
"train_x",
",",
"train_y",
")",
":",
"if",
"not",
"self",
".",
"_first_fitted",
":",
"raise",
"ValueError",
"(",
"\"The first_fit function needs to be called first.\"",
")",
"train_x",
",",
"train_y",
"=",
"np",
".",
"array",
"(",
"train_x",
")",
",",
"np",
".",
"array",
"(",
"train_y",
")",
"# Incrementally compute K",
"up_right_k",
"=",
"edit_distance_matrix",
"(",
"self",
".",
"_x",
",",
"train_x",
")",
"down_left_k",
"=",
"np",
".",
"transpose",
"(",
"up_right_k",
")",
"down_right_k",
"=",
"edit_distance_matrix",
"(",
"train_x",
")",
"up_k",
"=",
"np",
".",
"concatenate",
"(",
"(",
"self",
".",
"_distance_matrix",
",",
"up_right_k",
")",
",",
"axis",
"=",
"1",
")",
"down_k",
"=",
"np",
".",
"concatenate",
"(",
"(",
"down_left_k",
",",
"down_right_k",
")",
",",
"axis",
"=",
"1",
")",
"temp_distance_matrix",
"=",
"np",
".",
"concatenate",
"(",
"(",
"up_k",
",",
"down_k",
")",
",",
"axis",
"=",
"0",
")",
"k_matrix",
"=",
"bourgain_embedding_matrix",
"(",
"temp_distance_matrix",
")",
"diagonal",
"=",
"np",
".",
"diag_indices_from",
"(",
"k_matrix",
")",
"diagonal",
"=",
"(",
"diagonal",
"[",
"0",
"]",
"[",
"-",
"len",
"(",
"train_x",
")",
":",
"]",
",",
"diagonal",
"[",
"1",
"]",
"[",
"-",
"len",
"(",
"train_x",
")",
":",
"]",
")",
"k_matrix",
"[",
"diagonal",
"]",
"+=",
"self",
".",
"alpha",
"try",
":",
"self",
".",
"_l_matrix",
"=",
"cholesky",
"(",
"k_matrix",
",",
"lower",
"=",
"True",
")",
"# Line 2",
"except",
"LinAlgError",
":",
"return",
"self",
"self",
".",
"_x",
"=",
"np",
".",
"concatenate",
"(",
"(",
"self",
".",
"_x",
",",
"train_x",
")",
",",
"axis",
"=",
"0",
")",
"self",
".",
"_y",
"=",
"np",
".",
"concatenate",
"(",
"(",
"self",
".",
"_y",
",",
"train_y",
")",
",",
"axis",
"=",
"0",
")",
"self",
".",
"_distance_matrix",
"=",
"temp_distance_matrix",
"self",
".",
"_alpha_vector",
"=",
"cho_solve",
"(",
"(",
"self",
".",
"_l_matrix",
",",
"True",
")",
",",
"self",
".",
"_y",
")",
"# Line 3",
"return",
"self"
] | Incrementally fit the regressor. | [
"Incrementally",
"fit",
"the",
"regressor",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L160-L190 |
27,093 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | IncrementalGaussianProcess.first_fit | def first_fit(self, train_x, train_y):
""" Fit the regressor for the first time. """
train_x, train_y = np.array(train_x), np.array(train_y)
self._x = np.copy(train_x)
self._y = np.copy(train_y)
self._distance_matrix = edit_distance_matrix(self._x)
k_matrix = bourgain_embedding_matrix(self._distance_matrix)
k_matrix[np.diag_indices_from(k_matrix)] += self.alpha
self._l_matrix = cholesky(k_matrix, lower=True) # Line 2
self._alpha_vector = cho_solve((self._l_matrix, True), self._y) # Line 3
self._first_fitted = True
return self | python | def first_fit(self, train_x, train_y):
""" Fit the regressor for the first time. """
train_x, train_y = np.array(train_x), np.array(train_y)
self._x = np.copy(train_x)
self._y = np.copy(train_y)
self._distance_matrix = edit_distance_matrix(self._x)
k_matrix = bourgain_embedding_matrix(self._distance_matrix)
k_matrix[np.diag_indices_from(k_matrix)] += self.alpha
self._l_matrix = cholesky(k_matrix, lower=True) # Line 2
self._alpha_vector = cho_solve((self._l_matrix, True), self._y) # Line 3
self._first_fitted = True
return self | [
"def",
"first_fit",
"(",
"self",
",",
"train_x",
",",
"train_y",
")",
":",
"train_x",
",",
"train_y",
"=",
"np",
".",
"array",
"(",
"train_x",
")",
",",
"np",
".",
"array",
"(",
"train_y",
")",
"self",
".",
"_x",
"=",
"np",
".",
"copy",
"(",
"train_x",
")",
"self",
".",
"_y",
"=",
"np",
".",
"copy",
"(",
"train_y",
")",
"self",
".",
"_distance_matrix",
"=",
"edit_distance_matrix",
"(",
"self",
".",
"_x",
")",
"k_matrix",
"=",
"bourgain_embedding_matrix",
"(",
"self",
".",
"_distance_matrix",
")",
"k_matrix",
"[",
"np",
".",
"diag_indices_from",
"(",
"k_matrix",
")",
"]",
"+=",
"self",
".",
"alpha",
"self",
".",
"_l_matrix",
"=",
"cholesky",
"(",
"k_matrix",
",",
"lower",
"=",
"True",
")",
"# Line 2",
"self",
".",
"_alpha_vector",
"=",
"cho_solve",
"(",
"(",
"self",
".",
"_l_matrix",
",",
"True",
")",
",",
"self",
".",
"_y",
")",
"# Line 3",
"self",
".",
"_first_fitted",
"=",
"True",
"return",
"self"
] | Fit the regressor for the first time. | [
"Fit",
"the",
"regressor",
"for",
"the",
"first",
"time",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L198-L214 |
27,094 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | BayesianOptimizer.acq | def acq(self, graph):
''' estimate the value of generated graph
'''
mean, std = self.gpr.predict(np.array([graph.extract_descriptor()]))
if self.optimizemode is OptimizeMode.Maximize:
return mean + self.beta * std
return mean - self.beta * std | python | def acq(self, graph):
''' estimate the value of generated graph
'''
mean, std = self.gpr.predict(np.array([graph.extract_descriptor()]))
if self.optimizemode is OptimizeMode.Maximize:
return mean + self.beta * std
return mean - self.beta * std | [
"def",
"acq",
"(",
"self",
",",
"graph",
")",
":",
"mean",
",",
"std",
"=",
"self",
".",
"gpr",
".",
"predict",
"(",
"np",
".",
"array",
"(",
"[",
"graph",
".",
"extract_descriptor",
"(",
")",
"]",
")",
")",
"if",
"self",
".",
"optimizemode",
"is",
"OptimizeMode",
".",
"Maximize",
":",
"return",
"mean",
"+",
"self",
".",
"beta",
"*",
"std",
"return",
"mean",
"-",
"self",
".",
"beta",
"*",
"std"
] | estimate the value of generated graph | [
"estimate",
"the",
"value",
"of",
"generated",
"graph"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L396-L402 |
27,095 | Microsoft/nni | src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py | SearchTree.get_dict | def get_dict(self, u=None):
""" A recursive function to return the content of the tree in a dict."""
if u is None:
return self.get_dict(self.root)
children = []
for v in self.adj_list[u]:
children.append(self.get_dict(v))
ret = {"name": u, "children": children}
return ret | python | def get_dict(self, u=None):
""" A recursive function to return the content of the tree in a dict."""
if u is None:
return self.get_dict(self.root)
children = []
for v in self.adj_list[u]:
children.append(self.get_dict(v))
ret = {"name": u, "children": children}
return ret | [
"def",
"get_dict",
"(",
"self",
",",
"u",
"=",
"None",
")",
":",
"if",
"u",
"is",
"None",
":",
"return",
"self",
".",
"get_dict",
"(",
"self",
".",
"root",
")",
"children",
"=",
"[",
"]",
"for",
"v",
"in",
"self",
".",
"adj_list",
"[",
"u",
"]",
":",
"children",
".",
"append",
"(",
"self",
".",
"get_dict",
"(",
"v",
")",
")",
"ret",
"=",
"{",
"\"name\"",
":",
"u",
",",
"\"children\"",
":",
"children",
"}",
"return",
"ret"
] | A recursive function to return the content of the tree in a dict. | [
"A",
"recursive",
"function",
"to",
"return",
"the",
"content",
"of",
"the",
"tree",
"in",
"a",
"dict",
"."
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/src/sdk/pynni/nni/networkmorphism_tuner/bayesian.py#L480-L488 |
27,096 | Microsoft/nni | examples/trials/weight_sharing/ga_squad/graph.py | Layer.update_hash | def update_hash(self, layers: Iterable):
"""
Calculation of `hash_id` of Layer. Which is determined by the properties of itself, and the `hash_id`s of input layers
"""
if self.graph_type == LayerType.input.value:
return
hasher = hashlib.md5()
hasher.update(LayerType(self.graph_type).name.encode('ascii'))
hasher.update(str(self.size).encode('ascii'))
for i in self.input:
if layers[i].hash_id is None:
raise ValueError('Hash id of layer {}: {} not generated!'.format(i, layers[i]))
hasher.update(layers[i].hash_id.encode('ascii'))
self.hash_id = hasher.hexdigest() | python | def update_hash(self, layers: Iterable):
"""
Calculation of `hash_id` of Layer. Which is determined by the properties of itself, and the `hash_id`s of input layers
"""
if self.graph_type == LayerType.input.value:
return
hasher = hashlib.md5()
hasher.update(LayerType(self.graph_type).name.encode('ascii'))
hasher.update(str(self.size).encode('ascii'))
for i in self.input:
if layers[i].hash_id is None:
raise ValueError('Hash id of layer {}: {} not generated!'.format(i, layers[i]))
hasher.update(layers[i].hash_id.encode('ascii'))
self.hash_id = hasher.hexdigest() | [
"def",
"update_hash",
"(",
"self",
",",
"layers",
":",
"Iterable",
")",
":",
"if",
"self",
".",
"graph_type",
"==",
"LayerType",
".",
"input",
".",
"value",
":",
"return",
"hasher",
"=",
"hashlib",
".",
"md5",
"(",
")",
"hasher",
".",
"update",
"(",
"LayerType",
"(",
"self",
".",
"graph_type",
")",
".",
"name",
".",
"encode",
"(",
"'ascii'",
")",
")",
"hasher",
".",
"update",
"(",
"str",
"(",
"self",
".",
"size",
")",
".",
"encode",
"(",
"'ascii'",
")",
")",
"for",
"i",
"in",
"self",
".",
"input",
":",
"if",
"layers",
"[",
"i",
"]",
".",
"hash_id",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"'Hash id of layer {}: {} not generated!'",
".",
"format",
"(",
"i",
",",
"layers",
"[",
"i",
"]",
")",
")",
"hasher",
".",
"update",
"(",
"layers",
"[",
"i",
"]",
".",
"hash_id",
".",
"encode",
"(",
"'ascii'",
")",
")",
"self",
".",
"hash_id",
"=",
"hasher",
".",
"hexdigest",
"(",
")"
] | Calculation of `hash_id` of Layer. Which is determined by the properties of itself, and the `hash_id`s of input layers | [
"Calculation",
"of",
"hash_id",
"of",
"Layer",
".",
"Which",
"is",
"determined",
"by",
"the",
"properties",
"of",
"itself",
"and",
"the",
"hash_id",
"s",
"of",
"input",
"layers"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/weight_sharing/ga_squad/graph.py#L83-L96 |
27,097 | Microsoft/nni | examples/trials/mnist-batch-tune-keras/mnist-keras.py | create_mnist_model | def create_mnist_model(hyper_params, input_shape=(H, W, 1), num_classes=NUM_CLASSES):
'''
Create simple convolutional model
'''
layers = [
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(100, activation='relu'),
Dense(num_classes, activation='softmax')
]
model = Sequential(layers)
if hyper_params['optimizer'] == 'Adam':
optimizer = keras.optimizers.Adam(lr=hyper_params['learning_rate'])
else:
optimizer = keras.optimizers.SGD(lr=hyper_params['learning_rate'], momentum=0.9)
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=optimizer, metrics=['accuracy'])
return model | python | def create_mnist_model(hyper_params, input_shape=(H, W, 1), num_classes=NUM_CLASSES):
'''
Create simple convolutional model
'''
layers = [
Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D(pool_size=(2, 2)),
Flatten(),
Dense(100, activation='relu'),
Dense(num_classes, activation='softmax')
]
model = Sequential(layers)
if hyper_params['optimizer'] == 'Adam':
optimizer = keras.optimizers.Adam(lr=hyper_params['learning_rate'])
else:
optimizer = keras.optimizers.SGD(lr=hyper_params['learning_rate'], momentum=0.9)
model.compile(loss=keras.losses.categorical_crossentropy, optimizer=optimizer, metrics=['accuracy'])
return model | [
"def",
"create_mnist_model",
"(",
"hyper_params",
",",
"input_shape",
"=",
"(",
"H",
",",
"W",
",",
"1",
")",
",",
"num_classes",
"=",
"NUM_CLASSES",
")",
":",
"layers",
"=",
"[",
"Conv2D",
"(",
"32",
",",
"kernel_size",
"=",
"(",
"3",
",",
"3",
")",
",",
"activation",
"=",
"'relu'",
",",
"input_shape",
"=",
"input_shape",
")",
",",
"Conv2D",
"(",
"64",
",",
"(",
"3",
",",
"3",
")",
",",
"activation",
"=",
"'relu'",
")",
",",
"MaxPooling2D",
"(",
"pool_size",
"=",
"(",
"2",
",",
"2",
")",
")",
",",
"Flatten",
"(",
")",
",",
"Dense",
"(",
"100",
",",
"activation",
"=",
"'relu'",
")",
",",
"Dense",
"(",
"num_classes",
",",
"activation",
"=",
"'softmax'",
")",
"]",
"model",
"=",
"Sequential",
"(",
"layers",
")",
"if",
"hyper_params",
"[",
"'optimizer'",
"]",
"==",
"'Adam'",
":",
"optimizer",
"=",
"keras",
".",
"optimizers",
".",
"Adam",
"(",
"lr",
"=",
"hyper_params",
"[",
"'learning_rate'",
"]",
")",
"else",
":",
"optimizer",
"=",
"keras",
".",
"optimizers",
".",
"SGD",
"(",
"lr",
"=",
"hyper_params",
"[",
"'learning_rate'",
"]",
",",
"momentum",
"=",
"0.9",
")",
"model",
".",
"compile",
"(",
"loss",
"=",
"keras",
".",
"losses",
".",
"categorical_crossentropy",
",",
"optimizer",
"=",
"optimizer",
",",
"metrics",
"=",
"[",
"'accuracy'",
"]",
")",
"return",
"model"
] | Create simple convolutional model | [
"Create",
"simple",
"convolutional",
"model"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-batch-tune-keras/mnist-keras.py#L39-L60 |
27,098 | Microsoft/nni | examples/trials/mnist-batch-tune-keras/mnist-keras.py | load_mnist_data | def load_mnist_data(args):
'''
Load MNIST dataset
'''
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = (np.expand_dims(x_train, -1).astype(np.float) / 255.)[:args.num_train]
x_test = (np.expand_dims(x_test, -1).astype(np.float) / 255.)[:args.num_test]
y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)[:args.num_train]
y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)[:args.num_test]
LOG.debug('x_train shape: %s', (x_train.shape,))
LOG.debug('x_test shape: %s', (x_test.shape,))
return x_train, y_train, x_test, y_test | python | def load_mnist_data(args):
'''
Load MNIST dataset
'''
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = (np.expand_dims(x_train, -1).astype(np.float) / 255.)[:args.num_train]
x_test = (np.expand_dims(x_test, -1).astype(np.float) / 255.)[:args.num_test]
y_train = keras.utils.to_categorical(y_train, NUM_CLASSES)[:args.num_train]
y_test = keras.utils.to_categorical(y_test, NUM_CLASSES)[:args.num_test]
LOG.debug('x_train shape: %s', (x_train.shape,))
LOG.debug('x_test shape: %s', (x_test.shape,))
return x_train, y_train, x_test, y_test | [
"def",
"load_mnist_data",
"(",
"args",
")",
":",
"(",
"x_train",
",",
"y_train",
")",
",",
"(",
"x_test",
",",
"y_test",
")",
"=",
"mnist",
".",
"load_data",
"(",
")",
"x_train",
"=",
"(",
"np",
".",
"expand_dims",
"(",
"x_train",
",",
"-",
"1",
")",
".",
"astype",
"(",
"np",
".",
"float",
")",
"/",
"255.",
")",
"[",
":",
"args",
".",
"num_train",
"]",
"x_test",
"=",
"(",
"np",
".",
"expand_dims",
"(",
"x_test",
",",
"-",
"1",
")",
".",
"astype",
"(",
"np",
".",
"float",
")",
"/",
"255.",
")",
"[",
":",
"args",
".",
"num_test",
"]",
"y_train",
"=",
"keras",
".",
"utils",
".",
"to_categorical",
"(",
"y_train",
",",
"NUM_CLASSES",
")",
"[",
":",
"args",
".",
"num_train",
"]",
"y_test",
"=",
"keras",
".",
"utils",
".",
"to_categorical",
"(",
"y_test",
",",
"NUM_CLASSES",
")",
"[",
":",
"args",
".",
"num_test",
"]",
"LOG",
".",
"debug",
"(",
"'x_train shape: %s'",
",",
"(",
"x_train",
".",
"shape",
",",
")",
")",
"LOG",
".",
"debug",
"(",
"'x_test shape: %s'",
",",
"(",
"x_test",
".",
"shape",
",",
")",
")",
"return",
"x_train",
",",
"y_train",
",",
"x_test",
",",
"y_test"
] | Load MNIST dataset | [
"Load",
"MNIST",
"dataset"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/examples/trials/mnist-batch-tune-keras/mnist-keras.py#L62-L76 |
27,099 | Microsoft/nni | tools/nni_cmd/config_utils.py | Config.get_all_config | def get_all_config(self):
'''get all of config values'''
return json.dumps(self.config, indent=4, sort_keys=True, separators=(',', ':')) | python | def get_all_config(self):
'''get all of config values'''
return json.dumps(self.config, indent=4, sort_keys=True, separators=(',', ':')) | [
"def",
"get_all_config",
"(",
"self",
")",
":",
"return",
"json",
".",
"dumps",
"(",
"self",
".",
"config",
",",
"indent",
"=",
"4",
",",
"sort_keys",
"=",
"True",
",",
"separators",
"=",
"(",
"','",
",",
"':'",
")",
")"
] | get all of config values | [
"get",
"all",
"of",
"config",
"values"
] | c7cc8db32da8d2ec77a382a55089f4e17247ce41 | https://github.com/Microsoft/nni/blob/c7cc8db32da8d2ec77a382a55089f4e17247ce41/tools/nni_cmd/config_utils.py#L35-L37 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.