Kaggle competition: TMDB Box Office Prediction
<SOS> metric: RMSLE Kaggle data source: tmdb-box-office-prediction<compute_test_metric>
%matplotlib inline
plt.style.use('ggplot')
 %matplotlib notebook
py.init_notebook_mode(connected=True)
 warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 100)
 pd.set_option('display.max_rows', 100)
_train_preds_final = train_preds_final[:NUM_SEGMENTS]
print("OOF mae for different time_to_failure for refined data:")
 print("MAE for ttf>11 = {:.5f}".format(mean_absolute_error(y[index1], _train_preds_final[index1])))
 print("MAE for 9<ttf<11 = {:.5f}".format(mean_absolute_error(y[index2], _train_preds_final[index2])))
 print("MAE for 7<ttf<9 = {:.5f}".format(mean_absolute_error(y[index3], _train_preds_final[index3])))
 print("MAE for 5<ttf<7 = {:.5f}".format(mean_absolute_error(y[index4], _train_preds_final[index4])))
 print("MAE for 3<ttf<5 = {:.5f}".format(mean_absolute_error(y[index5], _train_preds_final[index5])))
 print("MAE for 1<ttf<3 = {:.5f}".format(mean_absolute_error(y[index6], _train_preds_final[index6])))
 print("MAE for ttf<1 = {:.5f}".format(mean_absolute_error(y[index7], _train_preds_final[index7])) )<import_modules>
train = pd.read_csv('.. /input/tmdb-box-office-prediction/train.csv')
 test = pd.read_csv('.. /input/tmdb-box-office-prediction/test.csv')
 sam_sub = pd.read_csv('.. /input/tmdb-box-office-prediction/sample_submission.csv')
 print("train dataset:", train.shape,"
","test dataset: ",test.shape,"
","sample_submission dataset:", sam_sub.shape )
import matplotlib.pyplot as plt
import numpy as np
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
import os
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.utils.data import Dataset, DataLoader
import os
import cv2
from tqdm import tqdm_notebook as tqdm
import time
from PIL import Image
import torch.nn.parallel
import torch.optim as optim
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 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 random
from torch.nn.utils import spectral_norm
from scipy.stats import truncnorm
import torch as th<define_variables>
train.sort_values(by='revenue', ascending=False ).head(20)[['title','revenue','release_date']]
kernel_start_time = time.perf_counter()
batch_size = 32<feature_engineering>
train.loc[train['id'] == 16,'revenue'] = 192864
train.loc[train['id'] == 90,'budget'] = 30000000
train.loc[train['id'] == 118,'budget'] = 60000000
train.loc[train['id'] == 149,'budget'] = 18000000
train.loc[train['id'] == 313,'revenue'] = 12000000
train.loc[train['id'] == 451,'revenue'] = 12000000
train.loc[train['id'] == 464,'budget'] = 20000000
train.loc[train['id'] == 470,'budget'] = 13000000
train.loc[train['id'] == 513,'budget'] = 930000
train.loc[train['id'] == 797,'budget'] = 8000000
train.loc[train['id'] == 819,'budget'] = 90000000
train.loc[train['id'] == 850,'budget'] = 90000000
train.loc[train['id'] == 1112,'budget'] = 7500000
train.loc[train['id'] == 1131,'budget'] = 4300000
train.loc[train['id'] == 1359,'budget'] = 10000000
train.loc[train['id'] == 1542,'budget'] = 1
train.loc[train['id'] == 1542,'budget'] = 15800000
train.loc[train['id'] == 1571,'budget'] = 4000000
train.loc[train['id'] == 1714,'budget'] = 46000000
train.loc[train['id'] == 1721,'budget'] = 17500000
train.loc[train['id'] == 1865,'revenue'] = 25000000
train.loc[train['id'] == 2268,'budget'] = 17500000
train.loc[train['id'] == 2491,'revenue'] = 6800000
train.loc[train['id'] == 2602,'budget'] = 31000000
train.loc[train['id'] == 2612,'budget'] = 15000000
train.loc[train['id'] == 2696,'budget'] = 10000000
train.loc[train['id'] == 2801,'budget'] = 10000000
test.loc[test['id'] == 3889,'budget'] = 15000000
test.loc[test['id'] == 6733,'budget'] = 5000000
test.loc[test['id'] == 3197,'budget'] = 8000000
test.loc[test['id'] == 6683,'budget'] = 50000000
test.loc[test['id'] == 5704,'budget'] = 4300000
test.loc[test['id'] == 6109,'budget'] = 281756
test.loc[test['id'] == 7242,'budget'] = 10000000
test.loc[test['id'] == 7021,'budget'] = 17540562
test.loc[test['id'] == 5591,'budget'] = 4000000
test.loc[test['id'] == 4282,'budget'] = 20000000
__print__ = print
def print(string):
os.system(f'echo "{string}"')
 __print__(string )<set_options>
def date_features(df):
df['release_date'] = pd.to_datetime(df['release_date'])
 df['release_year'] = df['release_date'].dt.year
df['release_month'] = df['release_date'].dt.month
df['release_day'] = df['release_date'].dt.day
df['release_quarter'] = df['release_date'].dt.quarter
df.drop(columns=['release_date'], inplace=True)
 return df
train=date_features(train)
 test=date_features(test)
 train['release_year'].head(10 )
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>
train['release_year'].iloc[np.where(train['release_year']> 2019)][:10]
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(68),
torchvision.transforms.RandomCrop(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)
 w = np.min(( xmax - xmin, ymax - ymin))
bbox =(xmin, ymin, xmin+w, ymin+w)
 object_img = required_transforms(img.crop(bbox))
imgs.append(object_img)
 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>
train['release_year']=np.where(train['release_year']> 2019, train['release_year']-100, train['release_year'])
 test['release_year']=np.where(test['release_year']> 2019, test['release_year']-100, test['release_year'] )
print('Started Creating Dataset')
 database = '.. /input/all-dogs/all-dogs/'
transform = transforms.Compose([transforms.RandomHorizontalFlip() ,
transforms.RandomRotation(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=25000)
 train_loader = torch.utils.data.DataLoader(train_data, shuffle=True,batch_size=batch_size, num_workers = 4)
 print('Finished Creating Dataset' )<normalization>
fillna_column = {'release_year':'mode','release_month':'mode',
'release_day':'mode'}
for k,v in fillna_column.items() :
if v == 'mode':
fill = train[k].mode() [0]
else:
fill = v
print(k, ': ', fill)
 train[k].fillna(value = fill, inplace = True)
 test[k].fillna(value = fill, inplace = True )
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>
def year_month_together(df):
year = df["release_year"].astype(int ).copy().astype(str)
 month=df['release_month'].astype(int ).copy().astype(str)
 day=df['release_day'].astype(int ).copy().astype(str)
 df["release_date"]= month.str.cat(day.str.cat(year,sep="/"), sep ="/")
 df['release_date']=pd.to_datetime(df['release_date'],format="%m/%d/%Y")
 df['release_dow'] = df['release_date'].dt.dayofweek
return df
train=year_month_together(train)
 test=year_month_together(test)
 train['release_date'].head(10 )
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>
dict_columns = ['belongs_to_collection', 'genres', 'production_companies',
'production_countries', 'spoken_languages', 'Keywords', 'cast', 'crew']
def text_to_dict(df):
for column in dict_columns:
df[column] = df[column].apply(lambda x: {} if pd.isna(x)else ast.literal_eval(x))
return df
train = text_to_dict(train)
 test = text_to_dict(test )
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.prelu1 = nn.PReLU(init=0.01)
 self.conv2 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 8, 4, 2, 1, bias=False))
self.prelu2 = nn.PReLU(init=0.01)
 self.conv3 = spectral_norm(nn.ConvTranspose2d(nfeats * 8, nfeats * 4, 4, 2, 1, bias=False))
self.prelu3 = nn.PReLU(init=0.01)
 self.conv4 = spectral_norm(nn.ConvTranspose2d(nfeats * 4, nfeats * 2, 4, 2, 1, bias=False))
self.prelu4 = nn.PReLU(init=0.01)
 self.conv5 = spectral_norm(nn.ConvTranspose2d(nfeats * 2, nfeats, 4, 2, 1, bias=False))
self.prelu5 = nn.PReLU(init=0.01)
 self.conv6 = spectral_norm(nn.ConvTranspose2d(nfeats, nchannels, 3, 1, 1, bias=False))
self.pixnorm = PixelwiseNorm()
def forward(self, x):
x = self.prelu1(self.conv1(x))
x = self.prelu2(self.conv2(x))
x = self.pixnorm(x)
 x = self.prelu3(self.conv3(x))
x = self.pixnorm(x)
 x = self.prelu4(self.conv4(x))
x = self.pixnorm(x)
 x = self.prelu5(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")
 batch_size = 32
lr = 1e-3
lr_d = 5e-4
beta1 = 0.5
epochs = 2000
num_iters_d = 2
nfg = 48
nfd = 48
nc = 3
criterion = nn.BCELoss()
nz = 128
fixed_noise = torch.randn(25, nz, 1, 1, device=device)
 real_label = 0.8
fake_label = 0.0
batch_size = train_loader.batch_size
netG = Generator(nz, nfg, nc ).to(device)
 netD = Discriminator(nc, nfd ).to(device)
 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 )<train_model>
train['belongs_to_collection'].apply(lambda x: len(x)if x != {} else 0 ).value_counts()
step = 0
for epoch in range(epochs):
print(f'Epoch: {epoch}')
 if time.perf_counter() - kernel_start_time > 32000:
print("Time limit reached! Stopping kernel!")
 break
for ii,(real_images)in enumerate(train_loader):
for __ in range(num_iters_d):
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)+ torch.zeros(( batch_size, 1)).uniform_(-0.1, 0.2 ).to(device)
 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)+ torch.zeros(( batch_size, 1)).uniform_(0.0, 0.2 ).to(device)
 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()
