Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<define_variables>
warnings.filterwarnings('ignore' )
black_list=[]<normalization>
train_df = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test_df = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 print('Number of Training Samples = {}'.format(train_df.shape[0]))
print('Number of Test Samples = {}
'.format(test_df.shape[0]))
print('Training X Shape = {}'.format(train_df.shape))
print('Training y Shape = {}
'.format(train_df['target'].shape[0]))
print('Test X Shape = {}'.format(test_df.shape))
print('Test y Shape = {}
'.format(test_df.shape[0]))
print('Index of Train Set:
', train_df.columns)
 print('Index of Test Set:
', test_df.columns )
class FullCroppedDogsFolderDataset(torchvision.datasets.vision.VisionDataset):
def __init__(self, root, transform=None, target_transform=None):
super().__init__(root, transform=transform, target_transform=target_transform)
 self.transform = transform
self.target_transform = target_transform
self.samples = self._load_subfolders_images(self.root)
 if len(self.samples)== 0:
raise RuntimeError("Found 0 files in subfolders of: {}".format(self.root))
def _load_subfolders_images(self, root):
IMG_EXTENSIONS =('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')
 def is_valid_file(x):
return torchvision.datasets.folder.has_file_allowed_extension(x, IMG_EXTENSIONS)
 required_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(64),
torchvision.transforms.CenterCrop(64),
])
 imgs = []
paths = []
for root, _, fnames in sorted(os.walk(root)) :
for fname in sorted(fnames):
path = os.path.join(root, fname)
 paths.append(path)
 pbar = tqdm(paths, desc='Loading cropped images')
 for path in pbar:
file = path.split('/')[-1]
if is_valid_file(path)and file not in black_list:
img = torchvision.datasets.folder.default_loader(path)
 annotation_basename = os.path.splitext(os.path.basename(path)) [0]
annotation_dirname = next(dirname for dirname in os.listdir('.. /input/annotation/Annotation/')if dirname.startswith(annotation_basename.split('_')[0]))
annotation_filename = os.path.join('.. /input/annotation/Annotation', annotation_dirname, annotation_basename)
 tree = ET.parse(annotation_filename)
 root = tree.getroot()
objects = root.findall('object')
 max_dogs_per_image = 1
if len(objects)<= max_dogs_per_image:
for o in objects:
bndbox = o.find('bndbox')
 xmin = int(bndbox.find('xmin' ).text)
 ymin = int(bndbox.find('ymin' ).text)
 xmax = int(bndbox.find('xmax' ).text)
 ymax = int(bndbox.find('ymax' ).text)
 w = xmax - xmin
h = ymax - ymin
if h > w:
diff = h - w
xmin = xmin - diff/2
xmax = xmax + diff/2
xmax = min(xmax,img.width)
 xmin = max(xmin,0)
 if w > h:
diff = w - h
ymin = ymin - diff/2
ymax = ymax + diff/2
ymax = min(ymax,img.height)
 ymin = max(ymin,0)
 bbox =(xmin, ymin, xmax, ymax)
 object_img = required_transforms(img.crop(bbox))
imgs.append(object_img)
 pbar.set_postfix_str("{} cropped images loaded".format(len(imgs)))
 return imgs
def __getitem__(self, index):
sample = self.samples[index]
target = 1
if self.transform is not None:
sample = self.transform(sample)
 if self.target_transform is not None:
target = self.target_transform(target)
 return sample, target
def __len__(self):
return len(self.samples )<set_options>
train_df.loc[1241]['text']
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device )<normalization>
train_df.loc[2301]['text']
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv')!= -1:
m.weight.data.normal_(0.0, 0.02)
 elif classname.find('BatchNorm')!= -1:
m.weight.data.normal_(1.0, 0.02)
 m.bias.data.fill_(0 )<choose_model_class>
train_df.groupby(['text'] ).nunique().sort_values(by='target', ascending=False)[0:18]
class G(nn.Module):
def __init__(self, nz=128, channels=3):
super(G, self ).__init__()
self.nz = nz
self.channels = channels
self.main = nn.Sequential(
 nn.ConvTranspose2d(self.nz, 1024, 4, 1, 0, bias = False),
nn.BatchNorm2d(1024),
nn.ReLU(True),
nn.ConvTranspose2d(1024, 512, 4, 2, 1, bias = False),
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias = False),
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias = False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128, 64, 4, 2, 1, bias = False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, self.channels, 3, 1, 1, bias = False),
nn.Tanh() 
)
 def forward(self, input):
output = self.main(input)
 return output<train_on_grid>
df_mislabeled = train_df.groupby(['text'] ).nunique().sort_values(by='target', ascending=False)
 df_mislabeled = df_mislabeled[df_mislabeled['target'] > 1]['target']
df_mislabeled_all = df_mislabeled.index.tolist()
print(f'Number of repeated tweets(after preprocessing): {len(df_mislabeled_all)}' )
netG = G(nz ).to(device)
 netG.apply(weights_init )<choose_model_class>
train_df['target_relabeled'] = train_df['target'].copy()
target_1_list = [
"CLEARED:incident with injury:I-495 inner loop Exit 31 - MD 97/Georgia Ave Silver Spring",
".POTUS
"Who is bringing the tornadoes and floods.Who is bringing the climate change.God is after America He is plaguing her
"RT NotExplained: The only known image of infamous hijacker D.B.Cooper.http://t.co/JlzK2HdeTG",
"Caution: breathing may be hazardous to your health." ]
for mislabeled_sample in df_mislabeled_all:
if mislabeled_sample in target_1_list:
train_df.loc[train_df['text'] == mislabeled_sample, 'target_relabeled'] = 1
else:
train_df.loc[train_df['text'] == mislabeled_sample, 'target_relabeled'] = 0
filter_mislabel =(train_df['target'] != train_df['target_relabeled'])
 print(f'Number of relabeled: {len(train_df[filter_mislabel])}')
 train_df[filter_mislabel][:12]
class D(nn.Module):
def __init__(self, channels=3):
super(D, self ).__init__()
self.channels = channels
self.main = nn.Sequential(
 nn.Conv2d(self.channels, 64, 4, 2, 1, bias = False),
nn.LeakyReLU(slope, inplace = True),
nn.Conv2d(64, 128, 4, 2, 1, bias = False),
nn.BatchNorm2d(128),
nn.LeakyReLU(slope, inplace = True),
nn.Conv2d(128, 256, 4, 2, 1, bias = False),
nn.BatchNorm2d(256),
nn.LeakyReLU(slope, inplace = True),
nn.Conv2d(256, 512, 4, 2, 1, bias = False),
nn.BatchNorm2d(512),
nn.LeakyReLU(slope, inplace = True),
nn.Conv2d(512, 1, 4, 1, 0, bias = False),
nn.Sigmoid() 
)
 def forward(self, input):
output = self.main(input)
 return output.view(-1 )<train_model>
def html_references(tweets):
texts = tweets
url_remove = r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F])) +'
texts = re.sub(url_remove, '', texts)
 entities_remove = r'&amp;|&gt;|&lt'
texts = re.sub(entities_remove, "", texts)
 words = texts.split()
words = [word.lower() for word in words]
return " ".join(words)
 train_df['clean_text'] = train_df['text'].apply(lambda x : html_references(x))
test_df['clean_text'] = test_df['text'].apply(lambda x : html_references(x))
netD = D().to(device)
 netD.apply(weights_init )<choose_model_class>
def decontraction(tweet):
tweet = re.sub(r"won't", " will not", tweet)
 tweet = re.sub(r"won't've", " will not have", tweet)
 tweet = re.sub(r"can't", " can not", tweet)
 tweet = re.sub(r"don't", " do not", tweet)
 tweet = re.sub(r"can't've", " can not have", tweet)
 tweet = re.sub(r"ma'am", " madam", tweet)
 tweet = re.sub(r"let's", " let us", tweet)
 tweet = re.sub(r"ain't", " am not", tweet)
 tweet = re.sub(r"shan't", " shall not", tweet)
 tweet = re.sub(r"sha
't", " shall not", tweet)
 tweet = re.sub(r"o'clock", " of the clock", tweet)
 tweet = re.sub(r"y'all", " you all", tweet)
 tweet = re.sub(r"n't", " not", tweet)
 tweet = re.sub(r"n't've", " not have", tweet)
 tweet = re.sub(r"'re", " are", tweet)
 tweet = re.sub(r"'s", " is", tweet)
 tweet = re.sub(r"'d", " would", tweet)
 tweet = re.sub(r"'d've", " would have", tweet)
 tweet = re.sub(r"'ll", " will", tweet)
 tweet = re.sub(r"'ll've", " will have", tweet)
 tweet = re.sub(r"'t", " not", tweet)
 tweet = re.sub(r"'ve", " have", tweet)
 tweet = re.sub(r"'m", " am", tweet)
 tweet = re.sub(r"'re", " are", tweet)
 return tweet
train_df['clean_text'] = train_df['clean_text'].apply(lambda x : decontraction(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : decontraction(x))
%%time
n_epochs = 286
criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr = 0.0003, betas =(0.5, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr = 0.0003, betas =(0.5, 0.999))
lr_schedulerD = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizerD, factor=0.9, patience=37, min_lr=0.0002)
 lr_schedulerG = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizerG, factor=0.9, patience=37, min_lr=0.0002)
 G_losses = []
