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

[PyTorch] y.size().numel() -> y.numel() #1666

Merged
merged 1 commit into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion chapter_linear-networks/softmax-regression-scratch.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ def train_epoch_ch3(net, train_iter, loss, updater): #@save
l.backward()
updater.step()
metric.add(float(l) * len(y), accuracy(y_hat, y),
y.size().numel())
y.numel())
else:
# Using custom built optimizer & loss criterion
l.sum().backward()
Expand Down
24 changes: 2 additions & 22 deletions d2l/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import time
import zipfile
from collections import defaultdict

import pandas as pd
import requests
from IPython import display
Expand Down Expand Up @@ -270,9 +269,7 @@ def train_epoch_ch3(net, train_iter, loss, updater):
updater.zero_grad()
l.backward()
updater.step()
metric.add(
float(l) * len(y), accuracy(y_hat, y),
y.size().numel())
metric.add(float(l) * len(y), accuracy(y_hat, y), y.numel())
else:
# Using custom built optimizer & loss criterion
l.sum().backward()
Expand Down Expand Up @@ -407,7 +404,6 @@ def download_extract(name, folder=None):
fp.extractall(base_dir)
return os.path.join(base_dir, folder) if folder else data_dir


def download_all():
"""Download all files in the DATA_HUB."""
for name in DATA_HUB:
Expand All @@ -429,7 +425,6 @@ def try_gpu(i=0):
return torch.device(f'cuda:{i}')
return torch.device('cpu')


def try_all_gpus():
"""Return all available GPUs, or [cpu(),] if no GPU exists."""
devices = [
Expand Down Expand Up @@ -528,7 +523,6 @@ def __init__(self, input_channels, num_channels, use_1x1conv=False,
self.conv3 = None
self.bn1 = nn.BatchNorm2d(num_channels)
self.bn2 = nn.BatchNorm2d(num_channels)
self.relu = nn.ReLU(inplace=True)

def forward(self, X):
Y = F.relu(self.bn1(self.conv1(X)))
Expand All @@ -543,7 +537,6 @@ def forward(self, X):
d2l.DATA_HUB['time_machine'] = (d2l.DATA_URL + 'timemachine.txt',
'090b5e7e70c295757f55df93cb0a180b9691891a')


def read_time_machine():
"""Load the time machine dataset into a list of text lines."""
with open(d2l.download('time_machine'), 'r') as f:
Expand Down Expand Up @@ -597,7 +590,6 @@ def to_tokens(self, indices):
return self.idx_to_token[indices]
return [self.idx_to_token[index] for index in indices]


def count_corpus(tokens):
"""Count token frequencies."""
# Here `tokens` is a 1D list or 2D list
Expand Down Expand Up @@ -844,7 +836,6 @@ def begin_state(self, device, batch_size=1):
d2l.DATA_HUB['fra-eng'] = (d2l.DATA_URL + 'fra-eng.zip',
'94646ad1522d915e7b0f9296181140edcf86a4f5')


def read_data_nmt():
"""Load the English-French dataset."""
data_dir = d2l.download_extract('fra-eng')
Expand Down Expand Up @@ -1116,7 +1107,7 @@ def show_heatmaps(matrices, xlabel, ylabel, titles=None, figsize=(2.5, 2.5),
ax.set_ylabel(ylabel)
if titles:
ax.set_title(titles[j])
fig.colorbar(pcm, ax=axes, shrink=0.6)
fig.colorbar(pcm, ax=axes, shrink=0.6);


# Defined in file: ./chapter_attention-mechanisms/attention-scoring-functions.md
Expand Down Expand Up @@ -1256,7 +1247,6 @@ def transpose_qkv(X, num_heads):
# `num_hiddens` / `num_heads`)
return X.reshape(-1, X.shape[2], X.shape[3])


def transpose_output(X, num_heads):
"""Reverse the operation of `transpose_qkv`"""
X = X.reshape(-1, num_heads, X.shape[1], X.shape[2])
Expand Down Expand Up @@ -1375,7 +1365,6 @@ def train_2d(trainer, steps=20):
results.append((x1, x2))
return results


def show_trace_2d(f, results):
"""Show the trace of 2D variables during optimization."""
d2l.set_figsize()
Expand All @@ -1391,7 +1380,6 @@ def show_trace_2d(f, results):
d2l.DATA_HUB['airfoil'] = (d2l.DATA_URL + 'airfoil_self_noise.dat',
'76e5be1548fd8222e5074cf0faae75edff8cf93f')


def get_data_ch11(batch_size=10, n=1500):
data = np.genfromtxt(d2l.download('airfoil'), dtype=np.float32,
delimiter='\t')
Expand Down Expand Up @@ -1578,7 +1566,6 @@ def box_corner_to_center(boxes):
boxes = d2l.stack((cx, cy, w, h), axis=-1)
return boxes


def box_center_to_corner(boxes):
"""Convert from (center, width, height) to (upper_left, bottom_right)"""
cx, cy, w, h = boxes[:, 0], boxes[:, 1], boxes[:, 2], boxes[:, 3]
Expand Down Expand Up @@ -1776,7 +1763,6 @@ def nms(boxes, scores, iou_threshold):
B = B[inds + 1]
return d2l.tensor(keep, device=boxes.device)


def multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5,
pos_threshold=0.00999999978):
device, batch_size = cls_probs.device, cls_probs.shape[0]
Expand Down Expand Up @@ -1835,7 +1821,6 @@ def read_data_bananas(is_train=True):
targets.append(list(target))
return images, torch.tensor(targets).unsqueeze(1) / 256


class BananasDataset(torch.utils.data.Dataset):
def __init__(self, is_train):
self.features, self.labels = read_data_bananas(is_train)
Expand All @@ -1848,7 +1833,6 @@ def __getitem__(self, idx):
def __len__(self):
return len(self.features)


def load_data_bananas(batch_size):
"""Load the bananas dataset."""
train_iter = torch.utils.data.DataLoader(BananasDataset(is_train=True),
Expand Down Expand Up @@ -1906,7 +1890,6 @@ def build_colormap2label():
colormap[2]] = i
return colormap2label


def voc_label_indices(colormap, colormap2label):
"""Map an RGB color to a label."""
colormap = colormap.permute(1, 2, 0).numpy().astype('int32')
Expand Down Expand Up @@ -1993,7 +1976,6 @@ def copyfile(filename, target_dir):
os.makedirs(target_dir, exist_ok=True)
shutil.copy(filename, target_dir)


def reorg_train_valid(data_dir, labels, valid_ratio):
# The number of examples of the class with the least examples in the
# training dataset
Expand Down Expand Up @@ -2039,7 +2021,6 @@ def reorg_test(data_dir):
d2l.DATA_HUB['ptb'] = (d2l.DATA_URL + 'ptb.zip',
'319d85e578af0cdc590547f26231e4e31cdf1e42')


def read_ptb():
data_dir = d2l.download_extract('ptb')
with open(os.path.join(data_dir, 'ptb.train.txt')) as f:
Expand Down Expand Up @@ -2328,7 +2309,6 @@ def forward(self, tokens, segments, valid_lens=None, pred_positions=None):
'https://s3.amazonaws.com/research.metamind.io/wikitext/'
'wikitext-2-v1.zip', '3c914d17d80b1459be871a5039ac23e752a53cbe')


def _read_wiki(data_dir):
file_name = os.path.join(data_dir, 'wiki.train.tokens')
with open(file_name, 'r') as f:
Expand Down