step += 1
lr_schedulerG.step(epoch)
 lr_schedulerD.step(epoch)
<import_modules>
train['collection_name'] = train['belongs_to_collection'].apply(lambda x: x[0]['name'] if x != {} else 0)
 train['has_collection'] = train['belongs_to_collection'].apply(lambda x: len(x)if x != {} else 0)
 test['collection_name'] = test['belongs_to_collection'].apply(lambda x: x[0]['name'] if x != {} else 0)
 test['has_collection'] = test['belongs_to_collection'].apply(lambda x: len(x)if x != {} else 0)
 train = train.drop(['belongs_to_collection'], axis=1)
 test = test.drop(['belongs_to_collection'], axis=1 )
Conv2DTranspose, Conv2D, Flatten, Dropout, Embedding, ReLU
print(os.listdir(".. /input"))<set_options>
train['collection_name'].value_counts() [1:10]
tf.enable_eager_execution()<define_variables>
train['num_genres'] = train['genres'].apply(lambda x: len(x)if x != {} else 0)
 train['all_genres'] = train['genres'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_genres = [m[0] for m in Counter([i for j in list_of_genres for i in j] ).most_common(15)]
for g in top_genres:
train['genre_' + g] = train['all_genres'].apply(lambda x: 1 if g in x else 0)
 test['num_genres'] = test['genres'].apply(lambda x: len(x)if x != {} else 0)
 test['all_genres'] = test['genres'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_genres:
test['genre_' + g] = test['all_genres'].apply(lambda x: 1 if g in x else 0)
 train = train.drop(['genres'], axis=1)
 test = test.drop(['genres'], axis=1 )
image_width = 64
image_height = 64
image_channels = 3
image_sample_size = 10000
image_output_dir = '.. /output_images/'
image_input_dir = '.. /input/all-dogs/all-dogs/'
image_ann_dir = ".. /input/annotation/Annotation/"
OUT_DIR = Path('.. /output_images/')
 MODEL_PATH = '.. /input/dog-face-generation-competition-kid-metric-input/classify_image_graph_def.pb'
TRAIN_DIR = Path('.. /input/all-dogs/all-dogs' )<feature_engineering>
drama=train.loc[train['genre_Drama']==1,]
comedy=train.loc[train['genre_Comedy']==1,]
action=train.loc[train['genre_Action']==1,]
thriller=train.loc[train['genre_Thriller']==1,]
text_drama = " ".join(review for review in drama.title)
 text_comedy = " ".join(review for review in comedy.title)
 text_action = " ".join(review for review in action.title)
 text_thriller = " ".join(review for review in thriller.title)
 wordcloud1 = WordCloud(stopwords=stopwords, background_color="white",colormap="Reds" ).generate(text_drama)
 wordcloud2 = WordCloud(stopwords=stopwords, background_color="white",colormap="Blues" ).generate(text_comedy)
 wordcloud3 = WordCloud(stopwords=stopwords, background_color="white",colormap="Greens" ).generate(text_action)
 wordcloud4 = WordCloud(stopwords=stopwords, background_color="white",colormap="Greys" ).generate(text_thriller)
 fig = plt.figure(figsize=(20, 10))
plt.subplot(221)
 plt.imshow(wordcloud1, interpolation='bilinear')
 plt.title('drama movie title')
 plt.axis("off")
 plt.subplot(222)
 plt.imshow(wordcloud2, interpolation='bilinear')
 plt.title('comedy movie title')
 plt.axis("off")
 plt.show()
fig = plt.figure(figsize=(20, 10))
plt.subplot(223)
 plt.imshow(wordcloud3, interpolation='bilinear')
 plt.title('action movie title')
 plt.axis("off")
 plt.subplot(224)
 plt.imshow(wordcloud4, interpolation='bilinear')
 plt.title('thriller movie title')
 plt.axis("off")
 plt.show()
dog_breed_dict = {}
for annotation in os.listdir(image_ann_dir):
annotations = annotation.split('-')
 dog_breed_dict[annotations[0]] = annotations[1]<load_pretrained>
drama_revenue=drama.groupby(['release_year'] ).mean() ['revenue']
comedy_revenue=comedy.groupby(['release_year'] ).mean() ['revenue']
action_revenue=action_revenue=action.groupby(['release_year'] ).mean() ['revenue']
thriller_revenue=thriller.groupby(['release_year'] ).mean() ['revenue']
revenue_concat = pd.concat([drama_revenue,comedy_revenue,action_revenue,thriller_revenue], axis=1)
 revenue_concat.columns=['drama','comedy','action','thriller']
revenue_concat.index=train.groupby(['release_year'] ).mean().index
def load_cropped_images(dog_breed_dict=dog_breed_dict, image_ann_dir=image_ann_dir, sample_size=25000,
image_width=image_width, image_height=image_height, image_channels=image_channels):
curIdx = 0
breeds = []
dog_images_np = np.zeros(( sample_size,image_width,image_height,image_channels))
for breed_folder in os.listdir(image_ann_dir):
for dog_ann in tqdm(os.listdir(image_ann_dir + breed_folder)) :
try:
img = read_image(os.path.join(image_input_dir, dog_ann + '.jpg'))
except FileNotFoundError:
continue
tree = ET.parse(os.path.join(image_ann_dir + breed_folder, dog_ann))
root = tree.getroot()
size = root.find('size')
 width = int(size.find('width' ).text)
 height = int(size.find('height' ).text)
 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_cropped = img[ymin:ymin+w, xmin:xmin+w, :]
if xmax - xmin > image_width:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_CUBIC
img_cropped = cv2.resize(img_cropped,(image_width, image_height),
interpolation=interpolation)
 dog_images_np[curIdx,:,:,:] = np.asarray(img_cropped)
 dog_breed_name = dog_breed_dict[dog_ann.split('_')[0]]
breeds.append(dog_breed_name)
 curIdx += 1
return dog_images_np, breeds<train_model>
train['num_companies'] = train['production_companies'].apply(lambda x: len(x)if x != {} else 0)
 train['all_production_companies'] = train['production_companies'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_companies = [m[0] for m in Counter([i for j in list_of_companies for i in j] ).most_common(30)]
for g in top_companies:
train['production_company_' + g] = train['all_production_companies'].apply(lambda x: 1 if g in x else 0)
 test['num_companies'] = test['production_companies'].apply(lambda x: len(x)if x != {} else 0)
 test['all_production_companies'] = test['production_companies'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_companies:
test['production_company_' + g] = test['all_production_companies'].apply(lambda x: 1 if g in x else 0)
 train = train.drop(['production_companies', 'all_production_companies'], axis=1)
 test = test.drop(['production_companies', 'all_production_companies'], axis=1 )
start_time = time.time()
dog_images_np, breeds = load_cropped_images(sample_size=22125)
 est_time = round(time.time() - start_time)
 print("Feature loading time: {}.".format(str(datetime.timedelta(seconds=est_time))))<define_variables>
Warner_Bros=train.loc[train['production_company_Warner Bros.']==1,]
Universal_Pictures=train.loc[train['production_company_Universal Pictures']==1,]
Twentieth_Century_Fox_Film=train.loc[train['production_company_Twentieth Century Fox Film Corporation']==1,]
Columbia_Pictures=train.loc[train['production_company_Columbia Pictures']==1,]
MGM=train.loc[train['production_company_Metro-Goldwyn-Mayer(MGM)']==1,]
New_Line_Cinema=train.loc[train['production_company_New Line Cinema']==1,]
Touchstone_Pictures=train.loc[train['production_company_Touchstone Pictures']==1,]
Walt_Disney=train.loc[train['production_company_Walt Disney Pictures']==1,]
Warner_Bros_revenue=Warner_Bros.groupby(['release_year'] ).mean() ['revenue']
Universal_Pictures_revenue=Universal_Pictures.groupby(['release_year'] ).mean() ['revenue']
Twentieth_Century_Fox_Film_revenue=Twentieth_Century_Fox_Film.groupby(['release_year'] ).mean() ['revenue']
Columbia_Pictures_revenue=Columbia_Pictures.groupby(['release_year'] ).mean() ['revenue']
MGM_revenue=MGM.groupby(['release_year'] ).mean() ['revenue']
New_Line_Cinema_revenue=New_Line_Cinema.groupby(['release_year'] ).mean() ['revenue']
Touchstone_Pictures_revenue=Touchstone_Pictures.groupby(['release_year'] ).mean() ['revenue']
Walt_Disney_revenue=Walt_Disney.groupby(['release_year'] ).mean() ['revenue']
prod_revenue_concat = pd.concat([Warner_Bros_revenue,Universal_Pictures_revenue,Twentieth_Century_Fox_Film_revenue,Columbia_Pictures_revenue,
MGM_revenue,New_Line_Cinema_revenue,Touchstone_Pictures_revenue,Walt_Disney_revenue], axis=1)
 prod_revenue_concat.columns=['Warner_Bros','Universal_Pictures','Twentieth_Century_Fox_Film','Columbia_Pictures','MGM','New_Line_Cinema','Touchstone_Pictures','Walt_Disney']
fig = plt.figure(figsize=(13, 7))
prod_revenue_concat.agg("mean",axis='rows' ).sort_values(ascending=True ).plot(kind='barh',x='Production Companies',y='Revenue',title='Mean Revenue(100 million dollars)of Top 8 Most Common Production Companies')
 plt.xlabel('Revenue(100 million dollars)' )
dog_images_np =(dog_images_np - 127.5)/ 127.5<data_type_conversions>
train['num_countries'] = train['production_countries'].apply(lambda x: len(x)if x != {} else 0)
 train['all_countries'] = train['production_countries'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_countries = [m[0] for m in Counter([i for j in list_of_countries for i in j] ).most_common(25)]
for g in top_countries:
train['production_country_' + g] = train['all_countries'].apply(lambda x: 1 if g in x else 0)
 test['num_countries'] = test['production_countries'].apply(lambda x: len(x)if x != {} else 0)
 test['all_countries'] = test['production_countries'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_countries:
test['production_country_' + g] = test['all_countries'].apply(lambda x: 1 if g in x else 0)
 train = train.drop(['production_countries', 'all_countries'], axis=1)
 test = test.drop(['production_countries', 'all_countries'], axis=1 )
dog_features_tf = tf.cast(dog_images_np, 'float32' )<init_hyperparams>
train['num_languages'] = train['spoken_languages'].apply(lambda x: len(x)if x != {} else 0)
 train['all_languages'] = train['spoken_languages'].apply(lambda x: ' '.join(sorted([i['iso_639_1'] for i in x])) if x != {} else '')
 top_languages = [m[0] for m in Counter([i for j in list_of_languages for i in j] ).most_common(30)]
for g in top_languages:
train['language_' + g] = train['all_languages'].apply(lambda x: 1 if g in x else 0)
 test['num_languages'] = test['spoken_languages'].apply(lambda x: len(x)if x != {} else 0)
 test['all_languages'] = test['spoken_languages'].apply(lambda x: ' '.join(sorted([i['iso_639_1'] for i in x])) if x != {} else '')
 for g in top_languages:
test['language_' + g] = test['all_languages'].apply(lambda x: 1 if g in x else 0)
 train = train.drop(['spoken_languages', 'all_languages'], axis=1)
 test = test.drop(['spoken_languages', 'all_languages'], axis=1 )
sample_size = 22125
batch_size = 128
weight_init_std = 0.02
weight_init_mean = 0.0
leaky_relu_slope = 0.2
downsize_factor = 2
dropout_rate = 0.5
scale_factor = 4 ** downsize_factor
lr_initial_d = 0.0002
lr_initial_g = 0.0002
lr_decay_steps = 1000
noise_dim = 128<create_dataframe>
text_drama = " ".join(review for review in drama['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else ''))
text_comedy = " ".join(review for review in comedy['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else ''))
text_action = " ".join(review for review in action['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else ''))
text_thriller = " ".join(review for review in thriller['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else ''))
wordcloud1 = WordCloud(stopwords=stopwords, background_color="white",colormap="Reds" ).generate(text_drama)
 wordcloud2 = WordCloud(stopwords=stopwords, background_color="white",colormap="Blues" ).generate(text_comedy)
 wordcloud3 = WordCloud(stopwords=stopwords, background_color="white",colormap="Greens" ).generate(text_action)
 wordcloud4 = WordCloud(stopwords=stopwords, background_color="white",colormap="Greys" ).generate(text_thriller)
 fig = plt.figure(figsize=(25, 20))
plt.subplot(221)
 plt.imshow(wordcloud1, interpolation='bilinear')
 plt.title('Drama Keywords')
 plt.axis("off")
 plt.subplot(222)
 plt.imshow(wordcloud2, interpolation='bilinear')
 plt.title('Comedy Keywords')
 plt.axis("off")
 plt.show()
fig = plt.figure(figsize=(25, 20))
plt.subplot(223)
 plt.imshow(wordcloud3, interpolation='bilinear')
 plt.title('Action Keywords')
 plt.axis("off")
 plt.subplot(224)
 plt.imshow(wordcloud4, interpolation='bilinear')
 plt.title('Thriller Keywords')
 plt.axis("off")
 plt.show()
dog_features_data = tf.data.Dataset.from_tensor_slices(dog_features_tf ).shuffle(sample_size ).map(flip ).batch(batch_size )<choose_model_class>
train['num_Keywords'] = train['Keywords'].apply(lambda x: len(x)if x != {} else 0)
 train['all_Keywords'] = train['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_keywords = [m[0] for m in Counter([i for j in list_of_keywords for i in j] ).most_common(30)]
for g in top_keywords:
train['keyword_' + g] = train['all_Keywords'].apply(lambda x: 1 if g in x else 0)
 test['num_Keywords'] = test['Keywords'].apply(lambda x: len(x)if x != {} else 0)
 test['all_Keywords'] = test['Keywords'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_keywords:
test['keyword_' + g] = test['all_Keywords'].apply(lambda x: 1 if g in x else 0)
 train = train.drop(['Keywords', 'all_Keywords'], axis=1)
 test = test.drop(['Keywords', 'all_Keywords'], axis=1 )
weight_initializer = tf.keras.initializers.TruncatedNormal(stddev=weight_init_std, mean=weight_init_mean,
seed=42 )<load_pretrained>
list_of_cast_names = list(train['cast'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 train['num_cast'] = train['cast'].apply(lambda x: len(x)if x != {} else 0)
 train['all_cast'] = train['cast'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_cast_names = [m[0] for m in Counter([i for j in list_of_cast_names for i in j] ).most_common(30)]
for g in top_cast_names:
train['cast_name_' + g] = train['all_cast'].apply(lambda x: 1 if g in x else 0)
 test['num_cast'] = test['cast'].apply(lambda x: len(x)if x != {} else 0)
 test['all_cast'] = test['cast'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_cast_names:
test['cast_name_' + g] = test['all_cast'].apply(lambda x: 1 if g in x else 0 )
class DenseSN(Dense):
def build(self, input_shape):
assert len(input_shape)>= 2
input_dim = input_shape[-1]
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 output = K.dot(inputs, W_bar)
 if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
 if self.activation is not None:
output = self.activation(output)
 return output
class ConvSN2D(Conv2D):
def build(self, input_shape):
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(input_dim, self.filters)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=self.rank + 2,
axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs, training=None):
def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 outputs = K.conv2d(
 inputs,
W_bar,
strides=self.strides,
padding=self.padding,
data_format=self.data_format,
dilation_rate=self.dilation_rate)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs
class ConvSN2DTranspose(Conv2DTranspose):
def build(self, input_shape):
if len(input_shape)!= 4:
raise ValueError('Inputs should have rank ' +
str(4)+
'; Received input shape:', str(input_shape))
if self.data_format == 'channels_first':
channel_axis = 1
else:
channel_axis = -1
if input_shape[channel_axis] is None:
raise ValueError('The channel dimension of the inputs '
'should be defined.Found `None`.')
 input_dim = input_shape[channel_axis]
kernel_shape = self.kernel_size +(self.filters, input_dim)
 self.kernel = self.add_weight(shape=kernel_shape,
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
 if self.use_bias:
self.bias = self.add_weight(shape=(self.filters,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
 else:
self.bias = None
self.u = self.add_weight(shape=tuple([1, self.kernel.shape.as_list() [-1]]),
initializer=initializers.RandomNormal(0, 1),
name='sn',
trainable=False)
 self.input_spec = InputSpec(ndim=4, axes={channel_axis: input_dim})
 self.built = True
def call(self, inputs):
input_shape = K.shape(inputs)
 batch_size = input_shape[0]
if self.data_format == 'channels_first':
h_axis, w_axis = 2, 3
else:
h_axis, w_axis = 1, 2
height, width = input_shape[h_axis], input_shape[w_axis]
kernel_h, kernel_w = self.kernel_size
stride_h, stride_w = self.strides
if self.output_padding is None:
out_pad_h = out_pad_w = None
else:
out_pad_h, out_pad_w = self.output_padding
out_height = conv_utils.deconv_length(height,
stride_h, kernel_h,
self.padding,
out_pad_h)
 out_width = conv_utils.deconv_length(width,
stride_w, kernel_w,
self.padding,
out_pad_w)
 if self.data_format == 'channels_first':
output_shape =(batch_size, self.filters, out_height, out_width)
 else:
output_shape =(batch_size, out_height, out_width, self.filters)
 def _l2normalize(v, eps=1e-12):
return v /(K.sum(v ** 2)** 0.5 + eps)
 def power_iteration(W, u):
_u = u
_v = _l2normalize(K.dot(_u, K.transpose(W)))
 _u = _l2normalize(K.dot(_v, W))
return _u, _v
W_shape = self.kernel.shape.as_list()
W_reshaped = K.reshape(self.kernel, [-1, W_shape[-1]])
 _u, _v = power_iteration(W_reshaped, self.u)
 sigma=K.dot(_v, W_reshaped)
 sigma=K.dot(sigma, K.transpose(_u))
W_bar = W_reshaped / sigma
if training in {0, False}:
W_bar = K.reshape(W_bar, W_shape)
 else:
with tf.control_dependencies([self.u.assign(_u)]):
W_bar = K.reshape(W_bar, W_shape)
 self.kernel = W_bar
outputs = K.conv2d_transpose(
 inputs,
self.kernel,
output_shape,
self.strides,
padding=self.padding,
data_format=self.data_format)
 if self.use_bias:
outputs = K.bias_add(
 outputs,
self.bias,
data_format=self.data_format)
 if self.activation is not None:
return self.activation(outputs)
 return outputs<choose_model_class>
cast_name_Samuel_L_Jackson=train.loc[train['cast_name_Samuel L.Jackson']==1,]
cast_name_Robert_De_Niro=train.loc[train['cast_name_Robert De Niro']==1,]
cast_name_Morgan_Freeman=train.loc[train['cast_name_Morgan Freeman']==1,]
cast_name_J_K_Simmons=train.loc[train['cast_name_J.K.Simmons']==1,]
cast_name_Bruce_Willis=train.loc[train['cast_name_Bruce Willis']==1,]
cast_name_Liam_Neeson=train.loc[train['cast_name_Liam Neeson']==1,]
cast_name_Susan_Sarandon=train.loc[train['cast_name_Susan Sarandon']==1,]
cast_name_Bruce_McGill=train.loc[train['cast_name_Bruce McGill']==1,]
cast_name_John_Turturro=train.loc[train['cast_name_John Turturro']==1,]
cast_name_Forest_Whitaker=train.loc[train['cast_name_Forest Whitaker']==1,]
cast_name_Samuel_L_Jackson_revenue=cast_name_Samuel_L_Jackson.mean() ['revenue']
cast_name_Robert_De_Niro_revenue=cast_name_Robert_De_Niro.mean() ['revenue']
cast_name_Morgan_Freeman_revenue=cast_name_Morgan_Freeman.mean() ['revenue']
cast_name_J_K_Simmons_revenue=cast_name_J_K_Simmons.mean() ['revenue']
cast_name_Bruce_Willis_revenue=cast_name_Bruce_Willis.mean() ['revenue']
cast_name_Liam_Neeson_revenue=cast_name_Liam_Neeson.mean() ['revenue']
cast_name_Susan_Sarandon_revenue=cast_name_Susan_Sarandon.mean() ['revenue']
cast_name_Bruce_McGill_revenue=cast_name_Bruce_McGill.mean() ['revenue']
cast_name_John_Turturro_revenue=cast_name_John_Turturro.mean() ['revenue']
cast_name_Forest_Whitaker_revenue=cast_name_Forest_Whitaker.mean() ['revenue']
cast_revenue_concat = pd.Series([cast_name_Samuel_L_Jackson_revenue,cast_name_Robert_De_Niro_revenue,cast_name_Morgan_Freeman_revenue,cast_name_J_K_Simmons_revenue,
cast_name_Bruce_Willis_revenue,cast_name_Liam_Neeson_revenue,cast_name_Susan_Sarandon_revenue,cast_name_Bruce_McGill_revenue,
cast_name_John_Turturro_revenue,cast_name_Forest_Whitaker_revenue])
 cast_revenue_concat.index=['Samuel L.Jackson','Robert De Niro','Morgan Freeman','J.K.Simmons','Bruce Willis','Liam Neeson','Susan Sarandon','Bruce McGill',
'John Turturro','Forest Whitaker']
fig = plt.figure(figsize=(13, 7))
cast_revenue_concat.sort_values(ascending=True ).plot(kind='barh',title='Mean Revenue(100 million dollars)by Top 10 Most Common Cast')
 plt.xlabel('Revenue(100 million dollars)' )
def transposed_conv(model, out_channels, ksize, stride_size, ptype='same'):
model.add(Conv2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def transposed_convSN(model, out_channels, ksize, stride_size, ptype='same'):
model.add(ConvSN2DTranspose(out_channels,(ksize, ksize),
strides=(stride_size, stride_size), padding=ptype,
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(ReLU())
 return model
def convSN(model, out_channels, ksize, stride_size):
model.add(ConvSN2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model
def conv(model, out_channels, ksize, stride_size):
model.add(Conv2D(out_channels,(ksize, ksize), strides=(stride_size, stride_size), padding='same',
kernel_initializer=weight_initializer, use_bias=False))
model.add(BatchNormalization())
 model.add(LeakyReLU(alpha=leaky_relu_slope))
return model<train_model>
list_of_cast_genders = list(train['cast'].apply(lambda x: [i['gender'] for i in x] if x != {} else [] ).values)
 list_of_cast_characters = list(train['cast'].apply(lambda x: [i['character'] for i in x] if x != {} else [] ).values)
 train['genders_0'] = train['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
train['genders_1'] = train['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
train['genders_2'] = train['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
top_cast_characters = [m[0] for m in Counter([i for j in list_of_cast_characters for i in j] ).most_common(15)]
for g in top_cast_characters:
train['cast_character_' + g] = train['cast'].apply(lambda x: 1 if g in str(x)else 0)
 test['genders_0'] = test['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
test['genders_1'] = test['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
test['genders_2'] = test['cast'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
for g in top_cast_characters:
test['cast_character_' + g] = test['cast'].apply(lambda x: 1 if g in str(x)else 0)
 train = train.drop(['cast'], axis=1)
 test = test.drop(['cast'], axis=1 )
print(image_height // scale_factor, image_width // scale_factor, 512 )<choose_model_class>
list_of_crew_names = list(train['crew'].apply(lambda x: [i['name'] for i in x] if x != {} else [] ).values)
 train['num_crew'] = train['crew'].apply(lambda x: len(x)if x != {} else 0)
 train['all_crew'] = train['crew'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 top_crew_names = [m[0] for m in Counter([i for j in list_of_crew_names for i in j] ).most_common(30)]
for g in top_crew_names:
train['crew_name_' + g] = train['all_crew'].apply(lambda x: 1 if g in x else 0)
 test['num_crew'] = test['crew'].apply(lambda x: len(x)if x != {} else 0)
 test['all_crew'] = test['crew'].apply(lambda x: ' '.join(sorted([i['name'] for i in x])) if x != {} else '')
 for g in top_crew_names:
test['crew_name_' + g] = test['all_crew'].apply(lambda x: 1 if g in x else 0 )
def DogGenerator() :
model = Sequential()
model.add(Dense(image_width // scale_factor * image_height // scale_factor * 128,
input_shape=(noise_dim,), kernel_initializer=weight_initializer))
model.add(Reshape(( image_height // scale_factor, image_width // scale_factor, 128)))
 model = transposed_conv(model, 512, ksize=5, stride_size=1)
 model.add(Dropout(dropout_rate))
model = transposed_conv(model, 256, ksize=5, stride_size=2)
 model.add(Dropout(dropout_rate))
model = transposed_conv(model, 128, ksize=5, stride_size=2)
 model = transposed_conv(model, 64, ksize=5, stride_size=2)
 model = transposed_conv(model, 32, ksize=5, stride_size=2)
 model.add(Dense(3, activation='tanh', kernel_initializer=weight_initializer))
return model<define_variables>
crew_name_Avy_Kaufman=train.loc[train['crew_name_Avy Kaufman']==1,]
crew_name_Robert_Rodriguez=train.loc[train['crew_name_Robert Rodriguez']==1,]
crew_name_Deborah_Aquila=train.loc[train['crew_name_Deborah Aquila']==1,]
crew_name_James_Newton_Howard=train.loc[train['crew_name_James Newton Howard']==1,]
crew_name_Mary_Vernieu=train.loc[train['crew_name_Mary Vernieu']==1,]
crew_name_Steven_Spielberg=train.loc[train['crew_name_Steven Spielberg']==1,]
crew_name_Luc_Besson=train.loc[train['crew_name_Luc Besson']==1,]
crew_name_Jerry_Goldsmith=train.loc[train['crew_name_Jerry Goldsmith']==1,]
crew_name_Francine_Maisler=train.loc[train['crew_name_Francine Maisler']==1,]
crew_name_Tricia_Wood=train.loc[train['crew_name_Tricia Wood']==1,]
crew_name_Avy_Kaufman_revenue=crew_name_Avy_Kaufman.mean() ['revenue']
crew_name_Robert_Rodriguez_revenue=crew_name_Robert_Rodriguez.mean() ['revenue']
crew_name_Deborah_Aquila_revenue=crew_name_Deborah_Aquila.mean() ['revenue']
crew_name_James_Newton_Howard_revenue=crew_name_James_Newton_Howard.mean() ['revenue']
crew_name_Mary_Vernieu_revenue=crew_name_Mary_Vernieu.mean() ['revenue']
crew_name_Steven_Spielberg_revenue=crew_name_Steven_Spielberg.mean() ['revenue']
crew_name_Luc_Besson_revenue=crew_name_Luc_Besson.mean() ['revenue']
crew_name_Jerry_Goldsmith_revenue=crew_name_Jerry_Goldsmith.mean() ['revenue']
crew_name_Francine_Maisler_revenue=crew_name_Francine_Maisler.mean() ['revenue']
crew_name_Tricia_Wood_revenue=crew_name_Tricia_Wood.mean() ['revenue']
crew_revenue_concat = pd.Series([crew_name_Avy_Kaufman_revenue,crew_name_Robert_Rodriguez_revenue,crew_name_Deborah_Aquila_revenue,crew_name_James_Newton_Howard_revenue,
crew_name_Mary_Vernieu_revenue,crew_name_Steven_Spielberg_revenue,crew_name_Luc_Besson_revenue,crew_name_Jerry_Goldsmith_revenue,
crew_name_Francine_Maisler_revenue,crew_name_Tricia_Wood_revenue])
 crew_revenue_concat.index=['Avy Kaufman','Robert Rodriguez','Deborah Aquila','James Newton Howard','Mary Vernieu','Steven Spielberg','Luc Besson','Jerry Goldsmith',
'Francine Maisler','Tricia Wood']
fig = plt.figure(figsize=(13, 7))
crew_revenue_concat.sort_values(ascending=True ).plot(kind='barh',title='Mean Revenue(100 million dollars)by Top 10 Most Common Crew')
 plt.xlabel('Revenue(100 million dollars)' )
def generate_latent_points(latent_dim, n_samples):
x_input = np.random.randn(latent_dim * n_samples)
 x_input = x_input.reshape(( n_samples, latent_dim))
return x_input<choose_model_class>
list_of_crew_jobs = list(train['crew'].apply(lambda x: [i['job'] for i in x] if x != {} else [] ).values)
 list_of_crew_genders = list(train['crew'].apply(lambda x: [i['gender'] for i in x] if x != {} else [] ).values)
 list_of_crew_departments = list(train['crew'].apply(lambda x: [i['department'] for i in x] if x != {} else [] ).values)
 train['genders_0'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
train['genders_1'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
train['genders_2'] = train['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
top_cast_characters = [m[0] for m in Counter([i for j in list_of_cast_characters for i in j] ).most_common(15)]
for g in top_cast_characters:
train['crew_character_' + g] = train['crew'].apply(lambda x: 1 if g in str(x)else 0)
 top_crew_jobs = [m[0] for m in Counter([i for j in list_of_crew_jobs for i in j] ).most_common(15)]
for j in top_crew_jobs:
train['jobs_' + j] = train['crew'].apply(lambda x: sum([1 for i in x if i['job'] == j]))
top_crew_departments = [m[0] for m in Counter([i for j in list_of_crew_departments for i in j] ).most_common(15)]
for j in top_crew_departments:
train['departments_' + j] = train['crew'].apply(lambda x: sum([1 for i in x if i['department'] == j]))
test['genders_0'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 0]))
test['genders_1'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 1]))
test['genders_2'] = test['crew'].apply(lambda x: sum([1 for i in x if i['gender'] == 2]))
for g in top_cast_characters:
test['crew_character_' + g] = test['crew'].apply(lambda x: 1 if g in str(x)else 0)
 for j in top_crew_jobs:
test['jobs_' + j] = test['crew'].apply(lambda x: sum([1 for i in x if i['job'] == j]))
for j in top_crew_departments:
test['departments_' + j] = test['crew'].apply(lambda x: sum([1 for i in x if i['department'] == j]))
train = train.drop(['crew'], axis=1)
 test = test.drop(['crew'], axis=1 )
def DogDiscriminator(spectral_normalization=True):
model = Sequential()
if spectral_normalization:
model.add(ConvSN2D(64,(5, 5), strides=(1,1), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = convSN(model, 64, ksize=5, stride_size=2)
 model = convSN(model, 128, ksize=5, stride_size=2)
 model = convSN(model, 256, ksize=5, stride_size=2)
 model.add(Flatten())
 model.add(DenseSN(1, activation='sigmoid'))
else:
model.add(Conv2D(64,(4, 4), strides=(2,2), padding='same', use_bias=False,
input_shape=[image_height, image_width, image_channels],
kernel_initializer=weight_initializer))
model.add(LeakyReLU(alpha=leaky_relu_slope))
model = conv(model, 64, ksize=4, stride_size=2)
 model = conv(model, 128, ksize=4, stride_size=2)
 model = conv(model, 256, ksize=4, stride_size=2)
 model.add(Flatten())
 model.add(Dense(1, activation='sigmoid'))
return model<choose_model_class>
power_six = train.id[train.budget > 1000][train.revenue < 100]
for k in power_six :
train.loc[train['id'] == k,'revenue'] = train.loc[train['id'] == k,'revenue'] * 1000000
decision = dog_discriminator(generated_image)
 print(decision )<categorify>
train['log_budget']=np.log1p(train['budget'])
 test['log_budget']=np.log1p(test['budget'])
 train['log_popularity']=np.log1p(train['popularity'])
 test['log_popularity']=np.log1p(test['popularity'] )
def smooth_positive_labels(y):
return y - 0.3 +(np.random.random(y.shape)* 0.5)
 def smooth_negative_labels(y):
return y + np.random.random(y.shape)* 0.3<categorify>
def prepare(df):
df['_budget_runtime_ratio'] = df['budget']/df['runtime']
df['_budget_popularity_ratio'] = df['budget']/df['popularity']
df['_budget_year_ratio'] = df['budget']/(df['release_year']*df['release_year'])
 df['_releaseYear_popularity_ratio'] = df['release_year']/df['popularity']
df['_releaseYear_popularity_ratio2'] = df['popularity']/df['release_year']
df['_year_to_log_budget'] = df['release_year'] / df['log_budget']
df['_year_to_log_popularity'] = df['release_year'] / df['log_popularity']
df['has_homepage'] = 0
df.loc[pd.isnull(df['homepage']),"has_homepage"] = 1
df['isTaglineNA'] = 0
df.loc[df['tagline'] == 0 ,"isTaglineNA"] = 1
df['isTitleDifferent'] = 1
df.loc[ df['original_title'] == df['title'] ,"isTitleDifferent"] = 0
df['isMovieReleased'] = 1
df.loc[ df['status'] != "Released" ,"isMovieReleased"] = 0
df['original_title_letter_count'] = df['original_title'].str.len()
df['original_title_word_count'] = df['original_title'].str.split().str.len()
df['title_word_count'] = df['title'].str.split().str.len()
df['overview_word_count'] = df['overview'].str.split().str.len()
df['tagline_word_count'] = df['tagline'].str.split().str.len()
df['meanruntimeByYear'] = df.groupby("release_year")["runtime"].aggregate('mean')
 df['meanPopularityByYear'] = df.groupby("release_year")["popularity"].aggregate('mean')
 df['meanBudgetByYear'] = df.groupby("release_year")["budget"].aggregate('mean')
 return df
train_new=prepare(train)
 test_new=prepare(test)
def noisy_labels(y, p_flip):
n_select = int(p_flip * int(y.shape[0]))
flip_ix = np.random.choice([i for i in range(int(y.shape[0])) ], size=n_select)
 op_list = []
for i in range(int(y.shape[0])) :
if i in flip_ix:
op_list.append(tf.subtract(1, y[i]))
else:
op_list.append(y[i])
 outputs = tf.stack(op_list)
 return outputs<choose_model_class>
train_new.to_csv("train_new.csv", index=False)
 test_new.to_csv("test_new.csv", index=False )
generator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_g, beta1=0.5)
 discriminator_optimizer = tf.train.AdamOptimizer(learning_rate=lr_initial_d, beta1=0.5)
 cross_entropy = tf.keras.losses.BinaryCrossentropy(from_logits=False )<compute_train_metric>
drop_columns=['homepage','imdb_id','poster_path','status','title', 'release_date','tagline', 'overview', 'original_title','all_genres','all_cast',
'original_language','collection_name','all_crew']
train_new=train_new.drop(drop_columns,axis=1)
 test_new=test_new.drop(drop_columns,axis=1 )
def discriminator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True, label_noise=True):
if label_noise and apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 real_output_smooth = smooth_positive_labels(real_output_noise)
 fake_output_smooth = smooth_negative_labels(fake_output_noise)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
elif label_noise and not apply_label_smoothing:
real_output_noise = noisy_labels(tf.ones_like(real_output), 0.05)
 fake_output_noise = noisy_labels(tf.zeros_like(fake_output), 0.05)
 if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_noise), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_noise), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_noise - tf.reduce_mean(fake_output_noise)- tf.ones_like(real_output_noise)))
 + tf.reduce_mean(tf.square(fake_output_noise - tf.reduce_mean(real_output_noise)+ tf.ones_like(fake_output_noise)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_noise)
 avg_real_logit = tf.reduce_mean(real_output_noise)
 D_r_tilde = tf.nn.sigmoid(real_output_noise - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_noise - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_noise)-(real_output_noise - tf.reduce_mean(fake_output_noise))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_noise)+(fake_output_noise - tf.reduce_mean(real_output_noise))))
total_loss = real_loss + fake_loss
return total_loss
elif apply_label_smoothing and not label_noise:
real_output_smooth = smooth_positive_labels(tf.ones_like(real_output))
fake_output_smooth = smooth_negative_labels(tf.zeros_like(fake_output))
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output_smooth), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output_smooth - tf.reduce_mean(fake_output_smooth)- tf.ones_like(real_output_smooth)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output_smooth)+ tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output_smooth)
 D_r_tilde = tf.nn.sigmoid(real_output_smooth - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output_smooth)-(real_output_smooth - tf.reduce_mean(fake_output_smooth))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)+(fake_output_smooth - tf.reduce_mean(real_output_smooth))))
total_loss = real_loss + fake_loss
return total_loss
else:
if loss_func == 'gan':
real_loss = cross_entropy(tf.ones_like(real_output), real_output)
 fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)- tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)+ tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_r_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_f_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)-(real_output - tf.reduce_mean(fake_output))))
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)+(fake_output - tf.reduce_mean(real_output))))
total_loss = real_loss + fake_loss
return total_loss<compute_train_metric>
X = train_new.drop(['id', 'revenue'], axis=1)
 y = np.log1p(train_new['revenue'])
 X_test = test_new.drop(['id'], axis=1)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.2 )
def generator_loss(real_output, fake_output, loss_func, apply_label_smoothing=True):
if apply_label_smoothing:
fake_output_smooth = smooth_negative_labels(tf.ones_like(fake_output))
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output_smooth), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output_smooth)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output_smooth - tf.reduce_mean(real_output)- tf.ones_like(fake_output_smooth)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output_smooth)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output_smooth - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output_smooth)-(fake_output_smooth - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output_smooth))))
loss = fake_loss + real_loss
return loss
else:
if loss_func == 'gan':
return cross_entropy(tf.ones_like(fake_output), fake_output)
 else:
if loss_func == 'ralsgan':
return(tf.reduce_mean(tf.square(real_output - tf.reduce_mean(fake_output)+ tf.ones_like(real_output)))
 + tf.reduce_mean(tf.square(fake_output - tf.reduce_mean(real_output)- tf.ones_like(fake_output)))) / 2.
 elif loss_func == 'rasgan':
avg_fake_logit = tf.reduce_mean(fake_output)
 avg_real_logit = tf.reduce_mean(real_output)
 D_r_tilde = tf.nn.sigmoid(real_output - avg_fake_logit)
 D_f_tilde = tf.nn.sigmoid(fake_output - avg_real_logit)
 total_loss = - tf.reduce_mean(tf.log(
 D_f_tilde + 1e-14)) - tf.reduce_mean(tf.log(1 - D_r_tilde + 1e-14))
return total_loss
elif loss_func == 'rahinge':
fake_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(fake_output)-(fake_output - tf.reduce_mean(real_output))))
real_loss = tf.reduce_mean(tf.nn.relu(tf.ones_like(real_output)+(real_output - tf.reduce_mean(fake_output))))
loss = fake_loss + real_loss
return loss 
<train_model>
params = {'num_leaves': 30,
'min_data_in_leaf': 20,
'objective': 'regression',
'max_depth': 5,
'learning_rate': 0.01,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_freq": 1,
"bagging_fraction": 0.9,
"bagging_seed": 11,
"metric": 'rmse',
"lambda_l1": 0.2,
"verbosity": -1}
lgb_model = lgb.LGBMRegressor(**params, n_estimators = 10000, nthread = 4, n_jobs = -1)
 lgb_model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=1000, early_stopping_rounds=200)
 eli5.show_weights(lgb_model, feature_filter=lambda x: x != '<BIAS>' )
checkpoint_dir = '/training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
 checkpoint = tf.train.Checkpoint(generator_optimizer=generator_optimizer,
discriminator_optimizer=discriminator_optimizer,
generator=dog_generator,
discriminator=dog_discriminator )<define_variables>
EPOCHS = 330
num_examples_to_generate = 16
seed = tf.random.normal([num_examples_to_generate, noise_dim] )<compute_train_metric>
opt_parameters = {'random_state': 501, 'objective': 'regression', 'num_leaves': 40, 'min_data_in_leaf': 15, 'max_depth': 4, 'learning_rate': 0.01, 'boosting_type': 'gbdt'}
params['learning_rate'] = opt_parameters['learning_rate']
params['max_depth'] = opt_parameters['max_depth']
params['num_leaves'] = opt_parameters['num_leaves']
params['min_data_in_leaf'] = opt_parameters['min_data_in_leaf']
def train_step(images, G_loss, D_loss, loss_type='gan'):
noise = tf.random.normal([batch_size, noise_dim])
 with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = dog_generator(noise, training=True)
 real_output = dog_discriminator(images, training=True)
 fake_output = dog_discriminator(generated_images, training=True)
 gen_loss = generator_loss(real_output, fake_output, loss_type, apply_label_smoothing=True)
 disc_loss = discriminator_loss(real_output, fake_output, loss_type,
apply_label_smoothing=True, label_noise=True)
 G_loss.append(gen_loss.numpy())
 D_loss.append(disc_loss.numpy())
 gradients_of_generator = gen_tape.gradient(gen_loss, dog_generator.trainable_variables)
 gradients_of_discriminator = disc_tape.gradient(disc_loss, dog_discriminator.trainable_variables)
 generator_optimizer.apply_gradients(zip(gradients_of_generator, dog_generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, dog_discriminator.trainable_variables))<init_hyperparams>
n_fold = 5
random_seed=2222
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 def train_model(X, X_test, y, params=None, folds=folds, model_type='lgb', plot_feature_importance=True, model=None):
oof = np.zeros(X.shape[0])
 prediction = np.zeros(X_test.shape[0])
 scores = []
feature_importance = pd.DataFrame()
for fold_n,(train_index, valid_index)in enumerate(folds.split(X)) :
print('Fold', fold_n, 'started at', time.ctime())
 if model_type == 'sklearn':
X_train, X_valid = X[train_index], X[valid_index]
else:
X_train, X_valid = X.values[train_index], X.values[valid_index]
y_train, y_valid = y[train_index], y[valid_index]
if model_type == 'lgb':
model = lgb.LGBMRegressor(**params, n_estimators = 10000, nthread = 4, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_valid, y_valid)], eval_metric='rmse',
verbose=500, early_stopping_rounds=200)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test, num_iteration=model.best_iteration_)
 if model_type == 'xgb':
train_data = xgb.DMatrix(data=X_train, label=y_train)
 valid_data = xgb.DMatrix(data=X_valid, label=y_valid)
 watchlist = [(train_data, 'train'),(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=10000, evals=watchlist, early_stopping_rounds=200, verbose_eval=500, params=params)
 y_pred_valid = model.predict(xgb.DMatrix(X_valid), ntree_limit=model.best_ntree_limit)
 y_pred = model.predict(xgb.DMatrix(X_test.values), ntree_limit=model.best_ntree_limit)
 if model_type == 'sklearn':
model = model
model.fit(X_train, y_train)
 y_pred_valid = model.predict(X_valid ).reshape(-1,)
 score = mean_squared_error(y_valid, y_pred_valid)
 y_pred = model.predict(X_test)
 if model_type == 'cat':
model = CatBoostRegressor(iterations=10000, eval_metric='RMSE', **params)
 model.fit(X_train, y_train, eval_set=(X_valid, y_valid), cat_features=[], use_best_model=True, verbose=False)
 y_pred_valid = model.predict(X_valid)
 y_pred = model.predict(X_test)
 oof[valid_index] = y_pred_valid.reshape(-1,)
 scores.append(mean_squared_error(y_valid, y_pred_valid)** 0.5)
 prediction += y_pred
if model_type == 'lgb':
fold_importance = pd.DataFrame()
fold_importance["feature"] = X.columns
fold_importance["importance"] = model.feature_importances_
fold_importance["fold"] = fold_n + 1
feature_importance = pd.concat([feature_importance, fold_importance], axis=0)
 prediction /= n_fold
print('CV mean score: {0:.4f}, std: {1:.4f}.'.format(np.mean(scores), np.std(scores)))
 if model_type == 'lgb':
feature_importance["importance"] /= n_fold
if plot_feature_importance:
cols = feature_importance[["feature", "importance"]].groupby("feature" ).mean().sort_values(
 by="importance", ascending=False)[:50].index
best_features = feature_importance.loc[feature_importance.feature.isin(cols)]
plt.figure(figsize=(16, 12)) ;
sns.barplot(x="importance", y="feature", data=best_features.sort_values(by="importance", ascending=False)) ;
plt.title('LGB Features(avg over folds)');
return oof, prediction, feature_importance
return oof, prediction
else:
return oof, prediction
class KernelEvalException(Exception):
pass
@dataclass
class MiFIDEvaluator(object):
model_path: str
train_images_path: str
feature_path: str = None
imsize: int = 64
output_layer: str = 'Pretrained_Net/pool_3:0'
input_layer: str = 'Pretrained_Net/ExpandDims:0'
output_shape: int = 2048
cosine_distance_eps: float = 0.1
batch_size: int = 50
fid_epsilon: float = 1e-14
def __post_init__(self):
tf.reset_default_graph()
self.create_model_graph()
with tf.Session() as sess:
if self.feature_path is None:
self.mu2, self.sigma2, self.features2 = self._handle_path_memorization(
 self.train_images_path, sess, is_checksize=False, is_check_png=False)
 else:
with np.load(self.feature_path)as f:
self.mu2, self.sigma2, self.features2 = f['m'], f['s'], f['features']
def create_model_graph(self):
with tf.gfile.FastGFile(self.model_path, 'rb')as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
 _ = tf.import_graph_def(graph_def, name='Pretrained_Net')
 def img_read_checks(self, filename, is_checksize=False, is_check_png=False):
im = Image.open(str(filename))
if is_checksize and im.size !=(self.imsize, self.imsize):
raise KernelEvalException(f'The images are not of size {check_imsize}')
 if is_check_png and im.format != 'PNG':
raise KernelEvalException('Only PNG images should be submitted.')
 if self.imsize is None:
return im
else:
return im.resize(( self.imsize, self.imsize), Image.ANTIALIAS)
 def _get_model_layer(self, sess):
layer = sess.graph.get_tensor_by_name(self.output_layer)
 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(self, images, sess):
inception_layer = self._get_model_layer(sess)
 n_images = images.shape[0]
if self.batch_size > n_images:
warnings.warn('batch size is bigger than the data size.setting batch size to data size')
 self.batch_size = n_images
n_batches = n_images // self.batch_size + 1
pred_arr = np.empty(( n_images, self.output_shape))
for i in range(n_batches):
start = i * self.batch_size
if start + self.batch_size < n_images:
end = start + self.batch_size
else:
end = n_images
batch = images[start:end]
pred = sess.run(inception_layer, {self.input_layer: batch})
 pred_arr[start:end] = pred.reshape(-1, self.output_shape)
 return pred_arr
def calculate_activation_statistics(self, images, sess):
act = self.get_activations(images, sess)
 mu = np.mean(act, axis=0)
 sigma = np.cov(act, rowvar=False)
 return mu, sigma, act
def _handle_path_memorization(self, path, sess, is_checksize, is_check_png):
path = Path(path)
 files = list(path.glob('*.jpg')) + list(path.glob('*.png'))
x = np.array([np.array(self.img_read_checks(fn, is_checksize, is_check_png)) for fn in files])
 m, s, features = self.calculate_activation_statistics(x, sess)
 del x
return m, s, features
def calculate_frechet_distance(self, mu1, sigma1):
mu1 = np.atleast_1d(mu1)
 mu2 = np.atleast_1d(self.mu2)
 sigma1 = np.atleast_2d(sigma1)
 sigma2 = np.atleast_2d(self.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'
covmean, _ = linalg.sqrtm(sigma1.dot(sigma2), disp=False)
 if not np.isfinite(covmean ).all() :
msg = f'fid calculation produces singular product; adding {self.eps} to diagonal of cov estimates'
warnings.warn(msg)
 offset = np.eye(sigma1.shape[0])* self.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(f'Imaginary component {m}')
 covmean = covmean.real
tr_covmean = np.trace(covmean)
 return(mu1 - mu2 ).dot(mu1 - mu2)+ np.trace(sigma1)+ np.trace(sigma2)- 2 * tr_covmean
@staticmethod
def normalize_rows(x):
return np.nan_to_num(x / np.linalg.norm(x, ord=2, axis=1, keepdims=True))
def cosine_distance(self, features1):
features1_nozero = features1[np.sum(features1, axis=1)!= 0]
features2_nozero = self.features2[np.sum(self.features2, axis=1)!= 0]
norm_f1 = self.normalize_rows(features1_nozero)
 norm_f2 = self.normalize_rows(features2_nozero)
 d = 1.0 - np.abs(np.matmul(norm_f1, norm_f2.T))
mean_min_d = np.mean(np.min(d, axis=1))
return mean_min_d
def calculate_kid_given_paths(self, user_images_unzipped_path):
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
 m1, s1, features1 = self._handle_path_memorization(
 user_images_unzipped_path, sess, is_checksize=True, is_check_png=True)
 fid_value = self.calculate_frechet_distance(m1, s1)
 distance = self.cosine_distance(features1)
 return fid_value, distance
def distance_thresholding(self, d):
if d < self.cosine_distance_eps:
return d
else:
return 1
def evaluate(self, user_images_unzipped_path):
fid_value, distance = self.calculate_kid_given_paths(user_images_unzipped_path)
 distance = self.distance_thresholding(distance)
 return fid_value, distance, fid_value /(distance + self.fid_epsilon )<load_pretrained>
start = time.time()
oof_lgb, prediction_lgb, _ = train_model(X, X_test, y, params=params, model_type='lgb')
 end = time.time()
print("time elapsed:",end - start, "second" )
def zip_images(filename='images.zip'):
z = zipfile.PyZipFile(filename, mode='w')
 for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 z.write(f)
 os.remove(f)
 z.close()
print('Saved final images for submission.')
 def save_images(directory=OUT_DIR):
for k in range(image_sample_size):
generated_image = dog_generator(tf.random.normal([1, noise_dim]), training=False)
 f = str(k)+'.png'
f = os.path.join(directory, f)
 img = np.array(generated_image)
 img =(img[0, :, :, :] + 1.) / 2.
 img = Image.fromarray(( 255*img ).astype('uint8' ).reshape(( image_height,image_width,image_channels)))
 img.save(f,'PNG')
 print('Saved temporary images for evaluation.' )<train_model>
xgb_params = {'eta': 0.01,
'objective': 'reg:linear',
'max_depth': 6,
'min_child_weight': 3,
'subsample': 0.8,
'colsample_bytree': 0.8,
'eval_metric': 'rmse',
'seed': 11,
'silent': True}
start = time.time()
oof_xgb, prediction_xgb = train_model(X, X_test, y, params=xgb_params, model_type='xgb')
 end = time.time()
print("time elapsed:",end - start, "second" )
<train_model><EOS> 
sam_sub['revenue'] = np.expm1(prediction_lgb)
 sam_sub.to_csv("lgb.csv", index=False)
 sam_sub['revenue'] = np.expm1(prediction_xgb)
 sam_sub.to_csv("xgb.csv", index=False)
 sam_sub['revenue'] = np.expm1(( prediction_lgb + prediction_xgb)/ 2)
 sam_sub.to_csv("blend_lgb_xgb.csv", index=False )