Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def train(net, train_iter, test_iter, loss, trainer, num_epochs,
ctx_list=d2l.try_all_gpus()):
num_batches, timer = len(train_iter), d2l.Timer()
for epoch in range(num_epochs):
# store training_loss, training_accuracy, num_examples, num_features
metric = [0.0] * 4
for i, (features, labels) in enumerate(train_iter):
timer.start()
l, acc = d2l.train_batch_ch12(
net, features, labels, loss, trainer, ctx_list)
metric = [a+b for a, b in zip(metric, (l, acc, labels.shape[0], labels.size))]
timer.stop()
if (i+1) % (num_batches // 5) == 0:
print('loss %.3f, train acc %.3f' % (metric[0]/metric[2], metric[1]/metric[3]))
test_acc = d2l.evaluate_accuracy_gpus(net, test_iter)
print('loss %.3f, train acc %.3f, test acc %.3f' % (
metric[0]/metric[2], metric[1]/metric[3], test_acc))
print('%.1f exampes/sec on %s' % (
metric[2]*num_epochs/timer.sum(), ctx_list))
def train(net, train_iter, test_iter, loss, trainer, num_epochs,
ctx_list=d2l.try_all_gpus()):
num_batches, timer = len(train_iter), d2l.Timer()
for epoch in range(num_epochs):
# store training_loss, training_accuracy, num_examples, num_features
metric = [0.0] * 4
for i, (features, labels) in enumerate(train_iter):
timer.start()
l, acc = d2l.train_batch_ch12(
net, features, labels, loss, trainer, ctx_list)
metric = [a+b for a, b in zip(metric, (l, acc, labels.shape[0], labels.size))]
timer.stop()
if (i+1) % (num_batches // 5) == 0:
print('loss %.3f, train acc %.3f' % (metric[0]/metric[2], metric[1]/metric[3]))
test_acc = d2l.evaluate_accuracy_gpus(net, test_iter)
print('loss %.3f, train acc %.3f, test acc %.3f' % (
metric[0]/metric[2], metric[1]/metric[3], test_acc))
print('%.1f exampes/sec on %s' % (
metric[2]*num_epochs/timer.sum(), ctx_list))
def train(net, train_iter, test_iter, loss, trainer, num_epochs,
ctx_list=d2l.try_all_gpus()):
num_batches, timer = len(train_iter), d2l.Timer()
for epoch in range(num_epochs):
# store training_loss, training_accuracy, num_examples, num_features
metric = [0.0] * 4
for i, (features, labels) in enumerate(train_iter):
timer.start()
l, acc = d2l.train_batch_ch12(
net, features, labels, loss, trainer, ctx_list)
metric = [a+b for a, b in zip(metric, (l, acc, labels.shape[0], labels.size))]
timer.stop()
if (i+1) % (num_batches // 5) == 0:
print('loss %.3f, train acc %.3f' % (metric[0]/metric[2], metric[1]/metric[3]))
test_acc = d2l.evaluate_accuracy_gpus(net, test_iter)
print('loss %.3f, train acc %.3f, test acc %.3f' % (
metric[0]/metric[2], metric[1]/metric[3], test_acc))
print('%.1f exampes/sec on %s' % (
metric[2]*num_epochs/timer.sum(), ctx_list))
def train(net, train_iter, test_iter, loss, trainer, num_epochs,
ctx_list=d2l.try_all_gpus()):
num_batches, timer = len(train_iter), d2l.Timer()
for epoch in range(num_epochs):
# store training_loss, training_accuracy, num_examples, num_features
metric = [0.0] * 4
for i, (features, labels) in enumerate(train_iter):
timer.start()
l, acc = d2l.train_batch_ch12(
net, features, labels, loss, trainer, ctx_list)
metric = [a+b for a, b in zip(metric, (l, acc, labels.shape[0], labels.size))]
timer.stop()
if (i+1) % (num_batches // 5) == 0:
print('loss %.3f, train acc %.3f' % (metric[0]/metric[2], metric[1]/metric[3]))
test_acc = d2l.evaluate_accuracy_gpus(net, test_iter)
print('loss %.3f, train acc %.3f, test acc %.3f' % (
metric[0]/metric[2], metric[1]/metric[3], test_acc))
print('%.1f exampes/sec on %s' % (
def load_data_time_machine(num_examples=10000):
"""Load the time machine data set (available in the English book)."""
with open('../data/timemachine.txt') as f:
raw_text = f.read()
lines = raw_text.split('\n')
text = ' '.join(' '.join(lines).lower().split())[:num_examples]
vocab = Vocab(text)
corpus_indices = [vocab[char] for char in text]
return corpus_indices, vocab
def resnet_block(num_channels, num_residuals, first_block=False):
blk = nn.Sequential()
for i in range(num_residuals):
if i == 0 and not first_block:
blk.add(Residual(num_channels, use_1x1conv=True, strides=2))
else:
blk.add(Residual(num_channels))
return blk
def resnet_block(num_channels, num_residuals, first_block=False):
blk = nn.Sequential()
for i in range(num_residuals):
if i == 0 and not first_block:
blk.add(Residual(num_channels, use_1x1conv=True, strides=2))
else:
blk.add(Residual(num_channels))
return blk
def __init__(self, num_channels, use_1x1conv=False, strides=1, **kwargs):
super(Residual, self).__init__(**kwargs)
self.conv1 = nn.Conv2D(num_channels, kernel_size=3, padding=1,
strides=strides)
self.conv2 = nn.Conv2D(num_channels, kernel_size=3, padding=1)
if use_1x1conv:
self.conv3 = nn.Conv2D(num_channels, kernel_size=1,
strides=strides)
else:
self.conv3 = None
self.bn1 = nn.BatchNorm()
self.bn2 = nn.BatchNorm()
def __init__(self, xlabel=None, ylabel=None, legend=[], xlim=None,
ylim=None, xscale='linear', yscale='linear', fmts=None,
nrows=1, ncols=1, figsize=(3.5, 2.5)):
"""Incrementally plot multiple lines."""
d2l.use_svg_display()
self.fig, self.axes = d2l.plt.subplots(nrows, ncols, figsize=figsize)
if nrows * ncols == 1: self.axes = [self.axes,]
# use a lambda to capture arguments
self.config_axes = lambda : d2l.set_axes(
self.axes[0], xlabel, ylabel, xlim, ylim, xscale, yscale, legend)
self.X, self.Y, self.fmts = None, None, fmts
def show_fashion_mnist(images, labels):
import d2l
d2l.use_svg_display()
# Here _ means that we ignore (not use) variables.
_, figs = d2l.plt.subplots(1, len(images), figsize=(12, 12))
for f, img, lbl in zip(figs, images, labels):
f.imshow(img.reshape((28, 28)).asnumpy())
f.set_title(lbl)
f.axes.get_xaxis().set_visible(False)
f.axes.get_yaxis().set_visible(False)