<jupyter_start><jupyter_text>Load the data<jupyter_code>path = untar_data(URLs.IMDB_SAMPLE)
path.ls()
df = pd.read_csv(path/'texts.csv')
df.head()
model_cls = AutoModelForSequenceClassification
print(BLURR.get_architectures())
pretrained_model_name = 'distilroberta-base'
doc(BLURR.get_hf_objects)
num_labels = 2
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, model_cls=model_cls, config_kwargs={'num_labels': num_labels})
print(hf_arch)
print(type(hf_config))
print(type(hf_tokenizer))
print(type(hf_model))
print(hf_config)<jupyter_output>RobertaConfig {
  "_name_or_path": "distilroberta-base",
  "architectures": [
    "RobertaForMaskedLM"
  ],
  "attention_probs_dropout_prob": 0.1,
  "bos_token_id": 0,
  "classifier_dropout": null,
  "eos_token_id": 2,
  "gradient_checkpointing": false,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "layer_norm_eps": 1e-05,
  "max_position_embeddings": 514,
  "model_type": "roberta",
  "num_attention_heads": 12,
  "num_hidden_layers": 6,
  "pad_token_id": 1,
  "position_embedding_type": "absolute",
  "transformers_version": "4.10.0",
  "type_vocab_size": 1,
  "use_cache": true,
  "vocab_size": 50265
}<jupyter_text>DataBlock API<jupyter_code>doc(HF_TextBlock)
blocks = (HF_TextBlock(hf_arch, hf_config, hf_tokenizer, hf_model, ),
          CategoryBlock)
dblock = DataBlock(blocks=blocks, 
                   get_x=ColReader('text'), 
                   get_y=ColReader('label'), 
                   splitter=ColSplitter(col='is_valid')
                   )<jupyter_output><empty_output><jupyter_text>HF_BeforeBatchTransforms gives for each sample input_ids (padding), attention_mask<jupyter_code>dblock.summary(df)
dls = dblock.dataloaders(source=df, verbose=True, bs=4)
b = dls.one_batch()
len(b), type(b[0]), type(b[1])
b[0]
dls.show_batch(dataloaders=dls, max_n=2, trunc_at=500)<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>doc(HF_BaseModelWrapper)
model = HF_BaseModelWrapper(hf_model)
doc(Learner)
#del learn
learn = Learner(dls, model, 
                opt_func=partial(OptimWrapper, opt=torch.optim.Adam), #partial(Adam)
                loss_func=CrossEntropyLossFlat(), 
                metrics=[accuracy], 
                cbs=[HF_BaseModelCallback], 
                splitter=hf_splitter).to_fp16()
learn.freeze()
#learn.summary() #Q: What is the purpose of HF_BaseModelWrapper, HF_BaseModelCallback, hf_splitter
print(len(learn.opt.param_groups))
learn.lr_find(suggest_funcs=[minimum, steep, valley, slide])
learn.fit_one_cycle(3, lr_max=slice(5e-4))
learn.unfreeze()
learn.lr_find(suggest_funcs=[minimum, steep, valley, slide])
learn.fit_one_cycle(2, lr_max=1e-5)
learn.show_results(learner=learn, max_n=3, trunc_at=500)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>learn.blurr_predict("excellent movie, loved the whole plot")
learn.blurr_predict(["the movie plot has lot of holes in it, hated the songs", "excellent movie, loved the whole plot"])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1324333011893282, 'fraction_numerical': 0.01832208293153327, 'mean_word_length': 5.854625550660793, 'pattern_counts': {'":': 23, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '555263', 'license': 'Apache-2.0', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1174, 'n_words': 175, 'path': '08-blurr_hf_fastai_imdb_sequence_classification.ipynb', 'repo_name': 'manisnesan/fastchai', 'size': '3111'}