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 |
|---|---|---|---|---|---|---|---|---|---|---|---|
28,600 | tensorflow/cleverhans | cleverhans/attacks/bapp.py | initialize | def initialize(decision_function, sample, shape, clip_min, clip_max):
"""
Efficient Implementation of BlendedUniformNoiseAttack in Foolbox.
"""
success = 0
num_evals = 0
# Find a misclassified random noise.
while True:
random_noise = np.random.uniform(clip_min, clip_max, size=shape)
success = decision_function(random_noise[None])[0]
if success:
break
num_evals += 1
message = "Initialization failed! Try to use a misclassified image as `target_image`"
assert num_evals < 1e4, message
# Binary search to minimize l2 distance to original image.
low = 0.0
high = 1.0
while high - low > 0.001:
mid = (high + low) / 2.0
blended = (1 - mid) * sample + mid * random_noise
success = decision_function(blended[None])[0]
if success:
high = mid
else:
low = mid
initialization = (1 - high) * sample + high * random_noise
return initialization | python | def initialize(decision_function, sample, shape, clip_min, clip_max):
"""
Efficient Implementation of BlendedUniformNoiseAttack in Foolbox.
"""
success = 0
num_evals = 0
# Find a misclassified random noise.
while True:
random_noise = np.random.uniform(clip_min, clip_max, size=shape)
success = decision_function(random_noise[None])[0]
if success:
break
num_evals += 1
message = "Initialization failed! Try to use a misclassified image as `target_image`"
assert num_evals < 1e4, message
# Binary search to minimize l2 distance to original image.
low = 0.0
high = 1.0
while high - low > 0.001:
mid = (high + low) / 2.0
blended = (1 - mid) * sample + mid * random_noise
success = decision_function(blended[None])[0]
if success:
high = mid
else:
low = mid
initialization = (1 - high) * sample + high * random_noise
return initialization | [
"def",
"initialize",
"(",
"decision_function",
",",
"sample",
",",
"shape",
",",
"clip_min",
",",
"clip_max",
")",
":",
"success",
"=",
"0",
"num_evals",
"=",
"0",
"# Find a misclassified random noise.",
"while",
"True",
":",
"random_noise",
"=",
"np",
".",
"random",
".",
"uniform",
"(",
"clip_min",
",",
"clip_max",
",",
"size",
"=",
"shape",
")",
"success",
"=",
"decision_function",
"(",
"random_noise",
"[",
"None",
"]",
")",
"[",
"0",
"]",
"if",
"success",
":",
"break",
"num_evals",
"+=",
"1",
"message",
"=",
"\"Initialization failed! Try to use a misclassified image as `target_image`\"",
"assert",
"num_evals",
"<",
"1e4",
",",
"message",
"# Binary search to minimize l2 distance to original image.",
"low",
"=",
"0.0",
"high",
"=",
"1.0",
"while",
"high",
"-",
"low",
">",
"0.001",
":",
"mid",
"=",
"(",
"high",
"+",
"low",
")",
"/",
"2.0",
"blended",
"=",
"(",
"1",
"-",
"mid",
")",
"*",
"sample",
"+",
"mid",
"*",
"random_noise",
"success",
"=",
"decision_function",
"(",
"blended",
"[",
"None",
"]",
")",
"[",
"0",
"]",
"if",
"success",
":",
"high",
"=",
"mid",
"else",
":",
"low",
"=",
"mid",
"initialization",
"=",
"(",
"1",
"-",
"high",
")",
"*",
"sample",
"+",
"high",
"*",
"random_noise",
"return",
"initialization"
] | Efficient Implementation of BlendedUniformNoiseAttack in Foolbox. | [
"Efficient",
"Implementation",
"of",
"BlendedUniformNoiseAttack",
"in",
"Foolbox",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/bapp.py#L471-L501 |
28,601 | tensorflow/cleverhans | cleverhans/attacks/bapp.py | geometric_progression_for_stepsize | def geometric_progression_for_stepsize(x, update, dist, decision_function,
current_iteration):
""" Geometric progression to search for stepsize.
Keep decreasing stepsize by half until reaching
the desired side of the boundary.
"""
epsilon = dist / np.sqrt(current_iteration)
while True:
updated = x + epsilon * update
success = decision_function(updated[None])[0]
if success:
break
else:
epsilon = epsilon / 2.0
return epsilon | python | def geometric_progression_for_stepsize(x, update, dist, decision_function,
current_iteration):
""" Geometric progression to search for stepsize.
Keep decreasing stepsize by half until reaching
the desired side of the boundary.
"""
epsilon = dist / np.sqrt(current_iteration)
while True:
updated = x + epsilon * update
success = decision_function(updated[None])[0]
if success:
break
else:
epsilon = epsilon / 2.0
return epsilon | [
"def",
"geometric_progression_for_stepsize",
"(",
"x",
",",
"update",
",",
"dist",
",",
"decision_function",
",",
"current_iteration",
")",
":",
"epsilon",
"=",
"dist",
"/",
"np",
".",
"sqrt",
"(",
"current_iteration",
")",
"while",
"True",
":",
"updated",
"=",
"x",
"+",
"epsilon",
"*",
"update",
"success",
"=",
"decision_function",
"(",
"updated",
"[",
"None",
"]",
")",
"[",
"0",
"]",
"if",
"success",
":",
"break",
"else",
":",
"epsilon",
"=",
"epsilon",
"/",
"2.0",
"return",
"epsilon"
] | Geometric progression to search for stepsize.
Keep decreasing stepsize by half until reaching
the desired side of the boundary. | [
"Geometric",
"progression",
"to",
"search",
"for",
"stepsize",
".",
"Keep",
"decreasing",
"stepsize",
"by",
"half",
"until",
"reaching",
"the",
"desired",
"side",
"of",
"the",
"boundary",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/bapp.py#L504-L519 |
28,602 | tensorflow/cleverhans | cleverhans/attacks/bapp.py | select_delta | def select_delta(dist_post_update, current_iteration,
clip_max, clip_min, d, theta, constraint):
"""
Choose the delta at the scale of distance
between x and perturbed sample.
"""
if current_iteration == 1:
delta = 0.1 * (clip_max - clip_min)
else:
if constraint == 'l2':
delta = np.sqrt(d) * theta * dist_post_update
elif constraint == 'linf':
delta = d * theta * dist_post_update
return delta | python | def select_delta(dist_post_update, current_iteration,
clip_max, clip_min, d, theta, constraint):
"""
Choose the delta at the scale of distance
between x and perturbed sample.
"""
if current_iteration == 1:
delta = 0.1 * (clip_max - clip_min)
else:
if constraint == 'l2':
delta = np.sqrt(d) * theta * dist_post_update
elif constraint == 'linf':
delta = d * theta * dist_post_update
return delta | [
"def",
"select_delta",
"(",
"dist_post_update",
",",
"current_iteration",
",",
"clip_max",
",",
"clip_min",
",",
"d",
",",
"theta",
",",
"constraint",
")",
":",
"if",
"current_iteration",
"==",
"1",
":",
"delta",
"=",
"0.1",
"*",
"(",
"clip_max",
"-",
"clip_min",
")",
"else",
":",
"if",
"constraint",
"==",
"'l2'",
":",
"delta",
"=",
"np",
".",
"sqrt",
"(",
"d",
")",
"*",
"theta",
"*",
"dist_post_update",
"elif",
"constraint",
"==",
"'linf'",
":",
"delta",
"=",
"d",
"*",
"theta",
"*",
"dist_post_update",
"return",
"delta"
] | Choose the delta at the scale of distance
between x and perturbed sample. | [
"Choose",
"the",
"delta",
"at",
"the",
"scale",
"of",
"distance",
"between",
"x",
"and",
"perturbed",
"sample",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/bapp.py#L522-L536 |
28,603 | tensorflow/cleverhans | cleverhans/attacks/fast_feature_adversaries.py | FastFeatureAdversaries.attack_single_step | def attack_single_step(self, x, eta, g_feat):
"""
TensorFlow implementation of the Fast Feature Gradient. This is a
single step attack similar to Fast Gradient Method that attacks an
internal representation.
:param x: the input placeholder
:param eta: A tensor the same shape as x that holds the perturbation.
:param g_feat: model's internal tensor for guide
:return: a tensor for the adversarial example
"""
adv_x = x + eta
a_feat = self.model.fprop(adv_x)[self.layer]
# feat.shape = (batch, c) or (batch, w, h, c)
axis = list(range(1, len(a_feat.shape)))
# Compute loss
# This is a targeted attack, hence the negative sign
loss = -reduce_sum(tf.square(a_feat - g_feat), axis)
# Define gradient of loss wrt input
grad, = tf.gradients(loss, adv_x)
# Multiply by constant epsilon
scaled_signed_grad = self.eps_iter * tf.sign(grad)
# Add perturbation to original example to obtain adversarial example
adv_x = adv_x + scaled_signed_grad
# If clipping is needed,
# reset all values outside of [clip_min, clip_max]
if (self.clip_min is not None) and (self.clip_max is not None):
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
adv_x = tf.stop_gradient(adv_x)
eta = adv_x - x
eta = clip_eta(eta, self.ord, self.eps)
return eta | python | def attack_single_step(self, x, eta, g_feat):
"""
TensorFlow implementation of the Fast Feature Gradient. This is a
single step attack similar to Fast Gradient Method that attacks an
internal representation.
:param x: the input placeholder
:param eta: A tensor the same shape as x that holds the perturbation.
:param g_feat: model's internal tensor for guide
:return: a tensor for the adversarial example
"""
adv_x = x + eta
a_feat = self.model.fprop(adv_x)[self.layer]
# feat.shape = (batch, c) or (batch, w, h, c)
axis = list(range(1, len(a_feat.shape)))
# Compute loss
# This is a targeted attack, hence the negative sign
loss = -reduce_sum(tf.square(a_feat - g_feat), axis)
# Define gradient of loss wrt input
grad, = tf.gradients(loss, adv_x)
# Multiply by constant epsilon
scaled_signed_grad = self.eps_iter * tf.sign(grad)
# Add perturbation to original example to obtain adversarial example
adv_x = adv_x + scaled_signed_grad
# If clipping is needed,
# reset all values outside of [clip_min, clip_max]
if (self.clip_min is not None) and (self.clip_max is not None):
adv_x = tf.clip_by_value(adv_x, self.clip_min, self.clip_max)
adv_x = tf.stop_gradient(adv_x)
eta = adv_x - x
eta = clip_eta(eta, self.ord, self.eps)
return eta | [
"def",
"attack_single_step",
"(",
"self",
",",
"x",
",",
"eta",
",",
"g_feat",
")",
":",
"adv_x",
"=",
"x",
"+",
"eta",
"a_feat",
"=",
"self",
".",
"model",
".",
"fprop",
"(",
"adv_x",
")",
"[",
"self",
".",
"layer",
"]",
"# feat.shape = (batch, c) or (batch, w, h, c)",
"axis",
"=",
"list",
"(",
"range",
"(",
"1",
",",
"len",
"(",
"a_feat",
".",
"shape",
")",
")",
")",
"# Compute loss",
"# This is a targeted attack, hence the negative sign",
"loss",
"=",
"-",
"reduce_sum",
"(",
"tf",
".",
"square",
"(",
"a_feat",
"-",
"g_feat",
")",
",",
"axis",
")",
"# Define gradient of loss wrt input",
"grad",
",",
"=",
"tf",
".",
"gradients",
"(",
"loss",
",",
"adv_x",
")",
"# Multiply by constant epsilon",
"scaled_signed_grad",
"=",
"self",
".",
"eps_iter",
"*",
"tf",
".",
"sign",
"(",
"grad",
")",
"# Add perturbation to original example to obtain adversarial example",
"adv_x",
"=",
"adv_x",
"+",
"scaled_signed_grad",
"# If clipping is needed,",
"# reset all values outside of [clip_min, clip_max]",
"if",
"(",
"self",
".",
"clip_min",
"is",
"not",
"None",
")",
"and",
"(",
"self",
".",
"clip_max",
"is",
"not",
"None",
")",
":",
"adv_x",
"=",
"tf",
".",
"clip_by_value",
"(",
"adv_x",
",",
"self",
".",
"clip_min",
",",
"self",
".",
"clip_max",
")",
"adv_x",
"=",
"tf",
".",
"stop_gradient",
"(",
"adv_x",
")",
"eta",
"=",
"adv_x",
"-",
"x",
"eta",
"=",
"clip_eta",
"(",
"eta",
",",
"self",
".",
"ord",
",",
"self",
".",
"eps",
")",
"return",
"eta"
] | TensorFlow implementation of the Fast Feature Gradient. This is a
single step attack similar to Fast Gradient Method that attacks an
internal representation.
:param x: the input placeholder
:param eta: A tensor the same shape as x that holds the perturbation.
:param g_feat: model's internal tensor for guide
:return: a tensor for the adversarial example | [
"TensorFlow",
"implementation",
"of",
"the",
"Fast",
"Feature",
"Gradient",
".",
"This",
"is",
"a",
"single",
"step",
"attack",
"similar",
"to",
"Fast",
"Gradient",
"Method",
"that",
"attacks",
"an",
"internal",
"representation",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/fast_feature_adversaries.py#L88-L129 |
28,604 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py | block35 | def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
"""Builds the 35x35 resnet block."""
with tf.variable_scope(scope, 'Block35', [net], reuse=reuse):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1')
with tf.variable_scope('Branch_1'):
tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3')
with tf.variable_scope('Branch_2'):
tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
tower_conv2_1 = slim.conv2d(tower_conv2_0, 48, 3, scope='Conv2d_0b_3x3')
tower_conv2_2 = slim.conv2d(tower_conv2_1, 64, 3, scope='Conv2d_0c_3x3')
mixed = tf.concat(
axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2])
up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,
activation_fn=None, scope='Conv2d_1x1')
net += scale * up
if activation_fn:
net = activation_fn(net)
return net | python | def block35(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
"""Builds the 35x35 resnet block."""
with tf.variable_scope(scope, 'Block35', [net], reuse=reuse):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, 32, 1, scope='Conv2d_1x1')
with tf.variable_scope('Branch_1'):
tower_conv1_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1_0, 32, 3, scope='Conv2d_0b_3x3')
with tf.variable_scope('Branch_2'):
tower_conv2_0 = slim.conv2d(net, 32, 1, scope='Conv2d_0a_1x1')
tower_conv2_1 = slim.conv2d(tower_conv2_0, 48, 3, scope='Conv2d_0b_3x3')
tower_conv2_2 = slim.conv2d(tower_conv2_1, 64, 3, scope='Conv2d_0c_3x3')
mixed = tf.concat(
axis=3, values=[tower_conv, tower_conv1_1, tower_conv2_2])
up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,
activation_fn=None, scope='Conv2d_1x1')
net += scale * up
if activation_fn:
net = activation_fn(net)
return net | [
"def",
"block35",
"(",
"net",
",",
"scale",
"=",
"1.0",
",",
"activation_fn",
"=",
"tf",
".",
"nn",
".",
"relu",
",",
"scope",
"=",
"None",
",",
"reuse",
"=",
"None",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"scope",
",",
"'Block35'",
",",
"[",
"net",
"]",
",",
"reuse",
"=",
"reuse",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"'Branch_0'",
")",
":",
"tower_conv",
"=",
"slim",
".",
"conv2d",
"(",
"net",
",",
"32",
",",
"1",
",",
"scope",
"=",
"'Conv2d_1x1'",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"'Branch_1'",
")",
":",
"tower_conv1_0",
"=",
"slim",
".",
"conv2d",
"(",
"net",
",",
"32",
",",
"1",
",",
"scope",
"=",
"'Conv2d_0a_1x1'",
")",
"tower_conv1_1",
"=",
"slim",
".",
"conv2d",
"(",
"tower_conv1_0",
",",
"32",
",",
"3",
",",
"scope",
"=",
"'Conv2d_0b_3x3'",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"'Branch_2'",
")",
":",
"tower_conv2_0",
"=",
"slim",
".",
"conv2d",
"(",
"net",
",",
"32",
",",
"1",
",",
"scope",
"=",
"'Conv2d_0a_1x1'",
")",
"tower_conv2_1",
"=",
"slim",
".",
"conv2d",
"(",
"tower_conv2_0",
",",
"48",
",",
"3",
",",
"scope",
"=",
"'Conv2d_0b_3x3'",
")",
"tower_conv2_2",
"=",
"slim",
".",
"conv2d",
"(",
"tower_conv2_1",
",",
"64",
",",
"3",
",",
"scope",
"=",
"'Conv2d_0c_3x3'",
")",
"mixed",
"=",
"tf",
".",
"concat",
"(",
"axis",
"=",
"3",
",",
"values",
"=",
"[",
"tower_conv",
",",
"tower_conv1_1",
",",
"tower_conv2_2",
"]",
")",
"up",
"=",
"slim",
".",
"conv2d",
"(",
"mixed",
",",
"net",
".",
"get_shape",
"(",
")",
"[",
"3",
"]",
",",
"1",
",",
"normalizer_fn",
"=",
"None",
",",
"activation_fn",
"=",
"None",
",",
"scope",
"=",
"'Conv2d_1x1'",
")",
"net",
"+=",
"scale",
"*",
"up",
"if",
"activation_fn",
":",
"net",
"=",
"activation_fn",
"(",
"net",
")",
"return",
"net"
] | Builds the 35x35 resnet block. | [
"Builds",
"the",
"35x35",
"resnet",
"block",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py#L35-L54 |
28,605 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py | block17 | def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
"""Builds the 17x17 resnet block."""
with tf.variable_scope(scope, 'Block17', [net], reuse=reuse):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1')
with tf.variable_scope('Branch_1'):
tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1_0, 160, [1, 7],
scope='Conv2d_0b_1x7')
tower_conv1_2 = slim.conv2d(tower_conv1_1, 192, [7, 1],
scope='Conv2d_0c_7x1')
mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2])
up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,
activation_fn=None, scope='Conv2d_1x1')
net += scale * up
if activation_fn:
net = activation_fn(net)
return net | python | def block17(net, scale=1.0, activation_fn=tf.nn.relu, scope=None, reuse=None):
"""Builds the 17x17 resnet block."""
with tf.variable_scope(scope, 'Block17', [net], reuse=reuse):
with tf.variable_scope('Branch_0'):
tower_conv = slim.conv2d(net, 192, 1, scope='Conv2d_1x1')
with tf.variable_scope('Branch_1'):
tower_conv1_0 = slim.conv2d(net, 128, 1, scope='Conv2d_0a_1x1')
tower_conv1_1 = slim.conv2d(tower_conv1_0, 160, [1, 7],
scope='Conv2d_0b_1x7')
tower_conv1_2 = slim.conv2d(tower_conv1_1, 192, [7, 1],
scope='Conv2d_0c_7x1')
mixed = tf.concat(axis=3, values=[tower_conv, tower_conv1_2])
up = slim.conv2d(mixed, net.get_shape()[3], 1, normalizer_fn=None,
activation_fn=None, scope='Conv2d_1x1')
net += scale * up
if activation_fn:
net = activation_fn(net)
return net | [
"def",
"block17",
"(",
"net",
",",
"scale",
"=",
"1.0",
",",
"activation_fn",
"=",
"tf",
".",
"nn",
".",
"relu",
",",
"scope",
"=",
"None",
",",
"reuse",
"=",
"None",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"scope",
",",
"'Block17'",
",",
"[",
"net",
"]",
",",
"reuse",
"=",
"reuse",
")",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"'Branch_0'",
")",
":",
"tower_conv",
"=",
"slim",
".",
"conv2d",
"(",
"net",
",",
"192",
",",
"1",
",",
"scope",
"=",
"'Conv2d_1x1'",
")",
"with",
"tf",
".",
"variable_scope",
"(",
"'Branch_1'",
")",
":",
"tower_conv1_0",
"=",
"slim",
".",
"conv2d",
"(",
"net",
",",
"128",
",",
"1",
",",
"scope",
"=",
"'Conv2d_0a_1x1'",
")",
"tower_conv1_1",
"=",
"slim",
".",
"conv2d",
"(",
"tower_conv1_0",
",",
"160",
",",
"[",
"1",
",",
"7",
"]",
",",
"scope",
"=",
"'Conv2d_0b_1x7'",
")",
"tower_conv1_2",
"=",
"slim",
".",
"conv2d",
"(",
"tower_conv1_1",
",",
"192",
",",
"[",
"7",
",",
"1",
"]",
",",
"scope",
"=",
"'Conv2d_0c_7x1'",
")",
"mixed",
"=",
"tf",
".",
"concat",
"(",
"axis",
"=",
"3",
",",
"values",
"=",
"[",
"tower_conv",
",",
"tower_conv1_2",
"]",
")",
"up",
"=",
"slim",
".",
"conv2d",
"(",
"mixed",
",",
"net",
".",
"get_shape",
"(",
")",
"[",
"3",
"]",
",",
"1",
",",
"normalizer_fn",
"=",
"None",
",",
"activation_fn",
"=",
"None",
",",
"scope",
"=",
"'Conv2d_1x1'",
")",
"net",
"+=",
"scale",
"*",
"up",
"if",
"activation_fn",
":",
"net",
"=",
"activation_fn",
"(",
"net",
")",
"return",
"net"
] | Builds the 17x17 resnet block. | [
"Builds",
"the",
"17x17",
"resnet",
"block",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py#L57-L74 |
28,606 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py | inception_resnet_v2 | def inception_resnet_v2(inputs, nb_classes=1001, is_training=True,
dropout_keep_prob=0.8,
reuse=None,
scope='InceptionResnetV2',
create_aux_logits=True,
num_classes=None):
"""Creates the Inception Resnet V2 model.
Args:
inputs: a 4-D tensor of size [batch_size, height, width, 3].
nb_classes: number of predicted classes.
is_training: whether is training or not.
dropout_keep_prob: float, the fraction to keep before final layer.
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
create_aux_logits: Whether to include the auxilliary logits.
num_classes: depricated alias for nb_classes
Returns:
logits: the logits outputs of the model.
end_points: the set of end_points from the inception model.
"""
if num_classes is not None:
warnings.warn("`num_classes` is deprecated. Switch to `nb_classes`."
" `num_classes` may be removed on or after 2019-04-23.")
nb_classes = num_classes
del num_classes
end_points = {}
with tf.variable_scope(scope, 'InceptionResnetV2', [inputs, nb_classes],
reuse=reuse) as var_scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
net, end_points = inception_resnet_v2_base(inputs, scope=var_scope)
if create_aux_logits:
with tf.variable_scope('AuxLogits'):
aux = end_points['PreAuxLogits']
aux = slim.avg_pool2d(aux, 5, stride=3, padding='VALID',
scope='Conv2d_1a_3x3')
aux = slim.conv2d(aux, 128, 1, scope='Conv2d_1b_1x1')
aux = slim.conv2d(aux, 768, aux.get_shape()[1:3],
padding='VALID', scope='Conv2d_2a_5x5')
aux = slim.flatten(aux)
aux = slim.fully_connected(aux, nb_classes, activation_fn=None,
scope='Logits')
end_points['AuxLogits'] = aux
with tf.variable_scope('Logits'):
net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',
scope='AvgPool_1a_8x8')
net = slim.flatten(net)
net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
scope='Dropout')
end_points['PreLogitsFlatten'] = net
logits = slim.fully_connected(net, nb_classes, activation_fn=None,
scope='Logits')
end_points['Logits'] = logits
end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions')
return logits, end_points | python | def inception_resnet_v2(inputs, nb_classes=1001, is_training=True,
dropout_keep_prob=0.8,
reuse=None,
scope='InceptionResnetV2',
create_aux_logits=True,
num_classes=None):
"""Creates the Inception Resnet V2 model.
Args:
inputs: a 4-D tensor of size [batch_size, height, width, 3].
nb_classes: number of predicted classes.
is_training: whether is training or not.
dropout_keep_prob: float, the fraction to keep before final layer.
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
create_aux_logits: Whether to include the auxilliary logits.
num_classes: depricated alias for nb_classes
Returns:
logits: the logits outputs of the model.
end_points: the set of end_points from the inception model.
"""
if num_classes is not None:
warnings.warn("`num_classes` is deprecated. Switch to `nb_classes`."
" `num_classes` may be removed on or after 2019-04-23.")
nb_classes = num_classes
del num_classes
end_points = {}
with tf.variable_scope(scope, 'InceptionResnetV2', [inputs, nb_classes],
reuse=reuse) as var_scope:
with slim.arg_scope([slim.batch_norm, slim.dropout],
is_training=is_training):
net, end_points = inception_resnet_v2_base(inputs, scope=var_scope)
if create_aux_logits:
with tf.variable_scope('AuxLogits'):
aux = end_points['PreAuxLogits']
aux = slim.avg_pool2d(aux, 5, stride=3, padding='VALID',
scope='Conv2d_1a_3x3')
aux = slim.conv2d(aux, 128, 1, scope='Conv2d_1b_1x1')
aux = slim.conv2d(aux, 768, aux.get_shape()[1:3],
padding='VALID', scope='Conv2d_2a_5x5')
aux = slim.flatten(aux)
aux = slim.fully_connected(aux, nb_classes, activation_fn=None,
scope='Logits')
end_points['AuxLogits'] = aux
with tf.variable_scope('Logits'):
net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',
scope='AvgPool_1a_8x8')
net = slim.flatten(net)
net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
scope='Dropout')
end_points['PreLogitsFlatten'] = net
logits = slim.fully_connected(net, nb_classes, activation_fn=None,
scope='Logits')
end_points['Logits'] = logits
end_points['Predictions'] = tf.nn.softmax(logits, name='Predictions')
return logits, end_points | [
"def",
"inception_resnet_v2",
"(",
"inputs",
",",
"nb_classes",
"=",
"1001",
",",
"is_training",
"=",
"True",
",",
"dropout_keep_prob",
"=",
"0.8",
",",
"reuse",
"=",
"None",
",",
"scope",
"=",
"'InceptionResnetV2'",
",",
"create_aux_logits",
"=",
"True",
",",
"num_classes",
"=",
"None",
")",
":",
"if",
"num_classes",
"is",
"not",
"None",
":",
"warnings",
".",
"warn",
"(",
"\"`num_classes` is deprecated. Switch to `nb_classes`.\"",
"\" `num_classes` may be removed on or after 2019-04-23.\"",
")",
"nb_classes",
"=",
"num_classes",
"del",
"num_classes",
"end_points",
"=",
"{",
"}",
"with",
"tf",
".",
"variable_scope",
"(",
"scope",
",",
"'InceptionResnetV2'",
",",
"[",
"inputs",
",",
"nb_classes",
"]",
",",
"reuse",
"=",
"reuse",
")",
"as",
"var_scope",
":",
"with",
"slim",
".",
"arg_scope",
"(",
"[",
"slim",
".",
"batch_norm",
",",
"slim",
".",
"dropout",
"]",
",",
"is_training",
"=",
"is_training",
")",
":",
"net",
",",
"end_points",
"=",
"inception_resnet_v2_base",
"(",
"inputs",
",",
"scope",
"=",
"var_scope",
")",
"if",
"create_aux_logits",
":",
"with",
"tf",
".",
"variable_scope",
"(",
"'AuxLogits'",
")",
":",
"aux",
"=",
"end_points",
"[",
"'PreAuxLogits'",
"]",
"aux",
"=",
"slim",
".",
"avg_pool2d",
"(",
"aux",
",",
"5",
",",
"stride",
"=",
"3",
",",
"padding",
"=",
"'VALID'",
",",
"scope",
"=",
"'Conv2d_1a_3x3'",
")",
"aux",
"=",
"slim",
".",
"conv2d",
"(",
"aux",
",",
"128",
",",
"1",
",",
"scope",
"=",
"'Conv2d_1b_1x1'",
")",
"aux",
"=",
"slim",
".",
"conv2d",
"(",
"aux",
",",
"768",
",",
"aux",
".",
"get_shape",
"(",
")",
"[",
"1",
":",
"3",
"]",
",",
"padding",
"=",
"'VALID'",
",",
"scope",
"=",
"'Conv2d_2a_5x5'",
")",
"aux",
"=",
"slim",
".",
"flatten",
"(",
"aux",
")",
"aux",
"=",
"slim",
".",
"fully_connected",
"(",
"aux",
",",
"nb_classes",
",",
"activation_fn",
"=",
"None",
",",
"scope",
"=",
"'Logits'",
")",
"end_points",
"[",
"'AuxLogits'",
"]",
"=",
"aux",
"with",
"tf",
".",
"variable_scope",
"(",
"'Logits'",
")",
":",
"net",
"=",
"slim",
".",
"avg_pool2d",
"(",
"net",
",",
"net",
".",
"get_shape",
"(",
")",
"[",
"1",
":",
"3",
"]",
",",
"padding",
"=",
"'VALID'",
",",
"scope",
"=",
"'AvgPool_1a_8x8'",
")",
"net",
"=",
"slim",
".",
"flatten",
"(",
"net",
")",
"net",
"=",
"slim",
".",
"dropout",
"(",
"net",
",",
"dropout_keep_prob",
",",
"is_training",
"=",
"is_training",
",",
"scope",
"=",
"'Dropout'",
")",
"end_points",
"[",
"'PreLogitsFlatten'",
"]",
"=",
"net",
"logits",
"=",
"slim",
".",
"fully_connected",
"(",
"net",
",",
"nb_classes",
",",
"activation_fn",
"=",
"None",
",",
"scope",
"=",
"'Logits'",
")",
"end_points",
"[",
"'Logits'",
"]",
"=",
"logits",
"end_points",
"[",
"'Predictions'",
"]",
"=",
"tf",
".",
"nn",
".",
"softmax",
"(",
"logits",
",",
"name",
"=",
"'Predictions'",
")",
"return",
"logits",
",",
"end_points"
] | Creates the Inception Resnet V2 model.
Args:
inputs: a 4-D tensor of size [batch_size, height, width, 3].
nb_classes: number of predicted classes.
is_training: whether is training or not.
dropout_keep_prob: float, the fraction to keep before final layer.
reuse: whether or not the network and its variables should be reused. To be
able to reuse 'scope' must be given.
scope: Optional variable_scope.
create_aux_logits: Whether to include the auxilliary logits.
num_classes: depricated alias for nb_classes
Returns:
logits: the logits outputs of the model.
end_points: the set of end_points from the inception model. | [
"Creates",
"the",
"Inception",
"Resnet",
"V2",
"model",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py#L288-L352 |
28,607 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py | inception_resnet_v2_arg_scope | def inception_resnet_v2_arg_scope(weight_decay=0.00004,
batch_norm_decay=0.9997,
batch_norm_epsilon=0.001):
"""Returns the scope with the default parameters for inception_resnet_v2.
Args:
weight_decay: the weight decay for weights variables.
batch_norm_decay: decay for the moving average of batch_norm momentums.
batch_norm_epsilon: small float added to variance to avoid dividing by zero.
Returns:
a arg_scope with the parameters needed for inception_resnet_v2.
"""
# Set weight_decay for weights in conv2d and fully_connected layers.
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_regularizer=slim.l2_regularizer(weight_decay),
biases_regularizer=slim.l2_regularizer(weight_decay)):
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
}
# Set activation_fn and parameters for batch_norm.
with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params) as scope:
return scope | python | def inception_resnet_v2_arg_scope(weight_decay=0.00004,
batch_norm_decay=0.9997,
batch_norm_epsilon=0.001):
"""Returns the scope with the default parameters for inception_resnet_v2.
Args:
weight_decay: the weight decay for weights variables.
batch_norm_decay: decay for the moving average of batch_norm momentums.
batch_norm_epsilon: small float added to variance to avoid dividing by zero.
Returns:
a arg_scope with the parameters needed for inception_resnet_v2.
"""
# Set weight_decay for weights in conv2d and fully_connected layers.
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_regularizer=slim.l2_regularizer(weight_decay),
biases_regularizer=slim.l2_regularizer(weight_decay)):
batch_norm_params = {
'decay': batch_norm_decay,
'epsilon': batch_norm_epsilon,
}
# Set activation_fn and parameters for batch_norm.
with slim.arg_scope([slim.conv2d], activation_fn=tf.nn.relu,
normalizer_fn=slim.batch_norm,
normalizer_params=batch_norm_params) as scope:
return scope | [
"def",
"inception_resnet_v2_arg_scope",
"(",
"weight_decay",
"=",
"0.00004",
",",
"batch_norm_decay",
"=",
"0.9997",
",",
"batch_norm_epsilon",
"=",
"0.001",
")",
":",
"# Set weight_decay for weights in conv2d and fully_connected layers.",
"with",
"slim",
".",
"arg_scope",
"(",
"[",
"slim",
".",
"conv2d",
",",
"slim",
".",
"fully_connected",
"]",
",",
"weights_regularizer",
"=",
"slim",
".",
"l2_regularizer",
"(",
"weight_decay",
")",
",",
"biases_regularizer",
"=",
"slim",
".",
"l2_regularizer",
"(",
"weight_decay",
")",
")",
":",
"batch_norm_params",
"=",
"{",
"'decay'",
":",
"batch_norm_decay",
",",
"'epsilon'",
":",
"batch_norm_epsilon",
",",
"}",
"# Set activation_fn and parameters for batch_norm.",
"with",
"slim",
".",
"arg_scope",
"(",
"[",
"slim",
".",
"conv2d",
"]",
",",
"activation_fn",
"=",
"tf",
".",
"nn",
".",
"relu",
",",
"normalizer_fn",
"=",
"slim",
".",
"batch_norm",
",",
"normalizer_params",
"=",
"batch_norm_params",
")",
"as",
"scope",
":",
"return",
"scope"
] | Returns the scope with the default parameters for inception_resnet_v2.
Args:
weight_decay: the weight decay for weights variables.
batch_norm_decay: decay for the moving average of batch_norm momentums.
batch_norm_epsilon: small float added to variance to avoid dividing by zero.
Returns:
a arg_scope with the parameters needed for inception_resnet_v2. | [
"Returns",
"the",
"scope",
"with",
"the",
"default",
"parameters",
"for",
"inception_resnet_v2",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dev_toolkit/sample_defenses/ens_adv_inception_resnet_v2/inception_resnet_v2.py#L358-L384 |
28,608 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | main | def main(args):
"""Validate all submissions and copy them into place"""
random.seed()
temp_dir = tempfile.mkdtemp()
logging.info('Created temporary directory: %s', temp_dir)
validator = SubmissionValidator(
source_dir=args.source_dir,
target_dir=args.target_dir,
temp_dir=temp_dir,
do_copy=args.copy,
use_gpu=args.use_gpu,
containers_file=args.containers_file)
validator.run()
logging.info('Deleting temporary directory: %s', temp_dir)
subprocess.call(['rm', '-rf', temp_dir]) | python | def main(args):
"""Validate all submissions and copy them into place"""
random.seed()
temp_dir = tempfile.mkdtemp()
logging.info('Created temporary directory: %s', temp_dir)
validator = SubmissionValidator(
source_dir=args.source_dir,
target_dir=args.target_dir,
temp_dir=temp_dir,
do_copy=args.copy,
use_gpu=args.use_gpu,
containers_file=args.containers_file)
validator.run()
logging.info('Deleting temporary directory: %s', temp_dir)
subprocess.call(['rm', '-rf', temp_dir]) | [
"def",
"main",
"(",
"args",
")",
":",
"random",
".",
"seed",
"(",
")",
"temp_dir",
"=",
"tempfile",
".",
"mkdtemp",
"(",
")",
"logging",
".",
"info",
"(",
"'Created temporary directory: %s'",
",",
"temp_dir",
")",
"validator",
"=",
"SubmissionValidator",
"(",
"source_dir",
"=",
"args",
".",
"source_dir",
",",
"target_dir",
"=",
"args",
".",
"target_dir",
",",
"temp_dir",
"=",
"temp_dir",
",",
"do_copy",
"=",
"args",
".",
"copy",
",",
"use_gpu",
"=",
"args",
".",
"use_gpu",
",",
"containers_file",
"=",
"args",
".",
"containers_file",
")",
"validator",
".",
"run",
"(",
")",
"logging",
".",
"info",
"(",
"'Deleting temporary directory: %s'",
",",
"temp_dir",
")",
"subprocess",
".",
"call",
"(",
"[",
"'rm'",
",",
"'-rf'",
",",
"temp_dir",
"]",
")"
] | Validate all submissions and copy them into place | [
"Validate",
"all",
"submissions",
"and",
"copy",
"them",
"into",
"place"
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L229-L243 |
28,609 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | ValidationStats._update_stat | def _update_stat(self, submission_type, increase_success, increase_fail):
"""Common method to update submission statistics."""
stat = self.stats.get(submission_type, (0, 0))
stat = (stat[0] + increase_success, stat[1] + increase_fail)
self.stats[submission_type] = stat | python | def _update_stat(self, submission_type, increase_success, increase_fail):
"""Common method to update submission statistics."""
stat = self.stats.get(submission_type, (0, 0))
stat = (stat[0] + increase_success, stat[1] + increase_fail)
self.stats[submission_type] = stat | [
"def",
"_update_stat",
"(",
"self",
",",
"submission_type",
",",
"increase_success",
",",
"increase_fail",
")",
":",
"stat",
"=",
"self",
".",
"stats",
".",
"get",
"(",
"submission_type",
",",
"(",
"0",
",",
"0",
")",
")",
"stat",
"=",
"(",
"stat",
"[",
"0",
"]",
"+",
"increase_success",
",",
"stat",
"[",
"1",
"]",
"+",
"increase_fail",
")",
"self",
".",
"stats",
"[",
"submission_type",
"]",
"=",
"stat"
] | Common method to update submission statistics. | [
"Common",
"method",
"to",
"update",
"submission",
"statistics",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L64-L68 |
28,610 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | ValidationStats.log_stats | def log_stats(self):
"""Print statistics into log."""
logging.info('Validation statistics: ')
for k, v in iteritems(self.stats):
logging.info('%s - %d valid out of %d total submissions',
k, v[0], v[0] + v[1]) | python | def log_stats(self):
"""Print statistics into log."""
logging.info('Validation statistics: ')
for k, v in iteritems(self.stats):
logging.info('%s - %d valid out of %d total submissions',
k, v[0], v[0] + v[1]) | [
"def",
"log_stats",
"(",
"self",
")",
":",
"logging",
".",
"info",
"(",
"'Validation statistics: '",
")",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"self",
".",
"stats",
")",
":",
"logging",
".",
"info",
"(",
"'%s - %d valid out of %d total submissions'",
",",
"k",
",",
"v",
"[",
"0",
"]",
",",
"v",
"[",
"0",
"]",
"+",
"v",
"[",
"1",
"]",
")"
] | Print statistics into log. | [
"Print",
"statistics",
"into",
"log",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L78-L83 |
28,611 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | SubmissionValidator.copy_submission_locally | def copy_submission_locally(self, cloud_path):
"""Copies submission from Google Cloud Storage to local directory.
Args:
cloud_path: path of the submission in Google Cloud Storage
Returns:
name of the local file where submission is copied to
"""
local_path = os.path.join(self.download_dir, os.path.basename(cloud_path))
cmd = ['gsutil', 'cp', cloud_path, local_path]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy submission locally')
return None
return local_path | python | def copy_submission_locally(self, cloud_path):
"""Copies submission from Google Cloud Storage to local directory.
Args:
cloud_path: path of the submission in Google Cloud Storage
Returns:
name of the local file where submission is copied to
"""
local_path = os.path.join(self.download_dir, os.path.basename(cloud_path))
cmd = ['gsutil', 'cp', cloud_path, local_path]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy submission locally')
return None
return local_path | [
"def",
"copy_submission_locally",
"(",
"self",
",",
"cloud_path",
")",
":",
"local_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"download_dir",
",",
"os",
".",
"path",
".",
"basename",
"(",
"cloud_path",
")",
")",
"cmd",
"=",
"[",
"'gsutil'",
",",
"'cp'",
",",
"cloud_path",
",",
"local_path",
"]",
"if",
"subprocess",
".",
"call",
"(",
"cmd",
")",
"!=",
"0",
":",
"logging",
".",
"error",
"(",
"'Can\\'t copy submission locally'",
")",
"return",
"None",
"return",
"local_path"
] | Copies submission from Google Cloud Storage to local directory.
Args:
cloud_path: path of the submission in Google Cloud Storage
Returns:
name of the local file where submission is copied to | [
"Copies",
"submission",
"from",
"Google",
"Cloud",
"Storage",
"to",
"local",
"directory",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L119-L133 |
28,612 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | SubmissionValidator.copy_submission_to_destination | def copy_submission_to_destination(self, src_filename, dst_subdir,
submission_id):
"""Copies submission to target directory.
Args:
src_filename: source filename of the submission
dst_subdir: subdirectory of the target directory where submission should
be copied to
submission_id: ID of the submission, will be used as a new
submission filename (before extension)
"""
extension = [e for e in ALLOWED_EXTENSIONS if src_filename.endswith(e)]
if len(extension) != 1:
logging.error('Invalid submission extension: %s', src_filename)
return
dst_filename = os.path.join(self.target_dir, dst_subdir,
submission_id + extension[0])
cmd = ['gsutil', 'cp', src_filename, dst_filename]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy submission to destination')
else:
logging.info('Submission copied to: %s', dst_filename) | python | def copy_submission_to_destination(self, src_filename, dst_subdir,
submission_id):
"""Copies submission to target directory.
Args:
src_filename: source filename of the submission
dst_subdir: subdirectory of the target directory where submission should
be copied to
submission_id: ID of the submission, will be used as a new
submission filename (before extension)
"""
extension = [e for e in ALLOWED_EXTENSIONS if src_filename.endswith(e)]
if len(extension) != 1:
logging.error('Invalid submission extension: %s', src_filename)
return
dst_filename = os.path.join(self.target_dir, dst_subdir,
submission_id + extension[0])
cmd = ['gsutil', 'cp', src_filename, dst_filename]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy submission to destination')
else:
logging.info('Submission copied to: %s', dst_filename) | [
"def",
"copy_submission_to_destination",
"(",
"self",
",",
"src_filename",
",",
"dst_subdir",
",",
"submission_id",
")",
":",
"extension",
"=",
"[",
"e",
"for",
"e",
"in",
"ALLOWED_EXTENSIONS",
"if",
"src_filename",
".",
"endswith",
"(",
"e",
")",
"]",
"if",
"len",
"(",
"extension",
")",
"!=",
"1",
":",
"logging",
".",
"error",
"(",
"'Invalid submission extension: %s'",
",",
"src_filename",
")",
"return",
"dst_filename",
"=",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"target_dir",
",",
"dst_subdir",
",",
"submission_id",
"+",
"extension",
"[",
"0",
"]",
")",
"cmd",
"=",
"[",
"'gsutil'",
",",
"'cp'",
",",
"src_filename",
",",
"dst_filename",
"]",
"if",
"subprocess",
".",
"call",
"(",
"cmd",
")",
"!=",
"0",
":",
"logging",
".",
"error",
"(",
"'Can\\'t copy submission to destination'",
")",
"else",
":",
"logging",
".",
"info",
"(",
"'Submission copied to: %s'",
",",
"dst_filename",
")"
] | Copies submission to target directory.
Args:
src_filename: source filename of the submission
dst_subdir: subdirectory of the target directory where submission should
be copied to
submission_id: ID of the submission, will be used as a new
submission filename (before extension) | [
"Copies",
"submission",
"to",
"target",
"directory",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L135-L157 |
28,613 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | SubmissionValidator.validate_and_copy_one_submission | def validate_and_copy_one_submission(self, submission_path):
"""Validates one submission and copies it to target directory.
Args:
submission_path: path in Google Cloud Storage of the submission file
"""
if os.path.exists(self.download_dir):
shutil.rmtree(self.download_dir)
os.makedirs(self.download_dir)
if os.path.exists(self.validate_dir):
shutil.rmtree(self.validate_dir)
os.makedirs(self.validate_dir)
logging.info('\n' + ('#' * 80) + '\n# Processing submission: %s\n'
+ '#' * 80, submission_path)
local_path = self.copy_submission_locally(submission_path)
metadata = self.base_validator.validate_submission(local_path)
if not metadata:
logging.error('Submission "%s" is INVALID', submission_path)
self.stats.add_failure()
return
submission_type = metadata['type']
container_name = metadata['container_gpu']
logging.info('Submission "%s" is VALID', submission_path)
self.list_of_containers.add(container_name)
self.stats.add_success(submission_type)
if self.do_copy:
submission_id = '{0:04}'.format(self.cur_submission_idx)
self.cur_submission_idx += 1
self.copy_submission_to_destination(submission_path,
TYPE_TO_DIR[submission_type],
submission_id)
self.id_to_path_mapping[submission_id] = submission_path | python | def validate_and_copy_one_submission(self, submission_path):
"""Validates one submission and copies it to target directory.
Args:
submission_path: path in Google Cloud Storage of the submission file
"""
if os.path.exists(self.download_dir):
shutil.rmtree(self.download_dir)
os.makedirs(self.download_dir)
if os.path.exists(self.validate_dir):
shutil.rmtree(self.validate_dir)
os.makedirs(self.validate_dir)
logging.info('\n' + ('#' * 80) + '\n# Processing submission: %s\n'
+ '#' * 80, submission_path)
local_path = self.copy_submission_locally(submission_path)
metadata = self.base_validator.validate_submission(local_path)
if not metadata:
logging.error('Submission "%s" is INVALID', submission_path)
self.stats.add_failure()
return
submission_type = metadata['type']
container_name = metadata['container_gpu']
logging.info('Submission "%s" is VALID', submission_path)
self.list_of_containers.add(container_name)
self.stats.add_success(submission_type)
if self.do_copy:
submission_id = '{0:04}'.format(self.cur_submission_idx)
self.cur_submission_idx += 1
self.copy_submission_to_destination(submission_path,
TYPE_TO_DIR[submission_type],
submission_id)
self.id_to_path_mapping[submission_id] = submission_path | [
"def",
"validate_and_copy_one_submission",
"(",
"self",
",",
"submission_path",
")",
":",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"self",
".",
"download_dir",
")",
":",
"shutil",
".",
"rmtree",
"(",
"self",
".",
"download_dir",
")",
"os",
".",
"makedirs",
"(",
"self",
".",
"download_dir",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"self",
".",
"validate_dir",
")",
":",
"shutil",
".",
"rmtree",
"(",
"self",
".",
"validate_dir",
")",
"os",
".",
"makedirs",
"(",
"self",
".",
"validate_dir",
")",
"logging",
".",
"info",
"(",
"'\\n'",
"+",
"(",
"'#'",
"*",
"80",
")",
"+",
"'\\n# Processing submission: %s\\n'",
"+",
"'#'",
"*",
"80",
",",
"submission_path",
")",
"local_path",
"=",
"self",
".",
"copy_submission_locally",
"(",
"submission_path",
")",
"metadata",
"=",
"self",
".",
"base_validator",
".",
"validate_submission",
"(",
"local_path",
")",
"if",
"not",
"metadata",
":",
"logging",
".",
"error",
"(",
"'Submission \"%s\" is INVALID'",
",",
"submission_path",
")",
"self",
".",
"stats",
".",
"add_failure",
"(",
")",
"return",
"submission_type",
"=",
"metadata",
"[",
"'type'",
"]",
"container_name",
"=",
"metadata",
"[",
"'container_gpu'",
"]",
"logging",
".",
"info",
"(",
"'Submission \"%s\" is VALID'",
",",
"submission_path",
")",
"self",
".",
"list_of_containers",
".",
"add",
"(",
"container_name",
")",
"self",
".",
"stats",
".",
"add_success",
"(",
"submission_type",
")",
"if",
"self",
".",
"do_copy",
":",
"submission_id",
"=",
"'{0:04}'",
".",
"format",
"(",
"self",
".",
"cur_submission_idx",
")",
"self",
".",
"cur_submission_idx",
"+=",
"1",
"self",
".",
"copy_submission_to_destination",
"(",
"submission_path",
",",
"TYPE_TO_DIR",
"[",
"submission_type",
"]",
",",
"submission_id",
")",
"self",
".",
"id_to_path_mapping",
"[",
"submission_id",
"]",
"=",
"submission_path"
] | Validates one submission and copies it to target directory.
Args:
submission_path: path in Google Cloud Storage of the submission file | [
"Validates",
"one",
"submission",
"and",
"copies",
"it",
"to",
"target",
"directory",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L159-L190 |
28,614 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | SubmissionValidator.save_id_to_path_mapping | def save_id_to_path_mapping(self):
"""Saves mapping from submission IDs to original filenames.
This mapping is saved as CSV file into target directory.
"""
if not self.id_to_path_mapping:
return
with open(self.local_id_to_path_mapping_file, 'w') as f:
writer = csv.writer(f)
writer.writerow(['id', 'path'])
for k, v in sorted(iteritems(self.id_to_path_mapping)):
writer.writerow([k, v])
cmd = ['gsutil', 'cp', self.local_id_to_path_mapping_file,
os.path.join(self.target_dir, 'id_to_path_mapping.csv')]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy id_to_path_mapping.csv to target directory') | python | def save_id_to_path_mapping(self):
"""Saves mapping from submission IDs to original filenames.
This mapping is saved as CSV file into target directory.
"""
if not self.id_to_path_mapping:
return
with open(self.local_id_to_path_mapping_file, 'w') as f:
writer = csv.writer(f)
writer.writerow(['id', 'path'])
for k, v in sorted(iteritems(self.id_to_path_mapping)):
writer.writerow([k, v])
cmd = ['gsutil', 'cp', self.local_id_to_path_mapping_file,
os.path.join(self.target_dir, 'id_to_path_mapping.csv')]
if subprocess.call(cmd) != 0:
logging.error('Can\'t copy id_to_path_mapping.csv to target directory') | [
"def",
"save_id_to_path_mapping",
"(",
"self",
")",
":",
"if",
"not",
"self",
".",
"id_to_path_mapping",
":",
"return",
"with",
"open",
"(",
"self",
".",
"local_id_to_path_mapping_file",
",",
"'w'",
")",
"as",
"f",
":",
"writer",
"=",
"csv",
".",
"writer",
"(",
"f",
")",
"writer",
".",
"writerow",
"(",
"[",
"'id'",
",",
"'path'",
"]",
")",
"for",
"k",
",",
"v",
"in",
"sorted",
"(",
"iteritems",
"(",
"self",
".",
"id_to_path_mapping",
")",
")",
":",
"writer",
".",
"writerow",
"(",
"[",
"k",
",",
"v",
"]",
")",
"cmd",
"=",
"[",
"'gsutil'",
",",
"'cp'",
",",
"self",
".",
"local_id_to_path_mapping_file",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"target_dir",
",",
"'id_to_path_mapping.csv'",
")",
"]",
"if",
"subprocess",
".",
"call",
"(",
"cmd",
")",
"!=",
"0",
":",
"logging",
".",
"error",
"(",
"'Can\\'t copy id_to_path_mapping.csv to target directory'",
")"
] | Saves mapping from submission IDs to original filenames.
This mapping is saved as CSV file into target directory. | [
"Saves",
"mapping",
"from",
"submission",
"IDs",
"to",
"original",
"filenames",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L192-L207 |
28,615 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py | SubmissionValidator.run | def run(self):
"""Runs validation of all submissions."""
cmd = ['gsutil', 'ls', os.path.join(self.source_dir, '**')]
try:
files_list = subprocess.check_output(cmd).split('\n')
except subprocess.CalledProcessError:
logging.error('Can''t read source directory')
all_submissions = [
s for s in files_list
if s.endswith('.zip') or s.endswith('.tar') or s.endswith('.tar.gz')
]
for submission_path in all_submissions:
self.validate_and_copy_one_submission(submission_path)
self.stats.log_stats()
self.save_id_to_path_mapping()
if self.containers_file:
with open(self.containers_file, 'w') as f:
f.write('\n'.join(sorted(self.list_of_containers))) | python | def run(self):
"""Runs validation of all submissions."""
cmd = ['gsutil', 'ls', os.path.join(self.source_dir, '**')]
try:
files_list = subprocess.check_output(cmd).split('\n')
except subprocess.CalledProcessError:
logging.error('Can''t read source directory')
all_submissions = [
s for s in files_list
if s.endswith('.zip') or s.endswith('.tar') or s.endswith('.tar.gz')
]
for submission_path in all_submissions:
self.validate_and_copy_one_submission(submission_path)
self.stats.log_stats()
self.save_id_to_path_mapping()
if self.containers_file:
with open(self.containers_file, 'w') as f:
f.write('\n'.join(sorted(self.list_of_containers))) | [
"def",
"run",
"(",
"self",
")",
":",
"cmd",
"=",
"[",
"'gsutil'",
",",
"'ls'",
",",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"source_dir",
",",
"'**'",
")",
"]",
"try",
":",
"files_list",
"=",
"subprocess",
".",
"check_output",
"(",
"cmd",
")",
".",
"split",
"(",
"'\\n'",
")",
"except",
"subprocess",
".",
"CalledProcessError",
":",
"logging",
".",
"error",
"(",
"'Can'",
"'t read source directory'",
")",
"all_submissions",
"=",
"[",
"s",
"for",
"s",
"in",
"files_list",
"if",
"s",
".",
"endswith",
"(",
"'.zip'",
")",
"or",
"s",
".",
"endswith",
"(",
"'.tar'",
")",
"or",
"s",
".",
"endswith",
"(",
"'.tar.gz'",
")",
"]",
"for",
"submission_path",
"in",
"all_submissions",
":",
"self",
".",
"validate_and_copy_one_submission",
"(",
"submission_path",
")",
"self",
".",
"stats",
".",
"log_stats",
"(",
")",
"self",
".",
"save_id_to_path_mapping",
"(",
")",
"if",
"self",
".",
"containers_file",
":",
"with",
"open",
"(",
"self",
".",
"containers_file",
",",
"'w'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"'\\n'",
".",
"join",
"(",
"sorted",
"(",
"self",
".",
"list_of_containers",
")",
")",
")"
] | Runs validation of all submissions. | [
"Runs",
"validation",
"of",
"all",
"submissions",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/validation_tool/validate_and_copy_submissions.py#L209-L226 |
28,616 | tensorflow/cleverhans | scripts/plot_success_fail_curve.py | main | def main(argv=None):
"""Takes the path to a directory with reports and renders success fail plots."""
report_paths = argv[1:]
fail_names = FLAGS.fail_names.split(',')
for report_path in report_paths:
plot_report_from_path(report_path, label=report_path, fail_names=fail_names)
pyplot.legend()
pyplot.xlim(-.01, 1.)
pyplot.ylim(0., 1.)
pyplot.show() | python | def main(argv=None):
"""Takes the path to a directory with reports and renders success fail plots."""
report_paths = argv[1:]
fail_names = FLAGS.fail_names.split(',')
for report_path in report_paths:
plot_report_from_path(report_path, label=report_path, fail_names=fail_names)
pyplot.legend()
pyplot.xlim(-.01, 1.)
pyplot.ylim(0., 1.)
pyplot.show() | [
"def",
"main",
"(",
"argv",
"=",
"None",
")",
":",
"report_paths",
"=",
"argv",
"[",
"1",
":",
"]",
"fail_names",
"=",
"FLAGS",
".",
"fail_names",
".",
"split",
"(",
"','",
")",
"for",
"report_path",
"in",
"report_paths",
":",
"plot_report_from_path",
"(",
"report_path",
",",
"label",
"=",
"report_path",
",",
"fail_names",
"=",
"fail_names",
")",
"pyplot",
".",
"legend",
"(",
")",
"pyplot",
".",
"xlim",
"(",
"-",
".01",
",",
"1.",
")",
"pyplot",
".",
"ylim",
"(",
"0.",
",",
"1.",
")",
"pyplot",
".",
"show",
"(",
")"
] | Takes the path to a directory with reports and renders success fail plots. | [
"Takes",
"the",
"path",
"to",
"a",
"directory",
"with",
"reports",
"and",
"renders",
"success",
"fail",
"plots",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/scripts/plot_success_fail_curve.py#L25-L38 |
28,617 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | is_unclaimed | def is_unclaimed(work):
"""Returns True if work piece is unclaimed."""
if work['is_completed']:
return False
cutoff_time = time.time() - MAX_PROCESSING_TIME
if (work['claimed_worker_id'] and
work['claimed_worker_start_time'] is not None
and work['claimed_worker_start_time'] >= cutoff_time):
return False
return True | python | def is_unclaimed(work):
"""Returns True if work piece is unclaimed."""
if work['is_completed']:
return False
cutoff_time = time.time() - MAX_PROCESSING_TIME
if (work['claimed_worker_id'] and
work['claimed_worker_start_time'] is not None
and work['claimed_worker_start_time'] >= cutoff_time):
return False
return True | [
"def",
"is_unclaimed",
"(",
"work",
")",
":",
"if",
"work",
"[",
"'is_completed'",
"]",
":",
"return",
"False",
"cutoff_time",
"=",
"time",
".",
"time",
"(",
")",
"-",
"MAX_PROCESSING_TIME",
"if",
"(",
"work",
"[",
"'claimed_worker_id'",
"]",
"and",
"work",
"[",
"'claimed_worker_start_time'",
"]",
"is",
"not",
"None",
"and",
"work",
"[",
"'claimed_worker_start_time'",
"]",
">=",
"cutoff_time",
")",
":",
"return",
"False",
"return",
"True"
] | Returns True if work piece is unclaimed. | [
"Returns",
"True",
"if",
"work",
"piece",
"is",
"unclaimed",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L46-L55 |
28,618 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.write_all_to_datastore | def write_all_to_datastore(self):
"""Writes all work pieces into datastore.
Each work piece is identified by ID. This method writes/updates only those
work pieces which IDs are stored in this class. For examples, if this class
has only work pieces with IDs '1' ... '100' and datastore already contains
work pieces with IDs '50' ... '200' then this method will create new
work pieces with IDs '1' ... '49', update work pieces with IDs
'50' ... '100' and keep unchanged work pieces with IDs '101' ... '200'.
"""
client = self._datastore_client
with client.no_transact_batch() as batch:
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
batch.put(client.entity(parent_key))
for work_id, work_val in iteritems(self._work):
entity = client.entity(client.key(KIND_WORK, work_id,
parent=parent_key))
entity.update(work_val)
batch.put(entity) | python | def write_all_to_datastore(self):
"""Writes all work pieces into datastore.
Each work piece is identified by ID. This method writes/updates only those
work pieces which IDs are stored in this class. For examples, if this class
has only work pieces with IDs '1' ... '100' and datastore already contains
work pieces with IDs '50' ... '200' then this method will create new
work pieces with IDs '1' ... '49', update work pieces with IDs
'50' ... '100' and keep unchanged work pieces with IDs '101' ... '200'.
"""
client = self._datastore_client
with client.no_transact_batch() as batch:
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
batch.put(client.entity(parent_key))
for work_id, work_val in iteritems(self._work):
entity = client.entity(client.key(KIND_WORK, work_id,
parent=parent_key))
entity.update(work_val)
batch.put(entity) | [
"def",
"write_all_to_datastore",
"(",
"self",
")",
":",
"client",
"=",
"self",
".",
"_datastore_client",
"with",
"client",
".",
"no_transact_batch",
"(",
")",
"as",
"batch",
":",
"parent_key",
"=",
"client",
".",
"key",
"(",
"KIND_WORK_TYPE",
",",
"self",
".",
"_work_type_entity_id",
")",
"batch",
".",
"put",
"(",
"client",
".",
"entity",
"(",
"parent_key",
")",
")",
"for",
"work_id",
",",
"work_val",
"in",
"iteritems",
"(",
"self",
".",
"_work",
")",
":",
"entity",
"=",
"client",
".",
"entity",
"(",
"client",
".",
"key",
"(",
"KIND_WORK",
",",
"work_id",
",",
"parent",
"=",
"parent_key",
")",
")",
"entity",
".",
"update",
"(",
"work_val",
")",
"batch",
".",
"put",
"(",
"entity",
")"
] | Writes all work pieces into datastore.
Each work piece is identified by ID. This method writes/updates only those
work pieces which IDs are stored in this class. For examples, if this class
has only work pieces with IDs '1' ... '100' and datastore already contains
work pieces with IDs '50' ... '200' then this method will create new
work pieces with IDs '1' ... '49', update work pieces with IDs
'50' ... '100' and keep unchanged work pieces with IDs '101' ... '200'. | [
"Writes",
"all",
"work",
"pieces",
"into",
"datastore",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L150-L168 |
28,619 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.read_all_from_datastore | def read_all_from_datastore(self):
"""Reads all work pieces from the datastore."""
self._work = {}
client = self._datastore_client
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
for entity in client.query_fetch(kind=KIND_WORK, ancestor=parent_key):
work_id = entity.key.flat_path[-1]
self.work[work_id] = dict(entity) | python | def read_all_from_datastore(self):
"""Reads all work pieces from the datastore."""
self._work = {}
client = self._datastore_client
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
for entity in client.query_fetch(kind=KIND_WORK, ancestor=parent_key):
work_id = entity.key.flat_path[-1]
self.work[work_id] = dict(entity) | [
"def",
"read_all_from_datastore",
"(",
"self",
")",
":",
"self",
".",
"_work",
"=",
"{",
"}",
"client",
"=",
"self",
".",
"_datastore_client",
"parent_key",
"=",
"client",
".",
"key",
"(",
"KIND_WORK_TYPE",
",",
"self",
".",
"_work_type_entity_id",
")",
"for",
"entity",
"in",
"client",
".",
"query_fetch",
"(",
"kind",
"=",
"KIND_WORK",
",",
"ancestor",
"=",
"parent_key",
")",
":",
"work_id",
"=",
"entity",
".",
"key",
".",
"flat_path",
"[",
"-",
"1",
"]",
"self",
".",
"work",
"[",
"work_id",
"]",
"=",
"dict",
"(",
"entity",
")"
] | Reads all work pieces from the datastore. | [
"Reads",
"all",
"work",
"pieces",
"from",
"the",
"datastore",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L170-L177 |
28,620 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase._read_undone_shard_from_datastore | def _read_undone_shard_from_datastore(self, shard_id=None):
"""Reads undone worke pieces which are assigned to shard with given id."""
self._work = {}
client = self._datastore_client
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
filters = [('is_completed', '=', False)]
if shard_id is not None:
filters.append(('shard_id', '=', shard_id))
for entity in client.query_fetch(kind=KIND_WORK, ancestor=parent_key,
filters=filters):
work_id = entity.key.flat_path[-1]
self.work[work_id] = dict(entity)
if len(self._work) >= MAX_WORK_RECORDS_READ:
break | python | def _read_undone_shard_from_datastore(self, shard_id=None):
"""Reads undone worke pieces which are assigned to shard with given id."""
self._work = {}
client = self._datastore_client
parent_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id)
filters = [('is_completed', '=', False)]
if shard_id is not None:
filters.append(('shard_id', '=', shard_id))
for entity in client.query_fetch(kind=KIND_WORK, ancestor=parent_key,
filters=filters):
work_id = entity.key.flat_path[-1]
self.work[work_id] = dict(entity)
if len(self._work) >= MAX_WORK_RECORDS_READ:
break | [
"def",
"_read_undone_shard_from_datastore",
"(",
"self",
",",
"shard_id",
"=",
"None",
")",
":",
"self",
".",
"_work",
"=",
"{",
"}",
"client",
"=",
"self",
".",
"_datastore_client",
"parent_key",
"=",
"client",
".",
"key",
"(",
"KIND_WORK_TYPE",
",",
"self",
".",
"_work_type_entity_id",
")",
"filters",
"=",
"[",
"(",
"'is_completed'",
",",
"'='",
",",
"False",
")",
"]",
"if",
"shard_id",
"is",
"not",
"None",
":",
"filters",
".",
"append",
"(",
"(",
"'shard_id'",
",",
"'='",
",",
"shard_id",
")",
")",
"for",
"entity",
"in",
"client",
".",
"query_fetch",
"(",
"kind",
"=",
"KIND_WORK",
",",
"ancestor",
"=",
"parent_key",
",",
"filters",
"=",
"filters",
")",
":",
"work_id",
"=",
"entity",
".",
"key",
".",
"flat_path",
"[",
"-",
"1",
"]",
"self",
".",
"work",
"[",
"work_id",
"]",
"=",
"dict",
"(",
"entity",
")",
"if",
"len",
"(",
"self",
".",
"_work",
")",
">=",
"MAX_WORK_RECORDS_READ",
":",
"break"
] | Reads undone worke pieces which are assigned to shard with given id. | [
"Reads",
"undone",
"worke",
"pieces",
"which",
"are",
"assigned",
"to",
"shard",
"with",
"given",
"id",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L179-L192 |
28,621 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.read_undone_from_datastore | def read_undone_from_datastore(self, shard_id=None, num_shards=None):
"""Reads undone work from the datastore.
If shard_id and num_shards are specified then this method will attempt
to read undone work for shard with id shard_id. If no undone work was found
then it will try to read shard (shard_id+1) and so on until either found
shard with undone work or all shards are read.
Args:
shard_id: Id of the start shard
num_shards: total number of shards
Returns:
id of the shard with undone work which was read. None means that work
from all datastore was read.
"""
if shard_id is not None:
shards_list = [(i + shard_id) % num_shards for i in range(num_shards)]
else:
shards_list = []
shards_list.append(None)
for shard in shards_list:
self._read_undone_shard_from_datastore(shard)
if self._work:
return shard
return None | python | def read_undone_from_datastore(self, shard_id=None, num_shards=None):
"""Reads undone work from the datastore.
If shard_id and num_shards are specified then this method will attempt
to read undone work for shard with id shard_id. If no undone work was found
then it will try to read shard (shard_id+1) and so on until either found
shard with undone work or all shards are read.
Args:
shard_id: Id of the start shard
num_shards: total number of shards
Returns:
id of the shard with undone work which was read. None means that work
from all datastore was read.
"""
if shard_id is not None:
shards_list = [(i + shard_id) % num_shards for i in range(num_shards)]
else:
shards_list = []
shards_list.append(None)
for shard in shards_list:
self._read_undone_shard_from_datastore(shard)
if self._work:
return shard
return None | [
"def",
"read_undone_from_datastore",
"(",
"self",
",",
"shard_id",
"=",
"None",
",",
"num_shards",
"=",
"None",
")",
":",
"if",
"shard_id",
"is",
"not",
"None",
":",
"shards_list",
"=",
"[",
"(",
"i",
"+",
"shard_id",
")",
"%",
"num_shards",
"for",
"i",
"in",
"range",
"(",
"num_shards",
")",
"]",
"else",
":",
"shards_list",
"=",
"[",
"]",
"shards_list",
".",
"append",
"(",
"None",
")",
"for",
"shard",
"in",
"shards_list",
":",
"self",
".",
"_read_undone_shard_from_datastore",
"(",
"shard",
")",
"if",
"self",
".",
"_work",
":",
"return",
"shard",
"return",
"None"
] | Reads undone work from the datastore.
If shard_id and num_shards are specified then this method will attempt
to read undone work for shard with id shard_id. If no undone work was found
then it will try to read shard (shard_id+1) and so on until either found
shard with undone work or all shards are read.
Args:
shard_id: Id of the start shard
num_shards: total number of shards
Returns:
id of the shard with undone work which was read. None means that work
from all datastore was read. | [
"Reads",
"undone",
"work",
"from",
"the",
"datastore",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L194-L219 |
28,622 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.try_pick_piece_of_work | def try_pick_piece_of_work(self, worker_id, submission_id=None):
"""Tries pick next unclaimed piece of work to do.
Attempt to claim work piece is done using Cloud Datastore transaction, so
only one worker can claim any work piece at a time.
Args:
worker_id: ID of current worker
submission_id: if not None then this method will try to pick
piece of work for this submission
Returns:
ID of the claimed work piece
"""
client = self._datastore_client
unclaimed_work_ids = None
if submission_id:
unclaimed_work_ids = [
k for k, v in iteritems(self.work)
if is_unclaimed(v) and (v['submission_id'] == submission_id)
]
if not unclaimed_work_ids:
unclaimed_work_ids = [k for k, v in iteritems(self.work)
if is_unclaimed(v)]
if unclaimed_work_ids:
next_work_id = random.choice(unclaimed_work_ids)
else:
return None
try:
with client.transaction() as transaction:
work_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id,
KIND_WORK, next_work_id)
work_entity = client.get(work_key, transaction=transaction)
if not is_unclaimed(work_entity):
return None
work_entity['claimed_worker_id'] = worker_id
work_entity['claimed_worker_start_time'] = get_integer_time()
transaction.put(work_entity)
except Exception:
return None
return next_work_id | python | def try_pick_piece_of_work(self, worker_id, submission_id=None):
"""Tries pick next unclaimed piece of work to do.
Attempt to claim work piece is done using Cloud Datastore transaction, so
only one worker can claim any work piece at a time.
Args:
worker_id: ID of current worker
submission_id: if not None then this method will try to pick
piece of work for this submission
Returns:
ID of the claimed work piece
"""
client = self._datastore_client
unclaimed_work_ids = None
if submission_id:
unclaimed_work_ids = [
k for k, v in iteritems(self.work)
if is_unclaimed(v) and (v['submission_id'] == submission_id)
]
if not unclaimed_work_ids:
unclaimed_work_ids = [k for k, v in iteritems(self.work)
if is_unclaimed(v)]
if unclaimed_work_ids:
next_work_id = random.choice(unclaimed_work_ids)
else:
return None
try:
with client.transaction() as transaction:
work_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id,
KIND_WORK, next_work_id)
work_entity = client.get(work_key, transaction=transaction)
if not is_unclaimed(work_entity):
return None
work_entity['claimed_worker_id'] = worker_id
work_entity['claimed_worker_start_time'] = get_integer_time()
transaction.put(work_entity)
except Exception:
return None
return next_work_id | [
"def",
"try_pick_piece_of_work",
"(",
"self",
",",
"worker_id",
",",
"submission_id",
"=",
"None",
")",
":",
"client",
"=",
"self",
".",
"_datastore_client",
"unclaimed_work_ids",
"=",
"None",
"if",
"submission_id",
":",
"unclaimed_work_ids",
"=",
"[",
"k",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"self",
".",
"work",
")",
"if",
"is_unclaimed",
"(",
"v",
")",
"and",
"(",
"v",
"[",
"'submission_id'",
"]",
"==",
"submission_id",
")",
"]",
"if",
"not",
"unclaimed_work_ids",
":",
"unclaimed_work_ids",
"=",
"[",
"k",
"for",
"k",
",",
"v",
"in",
"iteritems",
"(",
"self",
".",
"work",
")",
"if",
"is_unclaimed",
"(",
"v",
")",
"]",
"if",
"unclaimed_work_ids",
":",
"next_work_id",
"=",
"random",
".",
"choice",
"(",
"unclaimed_work_ids",
")",
"else",
":",
"return",
"None",
"try",
":",
"with",
"client",
".",
"transaction",
"(",
")",
"as",
"transaction",
":",
"work_key",
"=",
"client",
".",
"key",
"(",
"KIND_WORK_TYPE",
",",
"self",
".",
"_work_type_entity_id",
",",
"KIND_WORK",
",",
"next_work_id",
")",
"work_entity",
"=",
"client",
".",
"get",
"(",
"work_key",
",",
"transaction",
"=",
"transaction",
")",
"if",
"not",
"is_unclaimed",
"(",
"work_entity",
")",
":",
"return",
"None",
"work_entity",
"[",
"'claimed_worker_id'",
"]",
"=",
"worker_id",
"work_entity",
"[",
"'claimed_worker_start_time'",
"]",
"=",
"get_integer_time",
"(",
")",
"transaction",
".",
"put",
"(",
"work_entity",
")",
"except",
"Exception",
":",
"return",
"None",
"return",
"next_work_id"
] | Tries pick next unclaimed piece of work to do.
Attempt to claim work piece is done using Cloud Datastore transaction, so
only one worker can claim any work piece at a time.
Args:
worker_id: ID of current worker
submission_id: if not None then this method will try to pick
piece of work for this submission
Returns:
ID of the claimed work piece | [
"Tries",
"pick",
"next",
"unclaimed",
"piece",
"of",
"work",
"to",
"do",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L221-L261 |
28,623 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.update_work_as_completed | def update_work_as_completed(self, worker_id, work_id, other_values=None,
error=None):
"""Updates work piece in datastore as completed.
Args:
worker_id: ID of the worker which did the work
work_id: ID of the work which was done
other_values: dictionary with additonal values which should be saved
with the work piece
error: if not None then error occurred during computation of the work
piece. In such case work will be marked as completed with error.
Returns:
whether work was successfully updated
"""
client = self._datastore_client
try:
with client.transaction() as transaction:
work_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id,
KIND_WORK, work_id)
work_entity = client.get(work_key, transaction=transaction)
if work_entity['claimed_worker_id'] != worker_id:
return False
work_entity['is_completed'] = True
if other_values:
work_entity.update(other_values)
if error:
work_entity['error'] = text_type(error)
transaction.put(work_entity)
except Exception:
return False
return True | python | def update_work_as_completed(self, worker_id, work_id, other_values=None,
error=None):
"""Updates work piece in datastore as completed.
Args:
worker_id: ID of the worker which did the work
work_id: ID of the work which was done
other_values: dictionary with additonal values which should be saved
with the work piece
error: if not None then error occurred during computation of the work
piece. In such case work will be marked as completed with error.
Returns:
whether work was successfully updated
"""
client = self._datastore_client
try:
with client.transaction() as transaction:
work_key = client.key(KIND_WORK_TYPE, self._work_type_entity_id,
KIND_WORK, work_id)
work_entity = client.get(work_key, transaction=transaction)
if work_entity['claimed_worker_id'] != worker_id:
return False
work_entity['is_completed'] = True
if other_values:
work_entity.update(other_values)
if error:
work_entity['error'] = text_type(error)
transaction.put(work_entity)
except Exception:
return False
return True | [
"def",
"update_work_as_completed",
"(",
"self",
",",
"worker_id",
",",
"work_id",
",",
"other_values",
"=",
"None",
",",
"error",
"=",
"None",
")",
":",
"client",
"=",
"self",
".",
"_datastore_client",
"try",
":",
"with",
"client",
".",
"transaction",
"(",
")",
"as",
"transaction",
":",
"work_key",
"=",
"client",
".",
"key",
"(",
"KIND_WORK_TYPE",
",",
"self",
".",
"_work_type_entity_id",
",",
"KIND_WORK",
",",
"work_id",
")",
"work_entity",
"=",
"client",
".",
"get",
"(",
"work_key",
",",
"transaction",
"=",
"transaction",
")",
"if",
"work_entity",
"[",
"'claimed_worker_id'",
"]",
"!=",
"worker_id",
":",
"return",
"False",
"work_entity",
"[",
"'is_completed'",
"]",
"=",
"True",
"if",
"other_values",
":",
"work_entity",
".",
"update",
"(",
"other_values",
")",
"if",
"error",
":",
"work_entity",
"[",
"'error'",
"]",
"=",
"text_type",
"(",
"error",
")",
"transaction",
".",
"put",
"(",
"work_entity",
")",
"except",
"Exception",
":",
"return",
"False",
"return",
"True"
] | Updates work piece in datastore as completed.
Args:
worker_id: ID of the worker which did the work
work_id: ID of the work which was done
other_values: dictionary with additonal values which should be saved
with the work piece
error: if not None then error occurred during computation of the work
piece. In such case work will be marked as completed with error.
Returns:
whether work was successfully updated | [
"Updates",
"work",
"piece",
"in",
"datastore",
"as",
"completed",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L263-L294 |
28,624 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | WorkPiecesBase.compute_work_statistics | def compute_work_statistics(self):
"""Computes statistics from all work pieces stored in this class."""
result = {}
for v in itervalues(self.work):
submission_id = v['submission_id']
if submission_id not in result:
result[submission_id] = {
'completed': 0,
'num_errors': 0,
'error_messages': set(),
'eval_times': [],
'min_eval_time': None,
'max_eval_time': None,
'mean_eval_time': None,
'median_eval_time': None,
}
if not v['is_completed']:
continue
result[submission_id]['completed'] += 1
if 'error' in v and v['error']:
result[submission_id]['num_errors'] += 1
result[submission_id]['error_messages'].add(v['error'])
else:
result[submission_id]['eval_times'].append(float(v['elapsed_time']))
for v in itervalues(result):
if v['eval_times']:
v['min_eval_time'] = np.min(v['eval_times'])
v['max_eval_time'] = np.max(v['eval_times'])
v['mean_eval_time'] = np.mean(v['eval_times'])
v['median_eval_time'] = np.median(v['eval_times'])
return result | python | def compute_work_statistics(self):
"""Computes statistics from all work pieces stored in this class."""
result = {}
for v in itervalues(self.work):
submission_id = v['submission_id']
if submission_id not in result:
result[submission_id] = {
'completed': 0,
'num_errors': 0,
'error_messages': set(),
'eval_times': [],
'min_eval_time': None,
'max_eval_time': None,
'mean_eval_time': None,
'median_eval_time': None,
}
if not v['is_completed']:
continue
result[submission_id]['completed'] += 1
if 'error' in v and v['error']:
result[submission_id]['num_errors'] += 1
result[submission_id]['error_messages'].add(v['error'])
else:
result[submission_id]['eval_times'].append(float(v['elapsed_time']))
for v in itervalues(result):
if v['eval_times']:
v['min_eval_time'] = np.min(v['eval_times'])
v['max_eval_time'] = np.max(v['eval_times'])
v['mean_eval_time'] = np.mean(v['eval_times'])
v['median_eval_time'] = np.median(v['eval_times'])
return result | [
"def",
"compute_work_statistics",
"(",
"self",
")",
":",
"result",
"=",
"{",
"}",
"for",
"v",
"in",
"itervalues",
"(",
"self",
".",
"work",
")",
":",
"submission_id",
"=",
"v",
"[",
"'submission_id'",
"]",
"if",
"submission_id",
"not",
"in",
"result",
":",
"result",
"[",
"submission_id",
"]",
"=",
"{",
"'completed'",
":",
"0",
",",
"'num_errors'",
":",
"0",
",",
"'error_messages'",
":",
"set",
"(",
")",
",",
"'eval_times'",
":",
"[",
"]",
",",
"'min_eval_time'",
":",
"None",
",",
"'max_eval_time'",
":",
"None",
",",
"'mean_eval_time'",
":",
"None",
",",
"'median_eval_time'",
":",
"None",
",",
"}",
"if",
"not",
"v",
"[",
"'is_completed'",
"]",
":",
"continue",
"result",
"[",
"submission_id",
"]",
"[",
"'completed'",
"]",
"+=",
"1",
"if",
"'error'",
"in",
"v",
"and",
"v",
"[",
"'error'",
"]",
":",
"result",
"[",
"submission_id",
"]",
"[",
"'num_errors'",
"]",
"+=",
"1",
"result",
"[",
"submission_id",
"]",
"[",
"'error_messages'",
"]",
".",
"add",
"(",
"v",
"[",
"'error'",
"]",
")",
"else",
":",
"result",
"[",
"submission_id",
"]",
"[",
"'eval_times'",
"]",
".",
"append",
"(",
"float",
"(",
"v",
"[",
"'elapsed_time'",
"]",
")",
")",
"for",
"v",
"in",
"itervalues",
"(",
"result",
")",
":",
"if",
"v",
"[",
"'eval_times'",
"]",
":",
"v",
"[",
"'min_eval_time'",
"]",
"=",
"np",
".",
"min",
"(",
"v",
"[",
"'eval_times'",
"]",
")",
"v",
"[",
"'max_eval_time'",
"]",
"=",
"np",
".",
"max",
"(",
"v",
"[",
"'eval_times'",
"]",
")",
"v",
"[",
"'mean_eval_time'",
"]",
"=",
"np",
".",
"mean",
"(",
"v",
"[",
"'eval_times'",
"]",
")",
"v",
"[",
"'median_eval_time'",
"]",
"=",
"np",
".",
"median",
"(",
"v",
"[",
"'eval_times'",
"]",
")",
"return",
"result"
] | Computes statistics from all work pieces stored in this class. | [
"Computes",
"statistics",
"from",
"all",
"work",
"pieces",
"stored",
"in",
"this",
"class",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L296-L326 |
28,625 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | AttackWorkPieces.init_from_adversarial_batches | def init_from_adversarial_batches(self, adv_batches):
"""Initializes work pieces from adversarial batches.
Args:
adv_batches: dict with adversarial batches,
could be obtained as AversarialBatches.data
"""
for idx, (adv_batch_id, adv_batch_val) in enumerate(iteritems(adv_batches)):
work_id = ATTACK_WORK_ID_PATTERN.format(idx)
self.work[work_id] = {
'claimed_worker_id': None,
'claimed_worker_start_time': None,
'is_completed': False,
'error': None,
'elapsed_time': None,
'submission_id': adv_batch_val['submission_id'],
'shard_id': None,
'output_adversarial_batch_id': adv_batch_id,
} | python | def init_from_adversarial_batches(self, adv_batches):
"""Initializes work pieces from adversarial batches.
Args:
adv_batches: dict with adversarial batches,
could be obtained as AversarialBatches.data
"""
for idx, (adv_batch_id, adv_batch_val) in enumerate(iteritems(adv_batches)):
work_id = ATTACK_WORK_ID_PATTERN.format(idx)
self.work[work_id] = {
'claimed_worker_id': None,
'claimed_worker_start_time': None,
'is_completed': False,
'error': None,
'elapsed_time': None,
'submission_id': adv_batch_val['submission_id'],
'shard_id': None,
'output_adversarial_batch_id': adv_batch_id,
} | [
"def",
"init_from_adversarial_batches",
"(",
"self",
",",
"adv_batches",
")",
":",
"for",
"idx",
",",
"(",
"adv_batch_id",
",",
"adv_batch_val",
")",
"in",
"enumerate",
"(",
"iteritems",
"(",
"adv_batches",
")",
")",
":",
"work_id",
"=",
"ATTACK_WORK_ID_PATTERN",
".",
"format",
"(",
"idx",
")",
"self",
".",
"work",
"[",
"work_id",
"]",
"=",
"{",
"'claimed_worker_id'",
":",
"None",
",",
"'claimed_worker_start_time'",
":",
"None",
",",
"'is_completed'",
":",
"False",
",",
"'error'",
":",
"None",
",",
"'elapsed_time'",
":",
"None",
",",
"'submission_id'",
":",
"adv_batch_val",
"[",
"'submission_id'",
"]",
",",
"'shard_id'",
":",
"None",
",",
"'output_adversarial_batch_id'",
":",
"adv_batch_id",
",",
"}"
] | Initializes work pieces from adversarial batches.
Args:
adv_batches: dict with adversarial batches,
could be obtained as AversarialBatches.data | [
"Initializes",
"work",
"pieces",
"from",
"adversarial",
"batches",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L349-L367 |
28,626 | tensorflow/cleverhans | examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py | DefenseWorkPieces.init_from_class_batches | def init_from_class_batches(self, class_batches, num_shards=None):
"""Initializes work pieces from classification batches.
Args:
class_batches: dict with classification batches, could be obtained
as ClassificationBatches.data
num_shards: number of shards to split data into,
if None then no sharding is done.
"""
shards_for_submissions = {}
shard_idx = 0
for idx, (batch_id, batch_val) in enumerate(iteritems(class_batches)):
work_id = DEFENSE_WORK_ID_PATTERN.format(idx)
submission_id = batch_val['submission_id']
shard_id = None
if num_shards:
shard_id = shards_for_submissions.get(submission_id)
if shard_id is None:
shard_id = shard_idx % num_shards
shards_for_submissions[submission_id] = shard_id
shard_idx += 1
# Note: defense also might have following fields populated by worker:
# stat_correct, stat_error, stat_target_class, stat_num_images
self.work[work_id] = {
'claimed_worker_id': None,
'claimed_worker_start_time': None,
'is_completed': False,
'error': None,
'elapsed_time': None,
'submission_id': submission_id,
'shard_id': shard_id,
'output_classification_batch_id': batch_id,
} | python | def init_from_class_batches(self, class_batches, num_shards=None):
"""Initializes work pieces from classification batches.
Args:
class_batches: dict with classification batches, could be obtained
as ClassificationBatches.data
num_shards: number of shards to split data into,
if None then no sharding is done.
"""
shards_for_submissions = {}
shard_idx = 0
for idx, (batch_id, batch_val) in enumerate(iteritems(class_batches)):
work_id = DEFENSE_WORK_ID_PATTERN.format(idx)
submission_id = batch_val['submission_id']
shard_id = None
if num_shards:
shard_id = shards_for_submissions.get(submission_id)
if shard_id is None:
shard_id = shard_idx % num_shards
shards_for_submissions[submission_id] = shard_id
shard_idx += 1
# Note: defense also might have following fields populated by worker:
# stat_correct, stat_error, stat_target_class, stat_num_images
self.work[work_id] = {
'claimed_worker_id': None,
'claimed_worker_start_time': None,
'is_completed': False,
'error': None,
'elapsed_time': None,
'submission_id': submission_id,
'shard_id': shard_id,
'output_classification_batch_id': batch_id,
} | [
"def",
"init_from_class_batches",
"(",
"self",
",",
"class_batches",
",",
"num_shards",
"=",
"None",
")",
":",
"shards_for_submissions",
"=",
"{",
"}",
"shard_idx",
"=",
"0",
"for",
"idx",
",",
"(",
"batch_id",
",",
"batch_val",
")",
"in",
"enumerate",
"(",
"iteritems",
"(",
"class_batches",
")",
")",
":",
"work_id",
"=",
"DEFENSE_WORK_ID_PATTERN",
".",
"format",
"(",
"idx",
")",
"submission_id",
"=",
"batch_val",
"[",
"'submission_id'",
"]",
"shard_id",
"=",
"None",
"if",
"num_shards",
":",
"shard_id",
"=",
"shards_for_submissions",
".",
"get",
"(",
"submission_id",
")",
"if",
"shard_id",
"is",
"None",
":",
"shard_id",
"=",
"shard_idx",
"%",
"num_shards",
"shards_for_submissions",
"[",
"submission_id",
"]",
"=",
"shard_id",
"shard_idx",
"+=",
"1",
"# Note: defense also might have following fields populated by worker:",
"# stat_correct, stat_error, stat_target_class, stat_num_images",
"self",
".",
"work",
"[",
"work_id",
"]",
"=",
"{",
"'claimed_worker_id'",
":",
"None",
",",
"'claimed_worker_start_time'",
":",
"None",
",",
"'is_completed'",
":",
"False",
",",
"'error'",
":",
"None",
",",
"'elapsed_time'",
":",
"None",
",",
"'submission_id'",
":",
"submission_id",
",",
"'shard_id'",
":",
"shard_id",
",",
"'output_classification_batch_id'",
":",
"batch_id",
",",
"}"
] | Initializes work pieces from classification batches.
Args:
class_batches: dict with classification batches, could be obtained
as ClassificationBatches.data
num_shards: number of shards to split data into,
if None then no sharding is done. | [
"Initializes",
"work",
"pieces",
"from",
"classification",
"batches",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/eval_infra/code/eval_lib/work_data.py#L379-L411 |
28,627 | tensorflow/cleverhans | cleverhans/attacks/fast_gradient_method.py | FastGradientMethod.generate | def generate(self, x, **kwargs):
"""
Returns the graph for Fast Gradient Method adversarial examples.
:param x: The model's symbolic inputs.
:param kwargs: See `parse_params`
"""
# Parse and save attack-specific parameters
assert self.parse_params(**kwargs)
labels, _nb_classes = self.get_or_guess_labels(x, kwargs)
return fgm(
x,
self.model.get_logits(x),
y=labels,
eps=self.eps,
ord=self.ord,
clip_min=self.clip_min,
clip_max=self.clip_max,
targeted=(self.y_target is not None),
sanity_checks=self.sanity_checks) | python | def generate(self, x, **kwargs):
"""
Returns the graph for Fast Gradient Method adversarial examples.
:param x: The model's symbolic inputs.
:param kwargs: See `parse_params`
"""
# Parse and save attack-specific parameters
assert self.parse_params(**kwargs)
labels, _nb_classes = self.get_or_guess_labels(x, kwargs)
return fgm(
x,
self.model.get_logits(x),
y=labels,
eps=self.eps,
ord=self.ord,
clip_min=self.clip_min,
clip_max=self.clip_max,
targeted=(self.y_target is not None),
sanity_checks=self.sanity_checks) | [
"def",
"generate",
"(",
"self",
",",
"x",
",",
"*",
"*",
"kwargs",
")",
":",
"# Parse and save attack-specific parameters",
"assert",
"self",
".",
"parse_params",
"(",
"*",
"*",
"kwargs",
")",
"labels",
",",
"_nb_classes",
"=",
"self",
".",
"get_or_guess_labels",
"(",
"x",
",",
"kwargs",
")",
"return",
"fgm",
"(",
"x",
",",
"self",
".",
"model",
".",
"get_logits",
"(",
"x",
")",
",",
"y",
"=",
"labels",
",",
"eps",
"=",
"self",
".",
"eps",
",",
"ord",
"=",
"self",
".",
"ord",
",",
"clip_min",
"=",
"self",
".",
"clip_min",
",",
"clip_max",
"=",
"self",
".",
"clip_max",
",",
"targeted",
"=",
"(",
"self",
".",
"y_target",
"is",
"not",
"None",
")",
",",
"sanity_checks",
"=",
"self",
".",
"sanity_checks",
")"
] | Returns the graph for Fast Gradient Method adversarial examples.
:param x: The model's symbolic inputs.
:param kwargs: See `parse_params` | [
"Returns",
"the",
"graph",
"for",
"Fast",
"Gradient",
"Method",
"adversarial",
"examples",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/fast_gradient_method.py#L40-L61 |
28,628 | tensorflow/cleverhans | cleverhans/experimental/certification/nn.py | load_network_from_checkpoint | def load_network_from_checkpoint(checkpoint, model_json, input_shape=None):
"""Function to read the weights from checkpoint based on json description.
Args:
checkpoint: tensorflow checkpoint with trained model to
verify
model_json: path of json file with model description of
the network list of dictionary items for each layer
containing 'type', 'weight_var', 'bias_var' and
'is_transpose' 'type'is one of {'ff', 'ff_relu' or
'conv'}; 'weight_var' is the name of tf variable for
weights of layer i; 'bias_var' is the name of tf
variable for bias of layer i; 'is_transpose' is set to
True if the weights have to be transposed as per
convention Note that last layer is always feedforward
net_weights: list of numpy matrices of weights of each layer
convention: x[i+1] = W[i] x[i]
net_biases: list of numpy arrays of biases of each layer
net_layer_types: type of each layer ['ff' or 'ff_relu' or 'ff_conv'
or 'ff_conv_relu']
'ff': Simple feedforward layer with no activations
'ff_relu': Simple feedforward layer with ReLU activations
'ff_conv': Convolution layer with no activation
'ff_conv_relu': Convolution layer with ReLU activation
Raises:
ValueError: If layer_types are invalid or variable names
not found in checkpoint
"""
# Load checkpoint
reader = tf.train.load_checkpoint(checkpoint)
variable_map = reader.get_variable_to_shape_map()
checkpoint_variable_names = variable_map.keys()
# Parse JSON file for names
with tf.gfile.Open(model_json) as f:
list_model_var = json.load(f)
net_layer_types = []
net_weights = []
net_biases = []
cnn_params = []
# Checking validity of the input and adding to list
for layer_model_var in list_model_var:
if layer_model_var['type'] not in {'ff', 'ff_relu', 'conv'}:
raise ValueError('Invalid layer type in description')
if (layer_model_var['weight_var'] not in checkpoint_variable_names or
layer_model_var['bias_var'] not in checkpoint_variable_names):
raise ValueError('Variable names not found in checkpoint')
net_layer_types.append(layer_model_var['type'])
layer_weight = reader.get_tensor(layer_model_var['weight_var'])
layer_bias = reader.get_tensor(layer_model_var['bias_var'])
# TODO(aditirag): is there a way to automatically check when to transpose
# We want weights W such that x^{i+1} = W^i x^i + b^i
# Can think of a hack involving matching shapes but if shapes are equal
# it can be ambiguous
if layer_model_var['type'] in {'ff', 'ff_relu'}:
layer_weight = np.transpose(layer_weight)
cnn_params.append(None)
if layer_model_var['type'] in {'conv'}:
if 'stride' not in layer_model_var or 'padding' not in layer_model_var:
raise ValueError('Please define stride and padding for conv layers.')
cnn_params.append({'stride': layer_model_var['stride'], 'padding': layer_model_var['padding']})
net_weights.append(layer_weight)
net_biases.append(np.reshape(layer_bias, (np.size(layer_bias), 1)))
return NeuralNetwork(net_weights, net_biases, net_layer_types, input_shape, cnn_params) | python | def load_network_from_checkpoint(checkpoint, model_json, input_shape=None):
"""Function to read the weights from checkpoint based on json description.
Args:
checkpoint: tensorflow checkpoint with trained model to
verify
model_json: path of json file with model description of
the network list of dictionary items for each layer
containing 'type', 'weight_var', 'bias_var' and
'is_transpose' 'type'is one of {'ff', 'ff_relu' or
'conv'}; 'weight_var' is the name of tf variable for
weights of layer i; 'bias_var' is the name of tf
variable for bias of layer i; 'is_transpose' is set to
True if the weights have to be transposed as per
convention Note that last layer is always feedforward
net_weights: list of numpy matrices of weights of each layer
convention: x[i+1] = W[i] x[i]
net_biases: list of numpy arrays of biases of each layer
net_layer_types: type of each layer ['ff' or 'ff_relu' or 'ff_conv'
or 'ff_conv_relu']
'ff': Simple feedforward layer with no activations
'ff_relu': Simple feedforward layer with ReLU activations
'ff_conv': Convolution layer with no activation
'ff_conv_relu': Convolution layer with ReLU activation
Raises:
ValueError: If layer_types are invalid or variable names
not found in checkpoint
"""
# Load checkpoint
reader = tf.train.load_checkpoint(checkpoint)
variable_map = reader.get_variable_to_shape_map()
checkpoint_variable_names = variable_map.keys()
# Parse JSON file for names
with tf.gfile.Open(model_json) as f:
list_model_var = json.load(f)
net_layer_types = []
net_weights = []
net_biases = []
cnn_params = []
# Checking validity of the input and adding to list
for layer_model_var in list_model_var:
if layer_model_var['type'] not in {'ff', 'ff_relu', 'conv'}:
raise ValueError('Invalid layer type in description')
if (layer_model_var['weight_var'] not in checkpoint_variable_names or
layer_model_var['bias_var'] not in checkpoint_variable_names):
raise ValueError('Variable names not found in checkpoint')
net_layer_types.append(layer_model_var['type'])
layer_weight = reader.get_tensor(layer_model_var['weight_var'])
layer_bias = reader.get_tensor(layer_model_var['bias_var'])
# TODO(aditirag): is there a way to automatically check when to transpose
# We want weights W such that x^{i+1} = W^i x^i + b^i
# Can think of a hack involving matching shapes but if shapes are equal
# it can be ambiguous
if layer_model_var['type'] in {'ff', 'ff_relu'}:
layer_weight = np.transpose(layer_weight)
cnn_params.append(None)
if layer_model_var['type'] in {'conv'}:
if 'stride' not in layer_model_var or 'padding' not in layer_model_var:
raise ValueError('Please define stride and padding for conv layers.')
cnn_params.append({'stride': layer_model_var['stride'], 'padding': layer_model_var['padding']})
net_weights.append(layer_weight)
net_biases.append(np.reshape(layer_bias, (np.size(layer_bias), 1)))
return NeuralNetwork(net_weights, net_biases, net_layer_types, input_shape, cnn_params) | [
"def",
"load_network_from_checkpoint",
"(",
"checkpoint",
",",
"model_json",
",",
"input_shape",
"=",
"None",
")",
":",
"# Load checkpoint",
"reader",
"=",
"tf",
".",
"train",
".",
"load_checkpoint",
"(",
"checkpoint",
")",
"variable_map",
"=",
"reader",
".",
"get_variable_to_shape_map",
"(",
")",
"checkpoint_variable_names",
"=",
"variable_map",
".",
"keys",
"(",
")",
"# Parse JSON file for names",
"with",
"tf",
".",
"gfile",
".",
"Open",
"(",
"model_json",
")",
"as",
"f",
":",
"list_model_var",
"=",
"json",
".",
"load",
"(",
"f",
")",
"net_layer_types",
"=",
"[",
"]",
"net_weights",
"=",
"[",
"]",
"net_biases",
"=",
"[",
"]",
"cnn_params",
"=",
"[",
"]",
"# Checking validity of the input and adding to list",
"for",
"layer_model_var",
"in",
"list_model_var",
":",
"if",
"layer_model_var",
"[",
"'type'",
"]",
"not",
"in",
"{",
"'ff'",
",",
"'ff_relu'",
",",
"'conv'",
"}",
":",
"raise",
"ValueError",
"(",
"'Invalid layer type in description'",
")",
"if",
"(",
"layer_model_var",
"[",
"'weight_var'",
"]",
"not",
"in",
"checkpoint_variable_names",
"or",
"layer_model_var",
"[",
"'bias_var'",
"]",
"not",
"in",
"checkpoint_variable_names",
")",
":",
"raise",
"ValueError",
"(",
"'Variable names not found in checkpoint'",
")",
"net_layer_types",
".",
"append",
"(",
"layer_model_var",
"[",
"'type'",
"]",
")",
"layer_weight",
"=",
"reader",
".",
"get_tensor",
"(",
"layer_model_var",
"[",
"'weight_var'",
"]",
")",
"layer_bias",
"=",
"reader",
".",
"get_tensor",
"(",
"layer_model_var",
"[",
"'bias_var'",
"]",
")",
"# TODO(aditirag): is there a way to automatically check when to transpose",
"# We want weights W such that x^{i+1} = W^i x^i + b^i",
"# Can think of a hack involving matching shapes but if shapes are equal",
"# it can be ambiguous",
"if",
"layer_model_var",
"[",
"'type'",
"]",
"in",
"{",
"'ff'",
",",
"'ff_relu'",
"}",
":",
"layer_weight",
"=",
"np",
".",
"transpose",
"(",
"layer_weight",
")",
"cnn_params",
".",
"append",
"(",
"None",
")",
"if",
"layer_model_var",
"[",
"'type'",
"]",
"in",
"{",
"'conv'",
"}",
":",
"if",
"'stride'",
"not",
"in",
"layer_model_var",
"or",
"'padding'",
"not",
"in",
"layer_model_var",
":",
"raise",
"ValueError",
"(",
"'Please define stride and padding for conv layers.'",
")",
"cnn_params",
".",
"append",
"(",
"{",
"'stride'",
":",
"layer_model_var",
"[",
"'stride'",
"]",
",",
"'padding'",
":",
"layer_model_var",
"[",
"'padding'",
"]",
"}",
")",
"net_weights",
".",
"append",
"(",
"layer_weight",
")",
"net_biases",
".",
"append",
"(",
"np",
".",
"reshape",
"(",
"layer_bias",
",",
"(",
"np",
".",
"size",
"(",
"layer_bias",
")",
",",
"1",
")",
")",
")",
"return",
"NeuralNetwork",
"(",
"net_weights",
",",
"net_biases",
",",
"net_layer_types",
",",
"input_shape",
",",
"cnn_params",
")"
] | Function to read the weights from checkpoint based on json description.
Args:
checkpoint: tensorflow checkpoint with trained model to
verify
model_json: path of json file with model description of
the network list of dictionary items for each layer
containing 'type', 'weight_var', 'bias_var' and
'is_transpose' 'type'is one of {'ff', 'ff_relu' or
'conv'}; 'weight_var' is the name of tf variable for
weights of layer i; 'bias_var' is the name of tf
variable for bias of layer i; 'is_transpose' is set to
True if the weights have to be transposed as per
convention Note that last layer is always feedforward
net_weights: list of numpy matrices of weights of each layer
convention: x[i+1] = W[i] x[i]
net_biases: list of numpy arrays of biases of each layer
net_layer_types: type of each layer ['ff' or 'ff_relu' or 'ff_conv'
or 'ff_conv_relu']
'ff': Simple feedforward layer with no activations
'ff_relu': Simple feedforward layer with ReLU activations
'ff_conv': Convolution layer with no activation
'ff_conv_relu': Convolution layer with ReLU activation
Raises:
ValueError: If layer_types are invalid or variable names
not found in checkpoint | [
"Function",
"to",
"read",
"the",
"weights",
"from",
"checkpoint",
"based",
"on",
"json",
"description",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/experimental/certification/nn.py#L161-L226 |
28,629 | tensorflow/cleverhans | cleverhans/experimental/certification/nn.py | NeuralNetwork.forward_pass | def forward_pass(self, vector, layer_index, is_transpose=False, is_abs=False):
"""Performs forward pass through the layer weights at layer_index.
Args:
vector: vector that has to be passed through in forward pass
layer_index: index of the layer
is_transpose: whether the weights of the layer have to be transposed
is_abs: whether to take the absolute value of the weights
Returns:
tensor that corresponds to the forward pass through the layer
Raises:
ValueError: if the layer_index is negative or more than num hidden layers
"""
if(layer_index < 0 or layer_index > self.num_hidden_layers):
raise ValueError('Invalid layer index')
layer_type = self.layer_types[layer_index]
weight = self.weights[layer_index]
if is_abs:
weight = tf.abs(weight)
if is_transpose:
vector = tf.reshape(vector, self.output_shapes[layer_index])
else:
vector = tf.reshape(vector, self.input_shapes[layer_index])
if layer_type in {'ff', 'ff_relu'}:
if is_transpose:
weight = tf.transpose(weight)
return_vector = tf.matmul(weight, vector)
elif layer_type in {'conv', 'conv_relu'}:
if is_transpose:
return_vector = tf.nn.conv2d_transpose(vector,
weight,
output_shape=self.input_shapes[layer_index],
strides=[1, self.cnn_params[layer_index]['stride'],
self.cnn_params[layer_index]['stride'], 1],
padding=self.cnn_params[layer_index]['padding'])
else:
return_vector = tf.nn.conv2d(vector,
weight,
strides=[1, self.cnn_params[layer_index]['stride'],
self.cnn_params[layer_index]['stride'], 1],
padding=self.cnn_params[layer_index]['padding'])
else:
raise NotImplementedError('Unsupported layer type: {0}'.format(layer_type))
if is_transpose:
return tf.reshape(return_vector, (self.sizes[layer_index], 1))
return tf.reshape(return_vector, (self.sizes[layer_index + 1], 1)) | python | def forward_pass(self, vector, layer_index, is_transpose=False, is_abs=False):
"""Performs forward pass through the layer weights at layer_index.
Args:
vector: vector that has to be passed through in forward pass
layer_index: index of the layer
is_transpose: whether the weights of the layer have to be transposed
is_abs: whether to take the absolute value of the weights
Returns:
tensor that corresponds to the forward pass through the layer
Raises:
ValueError: if the layer_index is negative or more than num hidden layers
"""
if(layer_index < 0 or layer_index > self.num_hidden_layers):
raise ValueError('Invalid layer index')
layer_type = self.layer_types[layer_index]
weight = self.weights[layer_index]
if is_abs:
weight = tf.abs(weight)
if is_transpose:
vector = tf.reshape(vector, self.output_shapes[layer_index])
else:
vector = tf.reshape(vector, self.input_shapes[layer_index])
if layer_type in {'ff', 'ff_relu'}:
if is_transpose:
weight = tf.transpose(weight)
return_vector = tf.matmul(weight, vector)
elif layer_type in {'conv', 'conv_relu'}:
if is_transpose:
return_vector = tf.nn.conv2d_transpose(vector,
weight,
output_shape=self.input_shapes[layer_index],
strides=[1, self.cnn_params[layer_index]['stride'],
self.cnn_params[layer_index]['stride'], 1],
padding=self.cnn_params[layer_index]['padding'])
else:
return_vector = tf.nn.conv2d(vector,
weight,
strides=[1, self.cnn_params[layer_index]['stride'],
self.cnn_params[layer_index]['stride'], 1],
padding=self.cnn_params[layer_index]['padding'])
else:
raise NotImplementedError('Unsupported layer type: {0}'.format(layer_type))
if is_transpose:
return tf.reshape(return_vector, (self.sizes[layer_index], 1))
return tf.reshape(return_vector, (self.sizes[layer_index + 1], 1)) | [
"def",
"forward_pass",
"(",
"self",
",",
"vector",
",",
"layer_index",
",",
"is_transpose",
"=",
"False",
",",
"is_abs",
"=",
"False",
")",
":",
"if",
"(",
"layer_index",
"<",
"0",
"or",
"layer_index",
">",
"self",
".",
"num_hidden_layers",
")",
":",
"raise",
"ValueError",
"(",
"'Invalid layer index'",
")",
"layer_type",
"=",
"self",
".",
"layer_types",
"[",
"layer_index",
"]",
"weight",
"=",
"self",
".",
"weights",
"[",
"layer_index",
"]",
"if",
"is_abs",
":",
"weight",
"=",
"tf",
".",
"abs",
"(",
"weight",
")",
"if",
"is_transpose",
":",
"vector",
"=",
"tf",
".",
"reshape",
"(",
"vector",
",",
"self",
".",
"output_shapes",
"[",
"layer_index",
"]",
")",
"else",
":",
"vector",
"=",
"tf",
".",
"reshape",
"(",
"vector",
",",
"self",
".",
"input_shapes",
"[",
"layer_index",
"]",
")",
"if",
"layer_type",
"in",
"{",
"'ff'",
",",
"'ff_relu'",
"}",
":",
"if",
"is_transpose",
":",
"weight",
"=",
"tf",
".",
"transpose",
"(",
"weight",
")",
"return_vector",
"=",
"tf",
".",
"matmul",
"(",
"weight",
",",
"vector",
")",
"elif",
"layer_type",
"in",
"{",
"'conv'",
",",
"'conv_relu'",
"}",
":",
"if",
"is_transpose",
":",
"return_vector",
"=",
"tf",
".",
"nn",
".",
"conv2d_transpose",
"(",
"vector",
",",
"weight",
",",
"output_shape",
"=",
"self",
".",
"input_shapes",
"[",
"layer_index",
"]",
",",
"strides",
"=",
"[",
"1",
",",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'stride'",
"]",
",",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'stride'",
"]",
",",
"1",
"]",
",",
"padding",
"=",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'padding'",
"]",
")",
"else",
":",
"return_vector",
"=",
"tf",
".",
"nn",
".",
"conv2d",
"(",
"vector",
",",
"weight",
",",
"strides",
"=",
"[",
"1",
",",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'stride'",
"]",
",",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'stride'",
"]",
",",
"1",
"]",
",",
"padding",
"=",
"self",
".",
"cnn_params",
"[",
"layer_index",
"]",
"[",
"'padding'",
"]",
")",
"else",
":",
"raise",
"NotImplementedError",
"(",
"'Unsupported layer type: {0}'",
".",
"format",
"(",
"layer_type",
")",
")",
"if",
"is_transpose",
":",
"return",
"tf",
".",
"reshape",
"(",
"return_vector",
",",
"(",
"self",
".",
"sizes",
"[",
"layer_index",
"]",
",",
"1",
")",
")",
"return",
"tf",
".",
"reshape",
"(",
"return_vector",
",",
"(",
"self",
".",
"sizes",
"[",
"layer_index",
"+",
"1",
"]",
",",
"1",
")",
")"
] | Performs forward pass through the layer weights at layer_index.
Args:
vector: vector that has to be passed through in forward pass
layer_index: index of the layer
is_transpose: whether the weights of the layer have to be transposed
is_abs: whether to take the absolute value of the weights
Returns:
tensor that corresponds to the forward pass through the layer
Raises:
ValueError: if the layer_index is negative or more than num hidden layers | [
"Performs",
"forward",
"pass",
"through",
"the",
"layer",
"weights",
"at",
"layer_index",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/experimental/certification/nn.py#L111-L159 |
28,630 | tensorflow/cleverhans | cleverhans/devtools/version.py | dev_version | def dev_version():
"""
Returns a hexdigest of all the python files in the module.
"""
md5_hash = hashlib.md5()
py_files = sorted(list_files(suffix=".py"))
if not py_files:
return ''
for filename in py_files:
with open(filename, 'rb') as fobj:
content = fobj.read()
md5_hash.update(content)
return md5_hash.hexdigest() | python | def dev_version():
"""
Returns a hexdigest of all the python files in the module.
"""
md5_hash = hashlib.md5()
py_files = sorted(list_files(suffix=".py"))
if not py_files:
return ''
for filename in py_files:
with open(filename, 'rb') as fobj:
content = fobj.read()
md5_hash.update(content)
return md5_hash.hexdigest() | [
"def",
"dev_version",
"(",
")",
":",
"md5_hash",
"=",
"hashlib",
".",
"md5",
"(",
")",
"py_files",
"=",
"sorted",
"(",
"list_files",
"(",
"suffix",
"=",
"\".py\"",
")",
")",
"if",
"not",
"py_files",
":",
"return",
"''",
"for",
"filename",
"in",
"py_files",
":",
"with",
"open",
"(",
"filename",
",",
"'rb'",
")",
"as",
"fobj",
":",
"content",
"=",
"fobj",
".",
"read",
"(",
")",
"md5_hash",
".",
"update",
"(",
"content",
")",
"return",
"md5_hash",
".",
"hexdigest",
"(",
")"
] | Returns a hexdigest of all the python files in the module. | [
"Returns",
"a",
"hexdigest",
"of",
"all",
"the",
"python",
"files",
"in",
"the",
"module",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/devtools/version.py#L11-L24 |
28,631 | tensorflow/cleverhans | cleverhans/experimental/certification/utils.py | initialize_dual | def initialize_dual(neural_net_params_object, init_dual_file=None,
random_init_variance=0.01, init_nu=200.0):
"""Function to initialize the dual variables of the class.
Args:
neural_net_params_object: Object with the neural net weights, biases
and types
init_dual_file: Path to file containing dual variables, if the path
is empty, perform random initialization
Expects numpy dictionary with
lambda_pos_0, lambda_pos_1, ..
lambda_neg_0, lambda_neg_1, ..
lambda_quad_0, lambda_quad_1, ..
lambda_lu_0, lambda_lu_1, ..
random_init_variance: variance for random initialization
init_nu: Value to initialize nu variable with
Returns:
dual_var: dual variables initialized appropriately.
"""
lambda_pos = []
lambda_neg = []
lambda_quad = []
lambda_lu = []
if init_dual_file is None:
for i in range(0, neural_net_params_object.num_hidden_layers + 1):
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_pos.append(tf.get_variable('lambda_pos_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_neg.append(tf.get_variable('lambda_neg_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_quad.append(tf.get_variable('lambda_quad_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_lu.append(tf.get_variable('lambda_lu_' + str(i),
initializer=initializer,
dtype=tf.float32))
nu = tf.get_variable('nu', initializer=init_nu)
else:
# Loading from file
dual_var_init_val = np.load(init_dual_file).item()
for i in range(0, neural_net_params_object.num_hidden_layers + 1):
lambda_pos.append(
tf.get_variable('lambda_pos_' + str(i),
initializer=dual_var_init_val['lambda_pos'][i],
dtype=tf.float32))
lambda_neg.append(
tf.get_variable('lambda_neg_' + str(i),
initializer=dual_var_init_val['lambda_neg'][i],
dtype=tf.float32))
lambda_quad.append(
tf.get_variable('lambda_quad_' + str(i),
initializer=dual_var_init_val['lambda_quad'][i],
dtype=tf.float32))
lambda_lu.append(
tf.get_variable('lambda_lu_' + str(i),
initializer=dual_var_init_val['lambda_lu'][i],
dtype=tf.float32))
nu = tf.get_variable('nu', initializer=1.0*dual_var_init_val['nu'])
dual_var = {'lambda_pos': lambda_pos, 'lambda_neg': lambda_neg,
'lambda_quad': lambda_quad, 'lambda_lu': lambda_lu, 'nu': nu}
return dual_var | python | def initialize_dual(neural_net_params_object, init_dual_file=None,
random_init_variance=0.01, init_nu=200.0):
"""Function to initialize the dual variables of the class.
Args:
neural_net_params_object: Object with the neural net weights, biases
and types
init_dual_file: Path to file containing dual variables, if the path
is empty, perform random initialization
Expects numpy dictionary with
lambda_pos_0, lambda_pos_1, ..
lambda_neg_0, lambda_neg_1, ..
lambda_quad_0, lambda_quad_1, ..
lambda_lu_0, lambda_lu_1, ..
random_init_variance: variance for random initialization
init_nu: Value to initialize nu variable with
Returns:
dual_var: dual variables initialized appropriately.
"""
lambda_pos = []
lambda_neg = []
lambda_quad = []
lambda_lu = []
if init_dual_file is None:
for i in range(0, neural_net_params_object.num_hidden_layers + 1):
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_pos.append(tf.get_variable('lambda_pos_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_neg.append(tf.get_variable('lambda_neg_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_quad.append(tf.get_variable('lambda_quad_' + str(i),
initializer=initializer,
dtype=tf.float32))
initializer = (np.random.uniform(0, random_init_variance, size=(
neural_net_params_object.sizes[i], 1))).astype(np.float32)
lambda_lu.append(tf.get_variable('lambda_lu_' + str(i),
initializer=initializer,
dtype=tf.float32))
nu = tf.get_variable('nu', initializer=init_nu)
else:
# Loading from file
dual_var_init_val = np.load(init_dual_file).item()
for i in range(0, neural_net_params_object.num_hidden_layers + 1):
lambda_pos.append(
tf.get_variable('lambda_pos_' + str(i),
initializer=dual_var_init_val['lambda_pos'][i],
dtype=tf.float32))
lambda_neg.append(
tf.get_variable('lambda_neg_' + str(i),
initializer=dual_var_init_val['lambda_neg'][i],
dtype=tf.float32))
lambda_quad.append(
tf.get_variable('lambda_quad_' + str(i),
initializer=dual_var_init_val['lambda_quad'][i],
dtype=tf.float32))
lambda_lu.append(
tf.get_variable('lambda_lu_' + str(i),
initializer=dual_var_init_val['lambda_lu'][i],
dtype=tf.float32))
nu = tf.get_variable('nu', initializer=1.0*dual_var_init_val['nu'])
dual_var = {'lambda_pos': lambda_pos, 'lambda_neg': lambda_neg,
'lambda_quad': lambda_quad, 'lambda_lu': lambda_lu, 'nu': nu}
return dual_var | [
"def",
"initialize_dual",
"(",
"neural_net_params_object",
",",
"init_dual_file",
"=",
"None",
",",
"random_init_variance",
"=",
"0.01",
",",
"init_nu",
"=",
"200.0",
")",
":",
"lambda_pos",
"=",
"[",
"]",
"lambda_neg",
"=",
"[",
"]",
"lambda_quad",
"=",
"[",
"]",
"lambda_lu",
"=",
"[",
"]",
"if",
"init_dual_file",
"is",
"None",
":",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"neural_net_params_object",
".",
"num_hidden_layers",
"+",
"1",
")",
":",
"initializer",
"=",
"(",
"np",
".",
"random",
".",
"uniform",
"(",
"0",
",",
"random_init_variance",
",",
"size",
"=",
"(",
"neural_net_params_object",
".",
"sizes",
"[",
"i",
"]",
",",
"1",
")",
")",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"lambda_pos",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_pos_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"initializer",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"initializer",
"=",
"(",
"np",
".",
"random",
".",
"uniform",
"(",
"0",
",",
"random_init_variance",
",",
"size",
"=",
"(",
"neural_net_params_object",
".",
"sizes",
"[",
"i",
"]",
",",
"1",
")",
")",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"lambda_neg",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_neg_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"initializer",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"initializer",
"=",
"(",
"np",
".",
"random",
".",
"uniform",
"(",
"0",
",",
"random_init_variance",
",",
"size",
"=",
"(",
"neural_net_params_object",
".",
"sizes",
"[",
"i",
"]",
",",
"1",
")",
")",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"lambda_quad",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_quad_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"initializer",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"initializer",
"=",
"(",
"np",
".",
"random",
".",
"uniform",
"(",
"0",
",",
"random_init_variance",
",",
"size",
"=",
"(",
"neural_net_params_object",
".",
"sizes",
"[",
"i",
"]",
",",
"1",
")",
")",
")",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"lambda_lu",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_lu_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"initializer",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"nu",
"=",
"tf",
".",
"get_variable",
"(",
"'nu'",
",",
"initializer",
"=",
"init_nu",
")",
"else",
":",
"# Loading from file",
"dual_var_init_val",
"=",
"np",
".",
"load",
"(",
"init_dual_file",
")",
".",
"item",
"(",
")",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"neural_net_params_object",
".",
"num_hidden_layers",
"+",
"1",
")",
":",
"lambda_pos",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_pos_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"dual_var_init_val",
"[",
"'lambda_pos'",
"]",
"[",
"i",
"]",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"lambda_neg",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_neg_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"dual_var_init_val",
"[",
"'lambda_neg'",
"]",
"[",
"i",
"]",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"lambda_quad",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_quad_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"dual_var_init_val",
"[",
"'lambda_quad'",
"]",
"[",
"i",
"]",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"lambda_lu",
".",
"append",
"(",
"tf",
".",
"get_variable",
"(",
"'lambda_lu_'",
"+",
"str",
"(",
"i",
")",
",",
"initializer",
"=",
"dual_var_init_val",
"[",
"'lambda_lu'",
"]",
"[",
"i",
"]",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
")",
"nu",
"=",
"tf",
".",
"get_variable",
"(",
"'nu'",
",",
"initializer",
"=",
"1.0",
"*",
"dual_var_init_val",
"[",
"'nu'",
"]",
")",
"dual_var",
"=",
"{",
"'lambda_pos'",
":",
"lambda_pos",
",",
"'lambda_neg'",
":",
"lambda_neg",
",",
"'lambda_quad'",
":",
"lambda_quad",
",",
"'lambda_lu'",
":",
"lambda_lu",
",",
"'nu'",
":",
"nu",
"}",
"return",
"dual_var"
] | Function to initialize the dual variables of the class.
Args:
neural_net_params_object: Object with the neural net weights, biases
and types
init_dual_file: Path to file containing dual variables, if the path
is empty, perform random initialization
Expects numpy dictionary with
lambda_pos_0, lambda_pos_1, ..
lambda_neg_0, lambda_neg_1, ..
lambda_quad_0, lambda_quad_1, ..
lambda_lu_0, lambda_lu_1, ..
random_init_variance: variance for random initialization
init_nu: Value to initialize nu variable with
Returns:
dual_var: dual variables initialized appropriately. | [
"Function",
"to",
"initialize",
"the",
"dual",
"variables",
"of",
"the",
"class",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/experimental/certification/utils.py#L22-L93 |
28,632 | tensorflow/cleverhans | cleverhans/experimental/certification/utils.py | minimum_eigen_vector | def minimum_eigen_vector(x, num_steps, learning_rate, vector_prod_fn):
"""Computes eigenvector which corresponds to minimum eigenvalue.
Args:
x: initial value of eigenvector.
num_steps: number of optimization steps.
learning_rate: learning rate.
vector_prod_fn: function which takes x and returns product H*x.
Returns:
approximate value of eigenvector.
This function finds approximate value of eigenvector of matrix H which
corresponds to smallest (by absolute value) eigenvalue of H.
It works by solving optimization problem x^{T}*H*x -> min.
"""
x = tf.nn.l2_normalize(x)
for _ in range(num_steps):
x = eig_one_step(x, learning_rate, vector_prod_fn)
return x | python | def minimum_eigen_vector(x, num_steps, learning_rate, vector_prod_fn):
"""Computes eigenvector which corresponds to minimum eigenvalue.
Args:
x: initial value of eigenvector.
num_steps: number of optimization steps.
learning_rate: learning rate.
vector_prod_fn: function which takes x and returns product H*x.
Returns:
approximate value of eigenvector.
This function finds approximate value of eigenvector of matrix H which
corresponds to smallest (by absolute value) eigenvalue of H.
It works by solving optimization problem x^{T}*H*x -> min.
"""
x = tf.nn.l2_normalize(x)
for _ in range(num_steps):
x = eig_one_step(x, learning_rate, vector_prod_fn)
return x | [
"def",
"minimum_eigen_vector",
"(",
"x",
",",
"num_steps",
",",
"learning_rate",
",",
"vector_prod_fn",
")",
":",
"x",
"=",
"tf",
".",
"nn",
".",
"l2_normalize",
"(",
"x",
")",
"for",
"_",
"in",
"range",
"(",
"num_steps",
")",
":",
"x",
"=",
"eig_one_step",
"(",
"x",
",",
"learning_rate",
",",
"vector_prod_fn",
")",
"return",
"x"
] | Computes eigenvector which corresponds to minimum eigenvalue.
Args:
x: initial value of eigenvector.
num_steps: number of optimization steps.
learning_rate: learning rate.
vector_prod_fn: function which takes x and returns product H*x.
Returns:
approximate value of eigenvector.
This function finds approximate value of eigenvector of matrix H which
corresponds to smallest (by absolute value) eigenvalue of H.
It works by solving optimization problem x^{T}*H*x -> min. | [
"Computes",
"eigenvector",
"which",
"corresponds",
"to",
"minimum",
"eigenvalue",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/experimental/certification/utils.py#L162-L181 |
28,633 | tensorflow/cleverhans | cleverhans/experimental/certification/utils.py | tf_lanczos_smallest_eigval | def tf_lanczos_smallest_eigval(vector_prod_fn,
matrix_dim,
initial_vector,
num_iter=1000,
max_iter=1000,
collapse_tol=1e-9,
dtype=tf.float32):
"""Computes smallest eigenvector and eigenvalue using Lanczos in pure TF.
This function computes smallest eigenvector and eigenvalue of the matrix
which is implicitly specified by `vector_prod_fn`.
`vector_prod_fn` is a function which takes `x` and returns a product of matrix
in consideration and `x`.
Computation is done using Lanczos algorithm, see
https://en.wikipedia.org/wiki/Lanczos_algorithm#The_algorithm
Args:
vector_prod_fn: function which takes a vector as an input and returns
matrix vector product.
matrix_dim: dimentionality of the matrix.
initial_vector: guess vector to start the algorithm with
num_iter: user-defined number of iterations for the algorithm
max_iter: maximum number of iterations.
collapse_tol: tolerance to determine collapse of the Krylov subspace
dtype: type of data
Returns:
tuple of (eigenvalue, eigenvector) of smallest eigenvalue and corresponding
eigenvector.
"""
# alpha will store diagonal elements
alpha = tf.TensorArray(dtype, size=1, dynamic_size=True, element_shape=())
# beta will store off diagonal elements
beta = tf.TensorArray(dtype, size=0, dynamic_size=True, element_shape=())
# q will store Krylov space basis
q_vectors = tf.TensorArray(
dtype, size=1, dynamic_size=True, element_shape=(matrix_dim, 1))
# If start vector is all zeros, make it a random normal vector and run for max_iter
if tf.norm(initial_vector) < collapse_tol:
initial_vector = tf.random_normal(shape=(matrix_dim, 1), dtype=dtype)
num_iter = max_iter
w = initial_vector / tf.norm(initial_vector)
# Iteration 0 of Lanczos
q_vectors = q_vectors.write(0, w)
w_ = vector_prod_fn(w)
cur_alpha = tf.reduce_sum(w_ * w)
alpha = alpha.write(0, cur_alpha)
w_ = w_ - tf.scalar_mul(cur_alpha, w)
w_prev = w
w = w_
# Subsequent iterations of Lanczos
for i in tf.range(1, num_iter):
cur_beta = tf.norm(w)
if cur_beta < collapse_tol:
# return early if Krylov subspace collapsed
break
# cur_beta is larger than collapse_tol,
# so division will return finite result.
w = w / cur_beta
w_ = vector_prod_fn(w)
cur_alpha = tf.reduce_sum(w_ * w)
q_vectors = q_vectors.write(i, w)
alpha = alpha.write(i, cur_alpha)
beta = beta.write(i-1, cur_beta)
w_ = w_ - tf.scalar_mul(cur_alpha, w) - tf.scalar_mul(cur_beta, w_prev)
w_prev = w
w = w_
alpha = alpha.stack()
beta = beta.stack()
q_vectors = tf.reshape(q_vectors.stack(), (-1, matrix_dim))
offdiag_submatrix = tf.linalg.diag(beta)
tridiag_matrix = (tf.linalg.diag(alpha)
+ tf.pad(offdiag_submatrix, [[0, 1], [1, 0]])
+ tf.pad(offdiag_submatrix, [[1, 0], [0, 1]]))
eigvals, eigvecs = tf.linalg.eigh(tridiag_matrix)
smallest_eigval = eigvals[0]
smallest_eigvec = tf.matmul(tf.reshape(eigvecs[:, 0], (1, -1)),
q_vectors)
smallest_eigvec = smallest_eigvec / tf.norm(smallest_eigvec)
smallest_eigvec = tf.reshape(smallest_eigvec, (matrix_dim, 1))
return smallest_eigval, smallest_eigvec | python | def tf_lanczos_smallest_eigval(vector_prod_fn,
matrix_dim,
initial_vector,
num_iter=1000,
max_iter=1000,
collapse_tol=1e-9,
dtype=tf.float32):
"""Computes smallest eigenvector and eigenvalue using Lanczos in pure TF.
This function computes smallest eigenvector and eigenvalue of the matrix
which is implicitly specified by `vector_prod_fn`.
`vector_prod_fn` is a function which takes `x` and returns a product of matrix
in consideration and `x`.
Computation is done using Lanczos algorithm, see
https://en.wikipedia.org/wiki/Lanczos_algorithm#The_algorithm
Args:
vector_prod_fn: function which takes a vector as an input and returns
matrix vector product.
matrix_dim: dimentionality of the matrix.
initial_vector: guess vector to start the algorithm with
num_iter: user-defined number of iterations for the algorithm
max_iter: maximum number of iterations.
collapse_tol: tolerance to determine collapse of the Krylov subspace
dtype: type of data
Returns:
tuple of (eigenvalue, eigenvector) of smallest eigenvalue and corresponding
eigenvector.
"""
# alpha will store diagonal elements
alpha = tf.TensorArray(dtype, size=1, dynamic_size=True, element_shape=())
# beta will store off diagonal elements
beta = tf.TensorArray(dtype, size=0, dynamic_size=True, element_shape=())
# q will store Krylov space basis
q_vectors = tf.TensorArray(
dtype, size=1, dynamic_size=True, element_shape=(matrix_dim, 1))
# If start vector is all zeros, make it a random normal vector and run for max_iter
if tf.norm(initial_vector) < collapse_tol:
initial_vector = tf.random_normal(shape=(matrix_dim, 1), dtype=dtype)
num_iter = max_iter
w = initial_vector / tf.norm(initial_vector)
# Iteration 0 of Lanczos
q_vectors = q_vectors.write(0, w)
w_ = vector_prod_fn(w)
cur_alpha = tf.reduce_sum(w_ * w)
alpha = alpha.write(0, cur_alpha)
w_ = w_ - tf.scalar_mul(cur_alpha, w)
w_prev = w
w = w_
# Subsequent iterations of Lanczos
for i in tf.range(1, num_iter):
cur_beta = tf.norm(w)
if cur_beta < collapse_tol:
# return early if Krylov subspace collapsed
break
# cur_beta is larger than collapse_tol,
# so division will return finite result.
w = w / cur_beta
w_ = vector_prod_fn(w)
cur_alpha = tf.reduce_sum(w_ * w)
q_vectors = q_vectors.write(i, w)
alpha = alpha.write(i, cur_alpha)
beta = beta.write(i-1, cur_beta)
w_ = w_ - tf.scalar_mul(cur_alpha, w) - tf.scalar_mul(cur_beta, w_prev)
w_prev = w
w = w_
alpha = alpha.stack()
beta = beta.stack()
q_vectors = tf.reshape(q_vectors.stack(), (-1, matrix_dim))
offdiag_submatrix = tf.linalg.diag(beta)
tridiag_matrix = (tf.linalg.diag(alpha)
+ tf.pad(offdiag_submatrix, [[0, 1], [1, 0]])
+ tf.pad(offdiag_submatrix, [[1, 0], [0, 1]]))
eigvals, eigvecs = tf.linalg.eigh(tridiag_matrix)
smallest_eigval = eigvals[0]
smallest_eigvec = tf.matmul(tf.reshape(eigvecs[:, 0], (1, -1)),
q_vectors)
smallest_eigvec = smallest_eigvec / tf.norm(smallest_eigvec)
smallest_eigvec = tf.reshape(smallest_eigvec, (matrix_dim, 1))
return smallest_eigval, smallest_eigvec | [
"def",
"tf_lanczos_smallest_eigval",
"(",
"vector_prod_fn",
",",
"matrix_dim",
",",
"initial_vector",
",",
"num_iter",
"=",
"1000",
",",
"max_iter",
"=",
"1000",
",",
"collapse_tol",
"=",
"1e-9",
",",
"dtype",
"=",
"tf",
".",
"float32",
")",
":",
"# alpha will store diagonal elements",
"alpha",
"=",
"tf",
".",
"TensorArray",
"(",
"dtype",
",",
"size",
"=",
"1",
",",
"dynamic_size",
"=",
"True",
",",
"element_shape",
"=",
"(",
")",
")",
"# beta will store off diagonal elements",
"beta",
"=",
"tf",
".",
"TensorArray",
"(",
"dtype",
",",
"size",
"=",
"0",
",",
"dynamic_size",
"=",
"True",
",",
"element_shape",
"=",
"(",
")",
")",
"# q will store Krylov space basis",
"q_vectors",
"=",
"tf",
".",
"TensorArray",
"(",
"dtype",
",",
"size",
"=",
"1",
",",
"dynamic_size",
"=",
"True",
",",
"element_shape",
"=",
"(",
"matrix_dim",
",",
"1",
")",
")",
"# If start vector is all zeros, make it a random normal vector and run for max_iter",
"if",
"tf",
".",
"norm",
"(",
"initial_vector",
")",
"<",
"collapse_tol",
":",
"initial_vector",
"=",
"tf",
".",
"random_normal",
"(",
"shape",
"=",
"(",
"matrix_dim",
",",
"1",
")",
",",
"dtype",
"=",
"dtype",
")",
"num_iter",
"=",
"max_iter",
"w",
"=",
"initial_vector",
"/",
"tf",
".",
"norm",
"(",
"initial_vector",
")",
"# Iteration 0 of Lanczos",
"q_vectors",
"=",
"q_vectors",
".",
"write",
"(",
"0",
",",
"w",
")",
"w_",
"=",
"vector_prod_fn",
"(",
"w",
")",
"cur_alpha",
"=",
"tf",
".",
"reduce_sum",
"(",
"w_",
"*",
"w",
")",
"alpha",
"=",
"alpha",
".",
"write",
"(",
"0",
",",
"cur_alpha",
")",
"w_",
"=",
"w_",
"-",
"tf",
".",
"scalar_mul",
"(",
"cur_alpha",
",",
"w",
")",
"w_prev",
"=",
"w",
"w",
"=",
"w_",
"# Subsequent iterations of Lanczos",
"for",
"i",
"in",
"tf",
".",
"range",
"(",
"1",
",",
"num_iter",
")",
":",
"cur_beta",
"=",
"tf",
".",
"norm",
"(",
"w",
")",
"if",
"cur_beta",
"<",
"collapse_tol",
":",
"# return early if Krylov subspace collapsed",
"break",
"# cur_beta is larger than collapse_tol,",
"# so division will return finite result.",
"w",
"=",
"w",
"/",
"cur_beta",
"w_",
"=",
"vector_prod_fn",
"(",
"w",
")",
"cur_alpha",
"=",
"tf",
".",
"reduce_sum",
"(",
"w_",
"*",
"w",
")",
"q_vectors",
"=",
"q_vectors",
".",
"write",
"(",
"i",
",",
"w",
")",
"alpha",
"=",
"alpha",
".",
"write",
"(",
"i",
",",
"cur_alpha",
")",
"beta",
"=",
"beta",
".",
"write",
"(",
"i",
"-",
"1",
",",
"cur_beta",
")",
"w_",
"=",
"w_",
"-",
"tf",
".",
"scalar_mul",
"(",
"cur_alpha",
",",
"w",
")",
"-",
"tf",
".",
"scalar_mul",
"(",
"cur_beta",
",",
"w_prev",
")",
"w_prev",
"=",
"w",
"w",
"=",
"w_",
"alpha",
"=",
"alpha",
".",
"stack",
"(",
")",
"beta",
"=",
"beta",
".",
"stack",
"(",
")",
"q_vectors",
"=",
"tf",
".",
"reshape",
"(",
"q_vectors",
".",
"stack",
"(",
")",
",",
"(",
"-",
"1",
",",
"matrix_dim",
")",
")",
"offdiag_submatrix",
"=",
"tf",
".",
"linalg",
".",
"diag",
"(",
"beta",
")",
"tridiag_matrix",
"=",
"(",
"tf",
".",
"linalg",
".",
"diag",
"(",
"alpha",
")",
"+",
"tf",
".",
"pad",
"(",
"offdiag_submatrix",
",",
"[",
"[",
"0",
",",
"1",
"]",
",",
"[",
"1",
",",
"0",
"]",
"]",
")",
"+",
"tf",
".",
"pad",
"(",
"offdiag_submatrix",
",",
"[",
"[",
"1",
",",
"0",
"]",
",",
"[",
"0",
",",
"1",
"]",
"]",
")",
")",
"eigvals",
",",
"eigvecs",
"=",
"tf",
".",
"linalg",
".",
"eigh",
"(",
"tridiag_matrix",
")",
"smallest_eigval",
"=",
"eigvals",
"[",
"0",
"]",
"smallest_eigvec",
"=",
"tf",
".",
"matmul",
"(",
"tf",
".",
"reshape",
"(",
"eigvecs",
"[",
":",
",",
"0",
"]",
",",
"(",
"1",
",",
"-",
"1",
")",
")",
",",
"q_vectors",
")",
"smallest_eigvec",
"=",
"smallest_eigvec",
"/",
"tf",
".",
"norm",
"(",
"smallest_eigvec",
")",
"smallest_eigvec",
"=",
"tf",
".",
"reshape",
"(",
"smallest_eigvec",
",",
"(",
"matrix_dim",
",",
"1",
")",
")",
"return",
"smallest_eigval",
",",
"smallest_eigvec"
] | Computes smallest eigenvector and eigenvalue using Lanczos in pure TF.
This function computes smallest eigenvector and eigenvalue of the matrix
which is implicitly specified by `vector_prod_fn`.
`vector_prod_fn` is a function which takes `x` and returns a product of matrix
in consideration and `x`.
Computation is done using Lanczos algorithm, see
https://en.wikipedia.org/wiki/Lanczos_algorithm#The_algorithm
Args:
vector_prod_fn: function which takes a vector as an input and returns
matrix vector product.
matrix_dim: dimentionality of the matrix.
initial_vector: guess vector to start the algorithm with
num_iter: user-defined number of iterations for the algorithm
max_iter: maximum number of iterations.
collapse_tol: tolerance to determine collapse of the Krylov subspace
dtype: type of data
Returns:
tuple of (eigenvalue, eigenvector) of smallest eigenvalue and corresponding
eigenvector. | [
"Computes",
"smallest",
"eigenvector",
"and",
"eigenvalue",
"using",
"Lanczos",
"in",
"pure",
"TF",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/experimental/certification/utils.py#L184-L278 |
28,634 | tensorflow/cleverhans | cleverhans/attacks/carlini_wagner_l2.py | CarliniWagnerL2.generate | def generate(self, x, **kwargs):
"""
Return a tensor that constructs adversarial examples for the given
input. Generate uses tf.py_func in order to operate over tensors.
:param x: A tensor with the inputs.
:param kwargs: See `parse_params`
"""
assert self.sess is not None, \
'Cannot use `generate` when no `sess` was provided'
self.parse_params(**kwargs)
labels, nb_classes = self.get_or_guess_labels(x, kwargs)
attack = CWL2(self.sess, self.model, self.batch_size, self.confidence,
'y_target' in kwargs, self.learning_rate,
self.binary_search_steps, self.max_iterations,
self.abort_early, self.initial_const, self.clip_min,
self.clip_max, nb_classes,
x.get_shape().as_list()[1:])
def cw_wrap(x_val, y_val):
return np.array(attack.attack(x_val, y_val), dtype=self.np_dtype)
wrap = tf.py_func(cw_wrap, [x, labels], self.tf_dtype)
wrap.set_shape(x.get_shape())
return wrap | python | def generate(self, x, **kwargs):
"""
Return a tensor that constructs adversarial examples for the given
input. Generate uses tf.py_func in order to operate over tensors.
:param x: A tensor with the inputs.
:param kwargs: See `parse_params`
"""
assert self.sess is not None, \
'Cannot use `generate` when no `sess` was provided'
self.parse_params(**kwargs)
labels, nb_classes = self.get_or_guess_labels(x, kwargs)
attack = CWL2(self.sess, self.model, self.batch_size, self.confidence,
'y_target' in kwargs, self.learning_rate,
self.binary_search_steps, self.max_iterations,
self.abort_early, self.initial_const, self.clip_min,
self.clip_max, nb_classes,
x.get_shape().as_list()[1:])
def cw_wrap(x_val, y_val):
return np.array(attack.attack(x_val, y_val), dtype=self.np_dtype)
wrap = tf.py_func(cw_wrap, [x, labels], self.tf_dtype)
wrap.set_shape(x.get_shape())
return wrap | [
"def",
"generate",
"(",
"self",
",",
"x",
",",
"*",
"*",
"kwargs",
")",
":",
"assert",
"self",
".",
"sess",
"is",
"not",
"None",
",",
"'Cannot use `generate` when no `sess` was provided'",
"self",
".",
"parse_params",
"(",
"*",
"*",
"kwargs",
")",
"labels",
",",
"nb_classes",
"=",
"self",
".",
"get_or_guess_labels",
"(",
"x",
",",
"kwargs",
")",
"attack",
"=",
"CWL2",
"(",
"self",
".",
"sess",
",",
"self",
".",
"model",
",",
"self",
".",
"batch_size",
",",
"self",
".",
"confidence",
",",
"'y_target'",
"in",
"kwargs",
",",
"self",
".",
"learning_rate",
",",
"self",
".",
"binary_search_steps",
",",
"self",
".",
"max_iterations",
",",
"self",
".",
"abort_early",
",",
"self",
".",
"initial_const",
",",
"self",
".",
"clip_min",
",",
"self",
".",
"clip_max",
",",
"nb_classes",
",",
"x",
".",
"get_shape",
"(",
")",
".",
"as_list",
"(",
")",
"[",
"1",
":",
"]",
")",
"def",
"cw_wrap",
"(",
"x_val",
",",
"y_val",
")",
":",
"return",
"np",
".",
"array",
"(",
"attack",
".",
"attack",
"(",
"x_val",
",",
"y_val",
")",
",",
"dtype",
"=",
"self",
".",
"np_dtype",
")",
"wrap",
"=",
"tf",
".",
"py_func",
"(",
"cw_wrap",
",",
"[",
"x",
",",
"labels",
"]",
",",
"self",
".",
"tf_dtype",
")",
"wrap",
".",
"set_shape",
"(",
"x",
".",
"get_shape",
"(",
")",
")",
"return",
"wrap"
] | Return a tensor that constructs adversarial examples for the given
input. Generate uses tf.py_func in order to operate over tensors.
:param x: A tensor with the inputs.
:param kwargs: See `parse_params` | [
"Return",
"a",
"tensor",
"that",
"constructs",
"adversarial",
"examples",
"for",
"the",
"given",
"input",
".",
"Generate",
"uses",
"tf",
".",
"py_func",
"in",
"order",
"to",
"operate",
"over",
"tensors",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/carlini_wagner_l2.py#L58-L85 |
28,635 | tensorflow/cleverhans | cleverhans/attacks/carlini_wagner_l2.py | CWL2.attack | def attack(self, imgs, targets):
"""
Perform the L_2 attack on the given instance for the given targets.
If self.targeted is true, then the targets represents the target labels
If self.targeted is false, then targets are the original class labels
"""
r = []
for i in range(0, len(imgs), self.batch_size):
_logger.debug(
("Running CWL2 attack on instance %s of %s", i, len(imgs)))
r.extend(
self.attack_batch(imgs[i:i + self.batch_size],
targets[i:i + self.batch_size]))
return np.array(r) | python | def attack(self, imgs, targets):
"""
Perform the L_2 attack on the given instance for the given targets.
If self.targeted is true, then the targets represents the target labels
If self.targeted is false, then targets are the original class labels
"""
r = []
for i in range(0, len(imgs), self.batch_size):
_logger.debug(
("Running CWL2 attack on instance %s of %s", i, len(imgs)))
r.extend(
self.attack_batch(imgs[i:i + self.batch_size],
targets[i:i + self.batch_size]))
return np.array(r) | [
"def",
"attack",
"(",
"self",
",",
"imgs",
",",
"targets",
")",
":",
"r",
"=",
"[",
"]",
"for",
"i",
"in",
"range",
"(",
"0",
",",
"len",
"(",
"imgs",
")",
",",
"self",
".",
"batch_size",
")",
":",
"_logger",
".",
"debug",
"(",
"(",
"\"Running CWL2 attack on instance %s of %s\"",
",",
"i",
",",
"len",
"(",
"imgs",
")",
")",
")",
"r",
".",
"extend",
"(",
"self",
".",
"attack_batch",
"(",
"imgs",
"[",
"i",
":",
"i",
"+",
"self",
".",
"batch_size",
"]",
",",
"targets",
"[",
"i",
":",
"i",
"+",
"self",
".",
"batch_size",
"]",
")",
")",
"return",
"np",
".",
"array",
"(",
"r",
")"
] | Perform the L_2 attack on the given instance for the given targets.
If self.targeted is true, then the targets represents the target labels
If self.targeted is false, then targets are the original class labels | [
"Perform",
"the",
"L_2",
"attack",
"on",
"the",
"given",
"instance",
"for",
"the",
"given",
"targets",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/attacks/carlini_wagner_l2.py#L276-L291 |
28,636 | tensorflow/cleverhans | examples/RL-attack/train.py | maybe_load_model | def maybe_load_model(savedir, container):
"""Load model if present at the specified path."""
if savedir is None:
return
state_path = os.path.join(os.path.join(savedir, 'training_state.pkl.zip'))
if container is not None:
logger.log("Attempting to download model from Azure")
found_model = container.get(savedir, 'training_state.pkl.zip')
else:
found_model = os.path.exists(state_path)
if found_model:
state = pickle_load(state_path, compression=True)
model_dir = "model-{}".format(state["num_iters"])
if container is not None:
container.get(savedir, model_dir)
U.load_state(os.path.join(savedir, model_dir, "saved"))
logger.log("Loaded models checkpoint at {} iterations".format(
state["num_iters"]))
return state | python | def maybe_load_model(savedir, container):
"""Load model if present at the specified path."""
if savedir is None:
return
state_path = os.path.join(os.path.join(savedir, 'training_state.pkl.zip'))
if container is not None:
logger.log("Attempting to download model from Azure")
found_model = container.get(savedir, 'training_state.pkl.zip')
else:
found_model = os.path.exists(state_path)
if found_model:
state = pickle_load(state_path, compression=True)
model_dir = "model-{}".format(state["num_iters"])
if container is not None:
container.get(savedir, model_dir)
U.load_state(os.path.join(savedir, model_dir, "saved"))
logger.log("Loaded models checkpoint at {} iterations".format(
state["num_iters"]))
return state | [
"def",
"maybe_load_model",
"(",
"savedir",
",",
"container",
")",
":",
"if",
"savedir",
"is",
"None",
":",
"return",
"state_path",
"=",
"os",
".",
"path",
".",
"join",
"(",
"os",
".",
"path",
".",
"join",
"(",
"savedir",
",",
"'training_state.pkl.zip'",
")",
")",
"if",
"container",
"is",
"not",
"None",
":",
"logger",
".",
"log",
"(",
"\"Attempting to download model from Azure\"",
")",
"found_model",
"=",
"container",
".",
"get",
"(",
"savedir",
",",
"'training_state.pkl.zip'",
")",
"else",
":",
"found_model",
"=",
"os",
".",
"path",
".",
"exists",
"(",
"state_path",
")",
"if",
"found_model",
":",
"state",
"=",
"pickle_load",
"(",
"state_path",
",",
"compression",
"=",
"True",
")",
"model_dir",
"=",
"\"model-{}\"",
".",
"format",
"(",
"state",
"[",
"\"num_iters\"",
"]",
")",
"if",
"container",
"is",
"not",
"None",
":",
"container",
".",
"get",
"(",
"savedir",
",",
"model_dir",
")",
"U",
".",
"load_state",
"(",
"os",
".",
"path",
".",
"join",
"(",
"savedir",
",",
"model_dir",
",",
"\"saved\"",
")",
")",
"logger",
".",
"log",
"(",
"\"Loaded models checkpoint at {} iterations\"",
".",
"format",
"(",
"state",
"[",
"\"num_iters\"",
"]",
")",
")",
"return",
"state"
] | Load model if present at the specified path. | [
"Load",
"model",
"if",
"present",
"at",
"the",
"specified",
"path",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/RL-attack/train.py#L130-L149 |
28,637 | tensorflow/cleverhans | cleverhans_tutorials/__init__.py | check_installation | def check_installation(cur_file):
"""Warn user if running cleverhans from a different directory than tutorial."""
cur_dir = os.path.split(os.path.dirname(os.path.abspath(cur_file)))[0]
ch_dir = os.path.split(cleverhans.__path__[0])[0]
if cur_dir != ch_dir:
warnings.warn("It appears that you have at least two versions of "
"cleverhans installed, one at %s and one at"
" %s. You are running the tutorial script from the "
"former but python imported the library module from the "
"latter. This may cause errors, for example if the tutorial"
" version is newer than the library version and attempts to"
" call new features." % (cur_dir, ch_dir)) | python | def check_installation(cur_file):
"""Warn user if running cleverhans from a different directory than tutorial."""
cur_dir = os.path.split(os.path.dirname(os.path.abspath(cur_file)))[0]
ch_dir = os.path.split(cleverhans.__path__[0])[0]
if cur_dir != ch_dir:
warnings.warn("It appears that you have at least two versions of "
"cleverhans installed, one at %s and one at"
" %s. You are running the tutorial script from the "
"former but python imported the library module from the "
"latter. This may cause errors, for example if the tutorial"
" version is newer than the library version and attempts to"
" call new features." % (cur_dir, ch_dir)) | [
"def",
"check_installation",
"(",
"cur_file",
")",
":",
"cur_dir",
"=",
"os",
".",
"path",
".",
"split",
"(",
"os",
".",
"path",
".",
"dirname",
"(",
"os",
".",
"path",
".",
"abspath",
"(",
"cur_file",
")",
")",
")",
"[",
"0",
"]",
"ch_dir",
"=",
"os",
".",
"path",
".",
"split",
"(",
"cleverhans",
".",
"__path__",
"[",
"0",
"]",
")",
"[",
"0",
"]",
"if",
"cur_dir",
"!=",
"ch_dir",
":",
"warnings",
".",
"warn",
"(",
"\"It appears that you have at least two versions of \"",
"\"cleverhans installed, one at %s and one at\"",
"\" %s. You are running the tutorial script from the \"",
"\"former but python imported the library module from the \"",
"\"latter. This may cause errors, for example if the tutorial\"",
"\" version is newer than the library version and attempts to\"",
"\" call new features.\"",
"%",
"(",
"cur_dir",
",",
"ch_dir",
")",
")"
] | Warn user if running cleverhans from a different directory than tutorial. | [
"Warn",
"user",
"if",
"running",
"cleverhans",
"from",
"a",
"different",
"directory",
"than",
"tutorial",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans_tutorials/__init__.py#L13-L24 |
28,638 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dataset/download_images.py | get_image | def get_image(row, output_dir):
"""Downloads the image that corresponds to the given row.
Prints a notification if the download fails."""
if not download_image(image_id=row[0],
url=row[1],
x1=float(row[2]),
y1=float(row[3]),
x2=float(row[4]),
y2=float(row[5]),
output_dir=output_dir):
print("Download failed: " + str(row[0])) | python | def get_image(row, output_dir):
"""Downloads the image that corresponds to the given row.
Prints a notification if the download fails."""
if not download_image(image_id=row[0],
url=row[1],
x1=float(row[2]),
y1=float(row[3]),
x2=float(row[4]),
y2=float(row[5]),
output_dir=output_dir):
print("Download failed: " + str(row[0])) | [
"def",
"get_image",
"(",
"row",
",",
"output_dir",
")",
":",
"if",
"not",
"download_image",
"(",
"image_id",
"=",
"row",
"[",
"0",
"]",
",",
"url",
"=",
"row",
"[",
"1",
"]",
",",
"x1",
"=",
"float",
"(",
"row",
"[",
"2",
"]",
")",
",",
"y1",
"=",
"float",
"(",
"row",
"[",
"3",
"]",
")",
",",
"x2",
"=",
"float",
"(",
"row",
"[",
"4",
"]",
")",
",",
"y2",
"=",
"float",
"(",
"row",
"[",
"5",
"]",
")",
",",
"output_dir",
"=",
"output_dir",
")",
":",
"print",
"(",
"\"Download failed: \"",
"+",
"str",
"(",
"row",
"[",
"0",
"]",
")",
")"
] | Downloads the image that corresponds to the given row.
Prints a notification if the download fails. | [
"Downloads",
"the",
"image",
"that",
"corresponds",
"to",
"the",
"given",
"row",
".",
"Prints",
"a",
"notification",
"if",
"the",
"download",
"fails",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dataset/download_images.py#L57-L67 |
28,639 | tensorflow/cleverhans | examples/nips17_adversarial_competition/dataset/download_images.py | download_image | def download_image(image_id, url, x1, y1, x2, y2, output_dir):
"""Downloads one image, crops it, resizes it and saves it locally."""
output_filename = os.path.join(output_dir, image_id + '.png')
if os.path.exists(output_filename):
# Don't download image if it's already there
return True
try:
# Download image
url_file = urlopen(url)
if url_file.getcode() != 200:
return False
image_buffer = url_file.read()
# Crop, resize and save image
image = Image.open(BytesIO(image_buffer)).convert('RGB')
w = image.size[0]
h = image.size[1]
image = image.crop((int(x1 * w), int(y1 * h), int(x2 * w),
int(y2 * h)))
image = image.resize((299, 299), resample=Image.ANTIALIAS)
image.save(output_filename)
except IOError:
return False
return True | python | def download_image(image_id, url, x1, y1, x2, y2, output_dir):
"""Downloads one image, crops it, resizes it and saves it locally."""
output_filename = os.path.join(output_dir, image_id + '.png')
if os.path.exists(output_filename):
# Don't download image if it's already there
return True
try:
# Download image
url_file = urlopen(url)
if url_file.getcode() != 200:
return False
image_buffer = url_file.read()
# Crop, resize and save image
image = Image.open(BytesIO(image_buffer)).convert('RGB')
w = image.size[0]
h = image.size[1]
image = image.crop((int(x1 * w), int(y1 * h), int(x2 * w),
int(y2 * h)))
image = image.resize((299, 299), resample=Image.ANTIALIAS)
image.save(output_filename)
except IOError:
return False
return True | [
"def",
"download_image",
"(",
"image_id",
",",
"url",
",",
"x1",
",",
"y1",
",",
"x2",
",",
"y2",
",",
"output_dir",
")",
":",
"output_filename",
"=",
"os",
".",
"path",
".",
"join",
"(",
"output_dir",
",",
"image_id",
"+",
"'.png'",
")",
"if",
"os",
".",
"path",
".",
"exists",
"(",
"output_filename",
")",
":",
"# Don't download image if it's already there",
"return",
"True",
"try",
":",
"# Download image",
"url_file",
"=",
"urlopen",
"(",
"url",
")",
"if",
"url_file",
".",
"getcode",
"(",
")",
"!=",
"200",
":",
"return",
"False",
"image_buffer",
"=",
"url_file",
".",
"read",
"(",
")",
"# Crop, resize and save image",
"image",
"=",
"Image",
".",
"open",
"(",
"BytesIO",
"(",
"image_buffer",
")",
")",
".",
"convert",
"(",
"'RGB'",
")",
"w",
"=",
"image",
".",
"size",
"[",
"0",
"]",
"h",
"=",
"image",
".",
"size",
"[",
"1",
"]",
"image",
"=",
"image",
".",
"crop",
"(",
"(",
"int",
"(",
"x1",
"*",
"w",
")",
",",
"int",
"(",
"y1",
"*",
"h",
")",
",",
"int",
"(",
"x2",
"*",
"w",
")",
",",
"int",
"(",
"y2",
"*",
"h",
")",
")",
")",
"image",
"=",
"image",
".",
"resize",
"(",
"(",
"299",
",",
"299",
")",
",",
"resample",
"=",
"Image",
".",
"ANTIALIAS",
")",
"image",
".",
"save",
"(",
"output_filename",
")",
"except",
"IOError",
":",
"return",
"False",
"return",
"True"
] | Downloads one image, crops it, resizes it and saves it locally. | [
"Downloads",
"one",
"image",
"crops",
"it",
"resizes",
"it",
"and",
"saves",
"it",
"locally",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/nips17_adversarial_competition/dataset/download_images.py#L70-L92 |
28,640 | tensorflow/cleverhans | examples/robust_vision_benchmark/cleverhans_attack_example/utils.py | py_func_grad | def py_func_grad(func, inp, Tout, stateful=True, name=None, grad=None):
"""Custom py_func with gradient support
"""
# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
tf.RegisterGradient(rnd_name)(grad)
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name,
"PyFuncStateless": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name) | python | def py_func_grad(func, inp, Tout, stateful=True, name=None, grad=None):
"""Custom py_func with gradient support
"""
# Need to generate a unique name to avoid duplicates:
rnd_name = 'PyFuncGrad' + str(np.random.randint(0, 1E+8))
tf.RegisterGradient(rnd_name)(grad)
g = tf.get_default_graph()
with g.gradient_override_map({"PyFunc": rnd_name,
"PyFuncStateless": rnd_name}):
return tf.py_func(func, inp, Tout, stateful=stateful, name=name) | [
"def",
"py_func_grad",
"(",
"func",
",",
"inp",
",",
"Tout",
",",
"stateful",
"=",
"True",
",",
"name",
"=",
"None",
",",
"grad",
"=",
"None",
")",
":",
"# Need to generate a unique name to avoid duplicates:",
"rnd_name",
"=",
"'PyFuncGrad'",
"+",
"str",
"(",
"np",
".",
"random",
".",
"randint",
"(",
"0",
",",
"1E+8",
")",
")",
"tf",
".",
"RegisterGradient",
"(",
"rnd_name",
")",
"(",
"grad",
")",
"g",
"=",
"tf",
".",
"get_default_graph",
"(",
")",
"with",
"g",
".",
"gradient_override_map",
"(",
"{",
"\"PyFunc\"",
":",
"rnd_name",
",",
"\"PyFuncStateless\"",
":",
"rnd_name",
"}",
")",
":",
"return",
"tf",
".",
"py_func",
"(",
"func",
",",
"inp",
",",
"Tout",
",",
"stateful",
"=",
"stateful",
",",
"name",
"=",
"name",
")"
] | Custom py_func with gradient support | [
"Custom",
"py_func",
"with",
"gradient",
"support"
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/examples/robust_vision_benchmark/cleverhans_attack_example/utils.py#L25-L36 |
28,641 | tensorflow/cleverhans | cleverhans/plot/pyplot_image.py | get_logits_over_interval | def get_logits_over_interval(sess, model, x_data, fgsm_params,
min_epsilon=-10., max_epsilon=10.,
num_points=21):
"""Get logits when the input is perturbed in an interval in adv direction.
Args:
sess: Tf session
model: Model for which we wish to get logits.
x_data: Numpy array corresponding to single data.
point of shape [height, width, channels].
fgsm_params: Parameters for generating adversarial examples.
min_epsilon: Minimum value of epsilon over the interval.
max_epsilon: Maximum value of epsilon over the interval.
num_points: Number of points used to interpolate.
Returns:
Numpy array containing logits.
Raises:
ValueError if min_epsilon is larger than max_epsilon.
"""
# Get the height, width and number of channels
height = x_data.shape[0]
width = x_data.shape[1]
channels = x_data.shape[2]
x_data = np.expand_dims(x_data, axis=0)
import tensorflow as tf
from cleverhans.attacks import FastGradientMethod
# Define the data placeholder
x = tf.placeholder(dtype=tf.float32,
shape=[1, height,
width,
channels],
name='x')
# Define adv_x
fgsm = FastGradientMethod(model, sess=sess)
adv_x = fgsm.generate(x, **fgsm_params)
if min_epsilon > max_epsilon:
raise ValueError('Minimum epsilon is less than maximum epsilon')
eta = tf.nn.l2_normalize(adv_x - x, dim=0)
epsilon = tf.reshape(tf.lin_space(float(min_epsilon),
float(max_epsilon),
num_points),
(num_points, 1, 1, 1))
lin_batch = x + epsilon * eta
logits = model.get_logits(lin_batch)
with sess.as_default():
log_prob_adv_array = sess.run(logits,
feed_dict={x: x_data})
return log_prob_adv_array | python | def get_logits_over_interval(sess, model, x_data, fgsm_params,
min_epsilon=-10., max_epsilon=10.,
num_points=21):
"""Get logits when the input is perturbed in an interval in adv direction.
Args:
sess: Tf session
model: Model for which we wish to get logits.
x_data: Numpy array corresponding to single data.
point of shape [height, width, channels].
fgsm_params: Parameters for generating adversarial examples.
min_epsilon: Minimum value of epsilon over the interval.
max_epsilon: Maximum value of epsilon over the interval.
num_points: Number of points used to interpolate.
Returns:
Numpy array containing logits.
Raises:
ValueError if min_epsilon is larger than max_epsilon.
"""
# Get the height, width and number of channels
height = x_data.shape[0]
width = x_data.shape[1]
channels = x_data.shape[2]
x_data = np.expand_dims(x_data, axis=0)
import tensorflow as tf
from cleverhans.attacks import FastGradientMethod
# Define the data placeholder
x = tf.placeholder(dtype=tf.float32,
shape=[1, height,
width,
channels],
name='x')
# Define adv_x
fgsm = FastGradientMethod(model, sess=sess)
adv_x = fgsm.generate(x, **fgsm_params)
if min_epsilon > max_epsilon:
raise ValueError('Minimum epsilon is less than maximum epsilon')
eta = tf.nn.l2_normalize(adv_x - x, dim=0)
epsilon = tf.reshape(tf.lin_space(float(min_epsilon),
float(max_epsilon),
num_points),
(num_points, 1, 1, 1))
lin_batch = x + epsilon * eta
logits = model.get_logits(lin_batch)
with sess.as_default():
log_prob_adv_array = sess.run(logits,
feed_dict={x: x_data})
return log_prob_adv_array | [
"def",
"get_logits_over_interval",
"(",
"sess",
",",
"model",
",",
"x_data",
",",
"fgsm_params",
",",
"min_epsilon",
"=",
"-",
"10.",
",",
"max_epsilon",
"=",
"10.",
",",
"num_points",
"=",
"21",
")",
":",
"# Get the height, width and number of channels",
"height",
"=",
"x_data",
".",
"shape",
"[",
"0",
"]",
"width",
"=",
"x_data",
".",
"shape",
"[",
"1",
"]",
"channels",
"=",
"x_data",
".",
"shape",
"[",
"2",
"]",
"x_data",
"=",
"np",
".",
"expand_dims",
"(",
"x_data",
",",
"axis",
"=",
"0",
")",
"import",
"tensorflow",
"as",
"tf",
"from",
"cleverhans",
".",
"attacks",
"import",
"FastGradientMethod",
"# Define the data placeholder",
"x",
"=",
"tf",
".",
"placeholder",
"(",
"dtype",
"=",
"tf",
".",
"float32",
",",
"shape",
"=",
"[",
"1",
",",
"height",
",",
"width",
",",
"channels",
"]",
",",
"name",
"=",
"'x'",
")",
"# Define adv_x",
"fgsm",
"=",
"FastGradientMethod",
"(",
"model",
",",
"sess",
"=",
"sess",
")",
"adv_x",
"=",
"fgsm",
".",
"generate",
"(",
"x",
",",
"*",
"*",
"fgsm_params",
")",
"if",
"min_epsilon",
">",
"max_epsilon",
":",
"raise",
"ValueError",
"(",
"'Minimum epsilon is less than maximum epsilon'",
")",
"eta",
"=",
"tf",
".",
"nn",
".",
"l2_normalize",
"(",
"adv_x",
"-",
"x",
",",
"dim",
"=",
"0",
")",
"epsilon",
"=",
"tf",
".",
"reshape",
"(",
"tf",
".",
"lin_space",
"(",
"float",
"(",
"min_epsilon",
")",
",",
"float",
"(",
"max_epsilon",
")",
",",
"num_points",
")",
",",
"(",
"num_points",
",",
"1",
",",
"1",
",",
"1",
")",
")",
"lin_batch",
"=",
"x",
"+",
"epsilon",
"*",
"eta",
"logits",
"=",
"model",
".",
"get_logits",
"(",
"lin_batch",
")",
"with",
"sess",
".",
"as_default",
"(",
")",
":",
"log_prob_adv_array",
"=",
"sess",
".",
"run",
"(",
"logits",
",",
"feed_dict",
"=",
"{",
"x",
":",
"x_data",
"}",
")",
"return",
"log_prob_adv_array"
] | Get logits when the input is perturbed in an interval in adv direction.
Args:
sess: Tf session
model: Model for which we wish to get logits.
x_data: Numpy array corresponding to single data.
point of shape [height, width, channels].
fgsm_params: Parameters for generating adversarial examples.
min_epsilon: Minimum value of epsilon over the interval.
max_epsilon: Maximum value of epsilon over the interval.
num_points: Number of points used to interpolate.
Returns:
Numpy array containing logits.
Raises:
ValueError if min_epsilon is larger than max_epsilon. | [
"Get",
"logits",
"when",
"the",
"input",
"is",
"perturbed",
"in",
"an",
"interval",
"in",
"adv",
"direction",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/plot/pyplot_image.py#L84-L137 |
28,642 | tensorflow/cleverhans | cleverhans/plot/pyplot_image.py | linear_extrapolation_plot | def linear_extrapolation_plot(log_prob_adv_array, y, file_name,
min_epsilon=-10, max_epsilon=10,
num_points=21):
"""Generate linear extrapolation plot.
Args:
log_prob_adv_array: Numpy array containing log probabilities
y: Tf placeholder for the labels
file_name: Plot filename
min_epsilon: Minimum value of epsilon over the interval
max_epsilon: Maximum value of epsilon over the interval
num_points: Number of points used to interpolate
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
figure = plt.figure()
figure.canvas.set_window_title('Cleverhans: Linear Extrapolation Plot')
correct_idx = np.argmax(y, axis=0)
fig = plt.figure()
plt.xlabel('Epsilon')
plt.ylabel('Logits')
x_axis = np.linspace(min_epsilon, max_epsilon, num_points)
plt.xlim(min_epsilon - 1, max_epsilon + 1)
for i in range(y.shape[0]):
if i == correct_idx:
ls = '-'
linewidth = 5
else:
ls = '--'
linewidth = 2
plt.plot(
x_axis,
log_prob_adv_array[:, i],
ls=ls,
linewidth=linewidth,
label='{}'.format(i))
plt.legend(loc='best', fontsize=14)
plt.show()
fig.savefig(file_name)
plt.clf()
return figure | python | def linear_extrapolation_plot(log_prob_adv_array, y, file_name,
min_epsilon=-10, max_epsilon=10,
num_points=21):
"""Generate linear extrapolation plot.
Args:
log_prob_adv_array: Numpy array containing log probabilities
y: Tf placeholder for the labels
file_name: Plot filename
min_epsilon: Minimum value of epsilon over the interval
max_epsilon: Maximum value of epsilon over the interval
num_points: Number of points used to interpolate
"""
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
figure = plt.figure()
figure.canvas.set_window_title('Cleverhans: Linear Extrapolation Plot')
correct_idx = np.argmax(y, axis=0)
fig = plt.figure()
plt.xlabel('Epsilon')
plt.ylabel('Logits')
x_axis = np.linspace(min_epsilon, max_epsilon, num_points)
plt.xlim(min_epsilon - 1, max_epsilon + 1)
for i in range(y.shape[0]):
if i == correct_idx:
ls = '-'
linewidth = 5
else:
ls = '--'
linewidth = 2
plt.plot(
x_axis,
log_prob_adv_array[:, i],
ls=ls,
linewidth=linewidth,
label='{}'.format(i))
plt.legend(loc='best', fontsize=14)
plt.show()
fig.savefig(file_name)
plt.clf()
return figure | [
"def",
"linear_extrapolation_plot",
"(",
"log_prob_adv_array",
",",
"y",
",",
"file_name",
",",
"min_epsilon",
"=",
"-",
"10",
",",
"max_epsilon",
"=",
"10",
",",
"num_points",
"=",
"21",
")",
":",
"import",
"matplotlib",
"matplotlib",
".",
"use",
"(",
"'Agg'",
")",
"import",
"matplotlib",
".",
"pyplot",
"as",
"plt",
"figure",
"=",
"plt",
".",
"figure",
"(",
")",
"figure",
".",
"canvas",
".",
"set_window_title",
"(",
"'Cleverhans: Linear Extrapolation Plot'",
")",
"correct_idx",
"=",
"np",
".",
"argmax",
"(",
"y",
",",
"axis",
"=",
"0",
")",
"fig",
"=",
"plt",
".",
"figure",
"(",
")",
"plt",
".",
"xlabel",
"(",
"'Epsilon'",
")",
"plt",
".",
"ylabel",
"(",
"'Logits'",
")",
"x_axis",
"=",
"np",
".",
"linspace",
"(",
"min_epsilon",
",",
"max_epsilon",
",",
"num_points",
")",
"plt",
".",
"xlim",
"(",
"min_epsilon",
"-",
"1",
",",
"max_epsilon",
"+",
"1",
")",
"for",
"i",
"in",
"range",
"(",
"y",
".",
"shape",
"[",
"0",
"]",
")",
":",
"if",
"i",
"==",
"correct_idx",
":",
"ls",
"=",
"'-'",
"linewidth",
"=",
"5",
"else",
":",
"ls",
"=",
"'--'",
"linewidth",
"=",
"2",
"plt",
".",
"plot",
"(",
"x_axis",
",",
"log_prob_adv_array",
"[",
":",
",",
"i",
"]",
",",
"ls",
"=",
"ls",
",",
"linewidth",
"=",
"linewidth",
",",
"label",
"=",
"'{}'",
".",
"format",
"(",
"i",
")",
")",
"plt",
".",
"legend",
"(",
"loc",
"=",
"'best'",
",",
"fontsize",
"=",
"14",
")",
"plt",
".",
"show",
"(",
")",
"fig",
".",
"savefig",
"(",
"file_name",
")",
"plt",
".",
"clf",
"(",
")",
"return",
"figure"
] | Generate linear extrapolation plot.
Args:
log_prob_adv_array: Numpy array containing log probabilities
y: Tf placeholder for the labels
file_name: Plot filename
min_epsilon: Minimum value of epsilon over the interval
max_epsilon: Maximum value of epsilon over the interval
num_points: Number of points used to interpolate | [
"Generate",
"linear",
"extrapolation",
"plot",
"."
] | 97488e215760547b81afc53f5e5de8ba7da5bd98 | https://github.com/tensorflow/cleverhans/blob/97488e215760547b81afc53f5e5de8ba7da5bd98/cleverhans/plot/pyplot_image.py#L139-L182 |
28,643 | backtrader/backtrader | contrib/utils/iqfeed-to-influxdb.py | IQFeedTool._send_cmd | def _send_cmd(self, cmd: str):
"""Encode IQFeed API messages."""
self._sock.sendall(cmd.encode(encoding='latin-1', errors='strict')) | python | def _send_cmd(self, cmd: str):
"""Encode IQFeed API messages."""
self._sock.sendall(cmd.encode(encoding='latin-1', errors='strict')) | [
"def",
"_send_cmd",
"(",
"self",
",",
"cmd",
":",
"str",
")",
":",
"self",
".",
"_sock",
".",
"sendall",
"(",
"cmd",
".",
"encode",
"(",
"encoding",
"=",
"'latin-1'",
",",
"errors",
"=",
"'strict'",
")",
")"
] | Encode IQFeed API messages. | [
"Encode",
"IQFeed",
"API",
"messages",
"."
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/iqfeed-to-influxdb.py#L59-L61 |
28,644 | backtrader/backtrader | contrib/utils/iqfeed-to-influxdb.py | IQFeedTool.iq_query | def iq_query(self, message: str):
"""Send data query to IQFeed API."""
end_msg = '!ENDMSG!'
recv_buffer = 4096
# Send the historical data request message and buffer the data
self._send_cmd(message)
chunk = ""
data = ""
while True:
chunk = self._sock.recv(recv_buffer).decode('latin-1')
data += chunk
if chunk.startswith('E,'): # error condition
if chunk.startswith('E,!NO_DATA!'):
log.warn('No data available for the given symbol or dates')
return
else:
raise Exception(chunk)
elif end_msg in chunk:
break
# Clean up the data.
data = data[:-1 * (len(end_msg) + 3)]
data = "".join(data.split("\r"))
data = data.replace(",\n", ",")[:-1]
data = data.split(",")
return data | python | def iq_query(self, message: str):
"""Send data query to IQFeed API."""
end_msg = '!ENDMSG!'
recv_buffer = 4096
# Send the historical data request message and buffer the data
self._send_cmd(message)
chunk = ""
data = ""
while True:
chunk = self._sock.recv(recv_buffer).decode('latin-1')
data += chunk
if chunk.startswith('E,'): # error condition
if chunk.startswith('E,!NO_DATA!'):
log.warn('No data available for the given symbol or dates')
return
else:
raise Exception(chunk)
elif end_msg in chunk:
break
# Clean up the data.
data = data[:-1 * (len(end_msg) + 3)]
data = "".join(data.split("\r"))
data = data.replace(",\n", ",")[:-1]
data = data.split(",")
return data | [
"def",
"iq_query",
"(",
"self",
",",
"message",
":",
"str",
")",
":",
"end_msg",
"=",
"'!ENDMSG!'",
"recv_buffer",
"=",
"4096",
"# Send the historical data request message and buffer the data",
"self",
".",
"_send_cmd",
"(",
"message",
")",
"chunk",
"=",
"\"\"",
"data",
"=",
"\"\"",
"while",
"True",
":",
"chunk",
"=",
"self",
".",
"_sock",
".",
"recv",
"(",
"recv_buffer",
")",
".",
"decode",
"(",
"'latin-1'",
")",
"data",
"+=",
"chunk",
"if",
"chunk",
".",
"startswith",
"(",
"'E,'",
")",
":",
"# error condition",
"if",
"chunk",
".",
"startswith",
"(",
"'E,!NO_DATA!'",
")",
":",
"log",
".",
"warn",
"(",
"'No data available for the given symbol or dates'",
")",
"return",
"else",
":",
"raise",
"Exception",
"(",
"chunk",
")",
"elif",
"end_msg",
"in",
"chunk",
":",
"break",
"# Clean up the data.",
"data",
"=",
"data",
"[",
":",
"-",
"1",
"*",
"(",
"len",
"(",
"end_msg",
")",
"+",
"3",
")",
"]",
"data",
"=",
"\"\"",
".",
"join",
"(",
"data",
".",
"split",
"(",
"\"\\r\"",
")",
")",
"data",
"=",
"data",
".",
"replace",
"(",
"\",\\n\"",
",",
"\",\"",
")",
"[",
":",
"-",
"1",
"]",
"data",
"=",
"data",
".",
"split",
"(",
"\",\"",
")",
"return",
"data"
] | Send data query to IQFeed API. | [
"Send",
"data",
"query",
"to",
"IQFeed",
"API",
"."
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/iqfeed-to-influxdb.py#L63-L90 |
28,645 | backtrader/backtrader | contrib/utils/iqfeed-to-influxdb.py | IQFeedTool.get_historical_minute_data | def get_historical_minute_data(self, ticker: str):
"""Request historical 5 minute data from DTN."""
start = self._start
stop = self._stop
if len(stop) > 4:
stop = stop[:4]
if len(start) > 4:
start = start[:4]
for year in range(int(start), int(stop) + 1):
beg_time = ('%s0101000000' % year)
end_time = ('%s1231235959' % year)
msg = "HIT,%s,60,%s,%s,,,,1,,,s\r\n" % (ticker,
beg_time,
end_time)
try:
data = iq.iq_query(message=msg)
iq.add_data_to_df(data=data)
except Exception as err:
log.error('No data returned because %s', err)
try:
self.dfdb.write_points(self._ndf, ticker)
except InfluxDBClientError as err:
log.error('Write to database failed: %s' % err) | python | def get_historical_minute_data(self, ticker: str):
"""Request historical 5 minute data from DTN."""
start = self._start
stop = self._stop
if len(stop) > 4:
stop = stop[:4]
if len(start) > 4:
start = start[:4]
for year in range(int(start), int(stop) + 1):
beg_time = ('%s0101000000' % year)
end_time = ('%s1231235959' % year)
msg = "HIT,%s,60,%s,%s,,,,1,,,s\r\n" % (ticker,
beg_time,
end_time)
try:
data = iq.iq_query(message=msg)
iq.add_data_to_df(data=data)
except Exception as err:
log.error('No data returned because %s', err)
try:
self.dfdb.write_points(self._ndf, ticker)
except InfluxDBClientError as err:
log.error('Write to database failed: %s' % err) | [
"def",
"get_historical_minute_data",
"(",
"self",
",",
"ticker",
":",
"str",
")",
":",
"start",
"=",
"self",
".",
"_start",
"stop",
"=",
"self",
".",
"_stop",
"if",
"len",
"(",
"stop",
")",
">",
"4",
":",
"stop",
"=",
"stop",
"[",
":",
"4",
"]",
"if",
"len",
"(",
"start",
")",
">",
"4",
":",
"start",
"=",
"start",
"[",
":",
"4",
"]",
"for",
"year",
"in",
"range",
"(",
"int",
"(",
"start",
")",
",",
"int",
"(",
"stop",
")",
"+",
"1",
")",
":",
"beg_time",
"=",
"(",
"'%s0101000000'",
"%",
"year",
")",
"end_time",
"=",
"(",
"'%s1231235959'",
"%",
"year",
")",
"msg",
"=",
"\"HIT,%s,60,%s,%s,,,,1,,,s\\r\\n\"",
"%",
"(",
"ticker",
",",
"beg_time",
",",
"end_time",
")",
"try",
":",
"data",
"=",
"iq",
".",
"iq_query",
"(",
"message",
"=",
"msg",
")",
"iq",
".",
"add_data_to_df",
"(",
"data",
"=",
"data",
")",
"except",
"Exception",
"as",
"err",
":",
"log",
".",
"error",
"(",
"'No data returned because %s'",
",",
"err",
")",
"try",
":",
"self",
".",
"dfdb",
".",
"write_points",
"(",
"self",
".",
"_ndf",
",",
"ticker",
")",
"except",
"InfluxDBClientError",
"as",
"err",
":",
"log",
".",
"error",
"(",
"'Write to database failed: %s'",
"%",
"err",
")"
] | Request historical 5 minute data from DTN. | [
"Request",
"historical",
"5",
"minute",
"data",
"from",
"DTN",
"."
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/iqfeed-to-influxdb.py#L92-L118 |
28,646 | backtrader/backtrader | contrib/utils/iqfeed-to-influxdb.py | IQFeedTool.add_data_to_df | def add_data_to_df(self, data: np.array):
"""Build Pandas Dataframe in memory"""
col_names = ['high_p', 'low_p', 'open_p', 'close_p', 'volume', 'oi']
data = np.array(data).reshape(-1, len(col_names) + 1)
df = pd.DataFrame(data=data[:, 1:], index=data[:, 0],
columns=col_names)
df.index = pd.to_datetime(df.index)
# Sort the dataframe based on ascending dates.
df.sort_index(ascending=True, inplace=True)
# Convert dataframe columns to float and ints.
df[['high_p', 'low_p', 'open_p', 'close_p']] = df[
['high_p', 'low_p', 'open_p', 'close_p']].astype(float)
df[['volume', 'oi']] = df[['volume', 'oi']].astype(int)
if self._ndf.empty:
self._ndf = df
else:
self._ndf = self._ndf.append(df) | python | def add_data_to_df(self, data: np.array):
"""Build Pandas Dataframe in memory"""
col_names = ['high_p', 'low_p', 'open_p', 'close_p', 'volume', 'oi']
data = np.array(data).reshape(-1, len(col_names) + 1)
df = pd.DataFrame(data=data[:, 1:], index=data[:, 0],
columns=col_names)
df.index = pd.to_datetime(df.index)
# Sort the dataframe based on ascending dates.
df.sort_index(ascending=True, inplace=True)
# Convert dataframe columns to float and ints.
df[['high_p', 'low_p', 'open_p', 'close_p']] = df[
['high_p', 'low_p', 'open_p', 'close_p']].astype(float)
df[['volume', 'oi']] = df[['volume', 'oi']].astype(int)
if self._ndf.empty:
self._ndf = df
else:
self._ndf = self._ndf.append(df) | [
"def",
"add_data_to_df",
"(",
"self",
",",
"data",
":",
"np",
".",
"array",
")",
":",
"col_names",
"=",
"[",
"'high_p'",
",",
"'low_p'",
",",
"'open_p'",
",",
"'close_p'",
",",
"'volume'",
",",
"'oi'",
"]",
"data",
"=",
"np",
".",
"array",
"(",
"data",
")",
".",
"reshape",
"(",
"-",
"1",
",",
"len",
"(",
"col_names",
")",
"+",
"1",
")",
"df",
"=",
"pd",
".",
"DataFrame",
"(",
"data",
"=",
"data",
"[",
":",
",",
"1",
":",
"]",
",",
"index",
"=",
"data",
"[",
":",
",",
"0",
"]",
",",
"columns",
"=",
"col_names",
")",
"df",
".",
"index",
"=",
"pd",
".",
"to_datetime",
"(",
"df",
".",
"index",
")",
"# Sort the dataframe based on ascending dates.",
"df",
".",
"sort_index",
"(",
"ascending",
"=",
"True",
",",
"inplace",
"=",
"True",
")",
"# Convert dataframe columns to float and ints.",
"df",
"[",
"[",
"'high_p'",
",",
"'low_p'",
",",
"'open_p'",
",",
"'close_p'",
"]",
"]",
"=",
"df",
"[",
"[",
"'high_p'",
",",
"'low_p'",
",",
"'open_p'",
",",
"'close_p'",
"]",
"]",
".",
"astype",
"(",
"float",
")",
"df",
"[",
"[",
"'volume'",
",",
"'oi'",
"]",
"]",
"=",
"df",
"[",
"[",
"'volume'",
",",
"'oi'",
"]",
"]",
".",
"astype",
"(",
"int",
")",
"if",
"self",
".",
"_ndf",
".",
"empty",
":",
"self",
".",
"_ndf",
"=",
"df",
"else",
":",
"self",
".",
"_ndf",
"=",
"self",
".",
"_ndf",
".",
"append",
"(",
"df",
")"
] | Build Pandas Dataframe in memory | [
"Build",
"Pandas",
"Dataframe",
"in",
"memory"
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/iqfeed-to-influxdb.py#L120-L142 |
28,647 | backtrader/backtrader | contrib/utils/iqfeed-to-influxdb.py | IQFeedTool.get_tickers_from_file | def get_tickers_from_file(self, filename):
"""Load ticker list from txt file"""
if not os.path.exists(filename):
log.error("Ticker List file does not exist: %s", filename)
tickers = []
with io.open(filename, 'r') as fd:
for ticker in fd:
tickers.append(ticker.rstrip())
return tickers | python | def get_tickers_from_file(self, filename):
"""Load ticker list from txt file"""
if not os.path.exists(filename):
log.error("Ticker List file does not exist: %s", filename)
tickers = []
with io.open(filename, 'r') as fd:
for ticker in fd:
tickers.append(ticker.rstrip())
return tickers | [
"def",
"get_tickers_from_file",
"(",
"self",
",",
"filename",
")",
":",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"filename",
")",
":",
"log",
".",
"error",
"(",
"\"Ticker List file does not exist: %s\"",
",",
"filename",
")",
"tickers",
"=",
"[",
"]",
"with",
"io",
".",
"open",
"(",
"filename",
",",
"'r'",
")",
"as",
"fd",
":",
"for",
"ticker",
"in",
"fd",
":",
"tickers",
".",
"append",
"(",
"ticker",
".",
"rstrip",
"(",
")",
")",
"return",
"tickers"
] | Load ticker list from txt file | [
"Load",
"ticker",
"list",
"from",
"txt",
"file"
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/iqfeed-to-influxdb.py#L144-L153 |
28,648 | backtrader/backtrader | contrib/utils/influxdb-import.py | InfluxDBTool.write_dataframe_to_idb | def write_dataframe_to_idb(self, ticker):
"""Write Pandas Dataframe to InfluxDB database"""
cachepath = self._cache
cachefile = ('%s/%s-1M.csv.gz' % (cachepath, ticker))
if not os.path.exists(cachefile):
log.warn('Import file does not exist: %s' %
(cachefile))
return
df = pd.read_csv(cachefile, compression='infer', header=0,
infer_datetime_format=True)
df['Datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
df = df.set_index('Datetime')
df = df.drop(['Date', 'Time'], axis=1)
try:
self.dfdb.write_points(df, ticker)
except InfluxDBClientError as err:
log.error('Write to database failed: %s' % err) | python | def write_dataframe_to_idb(self, ticker):
"""Write Pandas Dataframe to InfluxDB database"""
cachepath = self._cache
cachefile = ('%s/%s-1M.csv.gz' % (cachepath, ticker))
if not os.path.exists(cachefile):
log.warn('Import file does not exist: %s' %
(cachefile))
return
df = pd.read_csv(cachefile, compression='infer', header=0,
infer_datetime_format=True)
df['Datetime'] = pd.to_datetime(df['Date'] + ' ' + df['Time'])
df = df.set_index('Datetime')
df = df.drop(['Date', 'Time'], axis=1)
try:
self.dfdb.write_points(df, ticker)
except InfluxDBClientError as err:
log.error('Write to database failed: %s' % err) | [
"def",
"write_dataframe_to_idb",
"(",
"self",
",",
"ticker",
")",
":",
"cachepath",
"=",
"self",
".",
"_cache",
"cachefile",
"=",
"(",
"'%s/%s-1M.csv.gz'",
"%",
"(",
"cachepath",
",",
"ticker",
")",
")",
"if",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"cachefile",
")",
":",
"log",
".",
"warn",
"(",
"'Import file does not exist: %s'",
"%",
"(",
"cachefile",
")",
")",
"return",
"df",
"=",
"pd",
".",
"read_csv",
"(",
"cachefile",
",",
"compression",
"=",
"'infer'",
",",
"header",
"=",
"0",
",",
"infer_datetime_format",
"=",
"True",
")",
"df",
"[",
"'Datetime'",
"]",
"=",
"pd",
".",
"to_datetime",
"(",
"df",
"[",
"'Date'",
"]",
"+",
"' '",
"+",
"df",
"[",
"'Time'",
"]",
")",
"df",
"=",
"df",
".",
"set_index",
"(",
"'Datetime'",
")",
"df",
"=",
"df",
".",
"drop",
"(",
"[",
"'Date'",
",",
"'Time'",
"]",
",",
"axis",
"=",
"1",
")",
"try",
":",
"self",
".",
"dfdb",
".",
"write_points",
"(",
"df",
",",
"ticker",
")",
"except",
"InfluxDBClientError",
"as",
"err",
":",
"log",
".",
"error",
"(",
"'Write to database failed: %s'",
"%",
"err",
")"
] | Write Pandas Dataframe to InfluxDB database | [
"Write",
"Pandas",
"Dataframe",
"to",
"InfluxDB",
"database"
] | 59ee9521f9887c2a1030c6f1db8c918a5816fd64 | https://github.com/backtrader/backtrader/blob/59ee9521f9887c2a1030c6f1db8c918a5816fd64/contrib/utils/influxdb-import.py#L29-L49 |
28,649 | AirtestProject/Airtest | playground/win_ide.py | WindowsInIDE.connect | def connect(self, **kwargs):
"""
Connect to window and set it foreground
Args:
**kwargs: optional arguments
Returns:
None
"""
self.app = self._app.connect(**kwargs)
try:
self._top_window = self.app.top_window().wrapper_object()
self.set_foreground()
except RuntimeError:
self._top_window = None | python | def connect(self, **kwargs):
"""
Connect to window and set it foreground
Args:
**kwargs: optional arguments
Returns:
None
"""
self.app = self._app.connect(**kwargs)
try:
self._top_window = self.app.top_window().wrapper_object()
self.set_foreground()
except RuntimeError:
self._top_window = None | [
"def",
"connect",
"(",
"self",
",",
"*",
"*",
"kwargs",
")",
":",
"self",
".",
"app",
"=",
"self",
".",
"_app",
".",
"connect",
"(",
"*",
"*",
"kwargs",
")",
"try",
":",
"self",
".",
"_top_window",
"=",
"self",
".",
"app",
".",
"top_window",
"(",
")",
".",
"wrapper_object",
"(",
")",
"self",
".",
"set_foreground",
"(",
")",
"except",
"RuntimeError",
":",
"self",
".",
"_top_window",
"=",
"None"
] | Connect to window and set it foreground
Args:
**kwargs: optional arguments
Returns:
None | [
"Connect",
"to",
"window",
"and",
"set",
"it",
"foreground"
] | 21583da2698a601cd632228228fc16d41f60a517 | https://github.com/AirtestProject/Airtest/blob/21583da2698a601cd632228228fc16d41f60a517/playground/win_ide.py#L19-L35 |
28,650 | AirtestProject/Airtest | playground/win_ide.py | WindowsInIDE.get_rect | def get_rect(self):
"""
Get rectangle of app or desktop resolution
Returns:
RECT(left, top, right, bottom)
"""
if self.handle:
left, top, right, bottom = win32gui.GetWindowRect(self.handle)
return RECT(left, top, right, bottom)
else:
desktop = win32gui.GetDesktopWindow()
left, top, right, bottom = win32gui.GetWindowRect(desktop)
return RECT(left, top, right, bottom) | python | def get_rect(self):
"""
Get rectangle of app or desktop resolution
Returns:
RECT(left, top, right, bottom)
"""
if self.handle:
left, top, right, bottom = win32gui.GetWindowRect(self.handle)
return RECT(left, top, right, bottom)
else:
desktop = win32gui.GetDesktopWindow()
left, top, right, bottom = win32gui.GetWindowRect(desktop)
return RECT(left, top, right, bottom) | [
"def",
"get_rect",
"(",
"self",
")",
":",
"if",
"self",
".",
"handle",
":",
"left",
",",
"top",
",",
"right",
",",
"bottom",
"=",
"win32gui",
".",
"GetWindowRect",
"(",
"self",
".",
"handle",
")",
"return",
"RECT",
"(",
"left",
",",
"top",
",",
"right",
",",
"bottom",
")",
"else",
":",
"desktop",
"=",
"win32gui",
".",
"GetDesktopWindow",
"(",
")",
"left",
",",
"top",
",",
"right",
",",
"bottom",
"=",
"win32gui",
".",
"GetWindowRect",
"(",
"desktop",
")",
"return",
"RECT",
"(",
"left",
",",
"top",
",",
"right",
",",
"bottom",
")"
] | Get rectangle of app or desktop resolution
Returns:
RECT(left, top, right, bottom) | [
"Get",
"rectangle",
"of",
"app",
"or",
"desktop",
"resolution"
] | 21583da2698a601cd632228228fc16d41f60a517 | https://github.com/AirtestProject/Airtest/blob/21583da2698a601cd632228228fc16d41f60a517/playground/win_ide.py#L37-L51 |
28,651 | AirtestProject/Airtest | playground/win_ide.py | WindowsInIDE.snapshot | def snapshot(self, filename="tmp.png"):
"""
Take a screenshot and save it to `tmp.png` filename by default
Args:
filename: name of file where to store the screenshot
Returns:
display the screenshot
"""
if not filename:
filename = "tmp.png"
if self.handle:
try:
screenshot(filename, self.handle)
except win32gui.error:
self.handle = None
screenshot(filename)
else:
screenshot(filename)
img = aircv.imread(filename)
os.remove(filename)
return img | python | def snapshot(self, filename="tmp.png"):
"""
Take a screenshot and save it to `tmp.png` filename by default
Args:
filename: name of file where to store the screenshot
Returns:
display the screenshot
"""
if not filename:
filename = "tmp.png"
if self.handle:
try:
screenshot(filename, self.handle)
except win32gui.error:
self.handle = None
screenshot(filename)
else:
screenshot(filename)
img = aircv.imread(filename)
os.remove(filename)
return img | [
"def",
"snapshot",
"(",
"self",
",",
"filename",
"=",
"\"tmp.png\"",
")",
":",
"if",
"not",
"filename",
":",
"filename",
"=",
"\"tmp.png\"",
"if",
"self",
".",
"handle",
":",
"try",
":",
"screenshot",
"(",
"filename",
",",
"self",
".",
"handle",
")",
"except",
"win32gui",
".",
"error",
":",
"self",
".",
"handle",
"=",
"None",
"screenshot",
"(",
"filename",
")",
"else",
":",
"screenshot",
"(",
"filename",
")",
"img",
"=",
"aircv",
".",
"imread",
"(",
"filename",
")",
"os",
".",
"remove",
"(",
"filename",
")",
"return",
"img"
] | Take a screenshot and save it to `tmp.png` filename by default
Args:
filename: name of file where to store the screenshot
Returns:
display the screenshot | [
"Take",
"a",
"screenshot",
"and",
"save",
"it",
"to",
"tmp",
".",
"png",
"filename",
"by",
"default"
] | 21583da2698a601cd632228228fc16d41f60a517 | https://github.com/AirtestProject/Airtest/blob/21583da2698a601cd632228228fc16d41f60a517/playground/win_ide.py#L53-L78 |
28,652 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _SimpleDecoder | def _SimpleDecoder(wire_type, decode_value):
"""Return a constructor for a decoder for fields of a particular type.
Args:
wire_type: The field's wire type.
decode_value: A function which decodes an individual value, e.g.
_DecodeVarint()
"""
def SpecificDecoder(field_number, is_repeated, is_packed, key, new_default):
if is_packed:
local_DecodeVarint = _DecodeVarint
def DecodePackedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
(endpoint, pos) = local_DecodeVarint(buffer, pos)
endpoint += pos
if endpoint > end:
raise _DecodeError('Truncated message.')
while pos < endpoint:
(element, pos) = decode_value(buffer, pos)
value.append(element)
if pos > endpoint:
del value[-1] # Discard corrupt value.
raise _DecodeError('Packed element was truncated.')
return pos
return DecodePackedField
elif is_repeated:
tag_bytes = encoder.TagBytes(field_number, wire_type)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(element, new_pos) = decode_value(buffer, pos)
value.append(element)
# Predict that the next tag is another copy of the same repeated
# field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos >= end:
# Prediction failed. Return.
if new_pos > end:
raise _DecodeError('Truncated message.')
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(field_dict[key], pos) = decode_value(buffer, pos)
if pos > end:
del field_dict[key] # Discard corrupt value.
raise _DecodeError('Truncated message.')
return pos
return DecodeField
return SpecificDecoder | python | def _SimpleDecoder(wire_type, decode_value):
"""Return a constructor for a decoder for fields of a particular type.
Args:
wire_type: The field's wire type.
decode_value: A function which decodes an individual value, e.g.
_DecodeVarint()
"""
def SpecificDecoder(field_number, is_repeated, is_packed, key, new_default):
if is_packed:
local_DecodeVarint = _DecodeVarint
def DecodePackedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
(endpoint, pos) = local_DecodeVarint(buffer, pos)
endpoint += pos
if endpoint > end:
raise _DecodeError('Truncated message.')
while pos < endpoint:
(element, pos) = decode_value(buffer, pos)
value.append(element)
if pos > endpoint:
del value[-1] # Discard corrupt value.
raise _DecodeError('Packed element was truncated.')
return pos
return DecodePackedField
elif is_repeated:
tag_bytes = encoder.TagBytes(field_number, wire_type)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(element, new_pos) = decode_value(buffer, pos)
value.append(element)
# Predict that the next tag is another copy of the same repeated
# field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos >= end:
# Prediction failed. Return.
if new_pos > end:
raise _DecodeError('Truncated message.')
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(field_dict[key], pos) = decode_value(buffer, pos)
if pos > end:
del field_dict[key] # Discard corrupt value.
raise _DecodeError('Truncated message.')
return pos
return DecodeField
return SpecificDecoder | [
"def",
"_SimpleDecoder",
"(",
"wire_type",
",",
"decode_value",
")",
":",
"def",
"SpecificDecoder",
"(",
"field_number",
",",
"is_repeated",
",",
"is_packed",
",",
"key",
",",
"new_default",
")",
":",
"if",
"is_packed",
":",
"local_DecodeVarint",
"=",
"_DecodeVarint",
"def",
"DecodePackedField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"(",
"endpoint",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"endpoint",
"+=",
"pos",
"if",
"endpoint",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"while",
"pos",
"<",
"endpoint",
":",
"(",
"element",
",",
"pos",
")",
"=",
"decode_value",
"(",
"buffer",
",",
"pos",
")",
"value",
".",
"append",
"(",
"element",
")",
"if",
"pos",
">",
"endpoint",
":",
"del",
"value",
"[",
"-",
"1",
"]",
"# Discard corrupt value.",
"raise",
"_DecodeError",
"(",
"'Packed element was truncated.'",
")",
"return",
"pos",
"return",
"DecodePackedField",
"elif",
"is_repeated",
":",
"tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_number",
",",
"wire_type",
")",
"tag_len",
"=",
"len",
"(",
"tag_bytes",
")",
"def",
"DecodeRepeatedField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"while",
"1",
":",
"(",
"element",
",",
"new_pos",
")",
"=",
"decode_value",
"(",
"buffer",
",",
"pos",
")",
"value",
".",
"append",
"(",
"element",
")",
"# Predict that the next tag is another copy of the same repeated",
"# field.",
"pos",
"=",
"new_pos",
"+",
"tag_len",
"if",
"buffer",
"[",
"new_pos",
":",
"pos",
"]",
"!=",
"tag_bytes",
"or",
"new_pos",
">=",
"end",
":",
"# Prediction failed. Return.",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"return",
"new_pos",
"return",
"DecodeRepeatedField",
"else",
":",
"def",
"DecodeField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"(",
"field_dict",
"[",
"key",
"]",
",",
"pos",
")",
"=",
"decode_value",
"(",
"buffer",
",",
"pos",
")",
"if",
"pos",
">",
"end",
":",
"del",
"field_dict",
"[",
"key",
"]",
"# Discard corrupt value.",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"return",
"pos",
"return",
"DecodeField",
"return",
"SpecificDecoder"
] | Return a constructor for a decoder for fields of a particular type.
Args:
wire_type: The field's wire type.
decode_value: A function which decodes an individual value, e.g.
_DecodeVarint() | [
"Return",
"a",
"constructor",
"for",
"a",
"decoder",
"for",
"fields",
"of",
"a",
"particular",
"type",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L190-L246 |
28,653 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _ModifiedDecoder | def _ModifiedDecoder(wire_type, decode_value, modify_value):
"""Like SimpleDecoder but additionally invokes modify_value on every value
before storing it. Usually modify_value is ZigZagDecode.
"""
# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but
# not enough to make a significant difference.
def InnerDecode(buffer, pos):
(result, new_pos) = decode_value(buffer, pos)
return (modify_value(result), new_pos)
return _SimpleDecoder(wire_type, InnerDecode) | python | def _ModifiedDecoder(wire_type, decode_value, modify_value):
"""Like SimpleDecoder but additionally invokes modify_value on every value
before storing it. Usually modify_value is ZigZagDecode.
"""
# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but
# not enough to make a significant difference.
def InnerDecode(buffer, pos):
(result, new_pos) = decode_value(buffer, pos)
return (modify_value(result), new_pos)
return _SimpleDecoder(wire_type, InnerDecode) | [
"def",
"_ModifiedDecoder",
"(",
"wire_type",
",",
"decode_value",
",",
"modify_value",
")",
":",
"# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but",
"# not enough to make a significant difference.",
"def",
"InnerDecode",
"(",
"buffer",
",",
"pos",
")",
":",
"(",
"result",
",",
"new_pos",
")",
"=",
"decode_value",
"(",
"buffer",
",",
"pos",
")",
"return",
"(",
"modify_value",
"(",
"result",
")",
",",
"new_pos",
")",
"return",
"_SimpleDecoder",
"(",
"wire_type",
",",
"InnerDecode",
")"
] | Like SimpleDecoder but additionally invokes modify_value on every value
before storing it. Usually modify_value is ZigZagDecode. | [
"Like",
"SimpleDecoder",
"but",
"additionally",
"invokes",
"modify_value",
"on",
"every",
"value",
"before",
"storing",
"it",
".",
"Usually",
"modify_value",
"is",
"ZigZagDecode",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L249-L260 |
28,654 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _StructPackDecoder | def _StructPackDecoder(wire_type, format):
"""Return a constructor for a decoder for a fixed-width field.
Args:
wire_type: The field's wire type.
format: The format string to pass to struct.unpack().
"""
value_size = struct.calcsize(format)
local_unpack = struct.unpack
# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but
# not enough to make a significant difference.
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
def InnerDecode(buffer, pos):
new_pos = pos + value_size
result = local_unpack(format, buffer[pos:new_pos])[0]
return (result, new_pos)
return _SimpleDecoder(wire_type, InnerDecode) | python | def _StructPackDecoder(wire_type, format):
"""Return a constructor for a decoder for a fixed-width field.
Args:
wire_type: The field's wire type.
format: The format string to pass to struct.unpack().
"""
value_size = struct.calcsize(format)
local_unpack = struct.unpack
# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but
# not enough to make a significant difference.
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
def InnerDecode(buffer, pos):
new_pos = pos + value_size
result = local_unpack(format, buffer[pos:new_pos])[0]
return (result, new_pos)
return _SimpleDecoder(wire_type, InnerDecode) | [
"def",
"_StructPackDecoder",
"(",
"wire_type",
",",
"format",
")",
":",
"value_size",
"=",
"struct",
".",
"calcsize",
"(",
"format",
")",
"local_unpack",
"=",
"struct",
".",
"unpack",
"# Reusing _SimpleDecoder is slightly slower than copying a bunch of code, but",
"# not enough to make a significant difference.",
"# Note that we expect someone up-stack to catch struct.error and convert",
"# it to _DecodeError -- this way we don't have to set up exception-",
"# handling blocks every time we parse one value.",
"def",
"InnerDecode",
"(",
"buffer",
",",
"pos",
")",
":",
"new_pos",
"=",
"pos",
"+",
"value_size",
"result",
"=",
"local_unpack",
"(",
"format",
",",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
")",
"[",
"0",
"]",
"return",
"(",
"result",
",",
"new_pos",
")",
"return",
"_SimpleDecoder",
"(",
"wire_type",
",",
"InnerDecode",
")"
] | Return a constructor for a decoder for a fixed-width field.
Args:
wire_type: The field's wire type.
format: The format string to pass to struct.unpack(). | [
"Return",
"a",
"constructor",
"for",
"a",
"decoder",
"for",
"a",
"fixed",
"-",
"width",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L263-L285 |
28,655 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _FloatDecoder | def _FloatDecoder():
"""Returns a decoder for a float field.
This code works around a bug in struct.unpack for non-finite 32-bit
floating-point values.
"""
local_unpack = struct.unpack
def InnerDecode(buffer, pos):
# We expect a 32-bit value in little-endian byte order. Bit 1 is the sign
# bit, bits 2-9 represent the exponent, and bits 10-32 are the significand.
new_pos = pos + 4
float_bytes = buffer[pos:new_pos]
# If this value has all its exponent bits set, then it's non-finite.
# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.
# To avoid that, we parse it specially.
if (float_bytes[3:4] in b'\x7F\xFF' and float_bytes[2:3] >= b'\x80'):
# If at least one significand bit is set...
if float_bytes[0:3] != b'\x00\x00\x80':
return (_NAN, new_pos)
# If sign bit is set...
if float_bytes[3:4] == b'\xFF':
return (_NEG_INF, new_pos)
return (_POS_INF, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
result = local_unpack('<f', float_bytes)[0]
return (result, new_pos)
return _SimpleDecoder(wire_format.WIRETYPE_FIXED32, InnerDecode) | python | def _FloatDecoder():
"""Returns a decoder for a float field.
This code works around a bug in struct.unpack for non-finite 32-bit
floating-point values.
"""
local_unpack = struct.unpack
def InnerDecode(buffer, pos):
# We expect a 32-bit value in little-endian byte order. Bit 1 is the sign
# bit, bits 2-9 represent the exponent, and bits 10-32 are the significand.
new_pos = pos + 4
float_bytes = buffer[pos:new_pos]
# If this value has all its exponent bits set, then it's non-finite.
# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.
# To avoid that, we parse it specially.
if (float_bytes[3:4] in b'\x7F\xFF' and float_bytes[2:3] >= b'\x80'):
# If at least one significand bit is set...
if float_bytes[0:3] != b'\x00\x00\x80':
return (_NAN, new_pos)
# If sign bit is set...
if float_bytes[3:4] == b'\xFF':
return (_NEG_INF, new_pos)
return (_POS_INF, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
result = local_unpack('<f', float_bytes)[0]
return (result, new_pos)
return _SimpleDecoder(wire_format.WIRETYPE_FIXED32, InnerDecode) | [
"def",
"_FloatDecoder",
"(",
")",
":",
"local_unpack",
"=",
"struct",
".",
"unpack",
"def",
"InnerDecode",
"(",
"buffer",
",",
"pos",
")",
":",
"# We expect a 32-bit value in little-endian byte order. Bit 1 is the sign",
"# bit, bits 2-9 represent the exponent, and bits 10-32 are the significand.",
"new_pos",
"=",
"pos",
"+",
"4",
"float_bytes",
"=",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
"# If this value has all its exponent bits set, then it's non-finite.",
"# In Python 2.4, struct.unpack will convert it to a finite 64-bit value.",
"# To avoid that, we parse it specially.",
"if",
"(",
"float_bytes",
"[",
"3",
":",
"4",
"]",
"in",
"b'\\x7F\\xFF'",
"and",
"float_bytes",
"[",
"2",
":",
"3",
"]",
">=",
"b'\\x80'",
")",
":",
"# If at least one significand bit is set...",
"if",
"float_bytes",
"[",
"0",
":",
"3",
"]",
"!=",
"b'\\x00\\x00\\x80'",
":",
"return",
"(",
"_NAN",
",",
"new_pos",
")",
"# If sign bit is set...",
"if",
"float_bytes",
"[",
"3",
":",
"4",
"]",
"==",
"b'\\xFF'",
":",
"return",
"(",
"_NEG_INF",
",",
"new_pos",
")",
"return",
"(",
"_POS_INF",
",",
"new_pos",
")",
"# Note that we expect someone up-stack to catch struct.error and convert",
"# it to _DecodeError -- this way we don't have to set up exception-",
"# handling blocks every time we parse one value.",
"result",
"=",
"local_unpack",
"(",
"'<f'",
",",
"float_bytes",
")",
"[",
"0",
"]",
"return",
"(",
"result",
",",
"new_pos",
")",
"return",
"_SimpleDecoder",
"(",
"wire_format",
".",
"WIRETYPE_FIXED32",
",",
"InnerDecode",
")"
] | Returns a decoder for a float field.
This code works around a bug in struct.unpack for non-finite 32-bit
floating-point values. | [
"Returns",
"a",
"decoder",
"for",
"a",
"float",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L288-L320 |
28,656 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _DoubleDecoder | def _DoubleDecoder():
"""Returns a decoder for a double field.
This code works around a bug in struct.unpack for not-a-number.
"""
local_unpack = struct.unpack
def InnerDecode(buffer, pos):
# We expect a 64-bit value in little-endian byte order. Bit 1 is the sign
# bit, bits 2-12 represent the exponent, and bits 13-64 are the significand.
new_pos = pos + 8
double_bytes = buffer[pos:new_pos]
# If this value has all its exponent bits set and at least one significand
# bit set, it's not a number. In Python 2.4, struct.unpack will treat it
# as inf or -inf. To avoid that, we treat it specially.
if ((double_bytes[7:8] in b'\x7F\xFF')
and (double_bytes[6:7] >= b'\xF0')
and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
return (_NAN, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
result = local_unpack('<d', double_bytes)[0]
return (result, new_pos)
return _SimpleDecoder(wire_format.WIRETYPE_FIXED64, InnerDecode) | python | def _DoubleDecoder():
"""Returns a decoder for a double field.
This code works around a bug in struct.unpack for not-a-number.
"""
local_unpack = struct.unpack
def InnerDecode(buffer, pos):
# We expect a 64-bit value in little-endian byte order. Bit 1 is the sign
# bit, bits 2-12 represent the exponent, and bits 13-64 are the significand.
new_pos = pos + 8
double_bytes = buffer[pos:new_pos]
# If this value has all its exponent bits set and at least one significand
# bit set, it's not a number. In Python 2.4, struct.unpack will treat it
# as inf or -inf. To avoid that, we treat it specially.
if ((double_bytes[7:8] in b'\x7F\xFF')
and (double_bytes[6:7] >= b'\xF0')
and (double_bytes[0:7] != b'\x00\x00\x00\x00\x00\x00\xF0')):
return (_NAN, new_pos)
# Note that we expect someone up-stack to catch struct.error and convert
# it to _DecodeError -- this way we don't have to set up exception-
# handling blocks every time we parse one value.
result = local_unpack('<d', double_bytes)[0]
return (result, new_pos)
return _SimpleDecoder(wire_format.WIRETYPE_FIXED64, InnerDecode) | [
"def",
"_DoubleDecoder",
"(",
")",
":",
"local_unpack",
"=",
"struct",
".",
"unpack",
"def",
"InnerDecode",
"(",
"buffer",
",",
"pos",
")",
":",
"# We expect a 64-bit value in little-endian byte order. Bit 1 is the sign",
"# bit, bits 2-12 represent the exponent, and bits 13-64 are the significand.",
"new_pos",
"=",
"pos",
"+",
"8",
"double_bytes",
"=",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
"# If this value has all its exponent bits set and at least one significand",
"# bit set, it's not a number. In Python 2.4, struct.unpack will treat it",
"# as inf or -inf. To avoid that, we treat it specially.",
"if",
"(",
"(",
"double_bytes",
"[",
"7",
":",
"8",
"]",
"in",
"b'\\x7F\\xFF'",
")",
"and",
"(",
"double_bytes",
"[",
"6",
":",
"7",
"]",
">=",
"b'\\xF0'",
")",
"and",
"(",
"double_bytes",
"[",
"0",
":",
"7",
"]",
"!=",
"b'\\x00\\x00\\x00\\x00\\x00\\x00\\xF0'",
")",
")",
":",
"return",
"(",
"_NAN",
",",
"new_pos",
")",
"# Note that we expect someone up-stack to catch struct.error and convert",
"# it to _DecodeError -- this way we don't have to set up exception-",
"# handling blocks every time we parse one value.",
"result",
"=",
"local_unpack",
"(",
"'<d'",
",",
"double_bytes",
")",
"[",
"0",
"]",
"return",
"(",
"result",
",",
"new_pos",
")",
"return",
"_SimpleDecoder",
"(",
"wire_format",
".",
"WIRETYPE_FIXED64",
",",
"InnerDecode",
")"
] | Returns a decoder for a double field.
This code works around a bug in struct.unpack for not-a-number. | [
"Returns",
"a",
"decoder",
"for",
"a",
"double",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L323-L350 |
28,657 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | StringDecoder | def StringDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a string field."""
local_DecodeVarint = _DecodeVarint
local_unicode = six.text_type
def _ConvertToUnicode(byte_str):
try:
return local_unicode(byte_str, 'utf-8')
except UnicodeDecodeError as e:
# add more information to the error message and re-raise it.
e.reason = '%s in field: %s' % (e, key.full_name)
raise
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
value.append(_ConvertToUnicode(buffer[pos:new_pos]))
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
field_dict[key] = _ConvertToUnicode(buffer[pos:new_pos])
return new_pos
return DecodeField | python | def StringDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a string field."""
local_DecodeVarint = _DecodeVarint
local_unicode = six.text_type
def _ConvertToUnicode(byte_str):
try:
return local_unicode(byte_str, 'utf-8')
except UnicodeDecodeError as e:
# add more information to the error message and re-raise it.
e.reason = '%s in field: %s' % (e, key.full_name)
raise
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
value.append(_ConvertToUnicode(buffer[pos:new_pos]))
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
field_dict[key] = _ConvertToUnicode(buffer[pos:new_pos])
return new_pos
return DecodeField | [
"def",
"StringDecoder",
"(",
"field_number",
",",
"is_repeated",
",",
"is_packed",
",",
"key",
",",
"new_default",
")",
":",
"local_DecodeVarint",
"=",
"_DecodeVarint",
"local_unicode",
"=",
"six",
".",
"text_type",
"def",
"_ConvertToUnicode",
"(",
"byte_str",
")",
":",
"try",
":",
"return",
"local_unicode",
"(",
"byte_str",
",",
"'utf-8'",
")",
"except",
"UnicodeDecodeError",
"as",
"e",
":",
"# add more information to the error message and re-raise it.",
"e",
".",
"reason",
"=",
"'%s in field: %s'",
"%",
"(",
"e",
",",
"key",
".",
"full_name",
")",
"raise",
"assert",
"not",
"is_packed",
"if",
"is_repeated",
":",
"tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_number",
",",
"wire_format",
".",
"WIRETYPE_LENGTH_DELIMITED",
")",
"tag_len",
"=",
"len",
"(",
"tag_bytes",
")",
"def",
"DecodeRepeatedField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"while",
"1",
":",
"(",
"size",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"pos",
"+",
"size",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated string.'",
")",
"value",
".",
"append",
"(",
"_ConvertToUnicode",
"(",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
")",
")",
"# Predict that the next tag is another copy of the same repeated field.",
"pos",
"=",
"new_pos",
"+",
"tag_len",
"if",
"buffer",
"[",
"new_pos",
":",
"pos",
"]",
"!=",
"tag_bytes",
"or",
"new_pos",
"==",
"end",
":",
"# Prediction failed. Return.",
"return",
"new_pos",
"return",
"DecodeRepeatedField",
"else",
":",
"def",
"DecodeField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"(",
"size",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"pos",
"+",
"size",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated string.'",
")",
"field_dict",
"[",
"key",
"]",
"=",
"_ConvertToUnicode",
"(",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
")",
"return",
"new_pos",
"return",
"DecodeField"
] | Returns a decoder for a string field. | [
"Returns",
"a",
"decoder",
"for",
"a",
"string",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L461-L504 |
28,658 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | BytesDecoder | def BytesDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a bytes field."""
local_DecodeVarint = _DecodeVarint
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
value.append(buffer[pos:new_pos])
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
field_dict[key] = buffer[pos:new_pos]
return new_pos
return DecodeField | python | def BytesDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a bytes field."""
local_DecodeVarint = _DecodeVarint
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
value.append(buffer[pos:new_pos])
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated string.')
field_dict[key] = buffer[pos:new_pos]
return new_pos
return DecodeField | [
"def",
"BytesDecoder",
"(",
"field_number",
",",
"is_repeated",
",",
"is_packed",
",",
"key",
",",
"new_default",
")",
":",
"local_DecodeVarint",
"=",
"_DecodeVarint",
"assert",
"not",
"is_packed",
"if",
"is_repeated",
":",
"tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_number",
",",
"wire_format",
".",
"WIRETYPE_LENGTH_DELIMITED",
")",
"tag_len",
"=",
"len",
"(",
"tag_bytes",
")",
"def",
"DecodeRepeatedField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"while",
"1",
":",
"(",
"size",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"pos",
"+",
"size",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated string.'",
")",
"value",
".",
"append",
"(",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
")",
"# Predict that the next tag is another copy of the same repeated field.",
"pos",
"=",
"new_pos",
"+",
"tag_len",
"if",
"buffer",
"[",
"new_pos",
":",
"pos",
"]",
"!=",
"tag_bytes",
"or",
"new_pos",
"==",
"end",
":",
"# Prediction failed. Return.",
"return",
"new_pos",
"return",
"DecodeRepeatedField",
"else",
":",
"def",
"DecodeField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"(",
"size",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"pos",
"+",
"size",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated string.'",
")",
"field_dict",
"[",
"key",
"]",
"=",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
"return",
"new_pos",
"return",
"DecodeField"
] | Returns a decoder for a bytes field. | [
"Returns",
"a",
"decoder",
"for",
"a",
"bytes",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L507-L541 |
28,659 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | GroupDecoder | def GroupDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a group field."""
end_tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_END_GROUP)
end_tag_len = len(end_tag_bytes)
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_START_GROUP)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
# Read sub-message.
pos = value.add()._InternalParse(buffer, pos, end)
# Read end tag.
new_pos = pos+end_tag_len
if buffer[pos:new_pos] != end_tag_bytes or new_pos > end:
raise _DecodeError('Missing group end tag.')
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
# Read sub-message.
pos = value._InternalParse(buffer, pos, end)
# Read end tag.
new_pos = pos+end_tag_len
if buffer[pos:new_pos] != end_tag_bytes or new_pos > end:
raise _DecodeError('Missing group end tag.')
return new_pos
return DecodeField | python | def GroupDecoder(field_number, is_repeated, is_packed, key, new_default):
"""Returns a decoder for a group field."""
end_tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_END_GROUP)
end_tag_len = len(end_tag_bytes)
assert not is_packed
if is_repeated:
tag_bytes = encoder.TagBytes(field_number,
wire_format.WIRETYPE_START_GROUP)
tag_len = len(tag_bytes)
def DecodeRepeatedField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
# Read sub-message.
pos = value.add()._InternalParse(buffer, pos, end)
# Read end tag.
new_pos = pos+end_tag_len
if buffer[pos:new_pos] != end_tag_bytes or new_pos > end:
raise _DecodeError('Missing group end tag.')
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeRepeatedField
else:
def DecodeField(buffer, pos, end, message, field_dict):
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
# Read sub-message.
pos = value._InternalParse(buffer, pos, end)
# Read end tag.
new_pos = pos+end_tag_len
if buffer[pos:new_pos] != end_tag_bytes or new_pos > end:
raise _DecodeError('Missing group end tag.')
return new_pos
return DecodeField | [
"def",
"GroupDecoder",
"(",
"field_number",
",",
"is_repeated",
",",
"is_packed",
",",
"key",
",",
"new_default",
")",
":",
"end_tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_number",
",",
"wire_format",
".",
"WIRETYPE_END_GROUP",
")",
"end_tag_len",
"=",
"len",
"(",
"end_tag_bytes",
")",
"assert",
"not",
"is_packed",
"if",
"is_repeated",
":",
"tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_number",
",",
"wire_format",
".",
"WIRETYPE_START_GROUP",
")",
"tag_len",
"=",
"len",
"(",
"tag_bytes",
")",
"def",
"DecodeRepeatedField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"while",
"1",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"# Read sub-message.",
"pos",
"=",
"value",
".",
"add",
"(",
")",
".",
"_InternalParse",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
"# Read end tag.",
"new_pos",
"=",
"pos",
"+",
"end_tag_len",
"if",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
"!=",
"end_tag_bytes",
"or",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Missing group end tag.'",
")",
"# Predict that the next tag is another copy of the same repeated field.",
"pos",
"=",
"new_pos",
"+",
"tag_len",
"if",
"buffer",
"[",
"new_pos",
":",
"pos",
"]",
"!=",
"tag_bytes",
"or",
"new_pos",
"==",
"end",
":",
"# Prediction failed. Return.",
"return",
"new_pos",
"return",
"DecodeRepeatedField",
"else",
":",
"def",
"DecodeField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"# Read sub-message.",
"pos",
"=",
"value",
".",
"_InternalParse",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
"# Read end tag.",
"new_pos",
"=",
"pos",
"+",
"end_tag_len",
"if",
"buffer",
"[",
"pos",
":",
"new_pos",
"]",
"!=",
"end_tag_bytes",
"or",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Missing group end tag.'",
")",
"return",
"new_pos",
"return",
"DecodeField"
] | Returns a decoder for a group field. | [
"Returns",
"a",
"decoder",
"for",
"a",
"group",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L544-L588 |
28,660 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | MapDecoder | def MapDecoder(field_descriptor, new_default, is_message_map):
"""Returns a decoder for a map field."""
key = field_descriptor
tag_bytes = encoder.TagBytes(field_descriptor.number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
local_DecodeVarint = _DecodeVarint
# Can't read _concrete_class yet; might not be initialized.
message_type = field_descriptor.message_type
def DecodeMap(buffer, pos, end, message, field_dict):
submsg = message_type._concrete_class()
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
# Read length.
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated message.')
# Read sub-message.
submsg.Clear()
if submsg._InternalParse(buffer, pos, new_pos) != new_pos:
# The only reason _InternalParse would return early is if it
# encountered an end-group tag.
raise _DecodeError('Unexpected end-group tag.')
if is_message_map:
value[submsg.key].MergeFrom(submsg.value)
else:
value[submsg.key] = submsg.value
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeMap | python | def MapDecoder(field_descriptor, new_default, is_message_map):
"""Returns a decoder for a map field."""
key = field_descriptor
tag_bytes = encoder.TagBytes(field_descriptor.number,
wire_format.WIRETYPE_LENGTH_DELIMITED)
tag_len = len(tag_bytes)
local_DecodeVarint = _DecodeVarint
# Can't read _concrete_class yet; might not be initialized.
message_type = field_descriptor.message_type
def DecodeMap(buffer, pos, end, message, field_dict):
submsg = message_type._concrete_class()
value = field_dict.get(key)
if value is None:
value = field_dict.setdefault(key, new_default(message))
while 1:
# Read length.
(size, pos) = local_DecodeVarint(buffer, pos)
new_pos = pos + size
if new_pos > end:
raise _DecodeError('Truncated message.')
# Read sub-message.
submsg.Clear()
if submsg._InternalParse(buffer, pos, new_pos) != new_pos:
# The only reason _InternalParse would return early is if it
# encountered an end-group tag.
raise _DecodeError('Unexpected end-group tag.')
if is_message_map:
value[submsg.key].MergeFrom(submsg.value)
else:
value[submsg.key] = submsg.value
# Predict that the next tag is another copy of the same repeated field.
pos = new_pos + tag_len
if buffer[new_pos:pos] != tag_bytes or new_pos == end:
# Prediction failed. Return.
return new_pos
return DecodeMap | [
"def",
"MapDecoder",
"(",
"field_descriptor",
",",
"new_default",
",",
"is_message_map",
")",
":",
"key",
"=",
"field_descriptor",
"tag_bytes",
"=",
"encoder",
".",
"TagBytes",
"(",
"field_descriptor",
".",
"number",
",",
"wire_format",
".",
"WIRETYPE_LENGTH_DELIMITED",
")",
"tag_len",
"=",
"len",
"(",
"tag_bytes",
")",
"local_DecodeVarint",
"=",
"_DecodeVarint",
"# Can't read _concrete_class yet; might not be initialized.",
"message_type",
"=",
"field_descriptor",
".",
"message_type",
"def",
"DecodeMap",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"message",
",",
"field_dict",
")",
":",
"submsg",
"=",
"message_type",
".",
"_concrete_class",
"(",
")",
"value",
"=",
"field_dict",
".",
"get",
"(",
"key",
")",
"if",
"value",
"is",
"None",
":",
"value",
"=",
"field_dict",
".",
"setdefault",
"(",
"key",
",",
"new_default",
"(",
"message",
")",
")",
"while",
"1",
":",
"# Read length.",
"(",
"size",
",",
"pos",
")",
"=",
"local_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"pos",
"+",
"size",
"if",
"new_pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"# Read sub-message.",
"submsg",
".",
"Clear",
"(",
")",
"if",
"submsg",
".",
"_InternalParse",
"(",
"buffer",
",",
"pos",
",",
"new_pos",
")",
"!=",
"new_pos",
":",
"# The only reason _InternalParse would return early is if it",
"# encountered an end-group tag.",
"raise",
"_DecodeError",
"(",
"'Unexpected end-group tag.'",
")",
"if",
"is_message_map",
":",
"value",
"[",
"submsg",
".",
"key",
"]",
".",
"MergeFrom",
"(",
"submsg",
".",
"value",
")",
"else",
":",
"value",
"[",
"submsg",
".",
"key",
"]",
"=",
"submsg",
".",
"value",
"# Predict that the next tag is another copy of the same repeated field.",
"pos",
"=",
"new_pos",
"+",
"tag_len",
"if",
"buffer",
"[",
"new_pos",
":",
"pos",
"]",
"!=",
"tag_bytes",
"or",
"new_pos",
"==",
"end",
":",
"# Prediction failed. Return.",
"return",
"new_pos",
"return",
"DecodeMap"
] | Returns a decoder for a map field. | [
"Returns",
"a",
"decoder",
"for",
"a",
"map",
"field",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L719-L759 |
28,661 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _SkipVarint | def _SkipVarint(buffer, pos, end):
"""Skip a varint value. Returns the new position."""
# Previously ord(buffer[pos]) raised IndexError when pos is out of range.
# With this code, ord(b'') raises TypeError. Both are handled in
# python_message.py to generate a 'Truncated message' error.
while ord(buffer[pos:pos+1]) & 0x80:
pos += 1
pos += 1
if pos > end:
raise _DecodeError('Truncated message.')
return pos | python | def _SkipVarint(buffer, pos, end):
"""Skip a varint value. Returns the new position."""
# Previously ord(buffer[pos]) raised IndexError when pos is out of range.
# With this code, ord(b'') raises TypeError. Both are handled in
# python_message.py to generate a 'Truncated message' error.
while ord(buffer[pos:pos+1]) & 0x80:
pos += 1
pos += 1
if pos > end:
raise _DecodeError('Truncated message.')
return pos | [
"def",
"_SkipVarint",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
":",
"# Previously ord(buffer[pos]) raised IndexError when pos is out of range.",
"# With this code, ord(b'') raises TypeError. Both are handled in",
"# python_message.py to generate a 'Truncated message' error.",
"while",
"ord",
"(",
"buffer",
"[",
"pos",
":",
"pos",
"+",
"1",
"]",
")",
"&",
"0x80",
":",
"pos",
"+=",
"1",
"pos",
"+=",
"1",
"if",
"pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"return",
"pos"
] | Skip a varint value. Returns the new position. | [
"Skip",
"a",
"varint",
"value",
".",
"Returns",
"the",
"new",
"position",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L765-L775 |
28,662 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _SkipLengthDelimited | def _SkipLengthDelimited(buffer, pos, end):
"""Skip a length-delimited value. Returns the new position."""
(size, pos) = _DecodeVarint(buffer, pos)
pos += size
if pos > end:
raise _DecodeError('Truncated message.')
return pos | python | def _SkipLengthDelimited(buffer, pos, end):
"""Skip a length-delimited value. Returns the new position."""
(size, pos) = _DecodeVarint(buffer, pos)
pos += size
if pos > end:
raise _DecodeError('Truncated message.')
return pos | [
"def",
"_SkipLengthDelimited",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
":",
"(",
"size",
",",
"pos",
")",
"=",
"_DecodeVarint",
"(",
"buffer",
",",
"pos",
")",
"pos",
"+=",
"size",
"if",
"pos",
">",
"end",
":",
"raise",
"_DecodeError",
"(",
"'Truncated message.'",
")",
"return",
"pos"
] | Skip a length-delimited value. Returns the new position. | [
"Skip",
"a",
"length",
"-",
"delimited",
"value",
".",
"Returns",
"the",
"new",
"position",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L785-L792 |
28,663 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _SkipGroup | def _SkipGroup(buffer, pos, end):
"""Skip sub-group. Returns the new position."""
while 1:
(tag_bytes, pos) = ReadTag(buffer, pos)
new_pos = SkipField(buffer, pos, end, tag_bytes)
if new_pos == -1:
return pos
pos = new_pos | python | def _SkipGroup(buffer, pos, end):
"""Skip sub-group. Returns the new position."""
while 1:
(tag_bytes, pos) = ReadTag(buffer, pos)
new_pos = SkipField(buffer, pos, end, tag_bytes)
if new_pos == -1:
return pos
pos = new_pos | [
"def",
"_SkipGroup",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
":",
"while",
"1",
":",
"(",
"tag_bytes",
",",
"pos",
")",
"=",
"ReadTag",
"(",
"buffer",
",",
"pos",
")",
"new_pos",
"=",
"SkipField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"tag_bytes",
")",
"if",
"new_pos",
"==",
"-",
"1",
":",
"return",
"pos",
"pos",
"=",
"new_pos"
] | Skip sub-group. Returns the new position. | [
"Skip",
"sub",
"-",
"group",
".",
"Returns",
"the",
"new",
"position",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L794-L802 |
28,664 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py | _FieldSkipper | def _FieldSkipper():
"""Constructs the SkipField function."""
WIRETYPE_TO_SKIPPER = [
_SkipVarint,
_SkipFixed64,
_SkipLengthDelimited,
_SkipGroup,
_EndGroup,
_SkipFixed32,
_RaiseInvalidWireType,
_RaiseInvalidWireType,
]
wiretype_mask = wire_format.TAG_TYPE_MASK
def SkipField(buffer, pos, end, tag_bytes):
"""Skips a field with the specified tag.
|pos| should point to the byte immediately after the tag.
Returns:
The new position (after the tag value), or -1 if the tag is an end-group
tag (in which case the calling loop should break).
"""
# The wire type is always in the first byte since varints are little-endian.
wire_type = ord(tag_bytes[0:1]) & wiretype_mask
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
return SkipField | python | def _FieldSkipper():
"""Constructs the SkipField function."""
WIRETYPE_TO_SKIPPER = [
_SkipVarint,
_SkipFixed64,
_SkipLengthDelimited,
_SkipGroup,
_EndGroup,
_SkipFixed32,
_RaiseInvalidWireType,
_RaiseInvalidWireType,
]
wiretype_mask = wire_format.TAG_TYPE_MASK
def SkipField(buffer, pos, end, tag_bytes):
"""Skips a field with the specified tag.
|pos| should point to the byte immediately after the tag.
Returns:
The new position (after the tag value), or -1 if the tag is an end-group
tag (in which case the calling loop should break).
"""
# The wire type is always in the first byte since varints are little-endian.
wire_type = ord(tag_bytes[0:1]) & wiretype_mask
return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
return SkipField | [
"def",
"_FieldSkipper",
"(",
")",
":",
"WIRETYPE_TO_SKIPPER",
"=",
"[",
"_SkipVarint",
",",
"_SkipFixed64",
",",
"_SkipLengthDelimited",
",",
"_SkipGroup",
",",
"_EndGroup",
",",
"_SkipFixed32",
",",
"_RaiseInvalidWireType",
",",
"_RaiseInvalidWireType",
",",
"]",
"wiretype_mask",
"=",
"wire_format",
".",
"TAG_TYPE_MASK",
"def",
"SkipField",
"(",
"buffer",
",",
"pos",
",",
"end",
",",
"tag_bytes",
")",
":",
"\"\"\"Skips a field with the specified tag.\n\n |pos| should point to the byte immediately after the tag.\n\n Returns:\n The new position (after the tag value), or -1 if the tag is an end-group\n tag (in which case the calling loop should break).\n \"\"\"",
"# The wire type is always in the first byte since varints are little-endian.",
"wire_type",
"=",
"ord",
"(",
"tag_bytes",
"[",
"0",
":",
"1",
"]",
")",
"&",
"wiretype_mask",
"return",
"WIRETYPE_TO_SKIPPER",
"[",
"wire_type",
"]",
"(",
"buffer",
",",
"pos",
",",
"end",
")",
"return",
"SkipField"
] | Constructs the SkipField function. | [
"Constructs",
"the",
"SkipField",
"function",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/internal/decoder.py#L822-L852 |
28,665 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/decision_tree_classifier.py | DecisionTreeClassifier.predict | def predict(self, dataset, output_type='class', missing_value_action='auto'):
"""
A flexible and advanced prediction API.
The target column is provided during
:func:`~turicreate.decision_tree.create`. If the target column is in the
`dataset` it will be ignored.
Parameters
----------
dataset : SFrame
A dataset that has the same columns that were used during training.
If the target column exists in ``dataset`` it will be ignored
while making predictions.
output_type : {'probability', 'margin', 'class', 'probability_vector'}, optional.
Form of the predictions which are one of:
- 'probability': Prediction probability associated with the True
class (not applicable for multi-class classification)
- 'margin': Margin associated with the prediction (not applicable
for multi-class classification)
- 'probability_vector': Prediction probability associated with each
class as a vector. The probability of the first class (sorted
alphanumerically by name of the class in the training set) is in
position 0 of the vector, the second in position 1 and so on.
- 'class': Class prediction. For multi-class classification, this
returns the class with maximum probability.
missing_value_action : str, optional
Action to perform when missing values are encountered. Can be
one of:
- 'auto': By default the model will treat missing value as is.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'error': Do not proceed with evaluation and terminate with
an error message.
Returns
-------
out : SArray
Predicted target value for each example (i.e. row) in the dataset.
See Also
----------
create, evaluate, classify
Examples
--------
>>> m.predict(testdata)
>>> m.predict(testdata, output_type='probability')
>>> m.predict(testdata, output_type='margin')
"""
_check_categorical_option_type('output_type', output_type,
['class', 'margin', 'probability', 'probability_vector'])
return super(_Classifier, self).predict(dataset,
output_type=output_type,
missing_value_action=missing_value_action) | python | def predict(self, dataset, output_type='class', missing_value_action='auto'):
"""
A flexible and advanced prediction API.
The target column is provided during
:func:`~turicreate.decision_tree.create`. If the target column is in the
`dataset` it will be ignored.
Parameters
----------
dataset : SFrame
A dataset that has the same columns that were used during training.
If the target column exists in ``dataset`` it will be ignored
while making predictions.
output_type : {'probability', 'margin', 'class', 'probability_vector'}, optional.
Form of the predictions which are one of:
- 'probability': Prediction probability associated with the True
class (not applicable for multi-class classification)
- 'margin': Margin associated with the prediction (not applicable
for multi-class classification)
- 'probability_vector': Prediction probability associated with each
class as a vector. The probability of the first class (sorted
alphanumerically by name of the class in the training set) is in
position 0 of the vector, the second in position 1 and so on.
- 'class': Class prediction. For multi-class classification, this
returns the class with maximum probability.
missing_value_action : str, optional
Action to perform when missing values are encountered. Can be
one of:
- 'auto': By default the model will treat missing value as is.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'error': Do not proceed with evaluation and terminate with
an error message.
Returns
-------
out : SArray
Predicted target value for each example (i.e. row) in the dataset.
See Also
----------
create, evaluate, classify
Examples
--------
>>> m.predict(testdata)
>>> m.predict(testdata, output_type='probability')
>>> m.predict(testdata, output_type='margin')
"""
_check_categorical_option_type('output_type', output_type,
['class', 'margin', 'probability', 'probability_vector'])
return super(_Classifier, self).predict(dataset,
output_type=output_type,
missing_value_action=missing_value_action) | [
"def",
"predict",
"(",
"self",
",",
"dataset",
",",
"output_type",
"=",
"'class'",
",",
"missing_value_action",
"=",
"'auto'",
")",
":",
"_check_categorical_option_type",
"(",
"'output_type'",
",",
"output_type",
",",
"[",
"'class'",
",",
"'margin'",
",",
"'probability'",
",",
"'probability_vector'",
"]",
")",
"return",
"super",
"(",
"_Classifier",
",",
"self",
")",
".",
"predict",
"(",
"dataset",
",",
"output_type",
"=",
"output_type",
",",
"missing_value_action",
"=",
"missing_value_action",
")"
] | A flexible and advanced prediction API.
The target column is provided during
:func:`~turicreate.decision_tree.create`. If the target column is in the
`dataset` it will be ignored.
Parameters
----------
dataset : SFrame
A dataset that has the same columns that were used during training.
If the target column exists in ``dataset`` it will be ignored
while making predictions.
output_type : {'probability', 'margin', 'class', 'probability_vector'}, optional.
Form of the predictions which are one of:
- 'probability': Prediction probability associated with the True
class (not applicable for multi-class classification)
- 'margin': Margin associated with the prediction (not applicable
for multi-class classification)
- 'probability_vector': Prediction probability associated with each
class as a vector. The probability of the first class (sorted
alphanumerically by name of the class in the training set) is in
position 0 of the vector, the second in position 1 and so on.
- 'class': Class prediction. For multi-class classification, this
returns the class with maximum probability.
missing_value_action : str, optional
Action to perform when missing values are encountered. Can be
one of:
- 'auto': By default the model will treat missing value as is.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'error': Do not proceed with evaluation and terminate with
an error message.
Returns
-------
out : SArray
Predicted target value for each example (i.e. row) in the dataset.
See Also
----------
create, evaluate, classify
Examples
--------
>>> m.predict(testdata)
>>> m.predict(testdata, output_type='probability')
>>> m.predict(testdata, output_type='margin') | [
"A",
"flexible",
"and",
"advanced",
"prediction",
"API",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/decision_tree_classifier.py#L210-L271 |
28,666 | apple/turicreate | src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py | Tracker.slave_envs | def slave_envs(self):
"""
get enviroment variables for slaves
can be passed in as args or envs
"""
if self.hostIP == 'dns':
host = socket.gethostname()
elif self.hostIP == 'ip':
host = socket.gethostbyname(socket.getfqdn())
else:
host = self.hostIP
return {'rabit_tracker_uri': host,
'rabit_tracker_port': self.port} | python | def slave_envs(self):
"""
get enviroment variables for slaves
can be passed in as args or envs
"""
if self.hostIP == 'dns':
host = socket.gethostname()
elif self.hostIP == 'ip':
host = socket.gethostbyname(socket.getfqdn())
else:
host = self.hostIP
return {'rabit_tracker_uri': host,
'rabit_tracker_port': self.port} | [
"def",
"slave_envs",
"(",
"self",
")",
":",
"if",
"self",
".",
"hostIP",
"==",
"'dns'",
":",
"host",
"=",
"socket",
".",
"gethostname",
"(",
")",
"elif",
"self",
".",
"hostIP",
"==",
"'ip'",
":",
"host",
"=",
"socket",
".",
"gethostbyname",
"(",
"socket",
".",
"getfqdn",
"(",
")",
")",
"else",
":",
"host",
"=",
"self",
".",
"hostIP",
"return",
"{",
"'rabit_tracker_uri'",
":",
"host",
",",
"'rabit_tracker_port'",
":",
"self",
".",
"port",
"}"
] | get enviroment variables for slaves
can be passed in as args or envs | [
"get",
"enviroment",
"variables",
"for",
"slaves",
"can",
"be",
"passed",
"in",
"as",
"args",
"or",
"envs"
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py#L144-L156 |
28,667 | apple/turicreate | src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py | Tracker.find_share_ring | def find_share_ring(self, tree_map, parent_map, r):
"""
get a ring structure that tends to share nodes with the tree
return a list starting from r
"""
nset = set(tree_map[r])
cset = nset - set([parent_map[r]])
if len(cset) == 0:
return [r]
rlst = [r]
cnt = 0
for v in cset:
vlst = self.find_share_ring(tree_map, parent_map, v)
cnt += 1
if cnt == len(cset):
vlst.reverse()
rlst += vlst
return rlst | python | def find_share_ring(self, tree_map, parent_map, r):
"""
get a ring structure that tends to share nodes with the tree
return a list starting from r
"""
nset = set(tree_map[r])
cset = nset - set([parent_map[r]])
if len(cset) == 0:
return [r]
rlst = [r]
cnt = 0
for v in cset:
vlst = self.find_share_ring(tree_map, parent_map, v)
cnt += 1
if cnt == len(cset):
vlst.reverse()
rlst += vlst
return rlst | [
"def",
"find_share_ring",
"(",
"self",
",",
"tree_map",
",",
"parent_map",
",",
"r",
")",
":",
"nset",
"=",
"set",
"(",
"tree_map",
"[",
"r",
"]",
")",
"cset",
"=",
"nset",
"-",
"set",
"(",
"[",
"parent_map",
"[",
"r",
"]",
"]",
")",
"if",
"len",
"(",
"cset",
")",
"==",
"0",
":",
"return",
"[",
"r",
"]",
"rlst",
"=",
"[",
"r",
"]",
"cnt",
"=",
"0",
"for",
"v",
"in",
"cset",
":",
"vlst",
"=",
"self",
".",
"find_share_ring",
"(",
"tree_map",
",",
"parent_map",
",",
"v",
")",
"cnt",
"+=",
"1",
"if",
"cnt",
"==",
"len",
"(",
"cset",
")",
":",
"vlst",
".",
"reverse",
"(",
")",
"rlst",
"+=",
"vlst",
"return",
"rlst"
] | get a ring structure that tends to share nodes with the tree
return a list starting from r | [
"get",
"a",
"ring",
"structure",
"that",
"tends",
"to",
"share",
"nodes",
"with",
"the",
"tree",
"return",
"a",
"list",
"starting",
"from",
"r"
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py#L174-L191 |
28,668 | apple/turicreate | src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py | Tracker.get_ring | def get_ring(self, tree_map, parent_map):
"""
get a ring connection used to recover local data
"""
assert parent_map[0] == -1
rlst = self.find_share_ring(tree_map, parent_map, 0)
assert len(rlst) == len(tree_map)
ring_map = {}
nslave = len(tree_map)
for r in range(nslave):
rprev = (r + nslave - 1) % nslave
rnext = (r + 1) % nslave
ring_map[rlst[r]] = (rlst[rprev], rlst[rnext])
return ring_map | python | def get_ring(self, tree_map, parent_map):
"""
get a ring connection used to recover local data
"""
assert parent_map[0] == -1
rlst = self.find_share_ring(tree_map, parent_map, 0)
assert len(rlst) == len(tree_map)
ring_map = {}
nslave = len(tree_map)
for r in range(nslave):
rprev = (r + nslave - 1) % nslave
rnext = (r + 1) % nslave
ring_map[rlst[r]] = (rlst[rprev], rlst[rnext])
return ring_map | [
"def",
"get_ring",
"(",
"self",
",",
"tree_map",
",",
"parent_map",
")",
":",
"assert",
"parent_map",
"[",
"0",
"]",
"==",
"-",
"1",
"rlst",
"=",
"self",
".",
"find_share_ring",
"(",
"tree_map",
",",
"parent_map",
",",
"0",
")",
"assert",
"len",
"(",
"rlst",
")",
"==",
"len",
"(",
"tree_map",
")",
"ring_map",
"=",
"{",
"}",
"nslave",
"=",
"len",
"(",
"tree_map",
")",
"for",
"r",
"in",
"range",
"(",
"nslave",
")",
":",
"rprev",
"=",
"(",
"r",
"+",
"nslave",
"-",
"1",
")",
"%",
"nslave",
"rnext",
"=",
"(",
"r",
"+",
"1",
")",
"%",
"nslave",
"ring_map",
"[",
"rlst",
"[",
"r",
"]",
"]",
"=",
"(",
"rlst",
"[",
"rprev",
"]",
",",
"rlst",
"[",
"rnext",
"]",
")",
"return",
"ring_map"
] | get a ring connection used to recover local data | [
"get",
"a",
"ring",
"connection",
"used",
"to",
"recover",
"local",
"data"
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py#L193-L206 |
28,669 | apple/turicreate | src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py | Tracker.get_link_map | def get_link_map(self, nslave):
"""
get the link map, this is a bit hacky, call for better algorithm
to place similar nodes together
"""
tree_map, parent_map = self.get_tree(nslave)
ring_map = self.get_ring(tree_map, parent_map)
rmap = {0 : 0}
k = 0
for i in range(nslave - 1):
k = ring_map[k][1]
rmap[k] = i + 1
ring_map_ = {}
tree_map_ = {}
parent_map_ ={}
for k, v in ring_map.items():
ring_map_[rmap[k]] = (rmap[v[0]], rmap[v[1]])
for k, v in tree_map.items():
tree_map_[rmap[k]] = [rmap[x] for x in v]
for k, v in parent_map.items():
if k != 0:
parent_map_[rmap[k]] = rmap[v]
else:
parent_map_[rmap[k]] = -1
return tree_map_, parent_map_, ring_map_ | python | def get_link_map(self, nslave):
"""
get the link map, this is a bit hacky, call for better algorithm
to place similar nodes together
"""
tree_map, parent_map = self.get_tree(nslave)
ring_map = self.get_ring(tree_map, parent_map)
rmap = {0 : 0}
k = 0
for i in range(nslave - 1):
k = ring_map[k][1]
rmap[k] = i + 1
ring_map_ = {}
tree_map_ = {}
parent_map_ ={}
for k, v in ring_map.items():
ring_map_[rmap[k]] = (rmap[v[0]], rmap[v[1]])
for k, v in tree_map.items():
tree_map_[rmap[k]] = [rmap[x] for x in v]
for k, v in parent_map.items():
if k != 0:
parent_map_[rmap[k]] = rmap[v]
else:
parent_map_[rmap[k]] = -1
return tree_map_, parent_map_, ring_map_ | [
"def",
"get_link_map",
"(",
"self",
",",
"nslave",
")",
":",
"tree_map",
",",
"parent_map",
"=",
"self",
".",
"get_tree",
"(",
"nslave",
")",
"ring_map",
"=",
"self",
".",
"get_ring",
"(",
"tree_map",
",",
"parent_map",
")",
"rmap",
"=",
"{",
"0",
":",
"0",
"}",
"k",
"=",
"0",
"for",
"i",
"in",
"range",
"(",
"nslave",
"-",
"1",
")",
":",
"k",
"=",
"ring_map",
"[",
"k",
"]",
"[",
"1",
"]",
"rmap",
"[",
"k",
"]",
"=",
"i",
"+",
"1",
"ring_map_",
"=",
"{",
"}",
"tree_map_",
"=",
"{",
"}",
"parent_map_",
"=",
"{",
"}",
"for",
"k",
",",
"v",
"in",
"ring_map",
".",
"items",
"(",
")",
":",
"ring_map_",
"[",
"rmap",
"[",
"k",
"]",
"]",
"=",
"(",
"rmap",
"[",
"v",
"[",
"0",
"]",
"]",
",",
"rmap",
"[",
"v",
"[",
"1",
"]",
"]",
")",
"for",
"k",
",",
"v",
"in",
"tree_map",
".",
"items",
"(",
")",
":",
"tree_map_",
"[",
"rmap",
"[",
"k",
"]",
"]",
"=",
"[",
"rmap",
"[",
"x",
"]",
"for",
"x",
"in",
"v",
"]",
"for",
"k",
",",
"v",
"in",
"parent_map",
".",
"items",
"(",
")",
":",
"if",
"k",
"!=",
"0",
":",
"parent_map_",
"[",
"rmap",
"[",
"k",
"]",
"]",
"=",
"rmap",
"[",
"v",
"]",
"else",
":",
"parent_map_",
"[",
"rmap",
"[",
"k",
"]",
"]",
"=",
"-",
"1",
"return",
"tree_map_",
",",
"parent_map_",
",",
"ring_map_"
] | get the link map, this is a bit hacky, call for better algorithm
to place similar nodes together | [
"get",
"the",
"link",
"map",
"this",
"is",
"a",
"bit",
"hacky",
"call",
"for",
"better",
"algorithm",
"to",
"place",
"similar",
"nodes",
"together"
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/xgboost/subtree/rabit/tracker/rabit_tracker.py#L208-L233 |
28,670 | apple/turicreate | deps/src/boost_1_68_0/tools/build/src/tools/msvc.py | maybe_rewrite_setup | def maybe_rewrite_setup(toolset, setup_script, setup_options, version, rewrite_setup='off'):
"""
Helper rule to generate a faster alternative to MSVC setup scripts.
We used to call MSVC setup scripts directly in every action, however in
newer MSVC versions (10.0+) they make long-lasting registry queries
which have a significant impact on build time.
"""
result = '"{}" {}'.format(setup_script, setup_options)
# At the moment we only know how to rewrite scripts with cmd shell.
if os.name == 'nt' and rewrite_setup != 'off':
basename = os.path.basename(setup_script)
filename, _ = os.path.splitext(basename)
setup_script_id = 'b2_{}_{}_{}'.format(toolset, version, filename)
if setup_options:
setup_script_id = '{}_{}'.format(setup_script_id, setup_options)
tempdir = os.environ.get('TEMP')
replacement = os.path.join(tempdir, setup_script_id + '.cmd')
if rewrite_setup == 'always' or not os.path.exists(replacement):
import subprocess
# call the setup script and print the environment after doing so
p = subprocess.Popen([
setup_script, setup_options, '>', 'nul', '&&', 'set',
], stdout=subprocess.PIPE, shell=True
)
stdout, _ = p.communicate()
diff_vars = []
for var in stdout.splitlines():
# returns a tuple of ('var-name', '=', 'value').
# partition is being used here (over something like .split())
# for two reasons:
# 1) an environment variable may have a value that contains an '=';
# .partition() will still return the correct key and value pair.
# 2) if the line doesn't contain an '=' at all, then the returned
# tuple will contain only empty strings rather than raising
# an exception.
key, _, value = var.partition('=')
# os.environ handles casing differences here. Usually the
# call to "set" above will produce pascal-cased environment
# variable names, so a normal python dict can't be used here.
# check for the existence of key in case the partitioning() above
# returned an empty key value pair.
if key and os.environ.get(key) != value:
diff_vars.append('SET {}={}'.format(key, value))
if diff_vars:
with open(replacement, 'wb') as f:
f.write(os.linesep.join(diff_vars))
result = '"{}"'.format(replacement)
else:
result = '"{}"'.format(replacement)
return result | python | def maybe_rewrite_setup(toolset, setup_script, setup_options, version, rewrite_setup='off'):
"""
Helper rule to generate a faster alternative to MSVC setup scripts.
We used to call MSVC setup scripts directly in every action, however in
newer MSVC versions (10.0+) they make long-lasting registry queries
which have a significant impact on build time.
"""
result = '"{}" {}'.format(setup_script, setup_options)
# At the moment we only know how to rewrite scripts with cmd shell.
if os.name == 'nt' and rewrite_setup != 'off':
basename = os.path.basename(setup_script)
filename, _ = os.path.splitext(basename)
setup_script_id = 'b2_{}_{}_{}'.format(toolset, version, filename)
if setup_options:
setup_script_id = '{}_{}'.format(setup_script_id, setup_options)
tempdir = os.environ.get('TEMP')
replacement = os.path.join(tempdir, setup_script_id + '.cmd')
if rewrite_setup == 'always' or not os.path.exists(replacement):
import subprocess
# call the setup script and print the environment after doing so
p = subprocess.Popen([
setup_script, setup_options, '>', 'nul', '&&', 'set',
], stdout=subprocess.PIPE, shell=True
)
stdout, _ = p.communicate()
diff_vars = []
for var in stdout.splitlines():
# returns a tuple of ('var-name', '=', 'value').
# partition is being used here (over something like .split())
# for two reasons:
# 1) an environment variable may have a value that contains an '=';
# .partition() will still return the correct key and value pair.
# 2) if the line doesn't contain an '=' at all, then the returned
# tuple will contain only empty strings rather than raising
# an exception.
key, _, value = var.partition('=')
# os.environ handles casing differences here. Usually the
# call to "set" above will produce pascal-cased environment
# variable names, so a normal python dict can't be used here.
# check for the existence of key in case the partitioning() above
# returned an empty key value pair.
if key and os.environ.get(key) != value:
diff_vars.append('SET {}={}'.format(key, value))
if diff_vars:
with open(replacement, 'wb') as f:
f.write(os.linesep.join(diff_vars))
result = '"{}"'.format(replacement)
else:
result = '"{}"'.format(replacement)
return result | [
"def",
"maybe_rewrite_setup",
"(",
"toolset",
",",
"setup_script",
",",
"setup_options",
",",
"version",
",",
"rewrite_setup",
"=",
"'off'",
")",
":",
"result",
"=",
"'\"{}\" {}'",
".",
"format",
"(",
"setup_script",
",",
"setup_options",
")",
"# At the moment we only know how to rewrite scripts with cmd shell.",
"if",
"os",
".",
"name",
"==",
"'nt'",
"and",
"rewrite_setup",
"!=",
"'off'",
":",
"basename",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"setup_script",
")",
"filename",
",",
"_",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"basename",
")",
"setup_script_id",
"=",
"'b2_{}_{}_{}'",
".",
"format",
"(",
"toolset",
",",
"version",
",",
"filename",
")",
"if",
"setup_options",
":",
"setup_script_id",
"=",
"'{}_{}'",
".",
"format",
"(",
"setup_script_id",
",",
"setup_options",
")",
"tempdir",
"=",
"os",
".",
"environ",
".",
"get",
"(",
"'TEMP'",
")",
"replacement",
"=",
"os",
".",
"path",
".",
"join",
"(",
"tempdir",
",",
"setup_script_id",
"+",
"'.cmd'",
")",
"if",
"rewrite_setup",
"==",
"'always'",
"or",
"not",
"os",
".",
"path",
".",
"exists",
"(",
"replacement",
")",
":",
"import",
"subprocess",
"# call the setup script and print the environment after doing so",
"p",
"=",
"subprocess",
".",
"Popen",
"(",
"[",
"setup_script",
",",
"setup_options",
",",
"'>'",
",",
"'nul'",
",",
"'&&'",
",",
"'set'",
",",
"]",
",",
"stdout",
"=",
"subprocess",
".",
"PIPE",
",",
"shell",
"=",
"True",
")",
"stdout",
",",
"_",
"=",
"p",
".",
"communicate",
"(",
")",
"diff_vars",
"=",
"[",
"]",
"for",
"var",
"in",
"stdout",
".",
"splitlines",
"(",
")",
":",
"# returns a tuple of ('var-name', '=', 'value').",
"# partition is being used here (over something like .split())",
"# for two reasons:",
"# 1) an environment variable may have a value that contains an '=';",
"# .partition() will still return the correct key and value pair.",
"# 2) if the line doesn't contain an '=' at all, then the returned",
"# tuple will contain only empty strings rather than raising",
"# an exception.",
"key",
",",
"_",
",",
"value",
"=",
"var",
".",
"partition",
"(",
"'='",
")",
"# os.environ handles casing differences here. Usually the",
"# call to \"set\" above will produce pascal-cased environment",
"# variable names, so a normal python dict can't be used here.",
"# check for the existence of key in case the partitioning() above",
"# returned an empty key value pair.",
"if",
"key",
"and",
"os",
".",
"environ",
".",
"get",
"(",
"key",
")",
"!=",
"value",
":",
"diff_vars",
".",
"append",
"(",
"'SET {}={}'",
".",
"format",
"(",
"key",
",",
"value",
")",
")",
"if",
"diff_vars",
":",
"with",
"open",
"(",
"replacement",
",",
"'wb'",
")",
"as",
"f",
":",
"f",
".",
"write",
"(",
"os",
".",
"linesep",
".",
"join",
"(",
"diff_vars",
")",
")",
"result",
"=",
"'\"{}\"'",
".",
"format",
"(",
"replacement",
")",
"else",
":",
"result",
"=",
"'\"{}\"'",
".",
"format",
"(",
"replacement",
")",
"return",
"result"
] | Helper rule to generate a faster alternative to MSVC setup scripts.
We used to call MSVC setup scripts directly in every action, however in
newer MSVC versions (10.0+) they make long-lasting registry queries
which have a significant impact on build time. | [
"Helper",
"rule",
"to",
"generate",
"a",
"faster",
"alternative",
"to",
"MSVC",
"setup",
"scripts",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/deps/src/boost_1_68_0/tools/build/src/tools/msvc.py#L626-L682 |
28,671 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/_classifier.py | create | def create(dataset, target, features=None, validation_set = 'auto',
verbose=True):
"""
Automatically create a suitable classifier model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : string
Name of the column containing the target variable. The values in this
column must be of string or integer type. String target variables are
automatically mapped to integers in the order in which they are
provided. For example, a target variable with 'cat' and 'dog' as
possible values is mapped to 0 and 1 respectively with 0 being the base
class and 1 being the reference class. Use `model.classes` to
retrieve the order in which the classes are mapped.
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained classifier model.
See Also
--------
turicreate.boosted_trees_classifier.BoostedTreesClassifier,
turicreate.logistic_classifier.LogisticClassifier,
turicreate.svm_classifier.SVMClassifier,
turicreate.nearest_neighbor_classifier.NearestNeighborClassifier
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
>>> data['is_expensive'] = data['price'] > 30000
# Selects the best model based on your data.
>>> model = tc.classifier.create(data, target='is_expensive',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.classify(data)
>>> results = model.evaluate(data)
"""
return _sl.create_classification_with_model_selector(
dataset,
target,
model_selector = _turicreate.extensions._supervised_learning._classifier_available_models,
features = features,
validation_set = validation_set,
verbose = verbose) | python | def create(dataset, target, features=None, validation_set = 'auto',
verbose=True):
"""
Automatically create a suitable classifier model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : string
Name of the column containing the target variable. The values in this
column must be of string or integer type. String target variables are
automatically mapped to integers in the order in which they are
provided. For example, a target variable with 'cat' and 'dog' as
possible values is mapped to 0 and 1 respectively with 0 being the base
class and 1 being the reference class. Use `model.classes` to
retrieve the order in which the classes are mapped.
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained classifier model.
See Also
--------
turicreate.boosted_trees_classifier.BoostedTreesClassifier,
turicreate.logistic_classifier.LogisticClassifier,
turicreate.svm_classifier.SVMClassifier,
turicreate.nearest_neighbor_classifier.NearestNeighborClassifier
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
>>> data['is_expensive'] = data['price'] > 30000
# Selects the best model based on your data.
>>> model = tc.classifier.create(data, target='is_expensive',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.classify(data)
>>> results = model.evaluate(data)
"""
return _sl.create_classification_with_model_selector(
dataset,
target,
model_selector = _turicreate.extensions._supervised_learning._classifier_available_models,
features = features,
validation_set = validation_set,
verbose = verbose) | [
"def",
"create",
"(",
"dataset",
",",
"target",
",",
"features",
"=",
"None",
",",
"validation_set",
"=",
"'auto'",
",",
"verbose",
"=",
"True",
")",
":",
"return",
"_sl",
".",
"create_classification_with_model_selector",
"(",
"dataset",
",",
"target",
",",
"model_selector",
"=",
"_turicreate",
".",
"extensions",
".",
"_supervised_learning",
".",
"_classifier_available_models",
",",
"features",
"=",
"features",
",",
"validation_set",
"=",
"validation_set",
",",
"verbose",
"=",
"verbose",
")"
] | Automatically create a suitable classifier model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : string
Name of the column containing the target variable. The values in this
column must be of string or integer type. String target variables are
automatically mapped to integers in the order in which they are
provided. For example, a target variable with 'cat' and 'dog' as
possible values is mapped to 0 and 1 respectively with 0 being the base
class and 1 being the reference class. Use `model.classes` to
retrieve the order in which the classes are mapped.
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained classifier model.
See Also
--------
turicreate.boosted_trees_classifier.BoostedTreesClassifier,
turicreate.logistic_classifier.LogisticClassifier,
turicreate.svm_classifier.SVMClassifier,
turicreate.nearest_neighbor_classifier.NearestNeighborClassifier
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
>>> data['is_expensive'] = data['price'] > 30000
# Selects the best model based on your data.
>>> model = tc.classifier.create(data, target='is_expensive',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.classify(data)
>>> results = model.evaluate(data) | [
"Automatically",
"create",
"a",
"suitable",
"classifier",
"model",
"based",
"on",
"the",
"provided",
"training",
"data",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/_classifier.py#L12-L106 |
28,672 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.add_column | def add_column(self, data, column_name="", inplace=False):
"""
Adds the specified column to this SFrame. The number of elements in
the data given must match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : SArray
The 'column' of data.
column_name : string
The name of the column. If no name is given, a default name is chosen.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
# Check type for pandas dataframe or SArray?
if not isinstance(data, SArray):
raise TypeError("Must give column as SArray")
if not isinstance(column_name, str):
raise TypeError("Invalid column name: must be str")
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.add_vertex_field(data.__proxy__, column_name)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.add_edge_field(data.__proxy__, column_name)
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).add_column(data, column_name, inplace=inplace) | python | def add_column(self, data, column_name="", inplace=False):
"""
Adds the specified column to this SFrame. The number of elements in
the data given must match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : SArray
The 'column' of data.
column_name : string
The name of the column. If no name is given, a default name is chosen.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
# Check type for pandas dataframe or SArray?
if not isinstance(data, SArray):
raise TypeError("Must give column as SArray")
if not isinstance(column_name, str):
raise TypeError("Invalid column name: must be str")
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.add_vertex_field(data.__proxy__, column_name)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.add_edge_field(data.__proxy__, column_name)
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).add_column(data, column_name, inplace=inplace) | [
"def",
"add_column",
"(",
"self",
",",
"data",
",",
"column_name",
"=",
"\"\"",
",",
"inplace",
"=",
"False",
")",
":",
"# Check type for pandas dataframe or SArray?",
"if",
"not",
"isinstance",
"(",
"data",
",",
"SArray",
")",
":",
"raise",
"TypeError",
"(",
"\"Must give column as SArray\"",
")",
"if",
"not",
"isinstance",
"(",
"column_name",
",",
"str",
")",
":",
"raise",
"TypeError",
"(",
"\"Invalid column name: must be str\"",
")",
"if",
"inplace",
":",
"self",
".",
"__is_dirty__",
"=",
"True",
"with",
"cython_context",
"(",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"add_vertex_field",
"(",
"data",
".",
"__proxy__",
",",
"column_name",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"add_edge_field",
"(",
"data",
".",
"__proxy__",
",",
"column_name",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"return",
"self",
"else",
":",
"return",
"super",
"(",
"GFrame",
",",
"self",
")",
".",
"add_column",
"(",
"data",
",",
"column_name",
",",
"inplace",
"=",
"inplace",
")"
] | Adds the specified column to this SFrame. The number of elements in
the data given must match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : SArray
The 'column' of data.
column_name : string
The name of the column. If no name is given, a default name is chosen.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place. | [
"Adds",
"the",
"specified",
"column",
"to",
"this",
"SFrame",
".",
"The",
"number",
"of",
"elements",
"in",
"the",
"data",
"given",
"must",
"match",
"every",
"other",
"column",
"of",
"the",
"SFrame",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L62-L101 |
28,673 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.add_columns | def add_columns(self, data, column_names=None, inplace=False):
"""
Adds columns to the SFrame. The number of elements in all columns must
match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : list[SArray] or SFrame
The columns to add.
column_names: list of string, optional
A list of column names. All names must be specified. ``column_names`` is
ignored if data is an SFrame.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
datalist = data
if isinstance(data, SFrame):
other = data
datalist = [other.select_column(name) for name in other.column_names()]
column_names = other.column_names()
my_columns = set(self.column_names())
for name in column_names:
if name in my_columns:
raise ValueError("Column '" + name + "' already exists in current SFrame")
else:
if not _is_non_string_iterable(datalist):
raise TypeError("datalist must be an iterable")
if not _is_non_string_iterable(column_names):
raise TypeError("column_names must be an iterable")
if not all([isinstance(x, SArray) for x in datalist]):
raise TypeError("Must give column as SArray")
if not all([isinstance(x, str) for x in column_names]):
raise TypeError("Invalid column name in list : must all be str")
if inplace:
for (data, name) in zip(datalist, column_names):
self.add_column(data, name)
return self
else:
return super(GFrame, self).add_column(datalist, column_names, inplace=inplace) | python | def add_columns(self, data, column_names=None, inplace=False):
"""
Adds columns to the SFrame. The number of elements in all columns must
match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : list[SArray] or SFrame
The columns to add.
column_names: list of string, optional
A list of column names. All names must be specified. ``column_names`` is
ignored if data is an SFrame.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
datalist = data
if isinstance(data, SFrame):
other = data
datalist = [other.select_column(name) for name in other.column_names()]
column_names = other.column_names()
my_columns = set(self.column_names())
for name in column_names:
if name in my_columns:
raise ValueError("Column '" + name + "' already exists in current SFrame")
else:
if not _is_non_string_iterable(datalist):
raise TypeError("datalist must be an iterable")
if not _is_non_string_iterable(column_names):
raise TypeError("column_names must be an iterable")
if not all([isinstance(x, SArray) for x in datalist]):
raise TypeError("Must give column as SArray")
if not all([isinstance(x, str) for x in column_names]):
raise TypeError("Invalid column name in list : must all be str")
if inplace:
for (data, name) in zip(datalist, column_names):
self.add_column(data, name)
return self
else:
return super(GFrame, self).add_column(datalist, column_names, inplace=inplace) | [
"def",
"add_columns",
"(",
"self",
",",
"data",
",",
"column_names",
"=",
"None",
",",
"inplace",
"=",
"False",
")",
":",
"datalist",
"=",
"data",
"if",
"isinstance",
"(",
"data",
",",
"SFrame",
")",
":",
"other",
"=",
"data",
"datalist",
"=",
"[",
"other",
".",
"select_column",
"(",
"name",
")",
"for",
"name",
"in",
"other",
".",
"column_names",
"(",
")",
"]",
"column_names",
"=",
"other",
".",
"column_names",
"(",
")",
"my_columns",
"=",
"set",
"(",
"self",
".",
"column_names",
"(",
")",
")",
"for",
"name",
"in",
"column_names",
":",
"if",
"name",
"in",
"my_columns",
":",
"raise",
"ValueError",
"(",
"\"Column '\"",
"+",
"name",
"+",
"\"' already exists in current SFrame\"",
")",
"else",
":",
"if",
"not",
"_is_non_string_iterable",
"(",
"datalist",
")",
":",
"raise",
"TypeError",
"(",
"\"datalist must be an iterable\"",
")",
"if",
"not",
"_is_non_string_iterable",
"(",
"column_names",
")",
":",
"raise",
"TypeError",
"(",
"\"column_names must be an iterable\"",
")",
"if",
"not",
"all",
"(",
"[",
"isinstance",
"(",
"x",
",",
"SArray",
")",
"for",
"x",
"in",
"datalist",
"]",
")",
":",
"raise",
"TypeError",
"(",
"\"Must give column as SArray\"",
")",
"if",
"not",
"all",
"(",
"[",
"isinstance",
"(",
"x",
",",
"str",
")",
"for",
"x",
"in",
"column_names",
"]",
")",
":",
"raise",
"TypeError",
"(",
"\"Invalid column name in list : must all be str\"",
")",
"if",
"inplace",
":",
"for",
"(",
"data",
",",
"name",
")",
"in",
"zip",
"(",
"datalist",
",",
"column_names",
")",
":",
"self",
".",
"add_column",
"(",
"data",
",",
"name",
")",
"return",
"self",
"else",
":",
"return",
"super",
"(",
"GFrame",
",",
"self",
")",
".",
"add_column",
"(",
"datalist",
",",
"column_names",
",",
"inplace",
"=",
"inplace",
")"
] | Adds columns to the SFrame. The number of elements in all columns must
match every other column of the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
data : list[SArray] or SFrame
The columns to add.
column_names: list of string, optional
A list of column names. All names must be specified. ``column_names`` is
ignored if data is an SFrame.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place. | [
"Adds",
"columns",
"to",
"the",
"SFrame",
".",
"The",
"number",
"of",
"elements",
"in",
"all",
"columns",
"must",
"match",
"every",
"other",
"column",
"of",
"the",
"SFrame",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L104-L154 |
28,674 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.remove_column | def remove_column(self, column_name, inplace=False):
"""
Removes the column with the given name from the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name : string
The name of the column to remove.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if column_name not in self.column_names():
raise KeyError('Cannot find column %s' % column_name)
if inplace:
self.__is_dirty__ = True
try:
with cython_context():
if self._is_vertex_frame():
assert column_name != '__id', 'Cannot remove \"__id\" column'
graph_proxy = self.__graph__.__proxy__.delete_vertex_field(column_name)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
assert column_name != '__src_id', 'Cannot remove \"__src_id\" column'
assert column_name != '__dst_id', 'Cannot remove \"__dst_id\" column'
graph_proxy = self.__graph__.__proxy__.delete_edge_field(column_name)
self.__graph__.__proxy__ = graph_proxy
return self
except:
self.__is_dirty__ = False
raise
else:
return super(GFrame, self).remove_column(column_name, inplace=inplace) | python | def remove_column(self, column_name, inplace=False):
"""
Removes the column with the given name from the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name : string
The name of the column to remove.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if column_name not in self.column_names():
raise KeyError('Cannot find column %s' % column_name)
if inplace:
self.__is_dirty__ = True
try:
with cython_context():
if self._is_vertex_frame():
assert column_name != '__id', 'Cannot remove \"__id\" column'
graph_proxy = self.__graph__.__proxy__.delete_vertex_field(column_name)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
assert column_name != '__src_id', 'Cannot remove \"__src_id\" column'
assert column_name != '__dst_id', 'Cannot remove \"__dst_id\" column'
graph_proxy = self.__graph__.__proxy__.delete_edge_field(column_name)
self.__graph__.__proxy__ = graph_proxy
return self
except:
self.__is_dirty__ = False
raise
else:
return super(GFrame, self).remove_column(column_name, inplace=inplace) | [
"def",
"remove_column",
"(",
"self",
",",
"column_name",
",",
"inplace",
"=",
"False",
")",
":",
"if",
"column_name",
"not",
"in",
"self",
".",
"column_names",
"(",
")",
":",
"raise",
"KeyError",
"(",
"'Cannot find column %s'",
"%",
"column_name",
")",
"if",
"inplace",
":",
"self",
".",
"__is_dirty__",
"=",
"True",
"try",
":",
"with",
"cython_context",
"(",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"assert",
"column_name",
"!=",
"'__id'",
",",
"'Cannot remove \\\"__id\\\" column'",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"delete_vertex_field",
"(",
"column_name",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"assert",
"column_name",
"!=",
"'__src_id'",
",",
"'Cannot remove \\\"__src_id\\\" column'",
"assert",
"column_name",
"!=",
"'__dst_id'",
",",
"'Cannot remove \\\"__dst_id\\\" column'",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"delete_edge_field",
"(",
"column_name",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"return",
"self",
"except",
":",
"self",
".",
"__is_dirty__",
"=",
"False",
"raise",
"else",
":",
"return",
"super",
"(",
"GFrame",
",",
"self",
")",
".",
"remove_column",
"(",
"column_name",
",",
"inplace",
"=",
"inplace",
")"
] | Removes the column with the given name from the SFrame.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name : string
The name of the column to remove.
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place. | [
"Removes",
"the",
"column",
"with",
"the",
"given",
"name",
"from",
"the",
"SFrame",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L157-L195 |
28,675 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.swap_columns | def swap_columns(self, column_name_1, column_name_2, inplace=False):
"""
Swaps the columns with the given names.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name_1 : string
Name of column to swap
column_name_2 : string
Name of other column to swap
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.swap_vertex_fields(column_name_1, column_name_2)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.swap_edge_fields(column_name_1, column_name_2)
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).swap_columns(column_name_1, column_name_2, inplace=inplace) | python | def swap_columns(self, column_name_1, column_name_2, inplace=False):
"""
Swaps the columns with the given names.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name_1 : string
Name of column to swap
column_name_2 : string
Name of other column to swap
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.swap_vertex_fields(column_name_1, column_name_2)
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.swap_edge_fields(column_name_1, column_name_2)
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).swap_columns(column_name_1, column_name_2, inplace=inplace) | [
"def",
"swap_columns",
"(",
"self",
",",
"column_name_1",
",",
"column_name_2",
",",
"inplace",
"=",
"False",
")",
":",
"if",
"inplace",
":",
"self",
".",
"__is_dirty__",
"=",
"True",
"with",
"cython_context",
"(",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"swap_vertex_fields",
"(",
"column_name_1",
",",
"column_name_2",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"swap_edge_fields",
"(",
"column_name_1",
",",
"column_name_2",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"return",
"self",
"else",
":",
"return",
"super",
"(",
"GFrame",
",",
"self",
")",
".",
"swap_columns",
"(",
"column_name_1",
",",
"column_name_2",
",",
"inplace",
"=",
"inplace",
")"
] | Swaps the columns with the given names.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
column_name_1 : string
Name of column to swap
column_name_2 : string
Name of other column to swap
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place. | [
"Swaps",
"the",
"columns",
"with",
"the",
"given",
"names",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L211-L243 |
28,676 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.rename | def rename(self, names, inplace=False):
"""
Rename the columns using the 'names' dict. This changes the names of
the columns given as the keys and replaces them with the names given as
the values.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
names : dict[string, string]
Dictionary of [old_name, new_name]
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if (type(names) is not dict):
raise TypeError('names must be a dictionary: oldname -> newname')
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.rename_vertex_fields(names.keys(), names.values())
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.rename_edge_fields(names.keys(), names.values())
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).rename(names, inplace=inplace) | python | def rename(self, names, inplace=False):
"""
Rename the columns using the 'names' dict. This changes the names of
the columns given as the keys and replaces them with the names given as
the values.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
names : dict[string, string]
Dictionary of [old_name, new_name]
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place.
"""
if (type(names) is not dict):
raise TypeError('names must be a dictionary: oldname -> newname')
if inplace:
self.__is_dirty__ = True
with cython_context():
if self._is_vertex_frame():
graph_proxy = self.__graph__.__proxy__.rename_vertex_fields(names.keys(), names.values())
self.__graph__.__proxy__ = graph_proxy
elif self._is_edge_frame():
graph_proxy = self.__graph__.__proxy__.rename_edge_fields(names.keys(), names.values())
self.__graph__.__proxy__ = graph_proxy
return self
else:
return super(GFrame, self).rename(names, inplace=inplace) | [
"def",
"rename",
"(",
"self",
",",
"names",
",",
"inplace",
"=",
"False",
")",
":",
"if",
"(",
"type",
"(",
"names",
")",
"is",
"not",
"dict",
")",
":",
"raise",
"TypeError",
"(",
"'names must be a dictionary: oldname -> newname'",
")",
"if",
"inplace",
":",
"self",
".",
"__is_dirty__",
"=",
"True",
"with",
"cython_context",
"(",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"rename_vertex_fields",
"(",
"names",
".",
"keys",
"(",
")",
",",
"names",
".",
"values",
"(",
")",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"graph_proxy",
"=",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"rename_edge_fields",
"(",
"names",
".",
"keys",
"(",
")",
",",
"names",
".",
"values",
"(",
")",
")",
"self",
".",
"__graph__",
".",
"__proxy__",
"=",
"graph_proxy",
"return",
"self",
"else",
":",
"return",
"super",
"(",
"GFrame",
",",
"self",
")",
".",
"rename",
"(",
"names",
",",
"inplace",
"=",
"inplace",
")"
] | Rename the columns using the 'names' dict. This changes the names of
the columns given as the keys and replaces them with the names given as
the values.
If inplace == False (default) this operation does not modify the
current SFrame, returning a new SFrame.
If inplace == True, this operation modifies the current
SFrame, returning self.
Parameters
----------
names : dict[string, string]
Dictionary of [old_name, new_name]
inplace : bool, optional. Defaults to False.
Whether the SFrame is modified in place. | [
"Rename",
"the",
"columns",
"using",
"the",
"names",
"dict",
".",
"This",
"changes",
"the",
"names",
"of",
"the",
"columns",
"given",
"as",
"the",
"keys",
"and",
"replaces",
"them",
"with",
"the",
"names",
"given",
"as",
"the",
"values",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L245-L279 |
28,677 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.num_rows | def num_rows(self):
"""
Returns the number of rows.
Returns
-------
out : int
Number of rows in the SFrame.
"""
if self._is_vertex_frame():
return self.__graph__.summary()['num_vertices']
elif self._is_edge_frame():
return self.__graph__.summary()['num_edges'] | python | def num_rows(self):
"""
Returns the number of rows.
Returns
-------
out : int
Number of rows in the SFrame.
"""
if self._is_vertex_frame():
return self.__graph__.summary()['num_vertices']
elif self._is_edge_frame():
return self.__graph__.summary()['num_edges'] | [
"def",
"num_rows",
"(",
"self",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"return",
"self",
".",
"__graph__",
".",
"summary",
"(",
")",
"[",
"'num_vertices'",
"]",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"return",
"self",
".",
"__graph__",
".",
"summary",
"(",
")",
"[",
"'num_edges'",
"]"
] | Returns the number of rows.
Returns
-------
out : int
Number of rows in the SFrame. | [
"Returns",
"the",
"number",
"of",
"rows",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L321-L333 |
28,678 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.column_names | def column_names(self):
"""
Returns the column names.
Returns
-------
out : list[string]
Column names of the SFrame.
"""
if self._is_vertex_frame():
return self.__graph__.__proxy__.get_vertex_fields()
elif self._is_edge_frame():
return self.__graph__.__proxy__.get_edge_fields() | python | def column_names(self):
"""
Returns the column names.
Returns
-------
out : list[string]
Column names of the SFrame.
"""
if self._is_vertex_frame():
return self.__graph__.__proxy__.get_vertex_fields()
elif self._is_edge_frame():
return self.__graph__.__proxy__.get_edge_fields() | [
"def",
"column_names",
"(",
"self",
")",
":",
"if",
"self",
".",
"_is_vertex_frame",
"(",
")",
":",
"return",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"get_vertex_fields",
"(",
")",
"elif",
"self",
".",
"_is_edge_frame",
"(",
")",
":",
"return",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"get_edge_fields",
"(",
")"
] | Returns the column names.
Returns
-------
out : list[string]
Column names of the SFrame. | [
"Returns",
"the",
"column",
"names",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L346-L358 |
28,679 | apple/turicreate | src/unity/python/turicreate/data_structures/gframe.py | GFrame.column_types | def column_types(self):
"""
Returns the column types.
Returns
-------
out : list[type]
Column types of the SFrame.
"""
if self.__type__ == VERTEX_GFRAME:
return self.__graph__.__proxy__.get_vertex_field_types()
elif self.__type__ == EDGE_GFRAME:
return self.__graph__.__proxy__.get_edge_field_types() | python | def column_types(self):
"""
Returns the column types.
Returns
-------
out : list[type]
Column types of the SFrame.
"""
if self.__type__ == VERTEX_GFRAME:
return self.__graph__.__proxy__.get_vertex_field_types()
elif self.__type__ == EDGE_GFRAME:
return self.__graph__.__proxy__.get_edge_field_types() | [
"def",
"column_types",
"(",
"self",
")",
":",
"if",
"self",
".",
"__type__",
"==",
"VERTEX_GFRAME",
":",
"return",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"get_vertex_field_types",
"(",
")",
"elif",
"self",
".",
"__type__",
"==",
"EDGE_GFRAME",
":",
"return",
"self",
".",
"__graph__",
".",
"__proxy__",
".",
"get_edge_field_types",
"(",
")"
] | Returns the column types.
Returns
-------
out : list[type]
Column types of the SFrame. | [
"Returns",
"the",
"column",
"types",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/data_structures/gframe.py#L360-L372 |
28,680 | apple/turicreate | src/unity/python/turicreate/toolkits/regression/_regression.py | create | def create(dataset, target, features=None, validation_set = 'auto',
verbose=True):
"""
Automatically create a suitable regression model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : str
The name of the column in ``dataset`` that is the prediction target.
This column must have a numeric type (int/float).
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained regression model.
See Also
--------
turicreate.linear_regression.LinearRegression,
turicreate.boosted_trees_regression.BoostedTreesRegression
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data)
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data)
"""
dataset, validation_set = _validate_data(dataset, target, features,
validation_set)
if validation_set is None:
validation_set = _turicreate.SFrame()
model_proxy = _turicreate.extensions.create_automatic_regression_model(
dataset, target, validation_set, {})
return _sl.wrap_model_proxy(model_proxy) | python | def create(dataset, target, features=None, validation_set = 'auto',
verbose=True):
"""
Automatically create a suitable regression model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : str
The name of the column in ``dataset`` that is the prediction target.
This column must have a numeric type (int/float).
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained regression model.
See Also
--------
turicreate.linear_regression.LinearRegression,
turicreate.boosted_trees_regression.BoostedTreesRegression
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data)
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data)
"""
dataset, validation_set = _validate_data(dataset, target, features,
validation_set)
if validation_set is None:
validation_set = _turicreate.SFrame()
model_proxy = _turicreate.extensions.create_automatic_regression_model(
dataset, target, validation_set, {})
return _sl.wrap_model_proxy(model_proxy) | [
"def",
"create",
"(",
"dataset",
",",
"target",
",",
"features",
"=",
"None",
",",
"validation_set",
"=",
"'auto'",
",",
"verbose",
"=",
"True",
")",
":",
"dataset",
",",
"validation_set",
"=",
"_validate_data",
"(",
"dataset",
",",
"target",
",",
"features",
",",
"validation_set",
")",
"if",
"validation_set",
"is",
"None",
":",
"validation_set",
"=",
"_turicreate",
".",
"SFrame",
"(",
")",
"model_proxy",
"=",
"_turicreate",
".",
"extensions",
".",
"create_automatic_regression_model",
"(",
"dataset",
",",
"target",
",",
"validation_set",
",",
"{",
"}",
")",
"return",
"_sl",
".",
"wrap_model_proxy",
"(",
"model_proxy",
")"
] | Automatically create a suitable regression model based on the provided
training data.
To use specific options of a desired model, use the ``create`` function
of the corresponding model.
Parameters
----------
dataset : SFrame
Dataset for training the model.
target : str
The name of the column in ``dataset`` that is the prediction target.
This column must have a numeric type (int/float).
features : list[string], optional
Names of the columns containing features. 'None' (the default) indicates
that all columns except the target variable should be used as features.
The features are columns in the input SFrame that can be of the
following types:
- *Numeric*: values of numeric type integer or float.
- *Categorical*: values of type string.
- *Array*: list of numeric (integer or float) values. Each list element
is treated as a separate feature in the model.
- *Dictionary*: key-value pairs with numeric (integer or float) values
Each key of a dictionary is treated as a separate feature and the
value in the dictionary corresponds to the value of the feature.
Dictionaries are ideal for representing sparse data.
Columns of type *list* are not supported. Convert such feature
columns to type array if all entries in the list are of numeric
types. If the lists contain data of mixed types, separate
them out into different columns.
validation_set : SFrame, optional
A dataset for monitoring the model's generalization performance. For
each row of the progress table, the chosen metrics are computed for
both the provided training dataset and the validation_set. The format
of this SFrame must be the same as the training set. By default this
argument is set to 'auto' and a validation set is automatically sampled
and used for progress printing. If validation_set is set to None, then
no additional metrics are computed. The default value is 'auto'.
verbose : boolean, optional
If True, print progress information during training.
Returns
-------
out : A trained regression model.
See Also
--------
turicreate.linear_regression.LinearRegression,
turicreate.boosted_trees_regression.BoostedTreesRegression
Examples
--------
.. sourcecode:: python
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data)
# Setup the data
>>> import turicreate as tc
>>> data = tc.SFrame('https://static.turi.com/datasets/regression/houses.csv')
# Selects the best model based on your data.
>>> model = tc.regression.create(data, target='price',
... features=['bath', 'bedroom', 'size'])
# Make predictions and evaluate results.
>>> predictions = model.predict(data)
>>> results = model.evaluate(data) | [
"Automatically",
"create",
"a",
"suitable",
"regression",
"model",
"based",
"on",
"the",
"provided",
"training",
"data",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/regression/_regression.py#L14-L116 |
28,681 | apple/turicreate | src/unity/python/turicreate/meta/asttools/mutators/prune_mutator.py | removable | def removable(self, node):
'''
node is removable only if all of its children are as well.
'''
throw_away = []
for child in self.children(node):
throw_away.append(self.visit(child))
if self.mode == 'exclusive':
return all(throw_away)
elif self.mode == 'inclusive':
return any(throw_away)
else:
raise TypeError("mode must be one of 'exclusive' or 'inclusive'") | python | def removable(self, node):
'''
node is removable only if all of its children are as well.
'''
throw_away = []
for child in self.children(node):
throw_away.append(self.visit(child))
if self.mode == 'exclusive':
return all(throw_away)
elif self.mode == 'inclusive':
return any(throw_away)
else:
raise TypeError("mode must be one of 'exclusive' or 'inclusive'") | [
"def",
"removable",
"(",
"self",
",",
"node",
")",
":",
"throw_away",
"=",
"[",
"]",
"for",
"child",
"in",
"self",
".",
"children",
"(",
"node",
")",
":",
"throw_away",
".",
"append",
"(",
"self",
".",
"visit",
"(",
"child",
")",
")",
"if",
"self",
".",
"mode",
"==",
"'exclusive'",
":",
"return",
"all",
"(",
"throw_away",
")",
"elif",
"self",
".",
"mode",
"==",
"'inclusive'",
":",
"return",
"any",
"(",
"throw_away",
")",
"else",
":",
"raise",
"TypeError",
"(",
"\"mode must be one of 'exclusive' or 'inclusive'\"",
")"
] | node is removable only if all of its children are as well. | [
"node",
"is",
"removable",
"only",
"if",
"all",
"of",
"its",
"children",
"are",
"as",
"well",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/meta/asttools/mutators/prune_mutator.py#L17-L30 |
28,682 | apple/turicreate | src/unity/python/turicreate/meta/asttools/mutators/prune_mutator.py | PruneVisitor.reduce | def reduce(self, body):
'''
remove nodes from a list
'''
i = 0
while i < len(body):
stmnt = body[i]
if self.visit(stmnt):
body.pop(i)
else:
i += 1 | python | def reduce(self, body):
'''
remove nodes from a list
'''
i = 0
while i < len(body):
stmnt = body[i]
if self.visit(stmnt):
body.pop(i)
else:
i += 1 | [
"def",
"reduce",
"(",
"self",
",",
"body",
")",
":",
"i",
"=",
"0",
"while",
"i",
"<",
"len",
"(",
"body",
")",
":",
"stmnt",
"=",
"body",
"[",
"i",
"]",
"if",
"self",
".",
"visit",
"(",
"stmnt",
")",
":",
"body",
".",
"pop",
"(",
"i",
")",
"else",
":",
"i",
"+=",
"1"
] | remove nodes from a list | [
"remove",
"nodes",
"from",
"a",
"list"
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/meta/asttools/mutators/prune_mutator.py#L52-L62 |
28,683 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/symbol_database.py | SymbolDatabase.RegisterMessage | def RegisterMessage(self, message):
"""Registers the given message type in the local database.
Calls to GetSymbol() and GetMessages() will return messages registered here.
Args:
message: a message.Message, to be registered.
Returns:
The provided message.
"""
desc = message.DESCRIPTOR
self._classes[desc.full_name] = message
self.pool.AddDescriptor(desc)
return message | python | def RegisterMessage(self, message):
"""Registers the given message type in the local database.
Calls to GetSymbol() and GetMessages() will return messages registered here.
Args:
message: a message.Message, to be registered.
Returns:
The provided message.
"""
desc = message.DESCRIPTOR
self._classes[desc.full_name] = message
self.pool.AddDescriptor(desc)
return message | [
"def",
"RegisterMessage",
"(",
"self",
",",
"message",
")",
":",
"desc",
"=",
"message",
".",
"DESCRIPTOR",
"self",
".",
"_classes",
"[",
"desc",
".",
"full_name",
"]",
"=",
"message",
"self",
".",
"pool",
".",
"AddDescriptor",
"(",
"desc",
")",
"return",
"message"
] | Registers the given message type in the local database.
Calls to GetSymbol() and GetMessages() will return messages registered here.
Args:
message: a message.Message, to be registered.
Returns:
The provided message. | [
"Registers",
"the",
"given",
"message",
"type",
"in",
"the",
"local",
"database",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/symbol_database.py#L68-L83 |
28,684 | apple/turicreate | src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/symbol_database.py | SymbolDatabase.GetMessages | def GetMessages(self, files):
# TODO(amauryfa): Fix the differences with MessageFactory.
"""Gets all registered messages from a specified file.
Only messages already created and registered will be returned; (this is the
case for imported _pb2 modules)
But unlike MessageFactory, this version also returns already defined nested
messages, but does not register any message extensions.
Args:
files: The file names to extract messages from.
Returns:
A dictionary mapping proto names to the message classes.
Raises:
KeyError: if a file could not be found.
"""
def _GetAllMessageNames(desc):
"""Walk a message Descriptor and recursively yields all message names."""
yield desc.full_name
for msg_desc in desc.nested_types:
for full_name in _GetAllMessageNames(msg_desc):
yield full_name
result = {}
for file_name in files:
file_desc = self.pool.FindFileByName(file_name)
for msg_desc in file_desc.message_types_by_name.values():
for full_name in _GetAllMessageNames(msg_desc):
try:
result[full_name] = self._classes[full_name]
except KeyError:
# This descriptor has no registered class, skip it.
pass
return result | python | def GetMessages(self, files):
# TODO(amauryfa): Fix the differences with MessageFactory.
"""Gets all registered messages from a specified file.
Only messages already created and registered will be returned; (this is the
case for imported _pb2 modules)
But unlike MessageFactory, this version also returns already defined nested
messages, but does not register any message extensions.
Args:
files: The file names to extract messages from.
Returns:
A dictionary mapping proto names to the message classes.
Raises:
KeyError: if a file could not be found.
"""
def _GetAllMessageNames(desc):
"""Walk a message Descriptor and recursively yields all message names."""
yield desc.full_name
for msg_desc in desc.nested_types:
for full_name in _GetAllMessageNames(msg_desc):
yield full_name
result = {}
for file_name in files:
file_desc = self.pool.FindFileByName(file_name)
for msg_desc in file_desc.message_types_by_name.values():
for full_name in _GetAllMessageNames(msg_desc):
try:
result[full_name] = self._classes[full_name]
except KeyError:
# This descriptor has no registered class, skip it.
pass
return result | [
"def",
"GetMessages",
"(",
"self",
",",
"files",
")",
":",
"# TODO(amauryfa): Fix the differences with MessageFactory.",
"def",
"_GetAllMessageNames",
"(",
"desc",
")",
":",
"\"\"\"Walk a message Descriptor and recursively yields all message names.\"\"\"",
"yield",
"desc",
".",
"full_name",
"for",
"msg_desc",
"in",
"desc",
".",
"nested_types",
":",
"for",
"full_name",
"in",
"_GetAllMessageNames",
"(",
"msg_desc",
")",
":",
"yield",
"full_name",
"result",
"=",
"{",
"}",
"for",
"file_name",
"in",
"files",
":",
"file_desc",
"=",
"self",
".",
"pool",
".",
"FindFileByName",
"(",
"file_name",
")",
"for",
"msg_desc",
"in",
"file_desc",
".",
"message_types_by_name",
".",
"values",
"(",
")",
":",
"for",
"full_name",
"in",
"_GetAllMessageNames",
"(",
"msg_desc",
")",
":",
"try",
":",
"result",
"[",
"full_name",
"]",
"=",
"self",
".",
"_classes",
"[",
"full_name",
"]",
"except",
"KeyError",
":",
"# This descriptor has no registered class, skip it.",
"pass",
"return",
"result"
] | Gets all registered messages from a specified file.
Only messages already created and registered will be returned; (this is the
case for imported _pb2 modules)
But unlike MessageFactory, this version also returns already defined nested
messages, but does not register any message extensions.
Args:
files: The file names to extract messages from.
Returns:
A dictionary mapping proto names to the message classes.
Raises:
KeyError: if a file could not be found. | [
"Gets",
"all",
"registered",
"messages",
"from",
"a",
"specified",
"file",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/deps/protobuf/python/google/protobuf/symbol_database.py#L137-L173 |
28,685 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | _check_prob_and_prob_vector | def _check_prob_and_prob_vector(predictions):
"""
Check that the predictionsa are either probabilities of prob-vectors.
"""
from .._deps import numpy
ptype = predictions.dtype
import array
if ptype not in [float, numpy.ndarray, array.array, int]:
err_msg = "Input `predictions` must be of numeric type (for binary "
err_msg += "classification) or array (of probability vectors) for "
err_msg += "multiclass classification."
raise TypeError(err_msg) | python | def _check_prob_and_prob_vector(predictions):
"""
Check that the predictionsa are either probabilities of prob-vectors.
"""
from .._deps import numpy
ptype = predictions.dtype
import array
if ptype not in [float, numpy.ndarray, array.array, int]:
err_msg = "Input `predictions` must be of numeric type (for binary "
err_msg += "classification) or array (of probability vectors) for "
err_msg += "multiclass classification."
raise TypeError(err_msg) | [
"def",
"_check_prob_and_prob_vector",
"(",
"predictions",
")",
":",
"from",
".",
".",
"_deps",
"import",
"numpy",
"ptype",
"=",
"predictions",
".",
"dtype",
"import",
"array",
"if",
"ptype",
"not",
"in",
"[",
"float",
",",
"numpy",
".",
"ndarray",
",",
"array",
".",
"array",
",",
"int",
"]",
":",
"err_msg",
"=",
"\"Input `predictions` must be of numeric type (for binary \"",
"err_msg",
"+=",
"\"classification) or array (of probability vectors) for \"",
"err_msg",
"+=",
"\"multiclass classification.\"",
"raise",
"TypeError",
"(",
"err_msg",
")"
] | Check that the predictionsa are either probabilities of prob-vectors. | [
"Check",
"that",
"the",
"predictionsa",
"are",
"either",
"probabilities",
"of",
"prob",
"-",
"vectors",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L36-L48 |
28,686 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | _supervised_evaluation_error_checking | def _supervised_evaluation_error_checking(targets, predictions):
"""
Perform basic error checking for the evaluation metrics. Check
types and sizes of the inputs.
"""
_raise_error_if_not_sarray(targets, "targets")
_raise_error_if_not_sarray(predictions, "predictions")
if (len(targets) != len(predictions)):
raise _ToolkitError(
"Input SArrays 'targets' and 'predictions' must be of the same length.") | python | def _supervised_evaluation_error_checking(targets, predictions):
"""
Perform basic error checking for the evaluation metrics. Check
types and sizes of the inputs.
"""
_raise_error_if_not_sarray(targets, "targets")
_raise_error_if_not_sarray(predictions, "predictions")
if (len(targets) != len(predictions)):
raise _ToolkitError(
"Input SArrays 'targets' and 'predictions' must be of the same length.") | [
"def",
"_supervised_evaluation_error_checking",
"(",
"targets",
",",
"predictions",
")",
":",
"_raise_error_if_not_sarray",
"(",
"targets",
",",
"\"targets\"",
")",
"_raise_error_if_not_sarray",
"(",
"predictions",
",",
"\"predictions\"",
")",
"if",
"(",
"len",
"(",
"targets",
")",
"!=",
"len",
"(",
"predictions",
")",
")",
":",
"raise",
"_ToolkitError",
"(",
"\"Input SArrays 'targets' and 'predictions' must be of the same length.\"",
")"
] | Perform basic error checking for the evaluation metrics. Check
types and sizes of the inputs. | [
"Perform",
"basic",
"error",
"checking",
"for",
"the",
"evaluation",
"metrics",
".",
"Check",
"types",
"and",
"sizes",
"of",
"the",
"inputs",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L50-L59 |
28,687 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | max_error | def max_error(targets, predictions):
r"""
Compute the maximum absolute deviation between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The maximum absolute deviation error between the two SArrays.
See Also
--------
rmse
Notes
-----
The maximum absolute deviation between two vectors, x and y, is defined as:
.. math::
\textrm{max error} = \max_{i \in 1,\ldots,N} \|x_i - y_i\|
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.max_error(targets, predictions)
2.5
"""
_supervised_evaluation_error_checking(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "max_error", {}) | python | def max_error(targets, predictions):
r"""
Compute the maximum absolute deviation between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The maximum absolute deviation error between the two SArrays.
See Also
--------
rmse
Notes
-----
The maximum absolute deviation between two vectors, x and y, is defined as:
.. math::
\textrm{max error} = \max_{i \in 1,\ldots,N} \|x_i - y_i\|
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.max_error(targets, predictions)
2.5
"""
_supervised_evaluation_error_checking(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "max_error", {}) | [
"def",
"max_error",
"(",
"targets",
",",
"predictions",
")",
":",
"_supervised_evaluation_error_checking",
"(",
"targets",
",",
"predictions",
")",
"return",
"_turicreate",
".",
"extensions",
".",
"_supervised_streaming_evaluator",
"(",
"targets",
",",
"predictions",
",",
"\"max_error\"",
",",
"{",
"}",
")"
] | r"""
Compute the maximum absolute deviation between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The maximum absolute deviation error between the two SArrays.
See Also
--------
rmse
Notes
-----
The maximum absolute deviation between two vectors, x and y, is defined as:
.. math::
\textrm{max error} = \max_{i \in 1,\ldots,N} \|x_i - y_i\|
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.max_error(targets, predictions)
2.5 | [
"r",
"Compute",
"the",
"maximum",
"absolute",
"deviation",
"between",
"two",
"SArrays",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L248-L288 |
28,688 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | rmse | def rmse(targets, predictions):
r"""
Compute the root mean squared error between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The RMSE between the two SArrays.
See Also
--------
max_error
Notes
-----
The root mean squared error between two vectors, x and y, is defined as:
.. math::
RMSE = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - y_i)^2}
References
----------
- `Wikipedia - root-mean-square deviation
<http://en.wikipedia.org/wiki/Root-mean-square_deviation>`_
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.rmse(targets, predictions)
1.2749117616525465
"""
_supervised_evaluation_error_checking(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "rmse", {}) | python | def rmse(targets, predictions):
r"""
Compute the root mean squared error between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The RMSE between the two SArrays.
See Also
--------
max_error
Notes
-----
The root mean squared error between two vectors, x and y, is defined as:
.. math::
RMSE = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - y_i)^2}
References
----------
- `Wikipedia - root-mean-square deviation
<http://en.wikipedia.org/wiki/Root-mean-square_deviation>`_
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.rmse(targets, predictions)
1.2749117616525465
"""
_supervised_evaluation_error_checking(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "rmse", {}) | [
"def",
"rmse",
"(",
"targets",
",",
"predictions",
")",
":",
"_supervised_evaluation_error_checking",
"(",
"targets",
",",
"predictions",
")",
"return",
"_turicreate",
".",
"extensions",
".",
"_supervised_streaming_evaluator",
"(",
"targets",
",",
"predictions",
",",
"\"rmse\"",
",",
"{",
"}",
")"
] | r"""
Compute the root mean squared error between two SArrays.
Parameters
----------
targets : SArray[float or int]
An Sarray of ground truth target values.
predictions : SArray[float or int]
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``.
Returns
-------
out : float
The RMSE between the two SArrays.
See Also
--------
max_error
Notes
-----
The root mean squared error between two vectors, x and y, is defined as:
.. math::
RMSE = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - y_i)^2}
References
----------
- `Wikipedia - root-mean-square deviation
<http://en.wikipedia.org/wiki/Root-mean-square_deviation>`_
Examples
--------
>>> targets = turicreate.SArray([3.14, 0.1, 50, -2.5])
>>> predictions = turicreate.SArray([3.1, 0.5, 50.3, -5])
>>> turicreate.evaluation.rmse(targets, predictions)
1.2749117616525465 | [
"r",
"Compute",
"the",
"root",
"mean",
"squared",
"error",
"between",
"two",
"SArrays",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L290-L336 |
28,689 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | confusion_matrix | def confusion_matrix(targets, predictions):
r"""
Compute the confusion matrix for classifier predictions.
Parameters
----------
targets : SArray
Ground truth class labels (cannot be of type float).
predictions : SArray
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``. The predictions
SArray cannot be of type float.
Returns
-------
out : SFrame
An SFrame containing counts for 'target_label', 'predicted_label' and
'count' corresponding to each pair of true and predicted labels.
See Also
--------
accuracy
Examples
--------
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([1, 0, 1, 0])
>>> turicreate.evaluation.confusion_matrix(targets, predictions)
"""
_supervised_evaluation_error_checking(targets, predictions)
_check_same_type_not_float(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "confusion_matrix_no_map", {}) | python | def confusion_matrix(targets, predictions):
r"""
Compute the confusion matrix for classifier predictions.
Parameters
----------
targets : SArray
Ground truth class labels (cannot be of type float).
predictions : SArray
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``. The predictions
SArray cannot be of type float.
Returns
-------
out : SFrame
An SFrame containing counts for 'target_label', 'predicted_label' and
'count' corresponding to each pair of true and predicted labels.
See Also
--------
accuracy
Examples
--------
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([1, 0, 1, 0])
>>> turicreate.evaluation.confusion_matrix(targets, predictions)
"""
_supervised_evaluation_error_checking(targets, predictions)
_check_same_type_not_float(targets, predictions)
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "confusion_matrix_no_map", {}) | [
"def",
"confusion_matrix",
"(",
"targets",
",",
"predictions",
")",
":",
"_supervised_evaluation_error_checking",
"(",
"targets",
",",
"predictions",
")",
"_check_same_type_not_float",
"(",
"targets",
",",
"predictions",
")",
"return",
"_turicreate",
".",
"extensions",
".",
"_supervised_streaming_evaluator",
"(",
"targets",
",",
"predictions",
",",
"\"confusion_matrix_no_map\"",
",",
"{",
"}",
")"
] | r"""
Compute the confusion matrix for classifier predictions.
Parameters
----------
targets : SArray
Ground truth class labels (cannot be of type float).
predictions : SArray
The prediction that corresponds to each target value.
This vector must have the same length as ``targets``. The predictions
SArray cannot be of type float.
Returns
-------
out : SFrame
An SFrame containing counts for 'target_label', 'predicted_label' and
'count' corresponding to each pair of true and predicted labels.
See Also
--------
accuracy
Examples
--------
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([1, 0, 1, 0])
>>> turicreate.evaluation.confusion_matrix(targets, predictions) | [
"r",
"Compute",
"the",
"confusion",
"matrix",
"for",
"classifier",
"predictions",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L337-L372 |
28,690 | apple/turicreate | src/unity/python/turicreate/toolkits/evaluation.py | auc | def auc(targets, predictions, average='macro', index_map=None):
r"""
Compute the area under the ROC curve for the given targets and predictions.
Parameters
----------
targets : SArray
An SArray containing the observed values. For binary classification,
the alpha-numerically first category is considered the reference
category.
predictions : SArray
Prediction probability that corresponds to each target value. This must
be of same length as ``targets``.
average : string, [None, 'macro' (default)]
Metric averaging strategies for multiclass classification. Averaging
strategies can be one of the following:
- None: No averaging is performed and a single metric is returned
for each class.
- 'macro': Calculate metrics for each label, and find their
unweighted mean. This does not take label imbalance into account.
index_map : dict[int], [None (default)]
For binary classification, a dictionary mapping the two target labels to
either 0 (negative) or 1 (positive). For multi-class classification, a
dictionary mapping potential target labels to the associated index into
the vectors in ``predictions``.
Returns
-------
out : float (for binary classification) or dict[float]
Score for the positive class (for binary classification) or an average
score for each class for multi-class classification. If
`average=None`, then a dictionary is returned where the key is the
class label and the value is the score for the corresponding class
label.
See Also
--------
roc_curve, confusion_matrix
Examples
--------
.. sourcecode:: python
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
This metric also works when the targets are strings (Here "cat" is chosen
as the reference class).
.. sourcecode:: python
>>> targets = turicreate.SArray(["cat", "dog", "dog", "cat"])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
For the multi-class setting, the auc-score can be averaged.
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ 1, 0, 2, 1])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
... [.9, .1, 0.0],
... [.8, .1, 0.1],
... [.3, .6, 0.1]])
# Macro average of the scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = 'macro')
0.8888888888888888
# Scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = None)
{0: 1.0, 1: 1.0, 2: 0.6666666666666666}
This metric also works for "string" targets in the multi-class setting
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ "dog", "cat", "foosa", "dog"])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
[.9, .1, 0.0],
[.8, .1, 0.1],
[.3, .6, 0.1]])
# Macro average.
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.8888888888888888
# Score for each class.
>>> auc = turicreate.evaluation.auc(targets, predictions, average=None)
{'cat': 1.0, 'dog': 1.0, 'foosa': 0.6666666666666666}
"""
_supervised_evaluation_error_checking(targets, predictions)
_check_categorical_option_type('average', average,
['macro', None])
_check_prob_and_prob_vector(predictions)
_check_target_not_float(targets)
_check_index_map(index_map)
opts = {"average": average,
"binary": predictions.dtype in [int, float]}
if index_map is not None:
opts['index_map'] = index_map
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "auc", opts) | python | def auc(targets, predictions, average='macro', index_map=None):
r"""
Compute the area under the ROC curve for the given targets and predictions.
Parameters
----------
targets : SArray
An SArray containing the observed values. For binary classification,
the alpha-numerically first category is considered the reference
category.
predictions : SArray
Prediction probability that corresponds to each target value. This must
be of same length as ``targets``.
average : string, [None, 'macro' (default)]
Metric averaging strategies for multiclass classification. Averaging
strategies can be one of the following:
- None: No averaging is performed and a single metric is returned
for each class.
- 'macro': Calculate metrics for each label, and find their
unweighted mean. This does not take label imbalance into account.
index_map : dict[int], [None (default)]
For binary classification, a dictionary mapping the two target labels to
either 0 (negative) or 1 (positive). For multi-class classification, a
dictionary mapping potential target labels to the associated index into
the vectors in ``predictions``.
Returns
-------
out : float (for binary classification) or dict[float]
Score for the positive class (for binary classification) or an average
score for each class for multi-class classification. If
`average=None`, then a dictionary is returned where the key is the
class label and the value is the score for the corresponding class
label.
See Also
--------
roc_curve, confusion_matrix
Examples
--------
.. sourcecode:: python
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
This metric also works when the targets are strings (Here "cat" is chosen
as the reference class).
.. sourcecode:: python
>>> targets = turicreate.SArray(["cat", "dog", "dog", "cat"])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
For the multi-class setting, the auc-score can be averaged.
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ 1, 0, 2, 1])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
... [.9, .1, 0.0],
... [.8, .1, 0.1],
... [.3, .6, 0.1]])
# Macro average of the scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = 'macro')
0.8888888888888888
# Scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = None)
{0: 1.0, 1: 1.0, 2: 0.6666666666666666}
This metric also works for "string" targets in the multi-class setting
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ "dog", "cat", "foosa", "dog"])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
[.9, .1, 0.0],
[.8, .1, 0.1],
[.3, .6, 0.1]])
# Macro average.
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.8888888888888888
# Score for each class.
>>> auc = turicreate.evaluation.auc(targets, predictions, average=None)
{'cat': 1.0, 'dog': 1.0, 'foosa': 0.6666666666666666}
"""
_supervised_evaluation_error_checking(targets, predictions)
_check_categorical_option_type('average', average,
['macro', None])
_check_prob_and_prob_vector(predictions)
_check_target_not_float(targets)
_check_index_map(index_map)
opts = {"average": average,
"binary": predictions.dtype in [int, float]}
if index_map is not None:
opts['index_map'] = index_map
return _turicreate.extensions._supervised_streaming_evaluator(targets,
predictions, "auc", opts) | [
"def",
"auc",
"(",
"targets",
",",
"predictions",
",",
"average",
"=",
"'macro'",
",",
"index_map",
"=",
"None",
")",
":",
"_supervised_evaluation_error_checking",
"(",
"targets",
",",
"predictions",
")",
"_check_categorical_option_type",
"(",
"'average'",
",",
"average",
",",
"[",
"'macro'",
",",
"None",
"]",
")",
"_check_prob_and_prob_vector",
"(",
"predictions",
")",
"_check_target_not_float",
"(",
"targets",
")",
"_check_index_map",
"(",
"index_map",
")",
"opts",
"=",
"{",
"\"average\"",
":",
"average",
",",
"\"binary\"",
":",
"predictions",
".",
"dtype",
"in",
"[",
"int",
",",
"float",
"]",
"}",
"if",
"index_map",
"is",
"not",
"None",
":",
"opts",
"[",
"'index_map'",
"]",
"=",
"index_map",
"return",
"_turicreate",
".",
"extensions",
".",
"_supervised_streaming_evaluator",
"(",
"targets",
",",
"predictions",
",",
"\"auc\"",
",",
"opts",
")"
] | r"""
Compute the area under the ROC curve for the given targets and predictions.
Parameters
----------
targets : SArray
An SArray containing the observed values. For binary classification,
the alpha-numerically first category is considered the reference
category.
predictions : SArray
Prediction probability that corresponds to each target value. This must
be of same length as ``targets``.
average : string, [None, 'macro' (default)]
Metric averaging strategies for multiclass classification. Averaging
strategies can be one of the following:
- None: No averaging is performed and a single metric is returned
for each class.
- 'macro': Calculate metrics for each label, and find their
unweighted mean. This does not take label imbalance into account.
index_map : dict[int], [None (default)]
For binary classification, a dictionary mapping the two target labels to
either 0 (negative) or 1 (positive). For multi-class classification, a
dictionary mapping potential target labels to the associated index into
the vectors in ``predictions``.
Returns
-------
out : float (for binary classification) or dict[float]
Score for the positive class (for binary classification) or an average
score for each class for multi-class classification. If
`average=None`, then a dictionary is returned where the key is the
class label and the value is the score for the corresponding class
label.
See Also
--------
roc_curve, confusion_matrix
Examples
--------
.. sourcecode:: python
>>> targets = turicreate.SArray([0, 1, 1, 0])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
This metric also works when the targets are strings (Here "cat" is chosen
as the reference class).
.. sourcecode:: python
>>> targets = turicreate.SArray(["cat", "dog", "dog", "cat"])
>>> predictions = turicreate.SArray([0.1, 0.35, 0.7, 0.99])
# Calculate the auc-score
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.5
For the multi-class setting, the auc-score can be averaged.
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ 1, 0, 2, 1])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
... [.9, .1, 0.0],
... [.8, .1, 0.1],
... [.3, .6, 0.1]])
# Macro average of the scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = 'macro')
0.8888888888888888
# Scores for each class.
>>> turicreate.evaluation.auc(targets, predictions, average = None)
{0: 1.0, 1: 1.0, 2: 0.6666666666666666}
This metric also works for "string" targets in the multi-class setting
.. sourcecode:: python
# Targets and Predictions
>>> targets = turicreate.SArray([ "dog", "cat", "foosa", "dog"])
>>> predictions = turicreate.SArray([[.1, .8, 0.1],
[.9, .1, 0.0],
[.8, .1, 0.1],
[.3, .6, 0.1]])
# Macro average.
>>> auc = turicreate.evaluation.auc(targets, predictions)
0.8888888888888888
# Score for each class.
>>> auc = turicreate.evaluation.auc(targets, predictions, average=None)
{'cat': 1.0, 'dog': 1.0, 'foosa': 0.6666666666666666} | [
"r",
"Compute",
"the",
"area",
"under",
"the",
"ROC",
"curve",
"for",
"the",
"given",
"targets",
"and",
"predictions",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/evaluation.py#L1150-L1269 |
28,691 | apple/turicreate | deps/src/boost_1_68_0/status/boost_check_library.py | check_library.get_library_meta | def get_library_meta(self):
'''
Fetches the meta data for the current library. The data could be in
the superlib meta data file. If we can't find the data None is returned.
'''
parent_dir = os.path.dirname(self.library_dir)
if self.test_file_exists(os.path.join(self.library_dir,'meta'),['libraries.json']):
with open(os.path.join(self.library_dir,'meta','libraries.json'),'r') as f:
meta_data = json.load(f)
if isinstance(meta_data,list):
for lib in meta_data:
if lib['key'] == self.library_key:
return lib
elif 'key' in meta_data and meta_data['key'] == self.library_key:
return meta_data
if not self.test_dir_exists(os.path.join(self.library_dir,'meta')) \
and self.test_file_exists(os.path.join(parent_dir,'meta'),['libraries.json']):
with open(os.path.join(parent_dir,'meta','libraries.json'),'r') as f:
libraries_json = json.load(f)
if isinstance(libraries_json,list):
for lib in libraries_json:
if lib['key'] == self.library_key:
return lib
return None | python | def get_library_meta(self):
'''
Fetches the meta data for the current library. The data could be in
the superlib meta data file. If we can't find the data None is returned.
'''
parent_dir = os.path.dirname(self.library_dir)
if self.test_file_exists(os.path.join(self.library_dir,'meta'),['libraries.json']):
with open(os.path.join(self.library_dir,'meta','libraries.json'),'r') as f:
meta_data = json.load(f)
if isinstance(meta_data,list):
for lib in meta_data:
if lib['key'] == self.library_key:
return lib
elif 'key' in meta_data and meta_data['key'] == self.library_key:
return meta_data
if not self.test_dir_exists(os.path.join(self.library_dir,'meta')) \
and self.test_file_exists(os.path.join(parent_dir,'meta'),['libraries.json']):
with open(os.path.join(parent_dir,'meta','libraries.json'),'r') as f:
libraries_json = json.load(f)
if isinstance(libraries_json,list):
for lib in libraries_json:
if lib['key'] == self.library_key:
return lib
return None | [
"def",
"get_library_meta",
"(",
"self",
")",
":",
"parent_dir",
"=",
"os",
".",
"path",
".",
"dirname",
"(",
"self",
".",
"library_dir",
")",
"if",
"self",
".",
"test_file_exists",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"library_dir",
",",
"'meta'",
")",
",",
"[",
"'libraries.json'",
"]",
")",
":",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"library_dir",
",",
"'meta'",
",",
"'libraries.json'",
")",
",",
"'r'",
")",
"as",
"f",
":",
"meta_data",
"=",
"json",
".",
"load",
"(",
"f",
")",
"if",
"isinstance",
"(",
"meta_data",
",",
"list",
")",
":",
"for",
"lib",
"in",
"meta_data",
":",
"if",
"lib",
"[",
"'key'",
"]",
"==",
"self",
".",
"library_key",
":",
"return",
"lib",
"elif",
"'key'",
"in",
"meta_data",
"and",
"meta_data",
"[",
"'key'",
"]",
"==",
"self",
".",
"library_key",
":",
"return",
"meta_data",
"if",
"not",
"self",
".",
"test_dir_exists",
"(",
"os",
".",
"path",
".",
"join",
"(",
"self",
".",
"library_dir",
",",
"'meta'",
")",
")",
"and",
"self",
".",
"test_file_exists",
"(",
"os",
".",
"path",
".",
"join",
"(",
"parent_dir",
",",
"'meta'",
")",
",",
"[",
"'libraries.json'",
"]",
")",
":",
"with",
"open",
"(",
"os",
".",
"path",
".",
"join",
"(",
"parent_dir",
",",
"'meta'",
",",
"'libraries.json'",
")",
",",
"'r'",
")",
"as",
"f",
":",
"libraries_json",
"=",
"json",
".",
"load",
"(",
"f",
")",
"if",
"isinstance",
"(",
"libraries_json",
",",
"list",
")",
":",
"for",
"lib",
"in",
"libraries_json",
":",
"if",
"lib",
"[",
"'key'",
"]",
"==",
"self",
".",
"library_key",
":",
"return",
"lib",
"return",
"None"
] | Fetches the meta data for the current library. The data could be in
the superlib meta data file. If we can't find the data None is returned. | [
"Fetches",
"the",
"meta",
"data",
"for",
"the",
"current",
"library",
".",
"The",
"data",
"could",
"be",
"in",
"the",
"superlib",
"meta",
"data",
"file",
".",
"If",
"we",
"can",
"t",
"find",
"the",
"data",
"None",
"is",
"returned",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/deps/src/boost_1_68_0/status/boost_check_library.py#L182-L205 |
28,692 | apple/turicreate | src/external/coremltools_wrap/coremltools/coremltools/converters/xgboost/_tree.py | convert | def convert(model, feature_names = None, target = 'target', force_32bit_float = True):
"""
Convert a trained XGBoost model to Core ML format.
Parameters
----------
decision_tree : Booster
A trained XGboost tree model.
feature_names: [str] | str
Names of input features that will be exposed in the Core ML model
interface.
Can be set to one of the following:
- None for using the feature names from the model.
- List of names of the input features that should be exposed in the
interface to the Core ML model. These input features are in the same
order as the XGboost model.
target: str
Name of the output feature name exposed to the Core ML model.
force_32bit_float: bool
If True, then the resulting CoreML model will use 32 bit floats internally.
Returns
-------
model:MLModel
Returns an MLModel instance representing a Core ML model.
Examples
--------
.. sourcecode:: python
# Convert it with default input and output names
>>> import coremltools
>>> coreml_model = coremltools.converters.xgboost.convert(model)
# Saving the Core ML model to a file.
>>> coremltools.save('my_model.mlmodel')
"""
return _MLModel(_convert_tree_ensemble(model, feature_names, target, force_32bit_float = force_32bit_float)) | python | def convert(model, feature_names = None, target = 'target', force_32bit_float = True):
"""
Convert a trained XGBoost model to Core ML format.
Parameters
----------
decision_tree : Booster
A trained XGboost tree model.
feature_names: [str] | str
Names of input features that will be exposed in the Core ML model
interface.
Can be set to one of the following:
- None for using the feature names from the model.
- List of names of the input features that should be exposed in the
interface to the Core ML model. These input features are in the same
order as the XGboost model.
target: str
Name of the output feature name exposed to the Core ML model.
force_32bit_float: bool
If True, then the resulting CoreML model will use 32 bit floats internally.
Returns
-------
model:MLModel
Returns an MLModel instance representing a Core ML model.
Examples
--------
.. sourcecode:: python
# Convert it with default input and output names
>>> import coremltools
>>> coreml_model = coremltools.converters.xgboost.convert(model)
# Saving the Core ML model to a file.
>>> coremltools.save('my_model.mlmodel')
"""
return _MLModel(_convert_tree_ensemble(model, feature_names, target, force_32bit_float = force_32bit_float)) | [
"def",
"convert",
"(",
"model",
",",
"feature_names",
"=",
"None",
",",
"target",
"=",
"'target'",
",",
"force_32bit_float",
"=",
"True",
")",
":",
"return",
"_MLModel",
"(",
"_convert_tree_ensemble",
"(",
"model",
",",
"feature_names",
",",
"target",
",",
"force_32bit_float",
"=",
"force_32bit_float",
")",
")"
] | Convert a trained XGBoost model to Core ML format.
Parameters
----------
decision_tree : Booster
A trained XGboost tree model.
feature_names: [str] | str
Names of input features that will be exposed in the Core ML model
interface.
Can be set to one of the following:
- None for using the feature names from the model.
- List of names of the input features that should be exposed in the
interface to the Core ML model. These input features are in the same
order as the XGboost model.
target: str
Name of the output feature name exposed to the Core ML model.
force_32bit_float: bool
If True, then the resulting CoreML model will use 32 bit floats internally.
Returns
-------
model:MLModel
Returns an MLModel instance representing a Core ML model.
Examples
--------
.. sourcecode:: python
# Convert it with default input and output names
>>> import coremltools
>>> coreml_model = coremltools.converters.xgboost.convert(model)
# Saving the Core ML model to a file.
>>> coremltools.save('my_model.mlmodel') | [
"Convert",
"a",
"trained",
"XGBoost",
"model",
"to",
"Core",
"ML",
"format",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/external/coremltools_wrap/coremltools/coremltools/converters/xgboost/_tree.py#L9-L51 |
28,693 | apple/turicreate | src/unity/python/turicreate/toolkits/_feature_engineering/_feature_engineering.py | Transformer.fit | def fit(self, data):
"""
Fit a transformer using the SFrame `data`.
Parameters
----------
data : SFrame
The data used to fit the transformer.
Returns
-------
self (A fitted version of the object)
See Also
--------
transform, fit_transform
Examples
--------
.. sourcecode:: python
{examples}
"""
_raise_error_if_not_sframe(data, "data")
self.__proxy__.fit(data)
return self | python | def fit(self, data):
"""
Fit a transformer using the SFrame `data`.
Parameters
----------
data : SFrame
The data used to fit the transformer.
Returns
-------
self (A fitted version of the object)
See Also
--------
transform, fit_transform
Examples
--------
.. sourcecode:: python
{examples}
"""
_raise_error_if_not_sframe(data, "data")
self.__proxy__.fit(data)
return self | [
"def",
"fit",
"(",
"self",
",",
"data",
")",
":",
"_raise_error_if_not_sframe",
"(",
"data",
",",
"\"data\"",
")",
"self",
".",
"__proxy__",
".",
"fit",
"(",
"data",
")",
"return",
"self"
] | Fit a transformer using the SFrame `data`.
Parameters
----------
data : SFrame
The data used to fit the transformer.
Returns
-------
self (A fitted version of the object)
See Also
--------
transform, fit_transform
Examples
--------
.. sourcecode:: python
{examples} | [
"Fit",
"a",
"transformer",
"using",
"the",
"SFrame",
"data",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/_feature_engineering/_feature_engineering.py#L236-L262 |
28,694 | apple/turicreate | src/unity/python/turicreate/toolkits/_tree_model_mixin.py | TreeModelMixin.extract_features | def extract_features(self, dataset, missing_value_action='auto'):
"""
For each example in the dataset, extract the leaf indices of
each tree as features.
For multiclass classification, each leaf index contains #num_class
numbers.
The returned feature vectors can be used as input to train another
supervised learning model such as a
:py:class:`~turicreate.logistic_classifier.LogisticClassifier`,
an :py:class:`~turicreate.svm_classifier.SVMClassifier`, or a
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the features used for model training, but does not require
a target column. Additional columns are ignored.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SArray
An SArray of dtype array.array containing extracted features.
Examples
--------
>>> data = turicreate.SFrame(
'https://static.turi.com/datasets/regression/houses.csv')
>>> # Regression Tree Models
>>> data['regression_tree_features'] = model.extract_features(data)
>>> # Classification Tree Models
>>> data['classification_tree_features'] = model.extract_features(data)
"""
_raise_error_if_not_sframe(dataset, "dataset")
if missing_value_action == 'auto':
missing_value_action = select_default_missing_value_policy(self,
'extract_features')
return self.__proxy__.extract_features(dataset, missing_value_action) | python | def extract_features(self, dataset, missing_value_action='auto'):
"""
For each example in the dataset, extract the leaf indices of
each tree as features.
For multiclass classification, each leaf index contains #num_class
numbers.
The returned feature vectors can be used as input to train another
supervised learning model such as a
:py:class:`~turicreate.logistic_classifier.LogisticClassifier`,
an :py:class:`~turicreate.svm_classifier.SVMClassifier`, or a
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the features used for model training, but does not require
a target column. Additional columns are ignored.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SArray
An SArray of dtype array.array containing extracted features.
Examples
--------
>>> data = turicreate.SFrame(
'https://static.turi.com/datasets/regression/houses.csv')
>>> # Regression Tree Models
>>> data['regression_tree_features'] = model.extract_features(data)
>>> # Classification Tree Models
>>> data['classification_tree_features'] = model.extract_features(data)
"""
_raise_error_if_not_sframe(dataset, "dataset")
if missing_value_action == 'auto':
missing_value_action = select_default_missing_value_policy(self,
'extract_features')
return self.__proxy__.extract_features(dataset, missing_value_action) | [
"def",
"extract_features",
"(",
"self",
",",
"dataset",
",",
"missing_value_action",
"=",
"'auto'",
")",
":",
"_raise_error_if_not_sframe",
"(",
"dataset",
",",
"\"dataset\"",
")",
"if",
"missing_value_action",
"==",
"'auto'",
":",
"missing_value_action",
"=",
"select_default_missing_value_policy",
"(",
"self",
",",
"'extract_features'",
")",
"return",
"self",
".",
"__proxy__",
".",
"extract_features",
"(",
"dataset",
",",
"missing_value_action",
")"
] | For each example in the dataset, extract the leaf indices of
each tree as features.
For multiclass classification, each leaf index contains #num_class
numbers.
The returned feature vectors can be used as input to train another
supervised learning model such as a
:py:class:`~turicreate.logistic_classifier.LogisticClassifier`,
an :py:class:`~turicreate.svm_classifier.SVMClassifier`, or a
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the features used for model training, but does not require
a target column. Additional columns are ignored.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SArray
An SArray of dtype array.array containing extracted features.
Examples
--------
>>> data = turicreate.SFrame(
'https://static.turi.com/datasets/regression/houses.csv')
>>> # Regression Tree Models
>>> data['regression_tree_features'] = model.extract_features(data)
>>> # Classification Tree Models
>>> data['classification_tree_features'] = model.extract_features(data) | [
"For",
"each",
"example",
"in",
"the",
"dataset",
"extract",
"the",
"leaf",
"indices",
"of",
"each",
"tree",
"as",
"features",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/_tree_model_mixin.py#L65-L120 |
28,695 | apple/turicreate | src/unity/python/turicreate/toolkits/_tree_model_mixin.py | TreeModelMixin._extract_features_with_missing | def _extract_features_with_missing(self, dataset, tree_id = 0,
missing_value_action = 'auto'):
"""
Extract features along with all the missing features associated with
a dataset.
Parameters
----------
dataset: bool
Dataset on which to make predictions.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SFrame
A table with two columns:
- leaf_id : Leaf id of the corresponding tree.
- missing_features : A list of missing feature, index pairs
"""
# Extract the features from only one tree.
sf = dataset
sf['leaf_id'] = self.extract_features(dataset, missing_value_action)\
.vector_slice(tree_id)\
.astype(int)
tree = self._get_tree(tree_id)
type_map = dict(zip(dataset.column_names(), dataset.column_types()))
def get_missing_features(row):
x = row['leaf_id']
path = tree.get_prediction_path(x)
missing_id = [] # List of "missing_id" children.
# For each node in the prediction path.
for p in path:
fname = p['feature']
idx = p['index']
f = row[fname]
if type_map[fname] in [int, float]:
if f is None:
missing_id.append(p['child_id'])
elif type_map[fname] in [dict]:
if f is None:
missing_id.append(p['child_id'])
if idx not in f:
missing_id.append(p['child_id'])
else:
pass
return missing_id
sf['missing_id'] = sf.apply(get_missing_features, list)
return sf[['leaf_id', 'missing_id']] | python | def _extract_features_with_missing(self, dataset, tree_id = 0,
missing_value_action = 'auto'):
"""
Extract features along with all the missing features associated with
a dataset.
Parameters
----------
dataset: bool
Dataset on which to make predictions.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SFrame
A table with two columns:
- leaf_id : Leaf id of the corresponding tree.
- missing_features : A list of missing feature, index pairs
"""
# Extract the features from only one tree.
sf = dataset
sf['leaf_id'] = self.extract_features(dataset, missing_value_action)\
.vector_slice(tree_id)\
.astype(int)
tree = self._get_tree(tree_id)
type_map = dict(zip(dataset.column_names(), dataset.column_types()))
def get_missing_features(row):
x = row['leaf_id']
path = tree.get_prediction_path(x)
missing_id = [] # List of "missing_id" children.
# For each node in the prediction path.
for p in path:
fname = p['feature']
idx = p['index']
f = row[fname]
if type_map[fname] in [int, float]:
if f is None:
missing_id.append(p['child_id'])
elif type_map[fname] in [dict]:
if f is None:
missing_id.append(p['child_id'])
if idx not in f:
missing_id.append(p['child_id'])
else:
pass
return missing_id
sf['missing_id'] = sf.apply(get_missing_features, list)
return sf[['leaf_id', 'missing_id']] | [
"def",
"_extract_features_with_missing",
"(",
"self",
",",
"dataset",
",",
"tree_id",
"=",
"0",
",",
"missing_value_action",
"=",
"'auto'",
")",
":",
"# Extract the features from only one tree.",
"sf",
"=",
"dataset",
"sf",
"[",
"'leaf_id'",
"]",
"=",
"self",
".",
"extract_features",
"(",
"dataset",
",",
"missing_value_action",
")",
".",
"vector_slice",
"(",
"tree_id",
")",
".",
"astype",
"(",
"int",
")",
"tree",
"=",
"self",
".",
"_get_tree",
"(",
"tree_id",
")",
"type_map",
"=",
"dict",
"(",
"zip",
"(",
"dataset",
".",
"column_names",
"(",
")",
",",
"dataset",
".",
"column_types",
"(",
")",
")",
")",
"def",
"get_missing_features",
"(",
"row",
")",
":",
"x",
"=",
"row",
"[",
"'leaf_id'",
"]",
"path",
"=",
"tree",
".",
"get_prediction_path",
"(",
"x",
")",
"missing_id",
"=",
"[",
"]",
"# List of \"missing_id\" children.",
"# For each node in the prediction path.",
"for",
"p",
"in",
"path",
":",
"fname",
"=",
"p",
"[",
"'feature'",
"]",
"idx",
"=",
"p",
"[",
"'index'",
"]",
"f",
"=",
"row",
"[",
"fname",
"]",
"if",
"type_map",
"[",
"fname",
"]",
"in",
"[",
"int",
",",
"float",
"]",
":",
"if",
"f",
"is",
"None",
":",
"missing_id",
".",
"append",
"(",
"p",
"[",
"'child_id'",
"]",
")",
"elif",
"type_map",
"[",
"fname",
"]",
"in",
"[",
"dict",
"]",
":",
"if",
"f",
"is",
"None",
":",
"missing_id",
".",
"append",
"(",
"p",
"[",
"'child_id'",
"]",
")",
"if",
"idx",
"not",
"in",
"f",
":",
"missing_id",
".",
"append",
"(",
"p",
"[",
"'child_id'",
"]",
")",
"else",
":",
"pass",
"return",
"missing_id",
"sf",
"[",
"'missing_id'",
"]",
"=",
"sf",
".",
"apply",
"(",
"get_missing_features",
",",
"list",
")",
"return",
"sf",
"[",
"[",
"'leaf_id'",
",",
"'missing_id'",
"]",
"]"
] | Extract features along with all the missing features associated with
a dataset.
Parameters
----------
dataset: bool
Dataset on which to make predictions.
missing_value_action: str, optional
Action to perform when missing values are encountered. This can be
one of:
- 'auto': Choose a model dependent missing value policy.
- 'impute': Proceed with evaluation by filling in the missing
values with the mean of the training data. Missing
values are also imputed if an entire column of data is
missing during evaluation.
- 'none': Treat missing value as is. Model must be able to handle
missing value.
- 'error' : Do not proceed with prediction and terminate with
an error message.
Returns
-------
out : SFrame
A table with two columns:
- leaf_id : Leaf id of the corresponding tree.
- missing_features : A list of missing feature, index pairs | [
"Extract",
"features",
"along",
"with",
"all",
"the",
"missing",
"features",
"associated",
"with",
"a",
"dataset",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/_tree_model_mixin.py#L122-L189 |
28,696 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py | _sort_topk_votes | def _sort_topk_votes(x, k):
"""
Sort a dictionary of classes and corresponding vote totals according to the
votes, then truncate to the highest 'k' classes.
"""
y = sorted(x.items(), key=lambda x: x[1], reverse=True)[:k]
return [{'class': i[0], 'votes': i[1]} for i in y] | python | def _sort_topk_votes(x, k):
"""
Sort a dictionary of classes and corresponding vote totals according to the
votes, then truncate to the highest 'k' classes.
"""
y = sorted(x.items(), key=lambda x: x[1], reverse=True)[:k]
return [{'class': i[0], 'votes': i[1]} for i in y] | [
"def",
"_sort_topk_votes",
"(",
"x",
",",
"k",
")",
":",
"y",
"=",
"sorted",
"(",
"x",
".",
"items",
"(",
")",
",",
"key",
"=",
"lambda",
"x",
":",
"x",
"[",
"1",
"]",
",",
"reverse",
"=",
"True",
")",
"[",
":",
"k",
"]",
"return",
"[",
"{",
"'class'",
":",
"i",
"[",
"0",
"]",
",",
"'votes'",
":",
"i",
"[",
"1",
"]",
"}",
"for",
"i",
"in",
"y",
"]"
] | Sort a dictionary of classes and corresponding vote totals according to the
votes, then truncate to the highest 'k' classes. | [
"Sort",
"a",
"dictionary",
"of",
"classes",
"and",
"corresponding",
"vote",
"totals",
"according",
"to",
"the",
"votes",
"then",
"truncate",
"to",
"the",
"highest",
"k",
"classes",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py#L33-L39 |
28,697 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py | _construct_auto_distance | def _construct_auto_distance(features, column_types):
"""
Construct a composite distance function for a set of features, based on the
types of those features.
NOTE: This function is very similar to
`:func:_nearest_neighbors.choose_auto_distance`. The function is separate
because the auto-distance logic different than for each nearest
neighbors-based toolkit.
Parameters
----------
features : list[str]
Names of for which to construct a distance function.
column_types : dict(string, type)
Names and types of all columns.
Returns
-------
dist : list[list]
A composite distance function. Each element of the inner list has three
elements: a list of feature names (strings), a distance function name
(string), and a weight (float).
"""
## Put input features into buckets based on type.
numeric_ftrs = []
string_ftrs = []
dict_ftrs = []
for ftr in features:
try:
ftr_type = column_types[ftr]
except:
raise ValueError("The specified feature does not exist in the " +
"input data.")
if ftr_type == str:
string_ftrs.append(ftr)
elif ftr_type == dict:
dict_ftrs.append(ftr)
elif ftr_type in [int, float, _array.array]:
numeric_ftrs.append(ftr)
else:
raise TypeError("Unable to automatically construct a distance " +
"function for feature '{}'. ".format(ftr) +
"For the nearest neighbor classifier, features " +
"must be of type integer, float, string, dictionary, " +
"or array.array.")
## Construct the distance function
dist = []
for ftr in string_ftrs:
dist.append([[ftr], 'levenshtein', 1])
if len(dict_ftrs) > 0:
dist.append([dict_ftrs, 'weighted_jaccard', len(dict_ftrs)])
if len(numeric_ftrs) > 0:
dist.append([numeric_ftrs, 'euclidean', len(numeric_ftrs)])
return dist | python | def _construct_auto_distance(features, column_types):
"""
Construct a composite distance function for a set of features, based on the
types of those features.
NOTE: This function is very similar to
`:func:_nearest_neighbors.choose_auto_distance`. The function is separate
because the auto-distance logic different than for each nearest
neighbors-based toolkit.
Parameters
----------
features : list[str]
Names of for which to construct a distance function.
column_types : dict(string, type)
Names and types of all columns.
Returns
-------
dist : list[list]
A composite distance function. Each element of the inner list has three
elements: a list of feature names (strings), a distance function name
(string), and a weight (float).
"""
## Put input features into buckets based on type.
numeric_ftrs = []
string_ftrs = []
dict_ftrs = []
for ftr in features:
try:
ftr_type = column_types[ftr]
except:
raise ValueError("The specified feature does not exist in the " +
"input data.")
if ftr_type == str:
string_ftrs.append(ftr)
elif ftr_type == dict:
dict_ftrs.append(ftr)
elif ftr_type in [int, float, _array.array]:
numeric_ftrs.append(ftr)
else:
raise TypeError("Unable to automatically construct a distance " +
"function for feature '{}'. ".format(ftr) +
"For the nearest neighbor classifier, features " +
"must be of type integer, float, string, dictionary, " +
"or array.array.")
## Construct the distance function
dist = []
for ftr in string_ftrs:
dist.append([[ftr], 'levenshtein', 1])
if len(dict_ftrs) > 0:
dist.append([dict_ftrs, 'weighted_jaccard', len(dict_ftrs)])
if len(numeric_ftrs) > 0:
dist.append([numeric_ftrs, 'euclidean', len(numeric_ftrs)])
return dist | [
"def",
"_construct_auto_distance",
"(",
"features",
",",
"column_types",
")",
":",
"## Put input features into buckets based on type.",
"numeric_ftrs",
"=",
"[",
"]",
"string_ftrs",
"=",
"[",
"]",
"dict_ftrs",
"=",
"[",
"]",
"for",
"ftr",
"in",
"features",
":",
"try",
":",
"ftr_type",
"=",
"column_types",
"[",
"ftr",
"]",
"except",
":",
"raise",
"ValueError",
"(",
"\"The specified feature does not exist in the \"",
"+",
"\"input data.\"",
")",
"if",
"ftr_type",
"==",
"str",
":",
"string_ftrs",
".",
"append",
"(",
"ftr",
")",
"elif",
"ftr_type",
"==",
"dict",
":",
"dict_ftrs",
".",
"append",
"(",
"ftr",
")",
"elif",
"ftr_type",
"in",
"[",
"int",
",",
"float",
",",
"_array",
".",
"array",
"]",
":",
"numeric_ftrs",
".",
"append",
"(",
"ftr",
")",
"else",
":",
"raise",
"TypeError",
"(",
"\"Unable to automatically construct a distance \"",
"+",
"\"function for feature '{}'. \"",
".",
"format",
"(",
"ftr",
")",
"+",
"\"For the nearest neighbor classifier, features \"",
"+",
"\"must be of type integer, float, string, dictionary, \"",
"+",
"\"or array.array.\"",
")",
"## Construct the distance function",
"dist",
"=",
"[",
"]",
"for",
"ftr",
"in",
"string_ftrs",
":",
"dist",
".",
"append",
"(",
"[",
"[",
"ftr",
"]",
",",
"'levenshtein'",
",",
"1",
"]",
")",
"if",
"len",
"(",
"dict_ftrs",
")",
">",
"0",
":",
"dist",
".",
"append",
"(",
"[",
"dict_ftrs",
",",
"'weighted_jaccard'",
",",
"len",
"(",
"dict_ftrs",
")",
"]",
")",
"if",
"len",
"(",
"numeric_ftrs",
")",
">",
"0",
":",
"dist",
".",
"append",
"(",
"[",
"numeric_ftrs",
",",
"'euclidean'",
",",
"len",
"(",
"numeric_ftrs",
")",
"]",
")",
"return",
"dist"
] | Construct a composite distance function for a set of features, based on the
types of those features.
NOTE: This function is very similar to
`:func:_nearest_neighbors.choose_auto_distance`. The function is separate
because the auto-distance logic different than for each nearest
neighbors-based toolkit.
Parameters
----------
features : list[str]
Names of for which to construct a distance function.
column_types : dict(string, type)
Names and types of all columns.
Returns
-------
dist : list[list]
A composite distance function. Each element of the inner list has three
elements: a list of feature names (strings), a distance function name
(string), and a weight (float). | [
"Construct",
"a",
"composite",
"distance",
"function",
"for",
"a",
"set",
"of",
"features",
"based",
"on",
"the",
"types",
"of",
"those",
"features",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py#L42-L108 |
28,698 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py | NearestNeighborClassifier._load_version | def _load_version(cls, state, version):
"""
A function to load a previously saved NearestNeighborClassifier model.
Parameters
----------
unpickler : GLUnpickler
A GLUnpickler file handler.
version : int
Version number maintained by the class writer.
"""
assert(version == cls._PYTHON_NN_CLASSIFIER_MODEL_VERSION)
knn_model = _tc.nearest_neighbors.NearestNeighborsModel(state['knn_model'])
del state['knn_model']
state['_target_type'] = eval(state['_target_type'])
return cls(knn_model, state) | python | def _load_version(cls, state, version):
"""
A function to load a previously saved NearestNeighborClassifier model.
Parameters
----------
unpickler : GLUnpickler
A GLUnpickler file handler.
version : int
Version number maintained by the class writer.
"""
assert(version == cls._PYTHON_NN_CLASSIFIER_MODEL_VERSION)
knn_model = _tc.nearest_neighbors.NearestNeighborsModel(state['knn_model'])
del state['knn_model']
state['_target_type'] = eval(state['_target_type'])
return cls(knn_model, state) | [
"def",
"_load_version",
"(",
"cls",
",",
"state",
",",
"version",
")",
":",
"assert",
"(",
"version",
"==",
"cls",
".",
"_PYTHON_NN_CLASSIFIER_MODEL_VERSION",
")",
"knn_model",
"=",
"_tc",
".",
"nearest_neighbors",
".",
"NearestNeighborsModel",
"(",
"state",
"[",
"'knn_model'",
"]",
")",
"del",
"state",
"[",
"'knn_model'",
"]",
"state",
"[",
"'_target_type'",
"]",
"=",
"eval",
"(",
"state",
"[",
"'_target_type'",
"]",
")",
"return",
"cls",
"(",
"knn_model",
",",
"state",
")"
] | A function to load a previously saved NearestNeighborClassifier model.
Parameters
----------
unpickler : GLUnpickler
A GLUnpickler file handler.
version : int
Version number maintained by the class writer. | [
"A",
"function",
"to",
"load",
"a",
"previously",
"saved",
"NearestNeighborClassifier",
"model",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py#L353-L369 |
28,699 | apple/turicreate | src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py | NearestNeighborClassifier.evaluate | def evaluate(self, dataset, metric='auto', max_neighbors=10, radius=None):
"""
Evaluate the model's predictive accuracy. This is done by predicting the
target class for instances in a new dataset and comparing to known
target values.
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the target and features used for model training. Additional
columns are ignored.
metric : str, optional
Name of the evaluation metric. Possible values are:
- 'auto': Returns all available metrics.
- 'accuracy': Classification accuracy.
- 'confusion_matrix': An SFrame with counts of possible
prediction/true label combinations.
- 'roc_curve': An SFrame containing information needed for an roc
curve (binary classification only).
max_neighbors : int, optional
Maximum number of neighbors to consider for each point.
radius : float, optional
Maximum distance from each point to a neighbor in the reference
dataset.
Returns
-------
out : dict
Evaluation results. The dictionary keys are *accuracy* and
*confusion_matrix* and *roc_curve* (if applicable).
See also
--------
create, predict, predict_topk, classify
Notes
-----
- Because the model randomly breaks ties between predicted classes, the
results of repeated calls to `evaluate` method may differ.
Examples
--------
>>> sf_train = turicreate.SFrame({'species': ['cat', 'dog', 'fossa', 'dog'],
... 'height': [9, 25, 20, 23],
... 'weight': [13, 28, 33, 22]})
>>> m = turicreate.nearest_neighbor_classifier.create(sf, target='species')
>>> ans = m.evaluate(sf_train, max_neighbors=2,
... metric='confusion_matrix')
>>> print ans['confusion_matrix']
+--------------+-----------------+-------+
| target_label | predicted_label | count |
+--------------+-----------------+-------+
| cat | dog | 1 |
| dog | dog | 2 |
| fossa | dog | 1 |
+--------------+-----------------+-------+
"""
## Validate the metric name
_raise_error_evaluation_metric_is_valid(metric,
['auto', 'accuracy', 'confusion_matrix', 'roc_curve'])
## Make sure the input dataset has a target column with an appropriate
# type.
target = self.target
_raise_error_if_column_exists(dataset, target, 'dataset', target)
if not dataset[target].dtype == str and not dataset[target].dtype == int:
raise TypeError("The target column of the evaluation dataset must "
"contain integers or strings.")
if self.num_classes != 2:
if (metric == 'roc_curve') or (metric == ['roc_curve']):
err_msg = "Currently, ROC curve is not supported for "
err_msg += "multi-class classification in this model."
raise _ToolkitError(err_msg)
else:
warn_msg = "WARNING: Ignoring `roc_curve`. "
warn_msg += "Not supported for multi-class classification."
print(warn_msg)
## Compute predictions with the input dataset.
ystar = self.predict(dataset, output_type='class',
max_neighbors=max_neighbors, radius=radius)
ystar_prob = self.predict(dataset, output_type='probability',
max_neighbors=max_neighbors, radius=radius)
## Compile accuracy metrics
results = {}
if metric in ['accuracy', 'auto']:
results['accuracy'] = _evaluation.accuracy(targets=dataset[target],
predictions=ystar)
if metric in ['confusion_matrix', 'auto']:
results['confusion_matrix'] = \
_evaluation.confusion_matrix(targets=dataset[target],
predictions=ystar)
if self.num_classes == 2:
if metric in ['roc_curve', 'auto']:
results['roc_curve'] = \
_evaluation.roc_curve(targets=dataset[target],
predictions=ystar_prob)
return results | python | def evaluate(self, dataset, metric='auto', max_neighbors=10, radius=None):
"""
Evaluate the model's predictive accuracy. This is done by predicting the
target class for instances in a new dataset and comparing to known
target values.
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the target and features used for model training. Additional
columns are ignored.
metric : str, optional
Name of the evaluation metric. Possible values are:
- 'auto': Returns all available metrics.
- 'accuracy': Classification accuracy.
- 'confusion_matrix': An SFrame with counts of possible
prediction/true label combinations.
- 'roc_curve': An SFrame containing information needed for an roc
curve (binary classification only).
max_neighbors : int, optional
Maximum number of neighbors to consider for each point.
radius : float, optional
Maximum distance from each point to a neighbor in the reference
dataset.
Returns
-------
out : dict
Evaluation results. The dictionary keys are *accuracy* and
*confusion_matrix* and *roc_curve* (if applicable).
See also
--------
create, predict, predict_topk, classify
Notes
-----
- Because the model randomly breaks ties between predicted classes, the
results of repeated calls to `evaluate` method may differ.
Examples
--------
>>> sf_train = turicreate.SFrame({'species': ['cat', 'dog', 'fossa', 'dog'],
... 'height': [9, 25, 20, 23],
... 'weight': [13, 28, 33, 22]})
>>> m = turicreate.nearest_neighbor_classifier.create(sf, target='species')
>>> ans = m.evaluate(sf_train, max_neighbors=2,
... metric='confusion_matrix')
>>> print ans['confusion_matrix']
+--------------+-----------------+-------+
| target_label | predicted_label | count |
+--------------+-----------------+-------+
| cat | dog | 1 |
| dog | dog | 2 |
| fossa | dog | 1 |
+--------------+-----------------+-------+
"""
## Validate the metric name
_raise_error_evaluation_metric_is_valid(metric,
['auto', 'accuracy', 'confusion_matrix', 'roc_curve'])
## Make sure the input dataset has a target column with an appropriate
# type.
target = self.target
_raise_error_if_column_exists(dataset, target, 'dataset', target)
if not dataset[target].dtype == str and not dataset[target].dtype == int:
raise TypeError("The target column of the evaluation dataset must "
"contain integers or strings.")
if self.num_classes != 2:
if (metric == 'roc_curve') or (metric == ['roc_curve']):
err_msg = "Currently, ROC curve is not supported for "
err_msg += "multi-class classification in this model."
raise _ToolkitError(err_msg)
else:
warn_msg = "WARNING: Ignoring `roc_curve`. "
warn_msg += "Not supported for multi-class classification."
print(warn_msg)
## Compute predictions with the input dataset.
ystar = self.predict(dataset, output_type='class',
max_neighbors=max_neighbors, radius=radius)
ystar_prob = self.predict(dataset, output_type='probability',
max_neighbors=max_neighbors, radius=radius)
## Compile accuracy metrics
results = {}
if metric in ['accuracy', 'auto']:
results['accuracy'] = _evaluation.accuracy(targets=dataset[target],
predictions=ystar)
if metric in ['confusion_matrix', 'auto']:
results['confusion_matrix'] = \
_evaluation.confusion_matrix(targets=dataset[target],
predictions=ystar)
if self.num_classes == 2:
if metric in ['roc_curve', 'auto']:
results['roc_curve'] = \
_evaluation.roc_curve(targets=dataset[target],
predictions=ystar_prob)
return results | [
"def",
"evaluate",
"(",
"self",
",",
"dataset",
",",
"metric",
"=",
"'auto'",
",",
"max_neighbors",
"=",
"10",
",",
"radius",
"=",
"None",
")",
":",
"## Validate the metric name",
"_raise_error_evaluation_metric_is_valid",
"(",
"metric",
",",
"[",
"'auto'",
",",
"'accuracy'",
",",
"'confusion_matrix'",
",",
"'roc_curve'",
"]",
")",
"## Make sure the input dataset has a target column with an appropriate",
"# type.",
"target",
"=",
"self",
".",
"target",
"_raise_error_if_column_exists",
"(",
"dataset",
",",
"target",
",",
"'dataset'",
",",
"target",
")",
"if",
"not",
"dataset",
"[",
"target",
"]",
".",
"dtype",
"==",
"str",
"and",
"not",
"dataset",
"[",
"target",
"]",
".",
"dtype",
"==",
"int",
":",
"raise",
"TypeError",
"(",
"\"The target column of the evaluation dataset must \"",
"\"contain integers or strings.\"",
")",
"if",
"self",
".",
"num_classes",
"!=",
"2",
":",
"if",
"(",
"metric",
"==",
"'roc_curve'",
")",
"or",
"(",
"metric",
"==",
"[",
"'roc_curve'",
"]",
")",
":",
"err_msg",
"=",
"\"Currently, ROC curve is not supported for \"",
"err_msg",
"+=",
"\"multi-class classification in this model.\"",
"raise",
"_ToolkitError",
"(",
"err_msg",
")",
"else",
":",
"warn_msg",
"=",
"\"WARNING: Ignoring `roc_curve`. \"",
"warn_msg",
"+=",
"\"Not supported for multi-class classification.\"",
"print",
"(",
"warn_msg",
")",
"## Compute predictions with the input dataset.",
"ystar",
"=",
"self",
".",
"predict",
"(",
"dataset",
",",
"output_type",
"=",
"'class'",
",",
"max_neighbors",
"=",
"max_neighbors",
",",
"radius",
"=",
"radius",
")",
"ystar_prob",
"=",
"self",
".",
"predict",
"(",
"dataset",
",",
"output_type",
"=",
"'probability'",
",",
"max_neighbors",
"=",
"max_neighbors",
",",
"radius",
"=",
"radius",
")",
"## Compile accuracy metrics",
"results",
"=",
"{",
"}",
"if",
"metric",
"in",
"[",
"'accuracy'",
",",
"'auto'",
"]",
":",
"results",
"[",
"'accuracy'",
"]",
"=",
"_evaluation",
".",
"accuracy",
"(",
"targets",
"=",
"dataset",
"[",
"target",
"]",
",",
"predictions",
"=",
"ystar",
")",
"if",
"metric",
"in",
"[",
"'confusion_matrix'",
",",
"'auto'",
"]",
":",
"results",
"[",
"'confusion_matrix'",
"]",
"=",
"_evaluation",
".",
"confusion_matrix",
"(",
"targets",
"=",
"dataset",
"[",
"target",
"]",
",",
"predictions",
"=",
"ystar",
")",
"if",
"self",
".",
"num_classes",
"==",
"2",
":",
"if",
"metric",
"in",
"[",
"'roc_curve'",
",",
"'auto'",
"]",
":",
"results",
"[",
"'roc_curve'",
"]",
"=",
"_evaluation",
".",
"roc_curve",
"(",
"targets",
"=",
"dataset",
"[",
"target",
"]",
",",
"predictions",
"=",
"ystar_prob",
")",
"return",
"results"
] | Evaluate the model's predictive accuracy. This is done by predicting the
target class for instances in a new dataset and comparing to known
target values.
Parameters
----------
dataset : SFrame
Dataset of new observations. Must include columns with the same
names as the target and features used for model training. Additional
columns are ignored.
metric : str, optional
Name of the evaluation metric. Possible values are:
- 'auto': Returns all available metrics.
- 'accuracy': Classification accuracy.
- 'confusion_matrix': An SFrame with counts of possible
prediction/true label combinations.
- 'roc_curve': An SFrame containing information needed for an roc
curve (binary classification only).
max_neighbors : int, optional
Maximum number of neighbors to consider for each point.
radius : float, optional
Maximum distance from each point to a neighbor in the reference
dataset.
Returns
-------
out : dict
Evaluation results. The dictionary keys are *accuracy* and
*confusion_matrix* and *roc_curve* (if applicable).
See also
--------
create, predict, predict_topk, classify
Notes
-----
- Because the model randomly breaks ties between predicted classes, the
results of repeated calls to `evaluate` method may differ.
Examples
--------
>>> sf_train = turicreate.SFrame({'species': ['cat', 'dog', 'fossa', 'dog'],
... 'height': [9, 25, 20, 23],
... 'weight': [13, 28, 33, 22]})
>>> m = turicreate.nearest_neighbor_classifier.create(sf, target='species')
>>> ans = m.evaluate(sf_train, max_neighbors=2,
... metric='confusion_matrix')
>>> print ans['confusion_matrix']
+--------------+-----------------+-------+
| target_label | predicted_label | count |
+--------------+-----------------+-------+
| cat | dog | 1 |
| dog | dog | 2 |
| fossa | dog | 1 |
+--------------+-----------------+-------+ | [
"Evaluate",
"the",
"model",
"s",
"predictive",
"accuracy",
".",
"This",
"is",
"done",
"by",
"predicting",
"the",
"target",
"class",
"for",
"instances",
"in",
"a",
"new",
"dataset",
"and",
"comparing",
"to",
"known",
"target",
"values",
"."
] | 74514c3f99e25b46f22c6e02977fe3da69221c2e | https://github.com/apple/turicreate/blob/74514c3f99e25b46f22c6e02977fe3da69221c2e/src/unity/python/turicreate/toolkits/classifier/nearest_neighbor_classifier.py#L734-L847 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.