Kaggle competition: Generative Dog Images
<SOS> metric: PostProcessorKernel Kaggle data source: generative-dog-images<feature_engineering>
import numpy as np
import pandas as pd
import os
import cv2
import random
import matplotlib.pyplot as plt
from PIL import Image
from skimage.util import random_noise
import xml.etree.ElementTree as ET
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
from torch.autograd import Variable
from collections import OrderedDict
from torch.utils.data import TensorDataset, DataLoader,Dataset
from tqdm import tqdm_notebook as tqdm
%%time
def f_experience(c, s):
it = {'memory':10,
'influence':0.5,
'inference':0.5,
'interest':0.9,
'sentiment':1e-10,
'harmony':0.5}
exp = {}
for i in range(len(c)) :
words = set([w for w in str(c[i] ).lower().split(' ')])
 for w in words:
try:
exp[w]['influence'] = exp[w]['influence'][1:] + [s[i]]
exp[w]['inference'] += 1
exp[w]['interest'] = exp[w]['interest'][1:] + [(exp[w]['interest'][it['memory']-1] +(s[i] * it['interest'])) /2]
exp[w]['sentiment'] += s[i]
except:
m = [0.for m_ in range(it['memory'])]
exp[w] = {}
exp[w]['influence'] = m[1:] + [s[i]]
exp[w]['inference'] = 1
exp[w]['interest'] = m[1:] + [s[i] * it['interest'] / 2]
exp[w]['sentiment'] = s[i]
for w in exp:
exp[w]['sentiment'] /= exp[w]['inference'] + it['sentiment']
exp[w]['inference'] /= len(c)* it['inference']
return exp
exp = f_experience(train['comment_text'].values, train['toxic'].values )<feature_engineering>
def seed_everything(seed=19960720):
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()
%%time
def features(df):
df['len'] = df['comment_text'].map(len)
 df['wlen'] = df['comment_text'].map(lambda x: len(str(x ).split(' ')))
 df['influence_sum'] = df['comment_text'].map(lambda x: np.sum([np.mean(exp[w]['influence'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['influence_mean'] = df['comment_text'].map(lambda x: np.mean([np.mean(exp[w]['influence'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['inference_sum'] = df['comment_text'].map(lambda x: np.sum([exp[w]['inference'] if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['inference_mean'] = df['comment_text'].map(lambda x: np.mean([exp[w]['inference'] if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['interest_sum'] = df['comment_text'].map(lambda x: np.sum([np.mean(exp[w]['interest'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['interest_mean'] = df['comment_text'].map(lambda x: np.mean([np.mean(exp[w]['interest'])if w in exp else 0 for w in str(x ).lower().split(' ')]))
df['sentiment_sum'] = df['comment_text'].map(lambda x: np.sum([exp[w]['sentiment'] if w in exp else 0.5 for w in str(x ).lower().split(' ')]))
df['sentiment_mean'] = df['comment_text'].map(lambda x: np.mean([exp[w]['sentiment'] if w in exp else 0.5 for w in str(x ).lower().split(' ')]))
return df
val = features(val)
 test= features(test )<train_on_grid>
def summary(model, input_size, batch_size=-1, device="cuda"):
def register_hook(module):
def hook(module, input, output):
class_name = str(module.__class__ ).split(".")[-1].split("'")[0]
module_idx = len(summary)
 m_key = "%s-%i" %(class_name, module_idx + 1)
 summary[m_key] = OrderedDict()
summary[m_key]["input_shape"] = list(input[0].size())
 summary[m_key]["input_shape"][0] = batch_size
if isinstance(output,(list, tuple)) :
summary[m_key]["output_shape"] = [
[-1] + list(o.size())[1:] for o in output
]
else:
summary[m_key]["output_shape"] = list(output.size())
 summary[m_key]["output_shape"][0] = batch_size
params = 0
if hasattr(module, "weight")and hasattr(module.weight, "size"):
params += torch.prod(torch.LongTensor(list(module.weight.size())))
 summary[m_key]["trainable"] = module.weight.requires_grad
if hasattr(module, "bias")and hasattr(module.bias, "size"):
params += torch.prod(torch.LongTensor(list(module.bias.size())))
 summary[m_key]["nb_params"] = params
if(
 not isinstance(module, nn.Sequential)
 and not isinstance(module, nn.ModuleList)
 and not(module == model)
):
hooks.append(module.register_forward_hook(hook))
device = device.lower()
assert device in [
"cuda",
"cpu",
], "Input device is not valid, please specify 'cuda' or 'cpu'"
if device == "cuda" and torch.cuda.is_available() :
dtype = torch.cuda.FloatTensor
else:
dtype = torch.FloatTensor
if isinstance(input_size, tuple):
input_size = [input_size]
x = [torch.rand(2, *in_size ).type(dtype)for in_size in input_size]
summary = OrderedDict()
hooks = []
model.apply(register_hook)
 model(*x)
 for h in hooks:
h.remove()
print("----------------------------------------------------------------")
 line_new = "{:>20} {:>25} {:>15}".format("Layer(type)", "Output Shape", "Param
print(line_new)
 print("================================================================")
 total_params = 0
total_output = 0
trainable_params = 0
for layer in summary:
line_new = "{:>20} {:>25} {:>15}".format(
 layer,
str(summary[layer]["output_shape"]),
"{0:,}".format(summary[layer]["nb_params"]), 
)
 total_params += summary[layer]["nb_params"]
total_output += np.prod(summary[layer]["output_shape"])
 if "trainable" in summary[layer]:
if summary[layer]["trainable"] == True:
trainable_params += summary[layer]["nb_params"]
print(line_new)
 total_input_size = abs(np.prod(input_size)* batch_size * 4./(1024 ** 2.))
 total_output_size = abs(2.* total_output * 4./(1024 ** 2.))
 total_params_size = abs(total_params.numpy() * 4./(1024 ** 2.))
 total_size = total_params_size + total_output_size + total_input_size
print("================================================================")
 print("Total params: {0:,}".format(total_params))
print("Trainable params: {0:,}".format(trainable_params))
print("Non-trainable params: {0:,}".format(total_params - trainable_params))
print("----------------------------------------------------------------")
 print("Input size(MB): %0.2f" % total_input_size)
 print("Forward/backward pass size(MB): %0.2f" % total_output_size)
 print("Params size(MB): %0.2f" % total_params_size)
 print("Estimated Total Size(MB): %0.2f" % total_size)
 print("----------------------------------------------------------------")
col = [c for c in val if c not in ['id', 'comment_text', 'toxic']]
x1, x2, y1, y2 = model_selection.train_test_split(val[col], val['toxic'], test_size=0.3, random_state=20)
 model = ensemble.ExtraTreesClassifier(n_estimators=1000, max_depth=7, n_jobs=-1, random_state=20)
 model.fit(x1, y1)
 print(metrics.roc_auc_score(y2, model.predict_proba(x2)[:,1].clip(0.,1.)))
model.fit(val[col], val['toxic'])
 test['toxic'] = model.predict_proba(test[col])[:,1].clip(0.,1.)
sub1 = test[['id', 'toxic']]<save_to_csv>
class Generator(nn.Module):
def __init__(self, nz=128, channels=3):
super(Generator, self ).__init__()
self.nz = nz
self.channels = channels
def convlayer(n_input, n_output, k_size=4, stride=2, padding=0):
block = [
nn.ConvTranspose2d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False),
nn.BatchNorm2d(n_output),
nn.ReLU(inplace=True),
]
return block
self.model = nn.Sequential(
 *convlayer(self.nz, 1024, 4, 1, 0),
*convlayer(1024, 512, 4, 2, 1),
*convlayer(512, 256, 4, 2, 1),
*convlayer(256, 128, 4, 2, 1),
*convlayer(128, 64, 4, 2, 1),
nn.ConvTranspose2d(64, self.channels, 3, 1, 1),
nn.Tanh() 
)
 def forward(self, z):
z = z.view(-1, self.nz, 1, 1)
 img = self.model(z)
 return img
sub1.rename(columns={'toxic':'toxic1'}, inplace=True)
 sub2.rename(columns={'toxic':'toxic2'}, inplace=True)
 sub3 = pd.merge(sub1, sub2, how='left', on='id')
 sub3['toxic'] =(sub3['toxic1'] * 0.1)+(sub3['toxic2'] * 0.9)
 sub3['toxic'] =(sub3['toxic2'] * 0.51)+(sub3['toxic'] * 0.49)
 sub3[['id', 'toxic']].to_csv('submission.csv', index=False )<prepare_output>
class Discriminator(nn.Module):
def __init__(self, channels=3):
super(Discriminator, self ).__init__()
self.channels = channels
def convlayer(n_input, n_output, k_size=4, stride=2, padding=0, bn=False):
block = [nn.Conv2d(n_input, n_output, kernel_size=k_size, stride=stride, padding=padding, bias=False)]
if bn:
block.append(nn.BatchNorm2d(n_output))
block.append(nn.LeakyReLU(0.2, inplace=True))
return block
self.model = nn.Sequential(
 *convlayer(self.channels, 64, 3, 1, 1),
*convlayer(64, 128, 4, 2, 1),
*convlayer(128, 256, 4, 2, 1, bn=True),
*convlayer(256, 512, 4, 2, 1, bn=True),
*convlayer(512, 1024, 4, 2, 1, bn=True),
nn.Conv2d(1024, 1, 4, 1, 0, bias=False), 
)
 def forward(self, imgs):
logits = self.model(imgs)
 out = torch.sigmoid(logits)
 return out.view(-1, 1 )
test = pd.DataFrame(['Howling with Wolf on Lügenpresse'], columns=['comment_text'])
 test['id'] = test.index
test= features(test)
 test['toxic'] = model.predict_proba(test[col])[:,1].clip(0.,1.)
test[['id', 'comment_text', 'toxic']].head()<define_variables>
image = glob.glob('.. /input/all-dogs/all-dogs/*')
 breed = glob.glob('.. /input/annotation/Annotation/*')
 annot = glob.glob('.. /input/annotation/Annotation/*/*')
 print(len(image), len(breed), len(annot))
MAX_LEN = 192
DROPOUT = 0.5
BATCH_SIZE = 16
TOTAL_STEPS_STAGE1 = 2000
VALIDATE_EVERY_STAGE1 = 200
TOTAL_STEPS_STAGE2 = 200
VALIDATE_EVERY_STAGE2 = 10
LR_TRANSFORMER = 5e-6
LR_HEAD = 1e-3
PRETRAINED_TOKENIZER= 'jplu/tf-xlm-roberta-large'
PRETRAINED_MODEL = '/kaggle/input/jigsaw-mlm-finetuned-xlm-r-large'
D = '/kaggle/input/jigsaw-multilingual-toxic-comment-classification/'
D_TRANS = '/kaggle/input/jigsaw-train-multilingual-coments-google-api/'
print(tf.__version__)
 logging.getLogger().setLevel(logging.NOTSET)
 AUTO = tf.data.experimental.AUTOTUNE<define_search_model>
def get_image(annot):

 img_path = '.. /input/all-dogs/all-dogs/'
file = annot.split('/')
 img_class = file[-2]
img_filename = img_path+file[-1]+'.jpg'
return img_filename, img_class
def connect_to_TPU() :

 try:
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
print('Running on TPU ', tpu.master())
 except ValueError:
tpu = None
if tpu:
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu)
 else:
strategy = tf.distribute.get_strategy()
global_batch_size = BATCH_SIZE * strategy.num_replicas_in_sync
return tpu, strategy, global_batch_size
tpu, strategy, global_batch_size = connect_to_TPU()
print("REPLICAS: ", strategy.num_replicas_in_sync )<load_from_csv>
select_dogs = []
select_bbox = []
def load_jigsaw_trans(langs=['tr','it','es','ru','fr','pt'],
columns=['comment_text', 'toxic']):
train_6langs=[]
for i in range(len(langs)) :
fn = D_TRANS+'jigsaw-toxic-comment-train-google-%s-cleaned.csv'%langs[i]
train_6langs.append(downsample(pd.read_csv(fn)[columns]))
return train_6langs
def downsample(df):

 ds_df= pd.concat([
df.query('toxic==1'),
df.query('toxic==0' ).sample(sum(df.toxic))
])
 return ds_df
train_df = pd.concat(load_jigsaw_trans())
 val_df = pd.read_csv(D+'validation.csv')
 test_df = pd.read_csv(D+'test.csv')
 sub_df = pd.read_csv(D+'sample_submission.csv' )<categorify>
def get_class_dict(breed):
class_dict = {}
for i in range(len(breed)) :
class_ = breed[i].split('/')
 class_dict[class_[-1]] = i
return class_dict
class_dict = get_class_dict(breed )
%%time
def regular_encode(texts, tokenizer, maxlen=512):
enc_di = tokenizer.batch_encode_plus(
 texts,
return_attention_masks=False,
return_token_type_ids=False,
pad_to_max_length=True,
max_length=maxlen 
)
 return np.array(enc_di['input_ids'])
 tokenizer = AutoTokenizer.from_pretrained(PRETRAINED_TOKENIZER)
 X_train = regular_encode(train_df.comment_text.values, tokenizer, maxlen=MAX_LEN)
 X_val = regular_encode(val_df.comment_text.values, tokenizer, maxlen=MAX_LEN)
 X_test = regular_encode(test_df.content.values, tokenizer, maxlen=MAX_LEN)
 y_train = train_df.toxic.values.reshape(-1,1)
 y_val = val_df.toxic.values.reshape(-1,1 )<create_dataframe>
def get_cleaned_data(image, annot, class_dict):
select_dogs = []
select_bbox = []
select_labels = []
for i in range(len(image)) :
bbox = get_bbox(annot[i])
 bbox = bbox[0]
dog, dog_class = get_image(annot[i])
 if dog == '.. /input/all-dogs/all-dogs/n02105855_2933.jpg':
continue
im = Image.open(dog)
 xdiff = abs(bbox[2] - bbox[0])
 ydiff = abs(bbox[3] - bbox[1])
 if(( abs(ydiff - xdiff)/min(ydiff, xdiff)<=0.5)and(( xdiff>=64)and(ydiff>=64))):
select_dogs.append(dog)
 select_bbox.append(bbox)
 select_labels.append(class_dict[dog_class])
 return select_dogs, select_bbox, select_labels
select_dogs, select_bbox, select_labels = get_cleaned_data(image, annot, class_dict )
def create_dist_dataset(X, y=None, training=False):
dataset = tf.data.Dataset.from_tensor_slices(X)
 if y is not None:
dataset_y = tf.data.Dataset.from_tensor_slices(y)
 dataset = tf.data.Dataset.zip(( dataset, dataset_y))
if training:
dataset = dataset.shuffle(len(X)).repeat()
dataset = dataset.batch(global_batch_size ).prefetch(AUTO)
 dist_dataset = strategy.experimental_distribute_dataset(dataset)
 return dist_dataset
train_dist_dataset = create_dist_dataset(X_train, y_train, True)
 val_dist_dataset = create_dist_dataset(X_val)
 test_dist_dataset = create_dist_dataset(X_test )<choose_model_class>
def prepare_labels(y):
values = np.array(y)
 onehot_encoder = OneHotEncoder(sparse=False, categories='auto')
 onehot_encoded = onehot_encoder.fit_transform(values.reshape(values.shape[0], 1))
y = onehot_encoded
return y
labels_encoded = prepare_labels(select_labels )
%%time
def create_model_and_optimizer() :
with strategy.scope() :
transformer_layer = TFRobertaModel.from_pretrained(PRETRAINED_MODEL)
 model = build_model(transformer_layer)
 optimizer_transformer = Adam(learning_rate=LR_TRANSFORMER)
 optimizer_head = Adam(learning_rate=LR_HEAD)
 return model, optimizer_transformer, optimizer_head
def build_model(transformer):
inp = Input(shape=(MAX_LEN,), dtype=tf.int32, name="input_word_ids")
 x = transformer(inp)[0][:, 0, :]
x = Dropout(DROPOUT )(x)
 out = Dense(1, activation='sigmoid', name='custom_head' )(x)
 model = Model(inputs=[inp], outputs=[out])
 return model
model, optimizer_transformer, optimizer_head = create_model_and_optimizer()
model.summary()<compute_train_metric>
from PIL import Image
from skimage import util
def define_losses_and_metrics() :
with strategy.scope() :
loss_object = tf.keras.losses.BinaryCrossentropy(
 reduction=tf.keras.losses.Reduction.NONE, from_logits=False)
 def compute_loss(labels, predictions):
per_example_loss = loss_object(labels, predictions)
 loss = tf.nn.compute_average_loss(
 per_example_loss, global_batch_size = global_batch_size)
 return loss
train_accuracy_metric = tf.keras.metrics.AUC(name='training_AUC')
 return compute_loss, train_accuracy_metric
def train(train_dist_dataset, val_dist_dataset=None, y_val=None,
total_steps=2000, validate_every=200):
best_weights, history = None, []
step = 0
for tensor in train_dist_dataset:
distributed_train_step(tensor)
 step+=1
if(step % validate_every == 0):
train_metric = train_accuracy_metric.result().numpy()
print("Step %d, train AUC: %.5f" %(step, train_metric))
if val_dist_dataset:
val_metric = roc_auc_score(y_val, predict(val_dist_dataset))
print("Step %d, val AUC: %.5f" %(step,val_metric))
history.append(val_metric)
 if history[-1] == max(history):
best_weights = model.get_weights()
train_accuracy_metric.reset_states()
if step == total_steps:
break
model.set_weights(best_weights)
 @tf.function
def distributed_train_step(data):
strategy.experimental_run_v2(train_step, args=(data,))
def train_step(inputs):
features, labels = inputs
transformer_trainable_variables = [ v for v in model.trainable_variables
if(( 'pooler' not in v.name)and 
('custom' not in v.name)) ]
head_trainable_variables = [ v for v in model.trainable_variables
if 'custom' in v.name]
with tf.GradientTape(persistent=True)as tape:
predictions = model(features, training=True)
 loss = compute_loss(labels, predictions)
 gradients_transformer = tape.gradient(loss, transformer_trainable_variables)
 gradients_head = tape.gradient(loss, head_trainable_variables)
 del tape
optimizer_transformer.apply_gradients(zip(gradients_transformer,
transformer_trainable_variables))
optimizer_head.apply_gradients(zip(gradients_head,
head_trainable_variables))
train_accuracy_metric.update_state(labels, predictions)
 def predict(dataset):
predictions = []
for tensor in dataset:
predictions.append(distributed_prediction_step(tensor))
predictions = np.vstack(list(map(np.vstack,predictions)))
 return predictions
@tf.function
def distributed_prediction_step(data):
predictions = strategy.experimental_run_v2(prediction_step, args=(data,))
return strategy.experimental_local_results(predictions)
 def prediction_step(inputs):
features = inputs
predictions = model(features, training=False)
 return predictions
compute_loss, train_accuracy_metric = define_losses_and_metrics()<train_model>
class dogs_Dataset(Dataset):
def __init__(self, datafolder, datatype='train', index=[], \
transform = transforms.Compose([transforms.RandomResizedCrop(128),transforms.ToTensor() ])) :
self.datafolder = datafolder
self.datatype = datatype
self.image_files_list = [select_dogs[i] for i in index]
self.bbox_list = [select_bbox[i] for i in index]
self.label_list = [labels_encoded[i] for i in index]
self.transform = transform
self.imgs = []
for idx in range(len(self.image_files_list)) :
img_name = self.image_files_list[idx]
bbox = self.bbox_list[idx]
img = cv2.imread(img_name, 1)[bbox[1]:bbox[3], bbox[0]:bbox[2]]
mean, var = 0, 0.01
noise = np.random.normal(mean, var ** 0.5, img.shape)
 out = img + noise
if out.min() < 0:
low_clip = -1.
 else:
low_clip = 0.
 out = np.clip(out, low_clip, 1.0)
 out = np.uint8(out*255)
 image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
self.imgs.append(image)
 def __len__(self):
return len(self.imgs)
 def __getitem__(self, idx):
image = self.imgs[idx]
image = self.transform(image)
 if self.datatype == 'train':
label = self.label_list[idx]
else:
label = torch.zeros(1)
 return image, label
%%time
train(train_dist_dataset, val_dist_dataset, y_val,
TOTAL_STEPS_STAGE1, VALIDATE_EVERY_STAGE1 )<split>
imgs, label = next(iter(train_loader))
imgs = imgs.numpy().transpose(0, 2, 3, 1 )
%%time
optimizer_head.learning_rate.assign(1e-4)
 X_train, X_val, y_train, y_val = train_test_split(X_val, y_val, test_size = 0.1)
 train_dist_dataset = create_dist_dataset(X_train, y_train, training=True)
 val_dist_dataset = create_dist_dataset(X_val, y_val)
 train(train_dist_dataset, val_dist_dataset, y_val,
total_steps = TOTAL_STEPS_STAGE2,
validate_every = VALIDATE_EVERY_STAGE2 )<save_to_csv>
LR_G = 0.0005
LR_D = 0.0005
beta1 = 0.5
real_label = 0.8
fake_label = 0.0
nz = 128
device = torch.device("cuda" if torch.cuda.is_available() else "cpu" )
%%time
sub_df['toxic'] = predict(test_dist_dataset)[:,0]
sub_df.to_csv('submission.csv', index=False )<choose_model_class>
netG = Generator(nz ).to(device)
 netD = Discriminator().to(device)
 criterion = nn.BCELoss()
optimizerD = optim.Adam(netD.parameters() , lr=LR_D, betas=(beta1, 0.999))
optimizerG = optim.Adam(netG.parameters() , lr=LR_G, betas=(beta1, 0.999))
fixed_noise = torch.randn(25, nz, 1, 1, device=device )
AUTO = tf.data.experimental.AUTOTUNE
tpu = tf.distribute.cluster_resolver.TPUClusterResolver()
tf.config.experimental_connect_to_cluster(tpu)
 tf.tpu.experimental.initialize_tpu_system(tpu)
 tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
 print(tpu_strategy.num_replicas_in_sync )<define_variables>
epochs = 600
MY_GCS_PATH=KaggleDatasets().get_gcs_path('dataencoding' )<categorify>
for epoch in range(epochs):
for ii,(real_images, train_labels)in enumerate(train_loader):
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)
 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)
 output = netD(fake.detach())
 errD_fake = criterion(output, labels)
 errD_fake.backward()
D_G_z1 = output.mean().item()
errD = errD_real + errD_fake
optimizerD.step()
netG.zero_grad()
labels.fill_(real_label)
 output = netD(fake)
 errG = criterion(output, labels)
 errG.backward()
D_G_z2 = output.mean().item()
optimizerG.step()
if(ii+1)%(len(train_loader)//2)== 0:
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)) : %.4f / %.4f'
%(epoch + 1, epochs, ii+1, len(train_loader),
errD.item() , errG.item() , D_x, D_G_z1, D_G_z2))
data_path = ".. /input/dataencoding/"
train_ids = np.load(os.path.join(data_path, "ids.npy"))
train_labels = np.load(os.path.join(data_path, "labels.npy")).astype(int)
 val_ids = np.load(os.path.join(data_path, "val_ids.npy"))
val_labels = np.load(os.path.join(data_path, "val_labels.npy"))<define_variables>
def truncated_normal(size, threshold=1):
values = truncnorm.rvs(-threshold, threshold, size=size)
 return values
MODEL = "jplu/tf-xlm-roberta-large"
SEQUENCE_LENGTH = 192
BATCH_SIZE = 16 * tpu_strategy.num_replicas_in_sync<filter>
gen_z = torch.randn(32, nz, 1, 1, device=device)
 gen_images = netG(gen_z ).to("cpu" ).clone().detach()
gen_images = gen_images.numpy().transpose(0, 2, 3, 1 )
<create_dataframe><EOS> 
if not os.path.exists('.. /output_images'):
os.mkdir('.. /output_images')
 im_batch_size = 50
n_images=10000
for i_batch in range(0, n_images, im_batch_size):
z = truncated_normal(( im_batch_size, nz, 1, 1), threshold=1)
 gen_z = torch.from_numpy(z ).float().to(device)
 gen_images =(netG(gen_z)+1.) /2.
 images = gen_images.to("cpu" ).clone().detach()
images = images.numpy().transpose(0, 2, 3, 1)
 for i_image in range(gen_images.size(0)) :
save_image(gen_images[i_image, :, :, :], os.path.join('.. /output_images', f'image_{i_batch+i_image:05d}.png'))
shutil.make_archive('images', 'zip', '.. /output_images' )