from d2l import torch as d2l import matplotlib.pyplot as plt import torch from torch import nn #@tab mxnet, pytorch T = 1000 # Generate a total of 1000 points time = d2l.arange(1, T + 1, dtype=d2l.float32) x = d2l.sin(0.01 * time) + d2l.normal(0, 0.2, (T, )) d2l.plot(time, [x], 'time', 'x', xlim=[1, 1000], figsize=(6, 3)) plt.show() #@tab mxnet, pytorch tau = 4 features = d2l.zeros((T - tau, tau)) for i in range(tau): features[:, i] = x[i:T - tau + i] labels = d2l.reshape(x[tau:], (-1, 1)) batch_size, n_train = 16, 600 # Only the first `n_train` examples are used for training train_iter = d2l.load_array((features[:n_train], labels[:n_train]), batch_size, is_train=True) # Function for initializing the weights of the network def init_weights(m): if type(m) == nn.Linear: nn.init.xavier_uniform_(m.weight)
import torch from torch import nn import matplotlib as mpl mpl.use('MacOSX') # n_train = 50 # No. of training examples n_train = 300 # No. of training examples x_train, _ = torch.sort(d2l.rand(n_train) * 5) # Training inputs def f(x): return 2 * d2l.sin(x) + x**0.8 y_train = f(x_train) + d2l.normal(0.0, 0.5, (n_train,)) # Training outputs x_test = d2l.arange(0, 5, 0.1) # Testing examples y_truth = f(x_test) # Ground-truth outputs for the testing examples n_test = len(x_test) # No. of testing examples n_test def plot_kernel_reg(y_hat): d2l.plot(x_test, [y_truth, y_hat], 'x', 'y', legend=['Truth', 'Pred'], xlim=[0, 5], ylim=[-1, 5]) d2l.plt.plot(x_train, y_train, 'o', alpha=0.5); y_hat = torch.repeat_interleave(y_train.mean(), n_test) plot_kernel_reg(y_hat)
print(predict('time traveller')) print(predict('traveller')) # RUN SCRIPT DEBUG = False device = d2l.try_gpu() num_epochs, lr = 500, 1 if DEBUG: device = 'cpu' num_epochs, lr = 100, 1 num_hiddens = 512 # batch_size, num_steps = 32, 35 batch_size, num_steps = 32, 35 train_iter, vocab = d2l.load_data_time_machine(batch_size, num_steps) X = d2l.reshape(d2l.arange(10), (2, 5)) net = RNNModelScratch(len(vocab), num_hiddens, device, get_params, init_rnn_state, rnn) state = net.begin_state(X.shape[0], device) # Y, new_state = net(X.to(d2l.try_gpu()), state) # Y.shape, len(new_state), new_state[0].shape train_ch8(net, train_iter, vocab, lr, num_epochs, device) print(net.params[-1]) # Default Performance Benchmarks: # perplexity 1.0, 135269.0 tokens/sec on cuda:0