Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<compute_test_metric>
import pandas as pd
def normalize_rows(x: np.ndarray):
return np.nan_to_num(x/np.linalg.norm(x, ord=2, axis=1, keepdims=True))
def cosine_distance(features1, features2):
features1_nozero = features1[np.sum(features1, axis=1)!= 0]
features2_nozero = features2[np.sum(features2, axis=1)!= 0]
norm_f1 = normalize_rows(features1_nozero)
 norm_f2 = normalize_rows(features2_nozero)
 d = 1.0 - np.abs(np.matmul(norm_f1, norm_f2.T))
mean_min_d = np.mean(np.min(d, axis=1))
return mean_min_d
def distance_thresholding(d, eps):
if d < eps:
return d
else:
return 1
def calculate_frechet_distance(mu1, sigma1, mu2, sigma2, eps=1e-6):
mu1 = np.atleast_1d(mu1)
 mu2 = np.atleast_1d(mu2)
 sigma1 = np.atleast_2d(sigma1)
 sigma2 = np.atleast_2d(sigma2)
 assert mu1.shape == mu2.shape, "Training and test mean vectors have different lengths"
assert sigma1.shape == sigma2.shape, "Training and test covariances have different dimensions"
diff = mu1 - mu2
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
 if not np.isfinite(covmean ).all() :
msg = "fid calculation produces singular product; adding %s to diagonal of cov estimates" % eps
warnings.warn(msg)
 offset = np.eye(sigma1.shape[0])* eps
covmean = linalg.sqrtm(( sigma1 + offset ).dot(sigma2 + offset))
if np.iscomplexobj(covmean):
if not np.allclose(np.diagonal(covmean ).imag, 0, atol=1e-3):
m = np.max(np.abs(covmean.imag))
raise ValueError("Imaginary component {}".format(m))
covmean = covmean.real
tr_covmean = np.trace(covmean)
 return diff.dot(diff)+ np.trace(sigma1)+ np.trace(sigma2)- 2 * tr_covmean
def calculate_activation_statistics(images, sess, model_name, batch_size=50, verbose=False):
act = get_activations(images, sess, model_name, batch_size, verbose)
 mu = np.mean(act, axis=0)
 sigma = np.cov(act, rowvar=False)
 return mu, sigma, act
def _handle_path_memorization(path, sess, model_name, is_checksize, is_check_png):
path = pathlib.Path(path)
 files = list(path.glob('*.jpg')) + list(path.glob('*.png'))
imsize = model_params[model_name]['imsize']
x = np.array([np.array(img_read_checks(fn, imsize, is_checksize, imsize, is_check_png)) for fn in files])
 m, s, features = calculate_activation_statistics(x, sess, model_name)
 del x
return m, s, features
def img_read_checks(filename, resize_to, is_checksize=False, check_imsize = 64, is_check_png = False):
im = Image.open(str(filename))
if is_checksize and im.size !=(check_imsize,check_imsize):
raise KernelEvalException('The images are not of size '+str(check_imsize))
if is_check_png and im.format != 'PNG':
raise KernelEvalException('Only PNG images should be submitted.')
 if resize_to is None:
return im
else:
return im.resize(( resize_to,resize_to),Image.ANTIALIAS)
 def calculate_kid_given_paths(paths, model_name, model_path, feature_path=None):

 tf.reset_default_graph()
create_model_graph(str(model_path))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 m1, s1, features1 = _handle_path_memorization(paths[0], sess, model_name, is_checksize = True, is_check_png = True)
 if feature_path is None:
m2, s2, features2 = _handle_path_memorization(paths[1], sess, model_name, is_checksize = False, is_check_png = False)
 else:
with np.load(feature_path)as f:
m2, s2, features2 = f['m'], f['s'], f['features']
fid_value = calculate_frechet_distance(m1, s1, m2, s2)
 distance = cosine_distance(features1, features2)
 return fid_value, distance<compute_train_metric>
train = pd.read_csv('.. /input/nlp-getting-started/train.csv')
 print(train.shape)
 train.head(5 )
def compute_mifid(generated_path, real_path, graph_path, model_params, eps=10e-15):
fid_value, distance = calculate_kid_given_paths([generated_path, real_path],
'Inception', graph_path)
 distance = distance_thresholding(distance, model_params['Inception']['cosine_distance_eps'])
 return fid_value /(distance + eps )<normalization>
test = pd.read_csv('.. /input/nlp-getting-started/test.csv')
 print(test.shape)
 test.head(5 )
