Kaggle competition: Natural Language Processing with Disaster Tweets
<SOS> metric: meanfscore Kaggle data source: natural-language-processing-with-disaster-tweets<prepare_x_and_y>
import torch
import pandas as pd
import numpy as np
from transformers import AutoModelForSequenceClassification, AutoTokenizer
x_test = all_data_test.iloc[:,1:].values
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1 )<predict_on_test>
class Dataset:
def __init__(self, text, tokenizer, max_len):
self.text = text
self.tokenizer = tokenizer
self.max_len = max_len
def __len__(self):
return len(self.text)
 def __getitem__(self, item):
text = str(self.text[item])
 inputs = self.tokenizer(
 text,
max_length=self.max_len,
padding="max_length",
truncation=True 
)
 ids = inputs["input_ids"]
mask = inputs["attention_mask"]
return {
"input_ids": torch.tensor(ids, dtype=torch.long),
"attention_mask": torch.tensor(mask, dtype=torch.long),
}
predictions = model.predict_classes(x_test/255.)<save_to_csv>
def generate_predictions(model_path, max_len):
model = AutoModelForSequenceClassification.from_pretrained(model_path)
 tokenizer = AutoTokenizer.from_pretrained(model_path)
 model.to("cuda")
 model.eval()
df = pd.read_csv(".. /input/nlp-getting-started/test.csv")
 dataset = Dataset(text=df.text.values, tokenizer=tokenizer, max_len=max_len)
 data_loader = torch.utils.data.DataLoader(
 dataset, batch_size=32, num_workers=4, pin_memory=True, shuffle=False 
)
 final_output = []
for b_idx, data in enumerate(data_loader):
with torch.no_grad() :
for key, value in data.items() :
data[key] = value.to("cuda")
 output = model(**data)
 output = torch.nn.functional.softmax(output.logits, dim=1)
 output = output.detach().cpu().numpy() [:, 1]
output =(output >= 0.6 ).astype(int ).tolist()
final_output.extend(output)
 torch.cuda.empty_cache()
return np.array(final_output )
output = pd.DataFrame({'id': all_data_test.id,
'label': predictions})
 output.to_csv("submission.csv",index=False )<import_modules>
preds = generate_predictions("abhishek/autonlp-fred2-2682064", max_len=64 )
print(tf.__version__ )<load_from_csv>
sample = pd.read_csv(".. /input/nlp-getting-started/sample_submission.csv")
 sample.target = preds
sample.to_csv("submission.csv", index=False )
<define_variables><EOS> 
sample.target.value_counts()