D_losses = []
for epoch in tqdm(range(n_epochs)) :
for i, data in enumerate(dataloader, 0):
D_losses_epoch = 0
G_losses_epoch = 0
netD.zero_grad()
input = data[0].to(device)
 batch_size = input.size(0)
 target = torch.full(( batch_size, 1), real_label, device=device)
 output = netD(input)
 errD_real = criterion(output, target)
 noise = Variable(torch.randn(batch_size, nz, 1, 1, device=device))
target = torch.full(( batch_size, 1), fake_label, device=device)
 fake = netG(noise)
 output = netD(fake.detach())
 errD_fake = criterion(output, target)
 errD = errD_real + errD_fake
errD.backward()
optimizerD.step()
D_losses_epoch += errD.item()
netG.zero_grad()
target = torch.full(( batch_size, 1), real_label, device=device)
 output = netD(fake)
 errG = criterion(output, target)
 errG.backward()
optimizerG.step()
G_losses_epoch += errG.item()
D_losses.append(errD.item())
 G_losses.append(errG.item())
 lr_schedulerD.step(D_losses_epoch)
 lr_schedulerG.step(G_losses_epoch)
 if epoch % 37 == 0:
for param_group in optimizerD.param_groups:
print('epoch', epoch, 'lr D:', param_group['lr'])
 for param_group in optimizerG.param_groups:
print('epoch', epoch, 'lr G:', param_group['lr'] )<compute_test_metric>
def filter_punctuations_etc(tweets):
words = tweets.split()
re_punc = re.compile('[%s]' % re.escape(string.punctuation))
words = [re_punc.sub('', w)for w in words]
re_print = re.compile('[^%s]' % re.escape(string.printable))
words = [re_print.sub(' ', w)for w in words]
return " ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : filter_punctuations_etc(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : filter_punctuations_etc(x))
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values<define_variables>
def separate_alphanumeric(tweets):
words = tweets
words = re.findall(r"[^\W\d_]+|\d+", words)
 return " ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : separate_alphanumeric(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : separate_alphanumeric(x))
if not os.path.exists('.. /output_images'):
os.mkdir('.. /output_images')
 im_batch_size = 50
n_images=10000
for i_batch in range(0, n_images, im_batch_size):
z = truncated_normal(( im_batch_size, nz, 1, 1), threshold=1)
 gen_z = torch.from_numpy(z ).float().to(device)
 gen_images = netG(gen_z)
 gen_images.mul_(0.5 ).add_(0.5)
 images = gen_images.to('cpu' ).clone().detach()
images = images.numpy().transpose(0, 2, 3, 1)
 for i_image in range(gen_images.size(0)) :