def l2normalize(v, eps=1e-12):
return v /(v.norm() + eps)
 class SpectralNorm(nn.Module):
def __init__(self, module, name='weight', power_iterations=1):
super(SpectralNorm, self ).__init__()
self.module = module
self.name = name
self.power_iterations = power_iterations
if not self._made_params() :
self._make_params()
def _update_u_v(self):
u = getattr(self.module, self.name + "_u")
 v = getattr(self.module, self.name + "_v")
 w = getattr(self.module, self.name + "_bar")
 height = w.data.shape[0]
for _ in range(self.power_iterations):
v.data = l2normalize(torch.mv(torch.t(w.view(height,-1 ).data), u.data))
u.data = l2normalize(torch.mv(w.view(height,-1 ).data, v.data))
sigma = u.dot(w.view(height, -1 ).mv(v))
setattr(self.module, self.name, w / sigma.expand_as(w))
def _made_params(self):
try:
u = getattr(self.module, self.name + "_u")
 v = getattr(self.module, self.name + "_v")
 w = getattr(self.module, self.name + "_bar")
 return True
except AttributeError:
return False
def _make_params(self):
w = getattr(self.module, self.name)
 height = w.data.shape[0]
width = w.view(height, -1 ).data.shape[1]
u = Parameter(w.data.new(height ).normal_(0, 1), requires_grad=False)
 v = Parameter(w.data.new(width ).normal_(0, 1), requires_grad=False)
 u.data = l2normalize(u.data)
 v.data = l2normalize(v.data)
 w_bar = Parameter(w.data)
 del self.module._parameters[self.name]
self.module.register_parameter(self.name + "_u", u)
 self.module.register_parameter(self.name + "_v", v)
 self.module.register_parameter(self.name + "_bar", w_bar)
 def forward(self, *args):
self._update_u_v()
return self.module.forward(*args )<train_model>
import re
class _equalized_conv2d(th.nn.Module):

 def __init__(self, c_in, c_out, k_size, stride=1, pad=0, bias=True):

 super(_equalized_conv2d, self ).__init__()
self.weight = th.nn.Parameter(th.nn.init.normal_(
 th.empty(c_out, c_in, *_pair(k_size)) 
))
self.use_bias = bias
self.stride = stride
self.pad = pad
if self.use_bias:
self.bias = th.nn.Parameter(th.FloatTensor(c_out ).fill_(0))
fan_in = prod(_pair(k_size)) * c_in
self.scale = sqrt(2)/ sqrt(fan_in)
 def forward(self, x):

 return conv2d(input=x,
weight=self.weight * self.scale,
bias=self.bias if self.use_bias else None,
stride=self.stride,
padding=self.pad)
 def extra_repr(self):
return ", ".join(map(str, self.weight.shape))
class _equalized_deconv2d(th.nn.Module):

 def __init__(self, c_in, c_out, k_size, stride=1, pad=0, bias=True):

 super(_equalized_deconv2d, self ).__init__()
self.weight = th.nn.Parameter(th.nn.init.normal_(
 th.empty(c_in, c_out, *_pair(k_size)) 
))
self.use_bias = bias
self.stride = stride
self.pad = pad
if self.use_bias:
self.bias = th.nn.Parameter(th.FloatTensor(c_out ).fill_(0))
fan_in = c_in
self.scale = sqrt(2)/ sqrt(fan_in)
 def forward(self, x):

 return conv_transpose2d(input=x,
weight=self.weight * self.scale,
bias=self.bias if self.use_bias else None,
stride=self.stride,
padding=self.pad)
 def extra_repr(self):
return ", ".join(map(str, self.weight.shape))
class _equalized_linear(th.nn.Module):

 def __init__(self, c_in, c_out, bias=True):

 super(_equalized_linear, self ).__init__()
self.weight = th.nn.Parameter(th.nn.init.normal_(
 th.empty(c_out, c_in)
))
self.use_bias = bias
if self.use_bias:
self.bias = th.nn.Parameter(th.FloatTensor(c_out ).fill_(0))
fan_in = c_in
self.scale = sqrt(2)/ sqrt(fan_in)
 def forward(self, x):

 return linear(x, self.weight * self.scale,
self.bias if self.use_bias else None )<normalization>
def urlremove(text):
return re.sub(r"(?:\@|https?\://)\S+", "", text)
 train['text'] = train['text'].apply(urlremove)
 test['text'] = test['text'].apply(urlremove)
 train['text']
class PixelwiseNorm(th.nn.Module):
def __init__(self):
super(PixelwiseNorm, self ).__init__()
def forward(self, x, alpha=1e-8):

 y = x.pow(2.).mean(dim=1, keepdim=True ).add(alpha ).sqrt()
