Kaggle competition: TMDB Box Office Prediction
def init_weights(m):
if type(m)== nn.Linear or type(m)== nn.Conv2d:
nn.init.xavier_uniform_(m.weight)
 m.bias.data.fill_(0.)
def snconv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True):
return nn.utils.spectral_norm(nn.Conv2d(in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding,
dilation=dilation,
groups=groups,
bias=bias))
def snlinear(in_features, out_features):
return nn.utils.spectral_norm(nn.Linear(in_features=in_features, out_features=out_features))
def sn_embedding(num_embeddings, embedding_dim):
return nn.utils.spectral_norm(nn.Embedding(num_embeddings=num_embeddings, embedding_dim=embedding_dim))
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
class MinibatchStdDev(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 = torch.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 = torch.cat([x, y], 1)
 return y
class GaussianNoise(nn.Module):

 def __init__(self, sigma=0.1, is_relative_detach=True):
super().__init__()
self.sigma = sigma
self.is_relative_detach = is_relative_detach
self.noise = torch.tensor(0.).to(device)
 def forward(self, x):
if self.training and self.sigma != 0:
scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
sampled_noise = self.noise.repeat(*x.size() ).normal_() * scale
x = x + sampled_noise
return x
class ConditionalBatchNorm2d(nn.Module):
def __init__(self, num_features, num_classes):
super().__init__()
self.num_features = num_features
self.bn = nn.BatchNorm2d(num_features)
 self.embed = nn.Embedding(num_classes, num_features * 2)
 self.embed.weight.data[:, :num_features].fill_(1.)
self.embed.weight.data[:, num_features:].zero_()
def forward(self, x, y):
out = self.bn(x)
 gamma, beta = self.embed(y ).chunk(2, 1)
 out = gamma.view(-1, self.num_features, 1, 1)* out + beta.view(-1, self.num_features, 1, 1)
 return out
class Self_Attn(nn.Module):

 def __init__(self, in_channels):
super(Self_Attn, self ).__init__()
self.in_channels = in_channels
self.snconv1x1_theta = snconv2d(in_channels=in_channels,
out_channels=in_channels//8,
kernel_size=1, stride=1, padding=0)
 self.snconv1x1_phi = snconv2d(in_channels=in_channels,
out_channels=in_channels//8,
kernel_size=1, stride=1, padding=0)
 self.snconv1x1_g = snconv2d(in_channels=in_channels,
out_channels=in_channels//2,
kernel_size=1, stride=1, padding=0)
 self.snconv1x1_attn = snconv2d(in_channels=in_channels//2,
out_channels=in_channels,
kernel_size=1, stride=1, padding=0)
 self.maxpool = nn.MaxPool2d(2, stride=2, padding=0)
 self.softmax = nn.Softmax(dim=-1)
 self.sigma = nn.Parameter(torch.zeros(1))
def forward(self, x):

 _, ch, h, w = x.size()
theta = self.snconv1x1_theta(x)
 theta = theta.view(-1, ch//8, h*w)
 phi = self.snconv1x1_phi(x)
 phi = self.maxpool(phi)
 phi = phi.view(-1, ch//8, h*w//4)
 attn = torch.bmm(theta.permute(0, 2, 1), phi)
 attn = self.softmax(attn)
 g = self.snconv1x1_g(x)
 g = self.maxpool(g)
 g = g.view(-1, ch//2, h*w//4)
 attn_g = torch.bmm(g, attn.permute(0, 2, 1))
attn_g = attn_g.view(-1, ch//2, h, w)
 attn_g = self.snconv1x1_attn(attn_g)
 out = x + self.sigma*attn_g
return out 
<choose_model_class>
train = pd.read_csv(".. /input/tmdb-box-office-prediction/train.csv")
 test = pd.read_csv(".. /input/tmdb-box-office-prediction/test.csv")
 imdb = pd.read_csv(".. /input/imdb-web-scraping-4/imdb.csv")
 train.head()
class GenBlock(nn.Module):
def __init__(self, in_channels, out_channels, num_classes):
super(GenBlock, self ).__init__()
self.cond_bn1 = ConditionalBatchNorm2d(in_channels, num_classes)
 self.relu = nn.ReLU(inplace=True)
 self.ps1 = nn.PixelShuffle(2),
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.cond_bn2 = ConditionalBatchNorm2d(out_channels, num_classes)
 self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
 def forward(self, x, labels):
x0 = x
x = self.cond_bn1(x, labels)
 x = self.relu(x)
 x = F.interpolate(x, scale_factor=2, mode='nearest')
 x = self.snconv2d1(x)
 x = self.cond_bn2(x, labels)
 x = self.relu(x)
 x = self.snconv2d2(x)
 x0 = F.interpolate(x0, scale_factor=2, mode='nearest')
 x0 = self.snconv2d0(x0)
 out = x + x0
return out
class DiscOptBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(DiscOptBlock, self ).__init__()
self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.relu = nn.ReLU(inplace=True)
 self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.downsample = nn.AvgPool2d(2)
 self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
 def forward(self, x):
x0 = x
x = self.snconv2d1(x)
 x = self.relu(x)
 x = self.snconv2d2(x)
 x = self.downsample(x)
 x0 = self.downsample(x0)
 x0 = self.snconv2d0(x0)
 out = x + x0
return out
class DiscBlock(nn.Module):
def __init__(self, in_channels, out_channels):
super(DiscBlock, self ).__init__()
self.relu = nn.LeakyReLU(0.2, inplace=True)
 self.snconv2d1 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.snconv2d2 = snconv2d(in_channels=out_channels, out_channels=out_channels, kernel_size=3, stride=1, padding=1)
 self.downsample = nn.AvgPool2d(2)
 self.ch_mismatch = False
if in_channels != out_channels:
self.ch_mismatch = True
self.snconv2d0 = snconv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=1, stride=1, padding=0)
 def forward(self, x, downsample=True):
x0 = x
x = self.relu(x)
 x = self.snconv2d1(x)
 x = self.relu(x)
 x = self.snconv2d2(x)
 if downsample:
x = self.downsample(x)
 if downsample or self.ch_mismatch:
x0 = self.snconv2d0(x0)
 if downsample:
x0 = self.downsample(x0)
 out = x + x0
return out
class Generator(nn.Module):

 def __init__(self):
super(Generator, self ).__init__()
self.z_dim = args.latent_dim
self.g_conv_dim = args.num_feature_maps_gen
self.snlinear0 = snlinear(in_features=self.z_dim, out_features=self.g_conv_dim*8*4*4)
 self.block1 = GenBlock(self.g_conv_dim*8, self.g_conv_dim*8, args.num_classes)
 self.block2 = GenBlock(self.g_conv_dim*8, self.g_conv_dim*4, args.num_classes)
 self.block3 = GenBlock(self.g_conv_dim*4, self.g_conv_dim*2, args.num_classes)
 self.self_attn = Self_Attn(self.g_conv_dim*2)
 self.block4 = GenBlock(self.g_conv_dim*2, self.g_conv_dim, args.num_classes)
 self.bn = nn.BatchNorm2d(self.g_conv_dim, eps=1e-5, momentum=0.0001, affine=True)
 self.relu = nn.ReLU(inplace=True)
 self.snconv2d1 = snconv2d(in_channels=self.g_conv_dim, out_channels=3, kernel_size=3, stride=1, padding=1)
 self.tanh = nn.Tanh()
self.apply(init_weights)
 def forward(self, z, labels):
act0 = self.snlinear0(z)
 act0 = act0.view(-1, self.g_conv_dim*8, 4, 4)
 act1 = self.block1(act0, labels)
 act2 = self.block2(act1, labels)
 act3 = self.block3(act2, labels)
 act3 = self.self_attn(act3)
 act4 = self.block4(act3, labels)
 act5 = self.relu(act4)
 act6 = self.snconv2d1(act5)
 act6 = self.tanh(act6)
 return act6
class Discriminator(nn.Module):

 def __init__(self):
super(Discriminator, self ).__init__()
self.d_conv_dim = args.num_feature_maps_disc
self.opt_block1 = DiscOptBlock(3, self.d_conv_dim)
 self.block1 = DiscBlock(self.d_conv_dim, self.d_conv_dim*2)
 self.self_attn = Self_Attn(self.d_conv_dim*2)
 self.block2 = DiscBlock(self.d_conv_dim*2, self.d_conv_dim*4)
 self.block3 = DiscBlock(self.d_conv_dim*4, self.d_conv_dim*4)
 self.block4 = DiscBlock(self.d_conv_dim*4, self.d_conv_dim*8)
 self.block5 = DiscBlock(self.d_conv_dim*8, self.d_conv_dim*8)
 self.relu = nn.ReLU(inplace=True)
 self.snlinear1 = snlinear(in_features=self.d_conv_dim*8, out_features=1)
 self.sn_embedding1 = sn_embedding(args.num_classes, self.d_conv_dim*8)
 self.apply(init_weights)
 nn.init.xavier_uniform_(self.sn_embedding1.weight)
 def forward(self, x, labels):
h0 = self.opt_block1(x)
 h1 = self.block1(h0)
 h1 = self.self_attn(h1)
 h2 = self.block2(h1)
 h3 = self.block3(h2)
 h4 = self.block4(h3)
 h5 = self.block5(h4, downsample=False)
 h5 = self.relu(h5)
 h6 = torch.sum(h5, dim=[2,3])
 output1 = torch.squeeze(self.snlinear1(h6))
h_labels = self.sn_embedding1(labels)
 proj = torch.mul(h6, h_labels)
 output2 = torch.sum(proj, dim=[1])
 output = output1 + output2
return output<choose_model_class>
train_rows = train.shape[0]
y_train = np.log1p(train["revenue"] ).values
train_ID = train['id']
test_ID = test['id']
all_data = pd.concat([train, test], sort=False)
 all_data.drop(['revenue'], axis=1, inplace=True)
 all_data.drop(['id'], axis=1, inplace=True)
 all_data = pd.merge(all_data, imdb, how="left", on="imdb_id")
 all_data.head()
netG = Generator().to(device)
 netD = Discriminator().to(device)
 weights_init(netG)
 weights_init(netD)
 print("Generator parameters: ", sum(p.numel() for p in netG.parameters() if p.requires_grad))
print("Discriminator parameters:", sum(p.numel() for p in netD.parameters() if p.requires_grad))
fixed_noise = torch.randn(64, args.latent_dim, device=device)
 dog_labels = torch.randint(0, len(encoded_dog_labels),(64,), device=device)
 real_label = 0.9
fake_label = 0.0
BCE_stable = nn.BCEWithLogitsLoss()
optimizerD = optim.Adam(netD.parameters() , lr=args.lrD, betas=(args.beta1, args.beta2))
optimizerG = optim.Adam(netG.parameters() , lr=args.lrG, betas=(args.beta1, args.beta2))
def step(engine, batch):
def train_D(images, labels):

 noise = torch.randn(args.batch_size, args.latent_dim, device=device)
 G_output = netG(noise, labels)
 Dx_score = netD(images, labels)
 DG_score = netD(G_output, labels)
 D_loss =(torch.mean(torch.nn.ReLU()(1.0 -(Dx_score - torch.mean(DG_score)))) +
torch.mean(torch.nn.ReLU()(1.0 +(DG_score - torch.mean(Dx_score)))))/2
return D_loss, F.sigmoid(Dx_score ).mean().item() , F.sigmoid(DG_score ).mean().item()
def train_G(images, labels):

 noise = torch.randn(args.batch_size, args.latent_dim, device=device)
 G_output = netG(noise, labels)
 Dx_score = netD(images, labels)
 DG_score = netD(G_output, labels)
 G_loss =(torch.mean(torch.nn.ReLU()(1.0 +(Dx_score - torch.mean(DG_score)))) +
torch.mean(torch.nn.ReLU()(1.0 -(DG_score - torch.mean(Dx_score)))))/2
return G_loss, F.sigmoid(DG_score ).mean().item()
images, labels = batch[0].to(device), batch[1].to(device)
 args.batch_size = images.size(0)
 D_step_loss = []
for _ in range(args.num_disc_update):
netD.zero_grad()
D_loss, Dx_score, DG_score1 = train_D(images, labels)
 D_loss.backward()
optimizerD.step()
D_step_loss.append(D_loss.item())
 netG.zero_grad()
G_loss, DG_score2 = train_G(images, labels)
 G_loss.backward()
optimizerG.step()
return {
'D_loss': np.mean(D_step_loss),
'G_loss': G_loss.item() ,
'Dx_score': Dx_score,
'DG_score1': DG_score1,
'DG_score2': DG_score2
}<define_search_model>
for c in ['belongs_to_collection', 'genres', 'production_companies', 'production_countries', 'spoken_languages',
'Keywords', 'cast', 'crew']:
all_data[c] = all_data[c].apply(lambda x: {} if pd.isna(x)else ast.literal_eval(x))
clear_output_dir()
trainer = Engine(step)
 checkpoint_handler = ModelCheckpoint(args.output_dir,
args.CKPT_PREFIX,
save_interval=1,
n_saved=10,
require_empty=False)
 timer = Timer(average=True)
 monitoring_metrics = ['D_loss', 'G_loss', 'Dx_score', 'DG_score1', 'DG_score2']
RunningAverage(alpha=args.alpha, output_transform=lambda x: x['D_loss'] ).attach(trainer, 'D_loss')
 RunningAverage(alpha=args.alpha, output_transform=lambda x: x['G_loss'] ).attach(trainer, 'G_loss')
 RunningAverage(alpha=args.alpha, output_transform=lambda x: x['Dx_score'] ).attach(trainer, 'Dx_score')
 RunningAverage(alpha=args.alpha, output_transform=lambda x: x['DG_score1'] ).attach(trainer, 'DG_score1')
 RunningAverage(alpha=args.alpha, output_transform=lambda x: x['DG_score2'] ).attach(trainer, 'DG_score2')
 pbar = ProgressBar()
pbar.attach(trainer, metric_names=monitoring_metrics)
 @trainer.on(Events.ITERATION_COMPLETED)
 def print_logs(engine):
if(engine.state.iteration - 1)% args.PRINT_FREQ == 0:
fname = os.path.join(args.output_dir, args.LOGS_FNAME)
 columns = ["iteration", ] + list(engine.state.metrics.keys())
 values = [str(engine.state.iteration), ] + \
[str(round(value, 5)) for value in engine.state.metrics.values() ]
with open(fname, 'a')as f:
if f.tell() == 0:
print('\t'.join(columns), file=f)
 print('\t'.join(values), file=f)
 message = '[{epoch}/{max_epoch}][{i}/{max_i}]'.format(epoch=engine.state.epoch,
max_epoch=args.num_epochs,
i=(engine.state.iteration % len(dataloader)) ,
max_i=len(dataloader))
for name, value in zip(columns, values):
message += ' | {name}: {value}'.format(name=name, value=value)
 pbar.log_message(message)
 @trainer.on(Events.EPOCH_COMPLETED)
 def save_fake_example(engine):
fake = netG(fixed_noise, dog_labels ).detach().cpu()
path = os.path.join(args.output_dir, args.FAKE_IMG_FNAME.format(engine.state.epoch))
vutils.save_image(( fake+1.) /2., path, normalize=True)
 @trainer.on(Events.EPOCH_COMPLETED)
 def save_real_example(engine):
img, _ = engine.state.batch
path = os.path.join(args.output_dir, args.REAL_IMG_FNAME.format(engine.state.epoch))
vutils.save_image(img, path, normalize=True)
 @trainer.on(Events.EPOCH_COMPLETED)
 def display_images(engine):
if engine.state.epoch % 10 == 0:
display(Image.open(os.path.join(args.output_dir, args.REAL_IMG_FNAME.format(engine.state.epoch))))
display(Image.open(os.path.join(args.output_dir, args.FAKE_IMG_FNAME.format(engine.state.epoch))))
trainer.add_event_handler(event_name=Events.EPOCH_COMPLETED,
handler=checkpoint_handler,
to_save={
'netG': netG,
'netD': netD
})
 timer.attach(trainer,
start=Events.EPOCH_STARTED,
resume=Events.EPOCH_STARTED,
pause=Events.EPOCH_COMPLETED,
step=Events.EPOCH_COMPLETED)
 @trainer.on(Events.EPOCH_COMPLETED)
 def print_times(engine):
pbar.log_message(f'Epoch {engine.state.epoch} done.Time per epoch: {timer.value() /60:.3f}[min]')
 timer.reset()
@trainer.on(Events.EPOCH_COMPLETED)
 def create_plots(engine):
try:
mpl.use('agg')
 except ImportError:
warnings.warn('Loss plots will not be generated -- pandas or matplotlib not found')
 else:
df = pd.read_csv(os.path.join(args.output_dir, args.LOGS_FNAME), delimiter='\t', index_col='iteration')
 _ = df.loc[:, list(engine.state.metrics.keys())].plot(subplots=True, figsize=(10, 10))
_ = plt.xlabel('Iteration number')
 fig = plt.gcf()
path = os.path.join(args.output_dir, args.PLOT_FNAME)
 fig.savefig(path)
 @trainer.on(Events.EPOCH_STARTED)
 def handle_timeout(engine):
if time.perf_counter() - kernel_start_time > 31000:
print("Time limit reached! Stopping kernel!")
 engine.terminate()
create_plots(engine)
 checkpoint_handler(engine, {
'netG_exception': netG,
'netD_exception': netD
})
 @trainer.on(Events.EXCEPTION_RAISED)
 def handle_exception(engine, e):
if isinstance(e, KeyboardInterrupt)and(engine.state.iteration > 1):
engine.terminate()
warnings.warn('KeyboardInterrupt caught.Exiting gracefully.')
 create_plots(engine)
 checkpoint_handler(engine, {
'netG_exception': netG,
'netD_exception': netD
})
 else:
raise e
trainer.run(dataloader, args.num_epochs )<set_options>
def print_few_values(col_name):
print("Sample values for", col_name)
 all_data[col_name].head(5 ).apply(lambda x: print(x))
def dictionary_sizes(col_name):
return(all_data[col_name].apply(lambda x: len(x)).value_counts())
 def print_dictionary_sizes(col_name):
print("
===================================================")
 print("Distribution of sizes for", col_name)
 print(dictionary_sizes(col_name))
def dict_name_list(d, name="name"):
return([i[name] for i in d] if d != {} else [])
 def dict_id_name_list(d, name="name"):
return([(i["id"],i[name])for i in d] if d != {} else [])
 def col_name_list(col_name, name="name"):
name_list_list = list(all_data[col_name].apply(lambda x: dict_name_list(x, name)).values)
 return([i for name_list in name_list_list for i in name_list])
 def col_id_name_list(col_name, name="name"):
tuple_list_list = list(all_data[col_name].apply(lambda x: dict_id_name_list(x, name)).values)
 return([i for tuple_list in tuple_list_list for i in tuple_list])
 def get_names_counter(col_name, name="name"):
name_list = col_name_list(col_name, name)
 return(collections.Counter(name_list))
def print_top_names(col_name, name="name"):
print("
===================================================")
 print("Top {0}s for {1}".format(name, col_name))
c = get_names_counter(col_name, name)
 print(c.most_common(20))
def EDA_dict(col_name):
print_few_values(col_name)
 print_dictionary_sizes(col_name)
 print_top_names(col_name)
 def add_dict_size_column(col_name):
all_data[col_name + "_size"] = all_data[col_name].apply(lambda x: len(x)if x != {} else 0)
 def add_dict_id_column(col_name):
c = col_name + "_id"
all_data[c] = all_data[col_name].apply(lambda x: x[0]["id"] if x != {} else 0)
 all_data[c] = all_data[c].astype("category")
 def add_dict_indicator_columns(col_name):
c = get_names_counter(col_name)
 top_names = [x[0] for x in c.most_common(20)]
for name in top_names:
all_data[col_name + "_" + name] = all_data[col_name].apply(lambda x: name in dict_name_list(x))
def drop_column(col_name):
all_data.drop([col_name], axis=1, inplace=True)
 def feature_engineer_dict(col_name):
add_dict_size_column(col_name)
 max_size = dictionary_sizes(col_name ).index.max()
if max_size == 1:
add_dict_id_column(col_name)
 else:
add_dict_indicator_columns(col_name)
 drop_column(col_name)
 def encode_column(col_name):
lbl = LabelEncoder()
lbl.fit(list(all_data[col_name].values))
all_data[col_name] = lbl.transform(list(all_data[col_name].values))
display(SVG(".. /output_dir/plot.svg"))<load_pretrained>
col_name = "belongs_to_collection"
EDA_dict(col_name )
display(Image.open(os.path.join(args.output_dir, args.REAL_IMG_FNAME.format(trainer.state.epoch-1))))
display(Image.open(os.path.join(args.output_dir, args.FAKE_IMG_FNAME.format(trainer.state.epoch-1))))<categorify>
col_name="genres"
EDA_dict(col_name )
def mse(imageA, imageB):
err = np.sum(( imageA.astype("float")- imageB.astype("float")) ** 2)
 err /= float(imageA.shape[0] * imageA.shape[1])
 return err
def analyse_generated_by_class(n_images=5):
good_breeds = []
for l in range(len(decoded_dog_labels)) :
sample = []
for _ in range(n_images):
noise = torch.randn(1, args.latent_dim, device=device)
 dog_label = torch.full(( 1,), l, device=device, dtype=torch.long)
 gen_image = netG(noise, dog_label ).to("cpu" ).clone().detach().squeeze(0)
 gen_image = gen_image.numpy().transpose(1, 2, 0)
 sample.append(gen_image)
 d = np.round(np.sum([mse(sample[k], sample[k+1])for k in range(len(sample)-1)])/n_images, 1)
 if d < 1.1: continue
print(f"Generated breed({d}): ", decoded_dog_labels[l])
 l_noise = torch.randn(n_images, args.latent_dim, device=device)
 l_labels = torch.randint(0, len(encoded_dog_labels),(n_images,), device=device)
 fake = netG(l_noise, l_labels ).detach().cpu()
path = os.path.join(args.output_dir, f'img_{l}.png')
 vutils.save_image(( fake+1.) /2., path, normalize=True)
 display(Image.open(os.path.join(args.output_dir, f'img_{l}.png')))
 good_breeds.append(l)
 return good_breeds<define_variables>
all_data.loc[all_data["release_date"].isnull() ]
all_data.loc[all_data["release_date"].isnull() , "release_date"] = "05/01/2000"
all_data["release_date"] = pd.to_datetime(all_data["release_date"])
 all_data["release_date_weekday"] = all_data["release_date"].dt.weekday.astype(int)
 all_data["release_date_month"] = all_data["release_date"].dt.month.astype(int)
 all_data["release_date_year"] = all_data["release_date"].dt.year.astype(int)
 all_data["release_date_year"] = np.where(all_data["release_date_year"]>2019, all_data["release_date_year"]-100, all_data["release_date_year"])
 all_data["imdb_year"]=all_data["imdb_year"].astype(int)
 print(all_data[abs(all_data["release_date_year"]-all_data["imdb_year"])>2][["imdb_id","release_date_year","imdb_year"]])
 all_data.loc[abs(all_data["release_date_year"]-all_data["imdb_year"])>2, "release_date_year"] = all_data["imdb_year"]
drop_column("release_date")
 drop_column("imdb_year" )
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values
def create_submit(good_breeds):
print("Creating submit")
 os.makedirs('.. /output_images', exist_ok=True)
 im_batch_size = 50
n_images = 10000
all_dog_labels = np.random.choice(good_breeds, size=n_images, replace=True)
 for i_batch in range(0, n_images, im_batch_size):
noise = torch.randn(im_batch_size, args.latent_dim, device=device)
 dog_labels = torch.from_numpy(all_dog_labels[i_batch:(i_batch+im_batch_size)] ).to(device)
 gen_images = netG(noise, dog_labels)
 gen_images =(gen_images.to("cpu" ).clone().detach() + 1)/ 2
for ii, img in enumerate(gen_images):
save_image(gen_images[ii, :, :, :], os.path.join('.. /output_images', f'image_{i_batch + ii:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<feature_engineering>
all_data.loc[all_data["runtime"].isnull() , "imdb_id"]
all_data.loc[all_data["runtime"]==0, "imdb_id"]
all_data.loc[all_data["runtime"]==0, "runtime"] = all_data["imdb_runtime"]
all_data.loc[all_data["runtime"].isnull() , "runtime"] = all_data["imdb_runtime"]
all_data["runtime"].fillna(all_data["runtime"].mean() , inplace=True)
 drop_column("imdb_runtime" )
good_breeds = analyse_generated_by_class(6)
 create_submit(good_breeds )<choose_model_class>
encode_column("original_language" )
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>
def get_names() :
tuple_list_list = list(all_data["cast"].apply(lambda x: [(cm["name"].split(" ")[0],cm["gender"])for cm in x] ).values)
 tuple_list_list.extend(list(all_data["crew"].apply(lambda x: [(cm["name"].split(" ")[0],cm["gender"])for cm in x] ).values))
tuple_list = [i for tuple_list in tuple_list_list for i in tuple_list]
names = set()
c = [None] * 3
for gender in range(0, 3):
t = [i[0] for i in tuple_list if i[1] == gender]
names.update(set(t))
c[gender] = collections.Counter(t)
 names_df = pd.DataFrame(data = list(names), columns=["name"])
 names_df["female_count"] = names_df["name"].apply(lambda x: c[1][x])
 names_df["male_count"] = names_df["name"].apply(lambda x: c[2][x])
 names_df["unknown_count"] = names_df["name"].apply(lambda x: c[0][x])
 names_df["total_count"] = names_df.apply(lambda x: x["female_count"]+x["male_count"]+x["unknown_count"], axis=1)
 return(names_df)
 names_df = get_names()
def ClassifyName(row):
fcount = row["female_count"]
mcount = row["male_count"]
cl = "TBD"
gender = 0
if(fcount + mcount < 5):
cl = "too few names"
elif(fcount == 0):
gender = 2
cl = "high confidence"
elif(mcount == 0):
gender = 1
cl = "high confidence"
else:
if(mcount / float(fcount)< 0.1):
gender = 1
cl = "unisex - pick one"
elif(fcount / float(mcount)< 0.1):
gender = 2
cl = "unisex - pick one"
else:
cl = "unisex - TBD"
return [gender, cl]
names_df = names_df.merge(names_df.apply(lambda x: ClassifyName(x), axis=1, result_type="expand"), left_index=True, right_index=True)
 names_df.rename(columns={0:"gender", 1:"classification"}, inplace=True)
 names_to_gender = dict()
def update_names_to_gender(row):
if(row["gender"] > 0):
names_to_gender[row["name"]] = row["gender"]
j = names_df.apply(lambda x: update_names_to_gender(x), axis=1)
 def fix_unknown_gender_row(row):
for cm in row:
if cm["gender"] == 0:
name = cm["name"]
first_name = name.split(" ")[0]
if(first_name in names_to_gender):
cm["gender"] = names_to_gender[first_name]
def fix_unknown_gender_col(col_name):
all_data[col_name].apply(lambda x: fix_unknown_gender_row(x))
if COMPUTE_LB:
shutil.unpack_archive('images.zip', extract_dir='.. /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<train_model>
col_name = "crew"
EDA_dict(col_name)
 print_top_names(col_name, name="job" )
print(f"Done in {(time.time() - starttime)/60:.4f} minutes." )<import_modules>
fix_unknown_gender_col(col_name)
 def get_most_common_gender(row):
genders = [mc["gender"] for mc in row]
if(genders == []):
return -1
return(collections.Counter(genders ).most_common(1)[0][0])
 top_jobs = [x[0] for x in get_names_counter(col_name, "job" ).most_common(10)]
for job in top_jobs:
c = "crew_" + job
print("-------------
",job,"
-------------")
 all_data[c] = all_data[col_name].apply(lambda x: [crew_member for crew_member in x if crew_member["job"]==job])
 all_data[c + "_common_gender"] = all_data[c].apply(lambda x: get_most_common_gender(x))
EDA_dict(c)
 feature_engineer_dict(c)
 drop_column(col_name )
import os
import PIL
import torchvision
import torchvision.datasets as dset
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
import xml.etree.ElementTree as ET
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
from torch.nn.init import xavier_uniform_
import time
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.optim as optim
from torch.nn.utils import spectral_norm
import torch.utils.data
import torchvision
import torchvision.datasets as dset
import torchvision.transforms as transforms
import torchvision.utils as vutils
from torch.autograd import Variable
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
import matplotlib.image as mpimg
import torch.nn.functional as F
from torch.nn import Parameter
import numpy as np
import os
import gzip, pickle
import tensorflow as tf
from scipy import linalg
import pathlib
import urllib
import warnings
from tqdm import tqdm
from PIL import Image
import zipfile
from tqdm import tqdm_notebook as tqdm 
<define_variables>
col_name = "cast"
kernel_start_time = time.perf_counter()<normalization>
fix_unknown_gender_col(col_name)
 genders = [x[0] for x in get_names_counter(col_name, "gender" ).most_common(2)]
for gender in genders:
c = "cast_gender_" + str(gender)
 all_data[c] = all_data[col_name].apply(lambda x: [cast_member for cast_member in x if cast_member["gender"]==gender])
 ",gender,"
-------------")
 EDA_dict(c)
 feature_engineer_dict(c)
 drop_column(col_name )
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
class MinibatchStdDev(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 = torch.sqrt(y.pow(2.0 ).mean(dim=0, keepdim=False)+ alpha)
 y = y.mean().view(1, 1, 1, 1)
 y = y.repeat(batch_size, 1, height, width)
 y = torch.cat([x, y], 1)
 return y<define_search_model>
all_data["no_homepage"] = all_data["homepage"].isnull()
print(all_data["no_homepage"].value_counts())
 multiple_homepages = all_data["homepage"].value_counts()
multiple_homepages = multiple_homepages[multiple_homepages>1].index.tolist()
print("There are ", len(multiple_homepages), "homepages which appear more than once")
 all_data.loc[all_data["homepage"].isin(multiple_homepages)== False, "homepage"] = ""
encode_column("homepage" )
class Generator(nn.Module):
def __init__(self, nz=128, num_classes=120, channels=3, nfeats=32):
super(Generator, self ).__init__()
self.nz = nz
self.num_classes = num_classes
self.channels = channels
self.label_emb = nn.Embedding(num_classes, nz)
 self.pixnorm = PixelwiseNorm()
self.conv1 = spectral_norm(nn.ConvTranspose2d(2*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, channels, 3, 1, 1, bias=False))
def forward(self, inputs):
z, labels = inputs
enc = self.label_emb(labels ).view(( -1, self.nz, 1, 1))
enc = F.normalize(enc, p=2, dim=1)
 x = torch.cat(( z, enc), 1)
 x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = self.pixnorm(x)
 x = F.relu(self.conv3(x))
x = self.pixnorm(x)
 x = F.relu(self.conv4(x))
x = self.pixnorm(x)
 x = F.relu(self.conv5(x))
x = self.pixnorm(x)
 x = torch.tanh(self.conv6(x))
return x
class Discriminator(nn.Module):
def __init__(self, num_classes=120, channels=3, nfeats=64):
super(Discriminator, self ).__init__()
self.channels = channels
self.num_classes = num_classes
self.label_emb = nn.Embedding(num_classes, 64*64)
 self.conv1 = nn.Conv2d(channels+1, nfeats, 5, 2, 2, bias=False)
 self.conv2 = spectral_norm(nn.Conv2d(nfeats, nfeats * 2, 5, 2, 2, bias=False))
self.bn2 = nn.BatchNorm2d(nfeats * 2)
 self.conv3 = spectral_norm(nn.Conv2d(nfeats * 2, nfeats * 4, 5, 2, 2, bias=False))
self.bn3 = nn.BatchNorm2d(nfeats * 4)
 self.conv4 = spectral_norm(nn.Conv2d(nfeats * 4, nfeats * 8, 5, 2, 2, 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, inputs):
imgs, labels = inputs
enc = self.label_emb(labels ).view(( -1, 1, 64, 64))
enc = F.normalize(enc, p=2, dim=1)
 x = torch.cat(( imgs, enc), 1)
 x = F.relu(self.conv1(x), 0.2)
 x = F.relu(self.bn2(self.conv2(x)) , 0.2)
 x = F.relu(self.bn3(self.conv3(x)) , 0.2)
 x = F.relu(self.bn4(self.conv4(x)) , 0.2)
 x = self.batch_discriminator(x)
 x = torch.sigmoid(self.conv5(x))
return x.view(-1, 1 )<load_pretrained>
all_data["no_budget"] =(all_data["budget"] == 0)
 all_data["log_budget"]=np.log1p(all_data["budget"] )
class DataGenerator(Dataset):
def __init__(self, directory, transform=None, n_samples=np.inf, crop_dogs=True):
self.directory = directory
self.transform = transform
self.n_samples = n_samples
self.samples, self.labels = self.load_dogs_data(directory, crop_dogs)
 def load_dogs_data(self, directory, crop_dogs):
required_transforms = torchvision.transforms.Compose([
torchvision.transforms.Resize(64),
torchvision.transforms.CenterCrop(64),
])
 imgs = []
labels = []
paths = []
for root, _, fnames in sorted(os.walk(directory)) :
for fname in sorted(fnames)[:min(self.n_samples, 999999999999999)]:
path = os.path.join(root, fname)
 paths.append(path)
 for path in paths:
try: img = dset.folder.default_loader(path)
 except: continue
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]))
if crop_dogs:
tree = ET.parse(os.path.join('.. /input/annotation/Annotation/',
annotation_dirname, annotation_basename))
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)
 object_img = required_transforms(img.crop(( xmin, ymin, xmax, ymax)))
 imgs.append(object_img)
 labels.append(annotation_dirname.split('-')[1].lower())
 else:
object_img = required_transforms(img)
 imgs.append(object_img)
 labels.append(annotation_dirname.split('-')[1].lower())
 return imgs, labels
def __getitem__(self, index):
sample = self.samples[index]
label = self.labels[index]
if self.transform is not None:
sample = self.transform(sample)
 return np.asarray(sample), label
def __len__(self):
return len(self.samples )<categorify>
all_data["mpaa_rating"] = all_data["mpaa_rating"].apply(lambda s: s.split(" ", 1)[1] if isinstance(s, str)else s)
 all_data["mpaa_rating_reason"] = all_data["mpaa_rating"].apply(lambda s: s.split(" ", 2)[2] if isinstance(s, str)else s)
 all_data["mpaa_rating"] = all_data["mpaa_rating"].apply(lambda s: s.split(" ", 1)[0] if isinstance(s, str)else s)
 all_data["mpaa_rating"].value_counts()
database = '.. /input/all-dogs/all-dogs/'
crop_dogs = True
n_samples = np.inf
BATCH_SIZE = 32
epochs = 1600
criterion = nn.BCELoss()
nz = 128
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 transform = transforms.Compose([transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor() ,
transforms.Normalize(( 0.5, 0.5, 0.5),(0.5, 0.5, 0.5)) ])
 train_data = DataGenerator(database, transform=transform, n_samples=n_samples, crop_dogs=crop_dogs)
 decoded_dog_labels = {i:breed for i, breed in enumerate(sorted(set(train_data.labels)))}
encoded_dog_labels = {breed:i for i, breed in enumerate(sorted(set(train_data.labels)))}
train_data.labels = [encoded_dog_labels[l] for l in train_data.labels]
train_loader = torch.utils.data.DataLoader(train_data, shuffle=True,
batch_size=BATCH_SIZE, num_workers=4)
 print("Dog breeds loaded: ", len(encoded_dog_labels))
print("Data samples loaded:", len(train_data))<choose_model_class>
def SplitReason(r):
s = r.strip().replace(" ", " " ).replace(", and for ", ", " ).replace(", and ", ", ")
 rl = s.rsplit(" and ", 1)
 rl2 = rl[0].split(", ")
 if len(rl)>1:
rl2.append(rl[1])
 return(rl2)
 col_name = "mpaa_rating_reason"
all_data[col_name] = all_data[col_name].apply(lambda s: SplitReason(s)if isinstance(s, str)else [])
 reason_list_list = list(all_data[col_name].values)
 top_reasons = [reason[0] for reason in collections.Counter([i for reason_list in reason_list_list for i in reason_list] ).most_common(100)]
for reason in top_reasons:
all_data[col_name + "_" + reason] = all_data[col_name].apply(lambda x: reason in x)
 all_data.head()
drop_column(col_name )
netG = Generator(nz, num_classes=len(encoded_dog_labels), nfeats=32 ).to(device)
 netD = Discriminator(num_classes=len(encoded_dog_labels), nfeats=32 ).to(device)
 print("Generator parameters: ", sum(p.numel() for p in netG.parameters() if p.requires_grad))
print("Discriminator parameters:", sum(p.numel() for p in netD.parameters() if p.requires_grad))
optimizerG = optim.Adam(netG.parameters() , lr=0.0002, betas=(0.5, 0.999))
optimizerD = optim.Adam(netD.parameters() , lr=0.0002, betas=(0.5, 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 )<define_variables>
all_data.drop(['imdb_id'], axis=1, inplace=True)
 all_data.drop(['original_title'], axis=1, inplace=True)
 all_data.drop(['overview'], axis=1, inplace=True)
 all_data.drop(['poster_path'], axis=1, inplace=True)
 all_data.drop(['status'], axis=1, inplace=True)
 all_data.drop(['tagline'], axis=1, inplace=True)
 all_data.drop(['title'], axis=1, inplace=True)
 all_data.drop(['Keywords'], axis=1, inplace=True)
 drop_column("imdb_release")
 drop_column("imdb_budget")
 drop_column("imdb_country" )
for epoch in range(epochs):
epoch_time = time.perf_counter()
if time.perf_counter() - kernel_start_time > 30000:
print("Time limit reached! Stopping kernel!"); break
for ii,(real_images, dog_labels)in tqdm(enumerate(train_loader),total=len(train_loader)) :
if real_images.shape[0]!= BATCH_SIZE: continue
real_labels = torch.squeeze(torch.empty(( BATCH_SIZE, 1), device=device ).uniform_(0.60, 0.8))
fake_labels = torch.squeeze(torch.empty(( BATCH_SIZE, 1), device=device ).uniform_(0.00, 0.2))
netD.zero_grad()
dog_labels = torch.tensor(dog_labels, device=device)
 real_images = real_images.to(device)
 output = netD(( real_images, dog_labels))
errD_real = criterion(output, real_labels)
 errD_real.backward()
D_x = output.mean().item()
noise = torch.randn(BATCH_SIZE, nz, 1, 1, device=device)
 fake_images = netG(( noise, dog_labels))
output = netD(( fake_images.detach() , dog_labels))
errD_fake = criterion(output, fake_labels)
 errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
output = netD(( fake_images, dog_labels))
errG = criterion(output, real_labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
lr_schedulerG.step(epoch)
 lr_schedulerD.step(epoch)
 print('%.2fs [%d/%d] Loss_D: %.4f Loss_G: %.4f' %(
 time.perf_counter() -epoch_time, epoch+1, epochs, errD.item() , errG.item()))
print('D(x): ',D_x,', D(G(z)) : ',D_G_z1)
 show_generated_img() 
<categorify>
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse = np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv=kf))
return(rmse)
 def eval_model(model, name):
start_time = time.time()
score = rmsle_cv(model)
 print("{} score: {:.4f}({:.4f}), execution time: {:.1f}".format(name, score.mean() , score.std() , time.time() -start_time))
def mse(imageA, imageB):
err = np.sum(( imageA.astype("float")- imageB.astype("float")) ** 2)
 err /= float(imageA.shape[0] * imageA.shape[1])
 return err
def analyse_generated_by_class(n_images=5):
good_breeds = []
for l in range(len(decoded_dog_labels)) :
sample = []
for _ in range(n_images):
noise = torch.randn(1, nz, 1, 1, device=device)
 dog_label = torch.full(( 1,), l, device=device, dtype=torch.long)
 gen_image = netG(( noise, dog_label)).to("cpu" ).clone().detach().squeeze(0)
 gen_image = gen_image.numpy().transpose(1, 2, 0)
 sample.append(gen_image)
 d = np.round(np.sum([mse(sample[k], sample[k+1])for k in range(len(sample)-1)])/n_images, 1)
 if d < 1.0: continue
print(f"Generated breed({d}): ", decoded_dog_labels[l])
 figure, axes = plt.subplots(1, len(sample), figsize=(64, 64))
for index, axis in enumerate(axes):
axis.axis('off')
 image_array =(sample[index] + 1.) / 2.
 axis.imshow(image_array)
 plt.show()
good_breeds.append(l)
 return good_breeds<define_variables>
mod_lasso = make_pipeline(RobustScaler() , Lasso(alpha=0.005, random_state=1))
eval_model(mod_lasso, "lasso")
 mod_enet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
eval_model(mod_enet, "enet")
 mod_cat = CatBoostRegressor(iterations=10000, learning_rate=0.01,
depth=5, eval_metric='RMSE',
colsample_bylevel=0.7, random_seed = 17, silent=True,
bagging_temperature = 0.2, early_stopping_rounds=200)
 eval_model(mod_cat, "cat")
 mod_gboost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=5, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state=5)
 eval_model(mod_gboost, "gboost")
 mod_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state=7, nthread=-1)
 eval_model(mod_xgb, "xgb")
 mod_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=8,
learning_rate=0.05, n_estimators=650,
max_bin=58, bagging_fraction=0.80,
bagging_freq=5, feature_fraction=0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf=7, min_sum_hessian_in_leaf=11)
 eval_model(mod_lgb, "lgb" )
def create_submit(good_breeds):
print("Creating submit")
 os.makedirs('.. /output_images', exist_ok=True)
 im_batch_size = 100
n_images = 10000
all_dog_labels = np.random.choice(good_breeds, size=n_images, replace=True)
 for i_batch in range(0, n_images, im_batch_size):
noise = torch.randn(im_batch_size, nz, 1, 1, device=device)
 dog_labels = torch.from_numpy(all_dog_labels[i_batch:(i_batch+im_batch_size)] ).to(device)
 gen_images = netG(( noise, dog_labels))
gen_images =(gen_images.to("cpu" ).clone().detach() + 1)/ 2
for ii, img in enumerate(gen_images):
save_image(gen_images[ii, :, :, :], os.path.join('.. /output_images', f'image_{i_batch + ii:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )<feature_engineering>
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features)
 mod_stacked = StackingAveragedModels(base_models =(mod_cat, mod_xgb, mod_gboost, mod_lgb), meta_model = mod_lasso)
 eval_model(mod_stacked, "stacked" )
good_breeds = analyse_generated_by_class(6)
 create_submit(good_breeds )<import_modules>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def predict(model):
model.fit(train.values, y_train)
 train_pred = model.predict(train.values)
 pred = np.expm1(model.predict(test.values))
print(rmsle(y_train, train_pred))
return(pred )
import datetime
import gc
import json
import math
import os
import time
import shutil
import sys
from functools import partial
from numba import jit, njit
from PIL import Image
from tqdm import tqdm, trange
import cv2
import numpy as np
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.nn import init
import torch.optim as optim
import torch.nn.functional as F
from torch.nn import Parameter as P
import torch.utils.data as data
from torch.utils.data import DataLoader
import torchvision
from torchvision import datasets
import torchvision.transforms as T
import albumentations as A
from albumentations.pytorch import ToTensor
import xml.etree.ElementTree as ET<define_variables>
prediction = predict(mod_lasso)
 prediction = predict(mod_enet)
 prediction = predict(mod_xgb)
 prediction = predict(mod_gboost)
 prediction = predict(mod_lgb)
 prediction = predict(mod_stacked )
<compute_test_metric><EOS> 
submission = pd.DataFrame()
submission['id'] = test_ID
submission['revenue'] = prediction
submission.to_csv('submission.csv', index=False )