parameters_cached = [x for x in parameters()] optim = torch.optim.SGD(parameters(), lr=args.learning_rate, momentum=0.0) # 0.02, 0.9 named_modules = {"rnn" : rnn, "output" : output, "char_embeddings" : char_embeddings, "optim" : optim} if args.load_from is not None: checkpoint = torch.load("/checkpoint/mhahn/"+args.load_from+".pth.tar") for name, module in named_modules.items(): module.load_state_dict(checkpoint[name]) from torch.autograd import Variable data = AcqdivReaderPartition(acqdivCorpusReader, partition="train").reshuffledIterator(blankBeforeEOS=False, originalIterator=AcqdivReader.iteratorMorph) numeric_with_blanks = [] indices_with_blanks = [] count = 0 print("Prepare chunks") for chunk in data: indices_with_blanks.append((chunk, -1)) numeric_with_blanks.append(stoi[" "]+3) countInChunk = 0 for char in chunk[0]: # print((char if char != "\n" else "\\n", stoi[char]+3 if char in stoi else 2)) indices_with_blanks.append((chunk, countInChunk))
optim = torch.optim.SGD(parameters(), lr=args.learning_rate, momentum=0.0) # 0.02, 0.9 named_modules = {"rnn" : rnn, "output" : output, "char_embeddings" : char_embeddings, "optim" : optim} # Load the model from the checkpoint if args.load_from is not None: checkpoint = torch.load(MODELS_HOME+"/"+args.load_from+".pth.tar") for name, module in named_modules.items(): module.load_state_dict(checkpoint[name]) from torch.autograd import Variable data = AcqdivReaderPartition(acqdivCorpusReader, partition="train").reshuffledIterator(blankBeforeEOS=False) # Read the data into a list of integers numeric_with_blanks = [] count = 0 print("Prepare chunks") for chunk in data: numeric_with_blanks.append(stoi[" "]+3) for char in chunk: count += 1 if char not in stoi: print(char) numeric_with_blanks.append(stoi[char]+3 if char in stoi else 2) # select a portion
def backward(loss, printHere): optim.zero_grad() if printHere: print(loss) loss.backward() torch.nn.utils.clip_grad_value_(parameters_cached, 5.0) #, norm_type="inf") optim.step() import time devLosses = [] for epoch in range(10000): print(epoch) training_data = AcqdivReaderPartition(acqdivCorpusReader, "train").reshuffledIterator() print("Got data") training_chars = prepareDatasetChunks(training_data, train=True) rnn_drop.train(True) startTime = time.time() trainChars = 0 counter = 0 while True: counter += 1 try: numeric = [next(training_chars) for _ in range(args.batchSize)] except StopIteration: break printHere = (counter % 50 == 0) loss, charCounts = forward(numeric, printHere=printHere, train=True)