y = x / y
return y<categorify>
def remove_emoji(text):
emoji_pattern = re.compile(
 u"(\ud83d[\ude00-\ude4f])|"
u"(\ud83c[\udf00-\uffff])|"
u"(\ud83d[\u0000-\uddff])|"
u"(\ud83d[\ude80-\udeff])|"
u"(\ud83c[\udde0-\uddff])"
"+", flags=re.UNICODE)
 return emoji_pattern.sub(r'', text)
 train['text'] = train['text'].apply(remove_emoji)
 test['text'] = test['text'].apply(remove_emoji)
 train['text']
class MinibatchStdDev(th.nn.Module):

 def __init__(self):

 super(MinibatchStdDev, self ).__init__()
def forward(self, x, alpha=1e-8):

 batch_size, _, height, width = x.shape
y = x - x.mean(dim=0, keepdim=True)
 y = th.sqrt(y.pow(2.).mean(dim=0, keepdim=False)+ alpha)
 y = y.mean().view(1, 1, 1, 1)
 y = y.repeat(batch_size, 1, height, width)
 y = th.cat([x, y], 1)
 return y<choose_model_class>
def decontracted(text):
if '’' in text:
text = text.replace("’", "'")
 text = re.sub(r"won't", "will not", text)
 text = re.sub(r"can't", "can not", text)
 text = re.sub(r"n't", " not", text)
 text = re.sub(r"'re", " are", text)
 text = re.sub(r"'s", " is", text)
 text = re.sub(r"'d", " would", text)
 text = re.sub(r"'ll", " will", text)
 text = re.sub(r"'t", " not", text)
 text = re.sub(r"'ve", " have", text)
 text = re.sub(r"'m", " am", text)
 return text
train['text'] = train['text'].apply(decontracted)
 test['text'] = test['text'].apply(decontracted)
 train['text']
class GenInitialBlock(th.nn.Module):
def __init__(self, in_channels, use_eql, use_spec_norm=False):
super(GenInitialBlock, self ).__init__()
if use_eql:
self.conv_1 = _equalized_deconv2d(in_channels, in_channels,(4, 4), bias=True)
 self.conv_2 = _equalized_conv2d(in_channels, in_channels,(3, 3), pad=1, bias=True)
 else:
self.conv_1 = ConvTranspose2d(in_channels, in_channels,(4, 4), bias=True)
 self.conv_2 = Conv2d(in_channels, in_channels,(3, 3), padding=1, bias=True)
 if use_spec_norm:
self.conv_1 = SpectralNorm(self.conv_1)
 self.conv_2 = SpectralNorm(self.conv_2)
 self.pixNorm = PixelwiseNorm()
self.lrelu = LeakyReLU(0.2)
 def forward(self, x):
y = th.unsqueeze(th.unsqueeze(x, -1), -1)
 y = self.lrelu(self.conv_1(y))
y = self.lrelu(self.conv_2(y))
y = self.pixNorm(y)
 return y<define_search_model>
def character_remove(text):
return re.sub('\W+|_', ' ', text)
 train.text = train.text.apply(character_remove)
 test.text = test.text.apply(character_remove)
 train.text
class GenGeneralConvBlock(th.nn.Module):
def __init__(self, in_channels, out_channels, use_eql, use_spec_norm=False):
super(GenGeneralConvBlock, self ).__init__()
self.upsample = lambda x: interpolate(x, scale_factor=2)
 if use_eql:
self.conv_1 = _equalized_conv2d(in_channels, out_channels,(3, 3),
pad=1, bias=True)
 self.conv_2 = _equalized_conv2d(out_channels, out_channels,(3, 3),
pad=1, bias=True)
 else:
self.conv_1 = Conv2d(in_channels, out_channels,(3, 3),
padding=1, bias=True)
 self.conv_2 = Conv2d(out_channels, out_channels,(3, 3),
padding=1, bias=True)
 if use_spec_norm:
self.conv_1 = SpectralNorm(self.conv_1)
 self.conv_2 = SpectralNorm(self.conv_2)
 self.pixNorm = PixelwiseNorm()
self.lrelu = LeakyReLU(0.2)
 def forward(self, x):

 y = self.upsample(x)
 y = self.pixNorm(self.lrelu(self.conv_1(y)))
 y = self.pixNorm(self.lrelu(self.conv_2(y)))
 return y<choose_model_class>
