Code Files for the Udemy Course: The Complete Neural Networks Bootcamp: Theory, Applications
You may find the course here
Note that the code for building a Chatbot using Transformers is included in a seperate repository here
Code Files for the Udemy Course: The Complete Neural Networks Bootcamp: Theory, Applications
Hi,
I think the code in RNN_Text Generation notebook is too complicated. You have to read the text file twice.
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.idx = 0
def add_word(self, word):
if word not in self.word2idx:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
def __len__(self):
return len(self.word2idx)
class TextProcess(object):
def __init__(self):
self.dictionary = Dictionary()
def get_data(self, path, batch_size=20):
with open(path, 'r') as f:
tokens = 0
for line in f:
words = line.split() + ['<eos>']
tokens += len(words)
for word in words:
self.dictionary.add_word(word)
#Create a 1-D tensor that contains the index of all the words in the file
rep_tensor = torch.LongTensor(tokens)
index = 0
with open(path, 'r') as f:
for line in f:
words = line.split() + ['<eos>']
for word in words:
rep_tensor[index] = self.dictionary.word2idx[word]
index += 1
#Find out how many batches we need
num_batches = rep_tensor.shape[0] // batch_size
#Remove the remainder (Filter out the ones that don't fit)
rep_tensor = rep_tensor[:num_batches*batch_size]
# return (batch_size,num_batches)
rep_tensor = rep_tensor.view(batch_size, -1)
return rep_tensor
Here is my suggestion:
class Dictionary(object):
def __init__(self):
self.word2idx = {}
self.idx2word = {}
self.idx = 0
def add_word(self, word):
if word not in self.word2idx:
self.word2idx[word] = self.idx
self.idx2word[self.idx] = word
self.idx += 1
return self.word2idx[word]
def __len__(self):
return len(self.word2idx)
class TextProcess(object):
def __init__(self):
self.dictionary = Dictionary()
def get_data(self, path, batch_size=20):
indices = []
with open(path, 'r') as f:
for line in f:
words = line.split() + ['<eos>']
for word in words:
idx = self.dictionary.add_word(word)
indices.append(idx)
# Create a 1D tensor that contains the index of all the words in the file
rep_tensor = torch.LongTensor(indices)
# Find out how many batches we need
num_batches = rep_tensor.shape[0] // batch_size
# Remove the remainder
rep_tensor = rep_tensor[:num_batches * batch_size]
# Return (batch_size, num_batches)
rep_tensor = rep_tensor.view(batch_size, -1)
return rep_tensor
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.