def get_statistics(model, data_loader=None, sequences=False): if data_loader is None: data_loader = test_loader_MNIST() model.eval() num_errs = 0.0 num_examples = 0 results = np.zeros((10, model.output_size), dtype='int32') with torch.no_grad(): for x, y in data_loader: # x = x.to(DEVICE).view(-1, 1, 28, 28).float() y = y.to(DEVICE) predictions = 0 if sequences: x = x.to(DEVICE).view(-1, model.n, 28 * 28).float() outputs = model.forward_sequences(x) _, predictions = outputs.max(dim=2) else: x = x.to(DEVICE).view(-1, 28 * 28).float() outputs = model.forward(x) _, predictions = outputs.data.max(dim=1) for i in range(10): x_ = predictions[y.data == i].cpu().numpy() x_unique, x_unique_count = np.unique(x_, return_counts=True) # x_unique_count = torch.stack([(x_ == x_u).sum() for x_u in x_unique]) for idx, occ in zip(x_unique, x_unique_count): results[i, idx] += occ num_errs += (predictions != y.data).sum().item() num_examples += x.size(0) model.train() return results
def get_statistics(model, data_loader=None, sequences=False, return_ngram=False): if data_loader is None: data_loader = test_loader_MNIST() model.eval() num_errs = 0.0 num_examples = 0 results = np.zeros((10, model.output_size), dtype='int32') stats = defaultdict(lambda: defaultdict(int)) with torch.no_grad(): for x, y in data_loader: # x = x.to(DEVICE).view(-1, 1, 28, 28).float() y = y.to(DEVICE) predictions = 0 if sequences: x = x.to(DEVICE).view(-1, model.n, 28 * 28).float() outputs = model.forward_sequences(x) _, predictions = outputs.max(dim=2) if return_ngram: for i in model.ngram: preds = predictions.cpu().numpy()[(y.cpu().numpy( ) == np.array(i)).prod(1).astype('bool')] if len(preds) > 0: idxs, cnts = np.unique(preds, axis=0, return_counts=True) for idx, cnt in zip(idxs, cnts): stats[tuple(i)][tuple(idx)] += cnt else: x = x.to(DEVICE).view(-1, 28 * 28).float() outputs = model.forward(x) _, predictions = outputs.data.max(dim=1) for i in range(10): x_ = predictions[y.data == i].cpu().numpy() x_unique, x_unique_count = np.unique(x_, return_counts=True) # x_unique_count = torch.stack([(x_ == x_u).sum() for x_u in x_unique]) for idx, occ in zip(x_unique, x_unique_count): results[i, idx] += occ num_errs += (predictions != y.data).sum().item() num_examples += x.size(0) model.train() stats.default_factory = None if return_ngram: return results, stats return results
from src.remote import mpl from src.training import SGD, SPDG from src.statistics import get_statistics import matplotlib.pyplot as plt torch.manual_seed(671470554) np.random.seed(70914308) # %% GENERATING DATASET ngram = Ngram(3) ngram[(0, 1, 2)] = 6. ngram[(1, 2, 3)] = 4. ngram.norm() data_loader = train_loader_MNIST() test_loader = test_loader_MNIST() sequence_loader = sequence_loader_MNIST(ngram, num_samples=40000) # %% REGULAR TRAINING (SGD) # model = Model(ngram) # model.to(DEVICE) # model.init_weights() # optimizer = torch.optim.Adam(model.primal.parameters()) # history = SGD(model, optimizer, data_loader, test_loader, num_epochs=1, log_every=50, test_every=1) # %% DUAL TRAINING model = Model(ngram, output_size=4) model.to(DEVICE) model.init_weights()