def remove_numbers(text):
pattern = r'[^a-zA-z.,!?/:;"'\s]'
return re.sub(pattern, '', text)
 train.text = train.text.apply(remove_numbers)
 train.text = train.text.str.lower()
train.text = train.text.str.strip()
test.text = test.text.apply(remove_numbers)
 test.text = test.text.str.lower()
test.text = test.text.str.strip()
train['text']
class DisGeneralConvBlock(th.nn.Module):

 def __init__(self, in_channels, out_channels, use_eql, use_spec_norm=False):

 super(DisGeneralConvBlock, self ).__init__()
if use_eql:
self.conv_1 = _equalized_conv2d(in_channels, in_channels,(3, 3), pad=1, bias=True)
 self.conv_2 = _equalized_conv2d(in_channels, out_channels,(3, 3), pad=1, bias=True)
 else:
self.conv_1 = Conv2d(in_channels, in_channels,(3, 3), padding=1, bias=True)
 self.conv_2 = Conv2d(in_channels, out_channels,(3, 3), padding=1, bias=True)
 if use_spec_norm:
self.conv_1 = SpectralNorm(self.conv_1)
 self.conv_2 = SpectralNorm(self.conv_2)
 self.downSampler = AvgPool2d(2)
 self.lrelu = LeakyReLU(0.2)
 def forward(self, x):
y = self.lrelu(self.conv_1(x))
y = self.lrelu(self.conv_2(y))
y = self.downSampler(y)
 return y<define_search_model>
from sklearn.feature_extraction.text import TfidfVectorizer
class ConDisFinalBlock(th.nn.Module):
def __init__(self, in_channels, num_classes, use_eql):
super(ConDisFinalBlock, self ).__init__()
self.batch_discriminator = MinibatchStdDev()
if use_eql:
self.conv_1 = _equalized_conv2d(in_channels + 1, in_channels,(3, 3), pad=1, bias=True)
 self.conv_2 = _equalized_conv2d(in_channels, in_channels,(4, 4), bias=True)
 self.conv_3 = _equalized_conv2d(in_channels, 1,(1, 1), bias=True)
 else:
self.conv_1 = Conv2d(in_channels + 1, in_channels,(3, 3), padding=1, bias=True)
 self.conv_2 = Conv2d(in_channels, in_channels,(4, 4), bias=True)
 self.conv_3 = Conv2d(in_channels, 1,(1, 1), bias=True)
 self.label_embedder = Embedding(num_classes, in_channels, max_norm=1)
 self.lrelu = LeakyReLU(0.2)
 nb_ft = 128
self.ft_matching_dense = nn.Linear(2 * in_channels, nb_ft)
 def forward(self, x, labels, return_ft=False):

 batch_size = x.size() [0]
y = self.batch_discriminator(x)
 y = self.lrelu(self.conv_1(y))
y = self.lrelu(self.conv_2(y))
y_ = y.view(( batch_size, -1))
labels = self.label_embedder(labels.cuda() ).view(( batch_size, -1))
if return_ft:
self.ft_matching_dense(torch.cat(( y_, labels), 1))
projection_scores =(y_ * labels ).sum(dim=-1)
 y = self.lrelu(self.conv_3(y))
final_score = y.view(-1)+ projection_scores
return final_score<normalization>
tfidf = TfidfVectorizer(min_df=1, max_df=0.3, ngram_range=(1, 2))
print(tfidf)
 text_tfidf = tfidf.fit_transform(train['text'])
 text_tfidf2 = tfidf.transform(test['text'])
class GaussianNoise(nn.Module):
def __init__(self, sigma=0.1):
super().__init__()
self.sigma = sigma
self.noise = torch.tensor(0 ).cuda()
def forward(self, x):
if self.training:
noise = self.noise.repeat(*x.size() ).float().normal_() * self.sigma
return x + noise
return x<choose_model_class>
clf = LogisticRegression(C=4.5)
 scores = model_selection.cross_val_score(clf, text_tfidf, train["target"], cv=3, scoring="f1")
 scores
class Generator(nn.Module):
def __init__(self, depth=5, latent_size=128, use_eql=True, use_spec_norm=False):
super(Generator, self ).__init__()
assert latent_size != 0 and(( latent_size &(latent_size - 1)) == 0), "latent size not a power of 2"
if depth >= 4: assert latent_size >= np.power(2, depth - 4), "latent size will diminish to zero"
self.use_eql = use_eql
self.use_spec_norm = use_spec_norm
self.depth = depth
self.latent_size = latent_size
self.initial_block = GenInitialBlock(self.latent_size, use_eql=self.use_eql, use_spec_norm=False)
 self.layers = ModuleList([])
 if self.use_eql:
