Skip to content

Commit c3ea5b4

Browse files
committed
Cleaning
1 parent b14ed8c commit c3ea5b4

File tree

2 files changed

+1
-36
lines changed

2 files changed

+1
-36
lines changed

NeuralModels/CaRNet.py

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def train(self, train_set: MyDataset, validation_set: MyDataset, lr: float, epoc
228228
# Initialize Loss: CrossEntropyLoss -> Softmax + NegativeLogLikelihoodLoss
229229
# Q. Why ignore_index is setted to <START> instead of <PAD>?
230230
# A. In the training, both output of the CaRNet and Target is a padded tensor, but when we compute the loss it will evaluate the tensor with pack_padded_sequence.
231-
# And since <START> token is hardcoded as output at t_0 and it is contained into the Target we could avoid the computation of loss on it, since will be 1.
231+
# And since <START> token is hardcoded as output at t_0 and it is contained into the Target we could avoid the computation of loss on it, since will be 0.
232232

233233
criterion = nn.CrossEntropyLoss(ignore_index=vocabulary.predefined_token_idx()["<START>"],reduction="sum").cuda() if self.device.type == "cuda" \
234234
else nn.CrossEntropyLoss(ignore_index=vocabulary.predefined_token_idx()["<START>"],reduction="sum")
@@ -533,29 +533,3 @@ def eval(self, image: object, vocabulary: Vocabulary):
533533

534534
self.switch_mode("training")
535535

536-
# Example of usage
537-
if __name__ == "__main__":
538-
from torch.utils.data import DataLoader
539-
from FactoryModels import *
540-
ds = MyDataset("./dataset", percentage=1)
541-
v = Vocabulary(ds,reload=True)
542-
543-
# Load Encoder and Decoder models
544-
decoder = FactoryDecoder(Decoder.RNetvI)
545-
encoder = FactoryEncoder(Encoder.CResNet50Attention)
546-
547-
dc = ds.get_fraction_of_dataset(percentage=70, delete_transfered_from_source=True)
548-
df = ds.get_fraction_of_dataset(percentage=30, delete_transfered_from_source=True)
549-
# use dataloader facilities which requires a preprocessed dataset
550-
551-
552-
dataloader_training = DataLoader(dc, batch_size=32,
553-
shuffle=True, num_workers=2, collate_fn = lambda data: ds.pack_minibatch_training(data,v))
554-
555-
dataloader_evaluation = DataLoader(df, batch_size=32,
556-
shuffle=True, num_workers=2, collate_fn = lambda data: ds.pack_minibatch_evaluation(data,v))
557-
558-
559-
net = CaRNet(encoder, decoder, "CaRNetvI",1596,512,0,len(v.word2id.keys()),v.embeddings.shape[1],"cuda:0")
560-
#net.load("CaRNetvI")
561-
net.train(dataloader_training,dataloader_evaluation,1e-3,500,v)

NeuralModels/Vocabulary.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,6 @@ def __len__(self):
145145
return len(self.word2id.keys())
146146

147147

148-
# ----------------------------------------------------------------
149-
# Usage example
150-
151-
if __name__ == '__main__':
152-
#Load the vocabulary
153-
pippo = MyDataset(...)
154-
v = Vocabulary(source_dataset=pippo)
155-
# Make a translation
156-
print(v.translate(["I","like","PLay","piano","."]))
157148

158149

159150

0 commit comments

Comments
 (0)