save_image(gen_images[i_image, :, :, :], os.path.join('.. /output_images', f'image_{i_batch+i_image:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<define_variables>
def cont_rep_char(text):
tchr = text.group(0)
 if len(tchr)> 1:
return tchr[0:2]
def unique_char(rep, tweets):
substitute = re.sub(r'(\w)\1+', rep, tweets)
 return substitute
train_df['clean_text'] =(train_df['clean_text'].astype('str' ).apply(lambda x : unique_char(cont_rep_char, x)))
 test_df['clean_text'] =(test_df['clean_text'].astype('str' ).apply(lambda x : unique_char(cont_rep_char, x)) )
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 PATH = '.. /input/generative-dog-images/all-dogs/'
ANNOTATION_PATH = ".. /input/generative-dog-images/annotation/Annotation/"
OUTPUT_PATH = '.. /output_images'
IMG_DIR = "output_images/"
IMAGE_SIZE = 64
BATCH_SIZE = 32
PRINT_EVERY=1000
SHOW_EVERY=1000
PLOT_EVERY=200
EPOCHS=100
lrD = 0.001
lrG = 0.001
beta1 = 0.5
beta2 = 0.999
nz = 128
NFEATS=512
SPECTRAL_NORM=True
NORMALIZATION='adain'
RANDOM_NOISE=True
USE_STYLE=True
LOSS='HINGE'
PIXEL_NORM=True
USE_SOFT_NOISY_LABELS = True
INVERT_LABELS = True
real_label = 0.9
fake_label = 0
N_IMAGES=10000
ComputeLB = True<load_pretrained>
!pip install pyspellchecker
class DogsDataset(Dataset):
def __init__(self, root, transform1=None, transform2=None):
self.root = root
self.transform1 = transform1
self.transform2 = transform2
self.samples, self.crops = self._load_subfolders_images(self.root)
 if len(self.samples)== 0:
raise RuntimeError("Found 0 files in subfolders of: {}".format(self.root))
def _load_subfolders_images(self, root):
IMG_EXTENSIONS =('.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')
 def is_valid_file(x):
return torchvision.datasets.folder.has_file_allowed_extension(x, IMG_EXTENSIONS)
 imgs = []
crops = []
paths = []
for root, _, fnames in sorted(os.walk(root)) :
for fname in sorted(fnames):
path = os.path.join(root, fname)
 paths.append(path)
 pbar = tqdm(paths, desc='Loading cropped images')
 for path in pbar:
if is_valid_file(path):
img = torchvision.datasets.folder.default_loader(path)
 annotation_basename = os.path.splitext(os.path.basename(path)) [0]
annotation_dirname = next(dirname for dirname in os.listdir(ANNOTATION_PATH)if dirname.startswith(annotation_basename.split('_')[0]))
annotation_filename = os.path.join(ANNOTATION_PATH, annotation_dirname, annotation_basename)
 tree = ET.parse(annotation_filename)
 root = tree.getroot()
size = root.find('size')
 width = int(size.find('width' ).text)
 height = int(size.find('height' ).text)
 objects = root.findall('object')
 objects = root.findall('object')
 for o in objects:
bndbox = o.find('bndbox')
 xmin = int(bndbox.find('xmin' ).text)
 ymin = int(bndbox.find('ymin' ).text)
 xmax = int(bndbox.find('xmax' ).text)
 ymax = int(bndbox.find('ymax' ).text)
 xmin = max(0, xmin - 4)
 xmax = min(width, xmax + 4)
 ymin = max(0, ymin - 4)
 ymax = min(height, ymax + 4)
 w = np.min(( xmax - xmin, ymax - ymin))
w = min(w, width, height)
 if w > xmax - xmin:
xmin = min(max(0, xmin - int(( w -(xmax - xmin)) /2)) , width - w)
 xmax = xmin + w
if w > ymax - ymin:
ymin = min(max(0, ymin - int(( w -(ymax - ymin)) /2)) , height - w)
 ymax = ymin + w
img_ = img.crop(( xmin, ymin, xmax, ymax))
if np.mean(img_)!= 0:
img = img_
imgs.append(path)
 crops.append(( xmin, ymin, xmax, ymax))
pbar.set_postfix_str("{} cropped images loaded".format(len(imgs)))
 return imgs, crops
def __getitem__(self, index):
sample = self.samples[index]
sample = torchvision.datasets.folder.default_loader(sample)
 sample = sample.crop(self.crops[index])
 if self.transform2 is not None:
sample = self.transform2(sample)
 return sample
def __len__(self):
return len(self.samples )<normalization>
spell = SpellChecker()
def correct_spellings(text):
corrected_text = []
misspelled_words = spell.unknown(text.split())
 for word in text.split() :
if word in misspelled_words:
corrected_text.append(spell.correction(word))
else:
corrected_text.append(word)
 return " ".join(corrected_text)
transform1 = None
transform2 = transforms.Compose([transforms.RandomResizedCrop(64,(0.85, 1.0),(1.0, 1.0)) ,
transforms.ColorJitter() ,
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 train_data = DogsDataset(
 PATH,
transform1,
transform2 
)
 train_loader = torch.utils.data.DataLoader(train_data, shuffle=True,
batch_size=BATCH_SIZE, num_workers=4)
 real_batch = next(iter(train_loader))
plt.figure(figsize=(8,8))
plt.axis("off")
 plt.title("Training Images")
 plt.imshow(np.transpose(make_grid(real_batch.to(device)[:64], padding=2, normalize=True ).cpu() ,(1,2,0)) )<choose_model_class>
!pip install wordninja
class BatchNormModulate2d(nn.Module):

 def __init__(self, num_features, dim_in, eps=2e-5, momentum=0.1, affine=True,
track_running_stats=True, use_sn=True):
super().__init__()
self.num_features = num_features
self.dim_in = dim_in
self.bn = nn.BatchNorm2d(num_features, affine=False)
 self.gamma = nn.Sequential(
 nn.Linear(dim_in, num_features, bias=True),
nn.LeakyReLU(0.2),
nn.Linear(num_features, num_features, bias=False)
)
 self.beta = nn.Sequential(
 nn.Linear(dim_in, num_features, bias=True),
nn.LeakyReLU(0.2),
nn.Linear(num_features, num_features, bias=False)
)
 def forward(self, x, z):
out = self.bn(x)
 gamma = self.gamma(z)
 beta = self.beta(z)
 out = gamma.view(-1, self.num_features, 1, 1)* out + beta.view(-1, self.num_features, 1, 1)
 return out
class PixelNorm(nn.Module):
def __init__(self, epsilon=1e-8):

 super(PixelNorm, self ).__init__()
self.epsilon = epsilon
def forward(self, x):
tmp = torch.mul(x, x)
 tmp1 = torch.rsqrt(torch.mean(tmp, dim=1, keepdim=True)+ self.epsilon)
 return x * tmp1
class GaussianNoise(nn.Module):
def __init__(self, channels):
super().__init__()
self.weight = nn.Parameter(torch.zeros(1, channels, 1, 1))
def forward(self, x, noise=None):
if noise is None:
noise = torch.randn(x.shape[0], 1, x.shape[2], x.shape[3], device=x.device, dtype=x.dtype)
 return x + self.weight.view(1, -1, 1, 1)* noise.to(x.device)
 class AdaIn(nn.Module):

 def __init__(self, in_channel, latent_dim):
super().__init__()
self.norm = nn.InstanceNorm2d(in_channel)
 self.style = nn.Linear(latent_dim, in_channel * 2)
 def forward(self, input, style):
style = self.style(style ).unsqueeze(2 ).unsqueeze(3)
 gamma, beta = style.chunk(2, 1)
 out = self.norm(input)
 out = gamma * out + beta
return out
class Projection(nn.Module):
def __init__(self,
nz,
in_channel,
out_channel,
shape,
bias=False,
spectral_norm=False,
normalization='selfmod',
random_noise=False,
use_style=False,
use_pixel_norm=False):
super().__init__()
self.shape = shape
self.linear = nn.Linear(in_channel, out_channel, bias=bias)
 self.conv = nn.Conv2d(shape[0], shape[0], 3, 1, 1, bias=bias)
 if spectral_norm:
self.linear = SN(self.linear)
 self.conv = SN(self.conv)
 self.noise1 = None
if random_noise:
self.noise1 = GaussianNoise(shape[0])
 self.noise2 = GaussianNoise(shape[0])
 self.pixel_norm = None
if use_pixel_norm:
self.pixel_norm = PixelNorm()
self.style1 = None
self.style2 = None
if normalization == 'adain':
self.norm1 = AdaIn(shape[0], nz)
 self.norm2 = AdaIn(shape[0], nz)
 else:
self.norm1 = BatchNormModulate2d(shape[0], nz)
 self.norm2 = BatchNormModulate2d(shape[0], nz)
 def forward(self, x, nz):
x = self.linear(x)
 x = x.view([x.shape[0]] + self.shape)
 if self.noise1 is not None:
x = self.noise1(x)
 x = F.leaky_relu(x, 0.2)
 if self.pixel_norm is not None:
x = self.pixel_norm(x)
 x = self.norm1(x, nz)
 x = self.conv(x)
 if self.noise2 is not None:
x = self.noise2(x)
 x = F.leaky_relu(x, 0.2)
 if self.pixel_norm is not None:
x = self.pixel_norm(x)
 x = self.norm2(x, nz)
 return x
class UpConvBlock(nn.Module):

 def __init__(self,
nz,
in_channel,
out_channel,
kernel=4,
stride=2,
padding=1,
bias=False,
spectral_norm=False,
normalization='selfmod',
random_noise=False,
use_style=False,
use_pixel_norm=False):
super().__init__()
self.conv1 = nn.ConvTranspose2d(in_channel, out_channel, kernel, stride, padding, bias=bias)
 self.conv2 = nn.Conv2d(out_channel, out_channel, 3, 1, 1, bias=bias)
 if spectral_norm:
self.conv1 = SN(self.conv1)
 self.conv2 = SN(self.conv2)
 self.noise1 = None
self.noise2 = None
if random_noise:
self.noise1 = GaussianNoise(out_channel)
 self.noise2 = GaussianNoise(out_channel)
 self.pixel_norm = None
if use_pixel_norm:
self.pixel_norm = PixelNorm()
self.style1 = None
self.style2 = None
if normalization == 'adain':
self.norm1 = AdaIn(out_channel, nz)
 self.norm2 = AdaIn(out_channel, nz)
 else:
self.norm1 = BatchNormModulate2d(out_channel, nz)
 self.norm2 = BatchNormModulate2d(out_channel, nz)
 def forward(self, x, latent):
x = self.conv1(x)
 if self.noise1 is not None:
x = self.noise1(x)
 x = F.leaky_relu(x, 0.2)
 if self.pixel_norm is not None:
x = self.pixel_norm(x)
 x = self.norm1(x, latent)
 x = self.conv2(x)
 if self.noise2 is not None:
x = self.noise2(x)
 x = F.leaky_relu(x, 0.2)
 if self.pixel_norm is not None:
x = self.pixel_norm(x)
 x = self.norm2(x, latent)
 return x
class Generator(nn.Module):
def __init__(self,
nz,
nfeats,
nchannels,
bias=False,
spectral_norm=False,
normalization='selfmod',
random_noise=False,
use_style=False,
use_pixel_norm=False):
super(Generator, self ).__init__()
d = nfeats*8
self.mapping = nn.Sequential(
 SN(nn.Linear(nz, nz, bias=bias)) ,
nn.LeakyReLU(0.2)
)
 self.linear = Projection(nz, nz, 8*8*nfeats, [nfeats, 8, 8], bias, spectral_norm, normalization, random_noise, use_style, use_pixel_norm)
 self.conv2 = UpConvBlock(nz, nfeats, nfeats//2, 4, 2, 1, bias, spectral_norm, normalization, random_noise, use_style, use_pixel_norm)
 self.conv3 = UpConvBlock(nz, nfeats//2, nfeats//4, 4, 2, 1, bias, spectral_norm, normalization, random_noise, use_style, use_pixel_norm)
 self.conv4 = UpConvBlock(nz, nfeats//4, nfeats//8, 4, 2, 1, bias, spectral_norm, normalization, random_noise, use_style, use_pixel_norm)
 self.conv6 = nn.Conv2d(nfeats//8, nchannels, 1, 1, 0, bias=bias)
 if spectral_norm:
self.conv6 = SN(self.conv6)
 def forward(self, x):
latent = self.mapping(x)
 out = self.linear(x, latent)
 out = self.conv2(out, latent)
 out = self.conv3(out, latent)
 out = self.conv4(out, latent)
 out = torch.tanh(self.conv6(out))
return out
class Discriminator(nn.Module):
def __init__(self, nchannels, nfeats):
super(Discriminator, self ).__init__()
d = nfeats
self.from_rgb = nn.Sequential(
 SN(nn.Conv2d(nchannels, d//8, 1, 1, 0, bias=False)) ,
nn.BatchNorm2d(d//8),
nn.LeakyReLU(0.2)
)
 self.conv2 = nn.Sequential(
 SN(nn.Conv2d(d//8, d//8, 3, 1, 1, bias=False)) ,
nn.BatchNorm2d(d//8),
nn.LeakyReLU(0.2),
SN(nn.Conv2d(d//8, d//4, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(d//4),
nn.LeakyReLU(0.2)
)
 self.conv3 = nn.Sequential(
 SN(nn.Conv2d(d//4, d//4, 3, 1, 1, bias=False)) ,
nn.BatchNorm2d(d//4),
nn.LeakyReLU(0.2),
SN(nn.Conv2d(d//4, d//2, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(d//2),
nn.LeakyReLU(0.2)
)
 self.conv4 = nn.Sequential(
 SN(nn.Conv2d(d//2, d//2, 3, 1, 1, bias=False)) ,
nn.BatchNorm2d(d//2),
nn.LeakyReLU(0.2),
SN(nn.Conv2d(d//2, d, 4, 2, 1, bias=False)) ,
nn.BatchNorm2d(d),
nn.LeakyReLU(0.2)
)
 self.conv5 = nn.Sequential(
 SN(nn.Conv2d(d, d, 3, 1, 1, bias=False)) ,
nn.BatchNorm2d(d),
nn.LeakyReLU(0.2)
)
 self.linear = SN(nn.Linear(8*8*d, 1, bias=False))
def forward(self, x):
x = self.from_rgb(x)
 x = self.conv2(x)
 x = self.conv3(x)
 x = self.conv4(x)
 x = self.conv5(x)
 x = x.view(x.shape[0], -1)
 x = self.linear(x)
 return x<compute_train_metric>
def split_attached_words(tweet):
words = wordninja.split(tweet)
 return" ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : split_attached_words(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : split_attached_words(x))
def gradient_penalty(x, y, f):
shape = [x.size(0)] + [1] *(x.dim() - 1)
 alpha = torch.rand(shape ).to(x.device)
 z = x + alpha *(y - x)
 z = Variable(z, requires_grad=True ).to(x.device)
 o = f(z)
 g = grad(o, z, grad_outputs=torch.ones(o.size() ).to(z.device), create_graph=True)[0].view(z.size(0), -1)
 gp =(( g.norm(p=2, dim=1)- 1)**2 ).mean()
return gp
def R1Penalty(real_img, f):
reals = Variable(real_img, requires_grad=True ).to(real_img.device)
 real_logit = f(reals)
 apply_loss_scaling = lambda x: x * torch.exp(x * torch.Tensor([np.float32(np.log(2.0)) ] ).to(real_img.device))
undo_loss_scaling = lambda x: x * torch.exp(-x * torch.Tensor([np.float32(np.log(2.0)) ] ).to(real_img.device))
real_logit = apply_loss_scaling(torch.sum(real_logit))
real_grads = grad(real_logit, reals, grad_outputs=torch.ones(real_logit.size() ).to(reals.device), create_graph=True)[0].view(reals.size(0), -1)
 real_grads = undo_loss_scaling(real_grads)
 r1_penalty = torch.sum(torch.mul(real_grads, real_grads))
return r1_penalty
def G_wgan(G, D, nz, batch_size):
noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images)
 G_loss = -fake_logit.mean()
return G_loss
def D_wgan_gp(G, D, real_images, nz, lammy=10.0, eps=0.001):
batch_size = real_images.shape[0]
real_logit = D(real_images)
 noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images.detach())
 D_loss = fake_logit.mean() - real_logit.mean()
D_loss += gradient_penalty(real_images.data, fake_images.data, D)* lammy
return D_loss
def D_NS(G, D, real_images, nz, real_labels, fake_labels):
batch_size = real_images.shape[0]
real_logit = D(real_images)
 D_loss_real = F.binary_cross_entropy_with_logits(real_logit, real_labels)
 noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images.detach())
 D_loss_fake = F.binary_cross_entropy_with_logits(fake_logit, fake_labels)
 D_loss = D_loss_real + D_loss_fake
return D_loss, D_loss_real.item() , D_loss_fake.item()
def G_NS(G, D, nz, batch_size, real_labels):
noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images)
 G_loss = F.binary_cross_entropy_with_logits(fake_logit, real_labels)
 return G_loss
def D_Hinge(G, D, real_images, nz):
batch_size = real_images.shape[0]
real_logit = D(real_images)
 D_loss_real = torch.mean(F.relu(1.0 - real_logit))
noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images.detach())
 D_loss_fake = torch.mean(F.relu(1.0 + fake_logit))
D_loss = D_loss_real + D_loss_fake
return D_loss, D_loss_real, D_loss_fake
def G_Hinge(G, D, nz, batch_size):
noise = torch.randn(batch_size, nz, device=device)
 fake_images = G(noise)
 fake_logit = D(fake_images)
 G_loss = -torch.mean(fake_logit)
 return G_loss
class Trainer:
def __init__(self, nz, G, D, r1_gamma=0.0, track_grads=False):
self.nz = nz
self.track_grads=track_grads
self.G = G
self.D = D
self.fixed_noise = torch.randn(64, self.nz)
 self.r1_gamma = r1_gamma
self.d_losses = []
self.g_losses = []
self.d_losses_real = []
self.d_losses_fake = []
self.img_list = []
self.g_grads = []
self.d_grads = []
def check_grads(self, model):
grads = []
for n, p in model.named_parameters() :
if not p.grad is None and p.requires_grad and "bias" not in n:
grads.append(float(p.grad.abs().mean()))
return grads
def train(self, epochs, loader, criterion, optim_G, optim_D, scheduler_D, scheduler_G, print_every, show_every, plot_every, loss='NS'):
step = 0
for epoch in tqdm(range(epochs)) :
for ii, real_images in enumerate(loader):
batch_size = real_images.size(0)
 if USE_SOFT_NOISY_LABELS:
real_labels = torch.empty(( batch_size, 1), device=device ).uniform_(0.80, 0.95)
 fake_labels = torch.empty(( batch_size, 1), device=device ).uniform_(0.05, 0.20)
 else:
real_labels = torch.full(( batch_size, 1), 0.95, device=device)
 fake_labels = torch.full(( batch_size, 1), 0.05, device=device)
 if INVERT_LABELS and random.random() < 0.01:
real_labels, fake_labels = fake_labels, real_labels
self.D.zero_grad()
real_images = real_images.to(device)
 if loss == 'WGAN':
D_loss = D_wgan_gp(self.G, self.D, real_images, self.nz)
 elif loss == 'HINGE':
D_loss, D_loss_real, D_loss_fake = D_Hinge(self.G, self.D, real_images, self.nz)
 else:
D_loss, D_loss_real, D_loss_fake = D_NS(self.G, self.D, real_images, self.nz, real_labels, fake_labels)
 D_loss.backward()
optim_D.step()
self.G.zero_grad()
if loss == 'WGAN':
G_loss = G_wgan(self.G, self.D, self.nz, batch_size)
 elif loss == 'HINGE':
G_loss = G_Hinge(self.G, self.D, self.nz, batch_size)
 else:
G_loss = G_NS(self.G, self.D, self.nz, batch_size, real_labels)
 G_loss.backward()
optim_G.step()
if step % print_every == 0:
print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)) : %.4f'
%(epoch, epochs, ii, len(loader),
D_loss.item() , G_loss.item() , D_loss_real, D_loss_fake))
if step % plot_every == 0:
self.d_losses.append(D_loss.item())
 self.g_losses.append(G_loss.item())
 self.d_losses_real.append(D_loss_real)
 self.d_losses_fake.append(D_loss_fake)
 if self.track_grads:
self.g_grads.append(self.check_grads(self.G))
self.d_grads.append(self.check_grads(self.D))
if(step % show_every == 0)or(( epoch == epochs-1)and(ii == len(loader)-1)) :
with torch.no_grad() :
fake = self.G(self.fixed_noise.to(device)).detach().cpu()
self.img_list.append(make_grid(fake, padding=2, normalize=True))
step += 1 
<choose_model_class>
def stopwords_shortwords(tweet):
words = tweet.split()
stop_words = set(stopwords.words('english'))
words = [w for w in words if not w in stop_words]
for word in words:
if word.isalpha() :
words = [word for word in words if len(word)> 1 ]
else:
words = [word for word in words]
return" ".join(words)
 train_df['clean_text'] = train_df['clean_text'].apply(lambda x : stopwords_shortwords(x))
test_df['clean_text'] = test_df['clean_text'].apply(lambda x : stopwords_shortwords(x))
netG = Generator(nz, NFEATS, 3, False, SPECTRAL_NORM, NORMALIZATION, RANDOM_NOISE, USE_STYLE, PIXEL_NORM ).to(device)
 print(netG )<choose_model_class>
train_df2, validate_df = train_test_split(train_df, test_size=0.075, random_state=0)
 train_df2 = train_df2.reset_index(drop=True)
 validate_df = validate_df.reset_index(drop=True )
netD = Discriminator(3, NFEATS ).to(device)
 print(netD )<normalization>
all_df=pd.concat([train_df,test_df])
 X_all = all_df['clean_text']
X_train = train_df2['clean_text']
y_train = train_df2['target_relabeled'].astype(int)
 X_validate= validate_df['clean_text']
y_validate = validate_df['target_relabeled'].astype(int)
 X_test = test_df['clean_text']
def weights_init(m):
if type(m)== nn.Linear or type(m)== nn.Conv2d or type(m)== nn.ConvTranspose2d:
torch.nn.init.kaiming_uniform(m.weight)
 if m.bias is not None:
m.bias.data.zero_() 
<choose_model_class>
def create_tokenizer(lines):
tokenizer = Tokenizer()
tokenizer.fit_on_texts(lines)
 return tokenizer
tokenizer = create_tokenizer(X_all)
 word_index=tokenizer.word_index
max_words = len(word_index)+ 1
print('unique words are : %d' % max_words )
netG.apply(weights_init)
 netD.apply(weights_init)
 criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr=lrD, betas=(beta1, beta2))
optimizerG = optim.Adam(netG.parameters() , lr=lrG, betas=(beta1, beta2))
scheduler_D = optim.lr_scheduler.ExponentialLR(optimizerD, gamma=0.99)
 scheduler_G = optim.lr_scheduler.ExponentialLR(optimizerG, gamma=0.99 )<train_model>
def encode_data(tokenizer, max_length, data):
encoded = tokenizer.texts_to_sequences(data)
 padded = pad_sequences(encoded, maxlen=max_length, padding= 'post')
 return padded
Xtrain = encode_data(tokenizer, max_length, X_train)
 Xvalidate = encode_data(tokenizer, max_length, X_validate)
 Xtest = encode_data(tokenizer, max_length, X_test )
trainer = Trainer(nz, netG, netD, track_grads=True)
 trainer.train(EPOCHS, train_loader, criterion, optimizerG, optimizerD, scheduler_D, scheduler_G, PRINT_EVERY, SHOW_EVERY, PLOT_EVERY, loss=LOSS)
<choose_model_class>
glove_dir = '.. /input/glove6b'
embeddings_index = {}
f = open(os.path.join(glove_dir, 'glove.6B.100d.txt'))
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
 embeddings_index[word] = coefs
f.close()
print('Found %s word vectors.' % len(embeddings_index))
class KernelEvalException(Exception):
pass
model_params = {
'Inception': {
'name': 'Inception',
'imsize': 64,
'output_layer': 'Pretrained_Net/pool_3:0',
'input_layer': 'Pretrained_Net/ExpandDims:0',
'output_shape': 2048,
'cosine_distance_eps': 0.1
}
}
def create_model_graph(pth):

 with tf.gfile.FastGFile(pth, 'rb')as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
 _ = tf.import_graph_def(graph_def, name='Pretrained_Net')
 def _get_model_layer(sess, model_name):
layername = model_params[model_name]['output_layer']
layer = sess.graph.get_tensor_by_name(layername)
 ops = layer.graph.get_operations()
for op_idx, op in enumerate(ops):
for o in op.outputs:
shape = o.get_shape()
if shape._dims != []:
shape = [s.value for s in shape]
new_shape = []
for j, s in enumerate(shape):
if s == 1 and j == 0:
new_shape.append(None)
 else:
new_shape.append(s)
 o.__dict__['_shape_val'] = tf.TensorShape(new_shape)
 return layer
def get_activations(images, sess, model_name, batch_size=50, verbose=False):

 inception_layer = _get_model_layer(sess, model_name)
 n_images = images.shape[0]
if batch_size > n_images:
print("warning: batch size is bigger than the data size.setting batch size to data size")
 batch_size = n_images
n_batches = n_images//batch_size + 1
pred_arr = np.empty(( n_images,model_params[model_name]['output_shape']))
for i in tqdm(range(n_batches)) :
if verbose:
print("\rPropagating batch %d/%d" %(i+1, n_batches), end="", flush=True)
 start = i*batch_size
if start+batch_size < n_images:
end = start+batch_size
else:
end = n_images
batch = images[start:end]
pred = sess.run(inception_layer, {model_params[model_name]['input_layer']: batch})
 pred_arr[start:end] = pred.reshape(-1,model_params[model_name]['output_shape'])
 if verbose:
print(" done")
 return pred_arr
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))
print('d.shape=',d.shape)
 print('np.min(d, axis=1 ).shape=',np.min(d, axis=1 ).shape)
 mean_min_d = np.mean(np.min(d, axis=1))
print('distance=',mean_min_d)
 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
print('covmean.shape=',covmean.shape)
 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, mm=[], ss=[], ff=[]):

 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 len(mm)!= 0:
m2 = mm
s2 = ss
features2 = ff
elif 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']
print('m1,m2 shape=',(m1.shape,m2.shape),'s1,s2=',(s1.shape,s2.shape))
print('starting calculating FID')
 fid_value = calculate_frechet_distance(m1, s1, m2, s2)
 print('done with FID, starting distance calculation')
 distance = cosine_distance(features1, features2)
 return fid_value, distance, m2, s2, features2<load_pretrained>
embedding_dim = 100
embedding_matrix = np.zeros(( max_words, embedding_dim))
for word, i in word_index.items() :
if i < max_words:
embedding_vector = embeddings_index.get(word)
 if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
if ComputeLB:
with zipfile.ZipFile(".. /working/images.zip","r")as z:
z.extractall(".. /tmp/images2/")
 m2 = []; s2 =[]; f2 = []
user_images_unzipped_path = '.. /tmp/images2/'
images_path = [user_images_unzipped_path,'.. /input/generative-dog-images/all-dogs/all-dogs/']
public_path = '.. /input/dog-face-generation-competition-kid-metric-input/classify_image_graph_def.pb'
fid_epsilon = 10e-15
fid_value_public, distance_public, m2, s2, f2 = calculate_kid_given_paths(images_path, 'Inception', public_path, mm=m2, ss=s2, ff=f2)
 distance_public = distance_thresholding(distance_public, model_params['Inception']['cosine_distance_eps'])
 print("FID_public: ", fid_value_public, "distance_public: ", distance_public, "multiplied_public: ",
fid_value_public /(distance_public + fid_epsilon))
! rm -r.. /tmp<import_modules>
model = Sequential()
model.add(Embedding(max_words, 100, input_length=max_length))
model.add(Bidirectional(LSTM(64, dropout=0.2, recurrent_dropout=0.2, return_sequences=True)))
 model.add(Bidirectional(LSTM(64, dropout=0.2, recurrent_dropout=0.2,)))
 model.add(Dense(64, activation= 'relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
batch_size = 32<define_variables>
model.layers[0].set_weights([embedding_matrix])
 model.layers[0].trainable = True
start = time()<set_options>
model.compile(loss= 'binary_crossentropy', optimizer=optimizers.Adam(lr=.0001), metrics=[ 'accuracy' ] )
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything()<load_pretrained>
model.fit(Xtrain, y_train, epochs=10, callbacks=callbacks, validation_data=(Xvalidate,y_validate))
class DataGenerator(Dataset):
def __init__(self, directory, transform=None, n_samples=np.inf):
self.directory = directory
self.transform = transform
self.n_samples = n_samples
self.samples = self._load_subfolders_images(directory)
 if len(self.samples)== 0:
raise RuntimeError("Found 0 files in subfolders of: {}".format(directory))
def _load_subfolders_images(self, root):
IMG_EXTENSIONS =(
 '.jpg', '.jpeg', '.png', '.ppm', '.bmp', '.pgm', '.tif', '.tiff', '.webp')
 def is_valid_file(x):
return torchvision.datasets.folder.has_file_allowed_extension(x, IMG_EXTENSIONS)
 required_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(64),
torchvision.transforms.CenterCrop(64),
])
 imgs = []
paths = []
for root, _, fnames in sorted(os.walk(root)) :
for fname in sorted(fnames)[:min(self.n_samples, 999999999999999)]:
path = os.path.join(root, fname)
 paths.append(path)
 for path in paths:
if is_valid_file(path):
img = dset.folder.default_loader(path)
 annotation_basename = os.path.splitext(os.path.basename(path)) [0]
annotation_dirname = next(
 dirname for dirname in os.listdir('.. /input/annotation/Annotation/')if
dirname.startswith(annotation_basename.split('_')[0]))
annotation_filename = os.path.join('.. /input/annotation/Annotation/',
annotation_dirname, annotation_basename)
 tree = ET.parse(annotation_filename)
 root = tree.getroot()
objects = root.findall('object')
 for o in objects:
bndbox = o.find('bndbox')
 xmin = int(bndbox.find('xmin' ).text)
 ymin = int(bndbox.find('ymin' ).text)
 xmax = int(bndbox.find('xmax' ).text)
 ymax = int(bndbox.find('ymax' ).text)
 xdelta = xmax - xmin
ydelta = ymax - ymin
if xdelta >= 64 and ydelta >= 64:
img2 = img.crop(( xmin, ymin, xmax, ymax))
image = required_transforms(img2)
 imgs.append(image)
 return imgs
def __getitem__(self, index):
sample = self.samples[index]
if self.transform is not None:
sample = self.transform(sample)
 return np.asarray(sample)
 def __len__(self):
return len(self.samples)
<categorify>
sample_submission=pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 y_pre=model.predict(Xtest)
 y_pre=np.round(y_pre ).astype(int ).reshape(3263)
 sub=pd.DataFrame({'id':sample_submission['id'].values.tolist() ,'target':y_pre})
 sub.to_csv('tweet5a.csv',index=False )
%%time
database = '.. /input/all-dogs/all-dogs/'
transform = transforms.Compose([transforms.RandomHorizontalFlip(p=0.3),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 train_data = DataGenerator(database, transform=transform,n_samples=25000)
 train_loader = torch.utils.data.DataLoader(train_data, shuffle=True,batch_size=batch_size, num_workers = 4)
<normalization>
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py
class PixelwiseNorm(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>
import tensorflow as tf
import tensorflow_hub as hub
import tokenization
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.models import Model
import tensorflow.keras.optimizers as optimizers
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<define_search_model>
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )
class Generator(nn.Module):
def __init__(self, nz, nfeats, nchannels):
super(Generator, self ).__init__()
self.conv1 = spectral_norm(nn.ConvTranspose2d(nz, nfeats * 8, 4, 1, 0, bias=False))
self.conv2 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 8, 4, 2, 1, bias=False))
self.conv3 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 4, 4, 2, 1, bias=False))
self.conv4 = spectral_norm(nn.ConvTranspose2d(nfeats * 4, nfeats * 2, 4, 2, 1, bias=False))
self.conv5 = spectral_norm(nn.ConvTranspose2d(nfeats * 2, nfeats, 4, 2, 1, bias=False))
self.conv6 = spectral_norm(nn.ConvTranspose2d(nfeats, nchannels, 3, 1, 1, bias=False))
self.pixnorm = PixelwiseNorm()
def forward(self, x):
x = F.leaky_relu(self.conv1(x))
x = F.leaky_relu(self.conv2(x))
x = self.pixnorm(x)
 x = F.leaky_relu(self.conv3(x))
x = self.pixnorm(x)
 x = F.leaky_relu(self.conv4(x))
x = self.pixnorm(x)
 x = F.leaky_relu(self.conv5(x))
x = self.pixnorm(x)
 x = torch.tanh(self.conv6(x))
return x
class Discriminator(nn.Module):
def __init__(self, nchannels, nfeats):
super(Discriminator, self ).__init__()
self.conv1 = nn.Conv2d(nchannels, nfeats, 4, 2, 1, bias=False)
 self.conv2 = spectral_norm(nn.Conv2d(nfeats, nfeats * 2, 4, 2, 1, bias=False))
self.bn2 = nn.BatchNorm2d(nfeats * 2)
 self.conv3 = spectral_norm(nn.Conv2d(nfeats * 2, nfeats * 4, 4, 2, 1, bias=False))
self.bn3 = nn.BatchNorm2d(nfeats * 4)
 self.conv4 = spectral_norm(nn.Conv2d(nfeats * 4, nfeats * 8, 4, 2, 1, bias=False))
self.bn4 = nn.MaxPool2d(2)
 self.batch_discriminator = MinibatchStdDev()
self.pixnorm = PixelwiseNorm()
self.conv5 = spectral_norm(nn.Conv2d(nfeats * 8 +1, 1, 2, 1, 0, bias=False))
def forward(self, x):
x = F.leaky_relu(self.conv1(x), 0.2)
 x = F.leaky_relu(self.bn2(self.conv2(x)) , 0.2)
 x = F.leaky_relu(self.bn3(self.conv3(x)) , 0.2)
 x = F.leaky_relu(self.bn4(self.conv4(x)) , 0.2)
 x = self.batch_discriminator(x)
 x = torch.sigmoid(self.conv5(x))
return x.view(-1, 1)
 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 nz = 128
lr = 0.0003
lr_d = 0.0001
beta1 = 0.5
epochs = 1600
netG = Generator(nz, 32, 3 ).to(device)
 netD = Discriminator(3, 48 ).to(device)
 criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr=lr, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr=lr_d, betas=(beta1, 0.999))
lr_schedulerG = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerG,
T_0=epochs//200, eta_min=0.00005)
 lr_schedulerD = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizerD,
T_0=epochs//200, eta_min=0.00005)
 fixed_noise = torch.randn(25, nz, 1, 1, device=device)
 real_label = 0.7
fake_label = 0.0
batch_size = train_loader.batch_size
step = 0
for epoch in range(epochs):
for ii,(real_images)in tqdm(enumerate(train_loader), total=len(train_loader)) :
end = time()
if(end -start)> 25000 :
break
netD.zero_grad()
real_images = real_images.to(device)
 batch_size = real_images.size(0)
 labels = torch.full(( batch_size, 1), real_label, device=device)+ np.random.uniform(-0.1, 0.1)
 output = netD(real_images)
 errD_real = criterion(output, labels)
 errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(batch_size, nz, 1, 1, device=device)
 fake = netG(noise)
 labels.fill_(fake_label)+ np.random.uniform(0, 0.2)
 output = netD(fake.detach())
 errD_fake = criterion(output, labels)
 errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
labels.fill_(real_label)
 output = netD(fake)
 errG = criterion(output, labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if step % 500 == 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)) : %.4f / %.4f'
%(epoch + 1, epochs, ii, len(train_loader),
errD.item() , errG.item() , D_x, D_G_z1, D_G_z2))
valid_image = netG(fixed_noise)
 step += 1
lr_schedulerG.step(epoch)
 lr_schedulerD.step(epoch)
 if epoch % 200 == 0:
show_generated_img()
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values
if not os.path.exists('.. /output_images'):
os.mkdir('.. /output_images')
 im_batch_size = 100
n_images=10000
for i_batch in range(0, n_images, im_batch_size):
z = truncated_normal(( im_batch_size, nz, 1, 1), threshold=1)
 gen_z = torch.from_numpy(z ).float().to(device)
 gen_images = netG(gen_z)
 images = gen_images.to("cpu" ).clone().detach()
images = images.numpy().transpose(0, 2, 3, 1)
 for i_image in range(gen_images.size(0)) :
save_image(( gen_images[i_image, :, :, :] +1.0)/2.0, os.path.join('.. /output_images', f'image_{i_batch+i_image:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<define_variables>
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )
INPUT_HEIGHT = INPUT_WIDTH = 64
OUTPUT_HEIGHT = OUTPUT_WIDTH = 64
CROP_HEIGHT = CROP_WIDTH = 64
BATCH_SIZE = 32
ANNOTATIONS_PATH = '.. /input/annotation/Annotation/'
IMAGES_PATH = '.. /input/all-dogs/all-dogs/'
Z_DIM, Z_STD = 64, 1
CLIP_Z = False
Y_DIM, Y_EMBEDDING_DIM = 120, 10
C_DIM = 3
GF_DIM = DF_DIM = 64
G_LR, D_LR = 1e-4, 1e-4
ADAM_BETA1, ADAM_BETA2, ADAM_EPSILON = 0.5, 0.99, 1e-8
PERFORM_SN = False
SN_ITERATIONS = 1
LOCAL_RUN = False
INPUT_NOISE_ANNEALING_STEPS = 3e5
LOSS = 'LEAKY_SIGMOID_CROSSENTROPY'
NOISY_LOSS_TARGET = True
INPUTS_NOISE = True
LABELS_NOISE = True
CONV_K = 7
TRAINING_TIME_LIMIT = 32000<import_modules>
def bert_encode(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )
import cv2
import tensorflow as tf
from tensorflow import concat
import tensorflow.contrib.slim as slim
import imageio
import numpy as np
import xml.etree.ElementTree as ET
import os
import time
import datetime
from glob import glob
from tqdm import tqdm_notebook as tqdm
import zipfile
from itertools import chain, islice, count
from IPython.core.display import display, HTML<normalization>
train_input = bert_encode(train_df.text.values, tokenizer, max_len=128)
 test_input = bert_encode(test_df.text.values, tokenizer, max_len=128)
 train_labels = train_df.target_relabeled.values
class batch_norm(object):
def __init__(self, epsilon=1e-5, momentum=0.9, name="batch_norm"):
with tf.variable_scope(name):
self.epsilon = epsilon
self.momentum = momentum
self.name = name
def __call__(self, x, train=True):
return tf.contrib.layers.batch_norm(x,
decay=self.momentum,
updates_collections=None,
epsilon=self.epsilon,
scale=True,
is_training=train,
scope=self.name, 
)
 def spectral_normed_weight(w, update_u=True):
if PERFORM_SN:
def l2normalize(v, eps=1e-12):
return v /(tf.reduce_sum(v ** 2)** 0.5 + eps)
 w_shape = w.shape.as_list()
w_mat = tf.reshape(w, [-1, w_shape[-1]])
 u = tf.get_variable('u', [1, w_shape[-1]], trainable=False,
initializer=tf.truncated_normal_initializer())
 u_ = u
for _ in range(SN_ITERATIONS):
v_ = l2normalize(tf.matmul(u_, w_mat, transpose_b=True))
u_ = l2normalize(tf.matmul(v_, w_mat))
sigma = tf.squeeze(tf.matmul(tf.matmul(v_, w_mat), u_, transpose_b=True))
w_mat /= sigma
if update_u:
with tf.control_dependencies([u.assign(u_)]):
w_normed = tf.reshape(w_mat, w_shape)
 else:
w_normed = tf.reshape(w_mat, w_shape)
 return w_normed
else:
return w
def conv2d(input_, output_dim,
k_h=CONV_K, k_w=CONV_K, d_h=2, d_w=2, stddev=0.02,
name="conv2d", update_u=True, add_bias=False):
with tf.variable_scope(name):
w = tf.get_variable('w', [k_h, k_w, input_.get_shape() [-1], output_dim],
initializer=tf.truncated_normal_initializer(stddev=stddev))
w = spectral_normed_weight(w, update_u=update_u)
 conv = tf.nn.conv2d(input_, w, strides=[1, d_h, d_w, 1], padding='SAME')
 if add_bias:
biases = tf.get_variable('biases', [output_dim], initializer=tf.constant_initializer(0.0))
conv = tf.nn.bias_add(conv, biases)
 return conv
def deconv2d(input_, output_shape,
k_h=CONV_K, k_w=CONV_K, d_h=2, d_w=2, stddev=0.02,
name="deconv2d", update_u=True, add_bias=False):
with tf.variable_scope(name):
w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape() [-1]],
initializer=tf.random_normal_initializer(stddev=stddev))
w = spectral_normed_weight(w, update_u=update_u)
 deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape, strides=[1, d_h, d_w, 1])
 if add_bias:
biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
deconv = tf.nn.bias_add(deconv, biases)
 return deconv
def lrelu(x, leak=0.2, name="lrelu"):
return tf.nn.leaky_relu(x, leak, name)
 def linear(input_, output_size, scope=None,
stddev=0.02, bias_start=0.0, update_u=True):
shape = input_.get_shape()
with tf.variable_scope(scope or "Linear"):
matrix = tf.get_variable("Matrix", [shape[1], output_size], tf.float32,
tf.random_normal_initializer(stddev=stddev))
matrix = spectral_normed_weight(matrix, update_u=update_u)
 bias = tf.get_variable("bias", [output_size], initializer=tf.constant_initializer(bias_start))
return tf.matmul(input_, matrix)+ bias
def add_coordinates(input_tensor):
bs, h_dim, w_dim, c = input_tensor.get_shape().as_list()
x = tf.stack([tf.range(w_dim, dtype=tf.float32)] * h_dim)/(w_dim - 1)* 2 - 1
y = tf.transpose(tf.stack([tf.range(h_dim, dtype=tf.float32)] * w_dim)) /(h_dim - 1)* 2 - 1
r =(( x**2 + y**2)/ 2)** 0.5
coords = tf.stack([tf.stack([x, y, r], axis=-1)] * bs)
 coords = tf.constant(tf.get_default_session().run(coords))
return tf.concat([input_tensor, coords], axis=-1 )<categorify>
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(optimizers.Adam(lr=2e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model
def timestamp(template='%Y%m%d_%H%M%S', ts=None):
return datetime.datetime.fromtimestamp(ts or time.time() ).strftime(template)
 def conv_out_size_same(size, stride):
return int(np.ceil(float(size)/ float(stride)) )<normalization>
model = build_model(bert_layer, max_len=128)
 model.summary()
def transform_X(x):
x = x / 128 - 255 / 256
return tf.image.random_flip_left_right(x )<define_variables>
callbacks2 = [
ModelCheckpoint('model_bert.h5', monitor='val_loss', save_best_only=True)
 ]
class DCGAN(object):
def __init__(self, sess, data):
self.start_time = time.time()
out_dir = './gan_out/{}/'.format(timestamp())
 self.checkpoint_dir = os.path.join(out_dir, 'checkpoints')
 os.makedirs(self.checkpoint_dir, exist_ok=True)
 self.sample_dir = os.path.join(out_dir, 'samples')
 os.makedirs(self.sample_dir, exist_ok=True)
 self.log_file = open(os.path.join(out_dir, 'log.txt'), 'w')
 self.sess = sess
self.global_step = tf.train.create_global_step()
self.load_data(*data)
 self.build_model()
def load_data(self, X, Y):
dataset = tf.data.Dataset.from_tensor_slices(( X, Y))
dataset = dataset.map(( lambda x, y:(x, tf.one_hot(y, Y_DIM, dtype=tf.float32))))
dataset = dataset.shuffle(len(X), reshuffle_each_iteration=True ).repeat()
dataset = dataset.map(( lambda x, y:(transform_X(x), y)) , num_parallel_calls=2)
 dataset = dataset.apply(tf.contrib.data.batch_and_drop_remainder(BATCH_SIZE)).prefetch(2)
 self.x_batch, self.y_batch = dataset.make_one_shot_iterator().get_next()
self.clear_x_batch, self.clear_y_batch = self.x_batch, self.y_batch
if INPUTS_NOISE:
self.x_batch = self.noisy_inputs(self.x_batch)
 if LABELS_NOISE:
self.y_batch = self.noisy_labels(self.y_batch)
 @staticmethod
def x_plus_x_squared(x):
return x + x * x
def get_d_real_loss(self, logits):
if LOSS == 'SIGMOID_CROSSENTROPY':
if NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.95, maxval=1.05, dtype=tf.float32)
 else:
target = tf.fill(tf.shape(logits), 1.)
elif LOSS == 'LEAKY_SIGMOID_CROSSENTROPY':
target = tf.fill(tf.shape(logits), 0.9)
 if NOISY_LOSS_TARGET:
target += tf.random.uniform([], minval=-0.025, maxval=0.025)
 elif NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.95, maxval=1.05, dtype=tf.float32)
 else:
target = 1.0
if LOSS == 'HINGE':
loss = tf.nn.relu(target - logits)
 elif LOSS == 'KL':
loss = tf.nn.softplus(-logits)
 elif LOSS in('SIGMOID_CROSSENTROPY', 'LEAKY_SIGMOID_CROSSENTROPY'):
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=target)
 elif LOSS == 'LEAKY_HINGE':
loss = tf.nn.leaky_relu(target - logits, -0.2)
 elif LOSS == 'SQUARED_LEAKY_HINGE':
loss = self.x_plus_x_squared(tf.nn.leaky_relu(target - logits, -0.2))
return tf.reduce_mean(loss)
 def get_d_fake_loss(self, logits):
if LOSS == 'SIGMOID_CROSSENTROPY':
if NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.05, maxval=0.05, dtype=tf.float32)
 else:
target = tf.fill(tf.shape(logits), 0.)
elif LOSS == 'LEAKY_SIGMOID_CROSSENTROPY':
target = tf.fill(tf.shape(logits), 0.1)
 if NOISY_LOSS_TARGET:
target += tf.random.uniform([], minval=-0.025, maxval=0.025)
 elif NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.95, maxval=1.05, dtype=tf.float32)
 else:
target = 1.0
if LOSS == 'HINGE':
loss = tf.nn.relu(target + logits)
 elif LOSS == 'KL':
loss = tf.nn.softplus(logits)
 elif LOSS in('SIGMOID_CROSSENTROPY', 'LEAKY_SIGMOID_CROSSENTROPY'):
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=target)
 elif LOSS == 'LEAKY_HINGE':
loss = tf.nn.leaky_relu(target + logits, -0.2)
 elif LOSS == 'SQUARED_LEAKY_HINGE':
loss = self.x_plus_x_squared(tf.nn.leaky_relu(target + logits, -0.2))
return tf.reduce_mean(loss)
 def get_g_loss(self, logits):
if LOSS == 'SIGMOID_CROSSENTROPY':
if NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.95, maxval=1.05, dtype=tf.float32)
 else:
target = tf.fill(tf.shape(logits), 1.)
elif LOSS == 'LEAKY_SIGMOID_CROSSENTROPY':
target = tf.fill(tf.shape(logits), 0.9)
 if NOISY_LOSS_TARGET:
target += tf.random.uniform([], minval=-0.025, maxval=0.025)
 elif NOISY_LOSS_TARGET:
target = tf.random.uniform(( BATCH_SIZE, 1), minval=0.95, maxval=1.05, dtype=tf.float32)
 else:
target = 1.0
if LOSS in('HINGE', 'KL'):
loss = -logits
elif LOSS in('SIGMOID_CROSSENTROPY', 'LEAKY_SIGMOID_CROSSENTROPY'):
loss = tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=target)
 elif LOSS == 'LEAKY_HINGE':
loss = tf.nn.leaky_relu(target - logits, -0.2)
 elif LOSS == 'SQUARED_LEAKY_HINGE':
loss = self.x_plus_x_squared(tf.nn.leaky_relu(target - logits, -0.2))
return tf.reduce_mean(loss)
 def build_model(self):
self.increment_global_step = self.global_step.assign_add(1)
 self.z_batch = tf.random.normal(( BATCH_SIZE, Z_DIM), 0, Z_STD)
 if CLIP_Z:
self.z_batch = tf.clip_by_value(self.z_batch, -1, 1)
 self.d_bn0 = batch_norm(name='d_bn0')
 self.d_bn1 = batch_norm(name='d_bn1')
 self.d_bn2 = batch_norm(name='d_bn2')
 self.d_bn3 = batch_norm(name='d_bn3')
 self.d_bn4 = batch_norm(name='d_bn4')
 self.g_bn0 = batch_norm(name='g_bn0')
 self.g_bn1 = batch_norm(name='g_bn1')
 self.g_bn2 = batch_norm(name='g_bn2')
 self.g_bn3 = batch_norm(name='g_bn3')
 self.g_bn4 = batch_norm(name='g_bn4')
 self.G = self.generator(self.z_batch, self.y_batch)
 z_sample, y_sample = self.sess.run([self.z_batch, self.y_batch])
 self.fixed_sampler = tf.cast(( self.generator(tf.constant(z_sample), tf.constant(y_sample), update_u=False)+ 1)* 127.5, tf.uint8)
 self.sampler = tf.cast(( self.generator(self.z_batch, self.y_batch, update_u=False)+ 1)* 127.5, tf.uint8)
 self.D_logits_real = self.discriminator(self.x_batch, self.y_batch)
 self.D_logits_fake = self.discriminator(self.G, self.y_batch)
 self.d_loss_real = self.get_d_real_loss(self.D_logits_real)
 self.d_loss_fake = self.get_d_fake_loss(self.D_logits_fake)
 self.d_loss = self.d_loss_real + self.d_loss_fake
self.g_loss = self.get_g_loss(self.D_logits_fake)
 self.get_vars()
self.d_optim = tf.train.AdamOptimizer(D_LR, beta1=ADAM_BETA1, beta2=ADAM_BETA2, epsilon=ADAM_EPSILON)\ 
.minimize(self.d_loss, var_list=self.d_vars)
 self.g_optim = tf.train.AdamOptimizer(G_LR, beta1=ADAM_BETA1, beta2=ADAM_BETA2, epsilon=ADAM_EPSILON)\ 
.minimize(self.g_loss, var_list=self.g_vars)
 self.saver = tf.train.Saver(max_to_keep=1)
 tf.global_variables_initializer().run(session=self.sess)
 def train(self):
info_template =(
 'step: {}, time: {:5.2f}, d_loss_fake: {:.4f}, '
'd_loss_real: {:.4f}, d_loss: {:.5f}, g_loss: {:.5f}')
 running_time = time.time() - self.start_time
while running_time < TRAINING_TIME_LIMIT:
d_fake, d_real, d, g, step, _, _ = self.sess.run(
 [self.d_loss_fake, self.d_loss_real, self.d_loss, self.g_loss,
self.increment_global_step,
self.d_optim, self.g_optim,
], 
)
 previous_running_time = running_time
running_time = time.time() - self.start_time
print(info_template.format(step, running_time,
d_fake, d_real, d, g),
file=self.log_file)
 if previous_running_time // 10 != running_time // 10:
print(info_template.format(step, running_time,
d_fake, d_real, d, g))
if previous_running_time // 1000 != running_time // 1000:
self.save_sample()
self.generate_zip(os.path.join(self.sample_dir, f'images{running_time // 1000}.zip'), 1000)
 self.save(self.checkpoint_dir)
 def noisy_labels(self, labels, mean=0, std=0.03):

 return tf.add(labels, tf.random_normal(tf.shape(labels), mean, std, dtype=tf.float32))
def noisy_inputs(self, inputs, mean=0, std=0.03, annealing_steps=INPUT_NOISE_ANNEALING_STEPS):
step = tf.cast(self.global_step, tf.float32)
 return tf.cond(step < annealing_steps,
lambda: tf.clip_by_value(tf.add(inputs, tf.random_normal(tf.shape(inputs),
mean,
std *(annealing_steps - step)/ annealing_steps,
dtype=tf.float32)) , -1, 1),
lambda: inputs)
 def get_vars(self):
t_vars = tf.trainable_variables()
self.d_vars = [var for var in t_vars if var.name.startswith('discriminator/d_')]
self.g_vars = [var for var in t_vars if var.name.startswith('generator/g_')]
self.sigma_ratio_vars = [var for var in t_vars if 'sigma_ratio' in var.name]
assert(set(self.d_vars)& set(self.g_vars)) == set()
assert(set(self.d_vars)| set(self.g_vars)) == set(t_vars)
 self.all_vars = t_vars
def discriminator(self, image, y, update_u=True):
with tf.variable_scope("discriminator", reuse=tf.AUTO_REUSE)as scope:
h0 = lrelu(self.d_bn0(conv2d(image, DF_DIM, name='d_h0_conv', update_u=update_u)))
 h1 = lrelu(self.d_bn1(conv2d(h0, DF_DIM * 2, name='d_h1_conv', update_u=update_u)))
 h2 = lrelu(self.d_bn2(conv2d(h1, DF_DIM * 4, name='d_h2_conv', update_u=update_u)))
 h3 = lrelu(self.d_bn3(conv2d(h2, DF_DIM * 8, name='d_h3_conv', update_u=update_u)))
 y_embedding = lrelu(self.d_bn4(linear(y, Y_EMBEDDING_DIM, 'd_y_embedding', update_u=update_u)))
 h4 = linear(concat([tf.reshape(h3, [BATCH_SIZE, -1]), y_embedding], 1), 1, 'd_h4_lin', update_u=update_u)
 return h4
def generator(self, z, y, is_training=True, update_u=True):
with tf.variable_scope("generator", reuse=tf.AUTO_REUSE)as scope:
y_embedding = lrelu(self.g_bn0(linear(y, Z_DIM, 'g_y_embedding', update_u=update_u)))
 z = concat([z, y_embedding], 1)
 s_h, s_w = OUTPUT_HEIGHT, OUTPUT_WIDTH
s_h2, s_w2 = conv_out_size_same(s_h, 2), conv_out_size_same(s_w, 2)
 s_h4, s_w4 = conv_out_size_same(s_h2, 2), conv_out_size_same(s_w2, 2)
 s_h8, s_w8 = conv_out_size_same(s_h4, 2), conv_out_size_same(s_w4, 2)
 s_h16, s_w16 = conv_out_size_same(s_h8, 2), conv_out_size_same(s_w8, 2)
 z_ = linear(z, GF_DIM * 8 * s_h16 * s_w16, 'g_h0_lin', update_u=update_u)
 h0 = tf.reshape(z_, [-1, s_h16, s_w16, GF_DIM * 8])
 h0 = lrelu(self.g_bn1(h0, train=is_training))
h1 = deconv2d(h0, [BATCH_SIZE, s_h8, s_w8, GF_DIM * 4], name='g_h1', update_u=update_u)
 h1 = lrelu(self.g_bn2(h1, train=is_training))
h2 = deconv2d(h1, [BATCH_SIZE, s_h4, s_w4, GF_DIM * 2], name='g_h2', update_u=update_u)
 h2 = lrelu(self.g_bn3(h2, train=is_training))
h3 = deconv2d(h2, [BATCH_SIZE, s_h2, s_w2, GF_DIM * 1], name='g_h3', update_u=update_u)
 h3 = lrelu(self.g_bn4(h3, train=is_training))
h4 = deconv2d(h3, [BATCH_SIZE, s_h, s_w, C_DIM], name='g_final_deconv', update_u=update_u)
 return tf.nn.tanh(h4)
 def generate_images(self):
images = self.sess.run(
 self.sampler, 
)
 return images
def save_sample(self):
images, step = self.sess.run(
 [self.fixed_sampler, self.global_step] 
)
 sample = np.vstack([np.hstack(images[i*16:(i+1)*16])for i in range(max(1, min(8, BATCH_SIZE // 16)))])
 sample_path = os.path.join(self.sample_dir, '{}.png'.format(step))
imageio.imwrite(sample_path, sample)
 imageio.imwrite('real_dogs_sample.png', sample)
 display(HTML(f'<img src="{sample_path}" alt="Sample of generated dogs." width="100%">'))
def save(self, checkpoint_dir, filename='model'):
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
 self.saver.save(self.sess, os.path.join(checkpoint_dir, filename))
def generate_zip(self, zip_path='images.zip', n_images=10000):
start_time = time.time()
with zipfile.PyZipFile(zip_path, 'w')as z:
for i, image in enumerate(islice(chain.from_iterable(gan.generate_images() for _ in count()), n_images)) :
path = f'{i}.png'
imageio.imwrite(path, image); z.write(path); os.remove(path)
 print(f'{time.time() - start_time:.1f}s to generate {n_images} images' )<train_model>
model.fit(train_input, train_labels, validation_split=0.075, epochs=3,
callbacks=callbacks2, batch_size=32 )
<find_best_params><EOS> 
sample_submission=pd.read_csv('/kaggle/input/nlp-getting-started/sample_submission.csv')
 y_pre2=model.predict(test_input)
 y_pre2=np.round(y_pre2 ).astype(int ).reshape(3263)
 sub2=pd.DataFrame({'id':sample_submission['id'].values.tolist() ,'target':y_pre2})
 sub2.to_csv('tweet5b.csv',index=False )