self.toRGB = lambda in_channels: _equalized_conv2d(in_channels, 3,(1, 1), bias=True)
 else:
self.toRGB = lambda in_channels: Conv2d(in_channels, 3,(1, 1), bias=True)
 self.rgb_converters = ModuleList([self.toRGB(self.latent_size)])
 for i in range(self.depth - 1):
if i <= 2:
layer = GenGeneralConvBlock(self.latent_size, self.latent_size, use_eql=self.use_eql, use_spec_norm=use_spec_norm)
 rgb = self.toRGB(self.latent_size)
 else:
in_size = int(self.latent_size // np.power(2, i - 3))
out_size = int(self.latent_size // np.power(2, i - 2))
layer = nn.Sequential(
 GenGeneralConvBlock(in_size, out_size, use_eql=self.use_eql, use_spec_norm=use_spec_norm), 
)
 rgb = self.toRGB(out_size)
 self.layers.append(layer)
 self.rgb_converters.append(rgb)
 self.temporaryUpsampler = lambda x: interpolate(x, scale_factor=2)
 self.tanh = nn.Tanh()
def forward(self, x, depth, alpha):
assert depth < self.depth, "Requested output depth cannot be produced"
y = self.initial_block(x)
 if depth > 0:
for block in self.layers[:depth - 1]:
y = block(y)
 residual = self.rgb_converters[depth - 1](self.temporaryUpsampler(y))
straight = self.rgb_converters[depth](self.layers[depth - 1](y))
out =(alpha * straight)+(( 1 - alpha)* residual)
 else:
out = self.rgb_converters[0](y)
 return self.tanh(out )<set_options>
clf.fit(text_tfidf, train["target"] )
class ConditionalDiscriminator(nn.Module):
def __init__(self, num_classes, height=7, feature_size=512, use_eql=True, use_spec_norm=False):
super(ConditionalDiscriminator, self ).__init__()
assert feature_size != 0 and(( feature_size &(feature_size - 1)) == 0), "latent size not a power of 2"
if height >= 4:
assert feature_size >= np.power(2, height - 4), "feature size cannot be produced"
self.use_eql = use_eql
self.use_spec_norm = use_spec_norm
self.height = height
self.feature_size = feature_size
self.num_classes = num_classes
self.noise = GaussianNoise(sigma=0.2)
 self.final_block = ConDisFinalBlock(self.feature_size, self.num_classes, use_eql=self.use_eql)
 self.layers = ModuleList([])
 if self.use_eql:
self.fromRGB = lambda out_channels: _equalized_conv2d(3, out_channels,(1, 1), bias=True)
 else:
self.fromRGB = lambda out_channels: Conv2d(3, out_channels,(1, 1), bias=True)
 rgb = self.fromRGB(self.feature_size)
 if use_spec_norm:
rgb = SpectralNorm(rgb)
 self.rgb_to_features = ModuleList([rgb])
 for i in range(self.height - 1):
if i > 2:
in_size = int(self.feature_size // np.power(2, i - 2))
out_size = int(self.feature_size // np.power(2, i - 3))
layer = nn.Sequential(
 DisGeneralConvBlock(in_size, out_size, use_eql=self.use_eql, use_spec_norm=use_spec_norm), 
)
 rgb = self.fromRGB(in_size)
 else:
layer = nn.Sequential(
 DisGeneralConvBlock(self.feature_size, self.feature_size, use_eql=self.use_eql, use_spec_norm=use_spec_norm), 
)
 rgb = self.fromRGB(self.feature_size)
 if use_spec_norm:
rgb = SpectralNorm(rgb)
 self.layers.append(layer)
 self.rgb_to_features.append(rgb)
 self.temporaryDownsampler = AvgPool2d(2)
 def forward(self, x, labels, height, alpha, return_ft=False):
assert height < self.height, "Requested output depth cannot be produced"
if height > 0:
residual = self.rgb_to_features[height - 1](self.temporaryDownsampler(x))
straight = self.layers[height - 1](
 self.rgb_to_features[height](x)
)
 y =(alpha * straight)+(( 1 - alpha)* residual)
 for block in reversed(self.layers[:height - 1]):
y = block(y)
 else:
y = self.rgb_to_features[0](x)
 out = self.final_block(y, labels, return_ft=return_ft)
 return out<compute_train_metric>
submission = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 submission["target"] = clf.predict(text_tfidf2)
 submission.to_csv("submission55.csv", index=False )