CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Natural Language Processing with Sequence Models/Week 2 - Recureent Neural Networks for Language Modelling/utils.py
Views: 13373
1
import numpy as np
2
import torch
3
import torch.nn as nn
4
import torch.nn.functional as F
5
import torch.optim as optim
6
7
def get_batch(source, i):
8
'''
9
returns a batch
10
'''
11
bptt = 35
12
seq_len = min(bptt, len(source) - 1 - i)
13
data = source[i:i+seq_len]
14
target = source[i+1:i+1+seq_len].view(-1)
15
16
return data, target
17
18
19
def batchify(data, bsz):
20
# Work out how cleanly we can divide the dataset into bsz parts.
21
nbatch = data.size(0) // bsz
22
# Trim off any extra elements that wouldn't cleanly fit (remainders).
23
data = data.narrow(0, 0, nbatch * bsz)
24
# Evenly divide the data across the bsz batches.
25
data = data.view(bsz, -1).t().contiguous()
26
return data
27
28
29
# to detach the hidden state from the graph.
30
def detach(hidden):
31
"""
32
This function detaches every single tensor.
33
"""
34
if isinstance(hidden, torch.Tensor):
35
return hidden.detach()
36
else:
37
return tuple(detach(v) for v in hidden)
38