<jupyter_start><jupyter_text>Chain: **define-by-run** scheme---<jupyter_code>import numpy as np
import chainer
from chainer import cuda, Function, gradient_check, report, training, utils, Variable
from chainer import datasets, iterators, optimizers, serializers
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
from chainer.training import extensions
x_data = np.array([5], dtype=np.float32)
x= Variable(x_data)

y= x**2 - 2*x + 1
# y is also Variable
y.data
# error propagation
y.backward()
# gradient is computed and stored in `grad`
x.grad
z = 2*x
y = x**2 - z + 1
# set return_grad = True
y.backward(retain_grad=True)
z.grad
z = 2*x
y = x**2 - z + 1
# did no set return_grad, no 
y.backward()
z.grad is None
x = Variable(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
y = x**2 - 2*x + 1
y.grad = np.ones((2, 3), dtype=np.float32)
y.backward()
x.grad<jupyter_output><empty_output><jupyter_text>links---<jupyter_code>#from 3dim to 2dim
# f = x.dot(W.T) + b
f = L.Linear(3,2)
print('f.W.data.shape', f.W.data.shape)
print("f.b.data:", f.b.data)
x = Variable(np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float32))
y = f(x)
y.data<jupyter_output><empty_output><jupyter_text>Example MINIST---<jupyter_code>train, test = datasets.get_mnist()
train_iter = iterators.SerialIterator(train, batch_size=100, shuffle=True)
# repeat=False, which means we stop iteration when all examples are visited. This option is usually required for the test/validation datasets
test_iter = iterators.SerialIterator(test, batch_size=100, repeat=False, shuffle=False)

class MLP(Chain):
    def __init__(self, n_units, n_out):
        super(MLP, self).__init__()
        with self.init_scope():
            # the size of the inputs to each layer will be inferred
            self.l1 = L.Linear(None, n_units)  # n_in -> n_units
            self.l2 = L.Linear(None, n_units)  # n_units -> n_units
            self.l3 = L.Linear(None, n_out)    # n_units -> n_out

    def __call__(self, x):
        h1 = F.relu(self.l1(x))
        h2 = F.relu(self.l2(h1))
        y = self.l3(h2)
        return y
model = L.Classifier(MLP(100, 10))  # the input size, 784, is inferred
optimizer = optimizers.SGD()
optimizer.setup(model)
updater = training.StandardUpdater(train_iter, optimizer)
trainer = training.Trainer(updater, (20, 'epoch'), out='result')
trainer.run()
trainer.extend(extensions.Evaluator(test_iter, model))
trainer.extend(extensions.LogReport())
trainer.extend(extensions.PrintReport(['epoch', 'main/accuracy', 'validation/main/accuracy']))
trainer.extend(extensions.ProgressBar())
trainer.run()  
#RNN
import numpy as np

import chainer.links as L
from chainer import Variable, Chain, optimizers


l = L.LSTM(100, 50)
l.reset_state()
x = Variable(np.random.rand(10, 100).astype(np.float32))
y = l(x)


class RNN(Chain):
    def __init__(self):
        super(RNN, self).__init__()
        with self.init_scope():
            self.embed = L.EmbedID(1000, 100) # word embedding
            self.mid = L.LSTM(100, 50) # 1st LSTM layer
            self.out = L.Linear(50, 1000) # deef-forward output layer

    def reset_state(self):
        self.mid.reset_state()

    def __call__(self,cur_word):
        # Given the current word ID, predict the next word
        x = self.embed(cur_word)
        h = self.mid(x)
        y = self.mid(h)
        return y

rnn = RNN()
model = L.Classifier(rnn)
optimizer = optimizers.SGD()
optimizer.setup(model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13984018264840184, 'fraction_numerical': 0.0273972602739726, 'mean_word_length': 3.7622282608695654, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '505735', 'license': 'MIT', 'n_tokens_mistral': 1342, 'n_tokens_neox': 1269, 'n_words': 321, 'path': 'Chainer_notes/Chainer_basic_tutorial.ipynb', 'repo_name': 'cyk1337/MT', 'size': '3504'}