Example #1
0
def plot_original_images():
    data_loader = get_pokemon_dataset()
    d2l.set_figsize((4, 4))
    num_rows = 4
    num_cols = 5
    scale = 1.5
    for X, y in data_loader:
        imgs = X[0:20, :, :, :].permute(0, 2, 3, 1) / 2 + 0.5
        figsize = (num_cols * scale, num_rows * scale)
        _, axes = d2l.plt.subplots(num_rows, num_cols, figsize=figsize)
        axes = axes.flatten()
        for i, (ax, img) in enumerate(zip(axes, imgs)):
            ax.imshow(d2l.numpy(img))
            ax.axes.get_xaxis().set_visible(False)
            ax.axes.get_yaxis().set_visible(False)
        plt.savefig("original.png")
        break
Example #2
0
    """Tokenize the English-French dataset."""
    source, target = [], []
    for i, line in enumerate(text.split('\n')):
        if num_examples and i > num_examples:
            break
        parts = line.split('\t')
        if len(parts) == 2:
            source.append(parts[0].split(' '))
            target.append(parts[1].split(' '))
    return source, target


source, target = tokenize_nmt(text)
print('source:', source[:6], 'target:', target[:6])

d2l.set_figsize()
_, _, patches = d2l.plt.hist([[len(l)
                               for l in source], [len(l) for l in target]],
                             label=['source', 'target'])
for patch in patches[1].patches:
    patch.set_hatch('/')
d2l.plt.legend(loc='upper right')

src_vocab = d2l.Vocab(source,
                      min_freq=2,
                      reserved_tokens=['<pad>', '<bos>', '<eos>'])

# len(src_vocab)


#@save
estimate = cumsum_counts / cumsum_counts.sum(dim=1, keepdims=True)

counts, cumsum_counts, estimate

# counts = multinomial.Multinomial(10, fair_probs).sample((500, ))
# counts

# %%
counts = multinomial.Multinomial(10, fair_probs).sample((500, ))
cum_counts = counts.cumsum(dim=0)

estimates = cum_counts / cum_counts.sum(dim=1, keepdims=True)
# cum_counts.shape [500, 6]
# cum_counts.sum(dim=1, keepdims=True).shape [500, 1]

d2l.set_figsize((6, 4.5))
for i in range(6):
    d2l.plt.plot(estimates[:, i].numpy(), label=("P(die=" + str(i + 1) + ")"))
d2l.plt.axhline(y=0.167, color='black', linestyle='dashed')
d2l.plt.gca().set_xlabel('Groups of experiments')
d2l.plt.gca().set_ylabel('Estimated probability')
d2l.plt.legend()

#%% 正态分布
import math
import numpy as np
from d2l import torch as d2l


def normal(x, mu, sigma):
    p = 1 / math.sqrt(2 * math.pi * sigma**2)