Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterate through all of dataloader each epoch. #65

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 12 additions & 23 deletions sparsecoding/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,18 @@ def learn_dictionary(self, dataset, n_epoch, batch_size):
losses = []

dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
iterloader = iter(dataloader)
for i in range(n_epoch):
try:
batch = next(iterloader)
except StopIteration:
dataloader = DataLoader(dataset, batch_size=batch_size,
shuffle=True)
iterloader = iter(dataloader)
batch = next(iterloader)

# infer coefficients
a = self.inference_method.infer(batch, self.dictionary)

# update dictionary
self.update_dictionary(batch, a)

# normalize dictionary
self.normalize_dictionary()

# compute current loss
loss = self.compute_loss(batch, a)

losses.append(loss)
for _ in range(n_epoch):
loss = 0.0
for batch in dataloader:
# infer coefficients
a = self.inference_method.infer(batch, self.dictionary)
# update dictionary
self.update_dictionary(batch, a)
# normalize dictionary
self.normalize_dictionary()
# compute current loss
loss += self.compute_loss(batch, a)
losses.append(loss/len(dataloader))
return np.asarray(losses)

def compute_loss(self, data, a):
Expand Down
Loading