I am working on the file "Latent_Me.ipynb".
The first task is:
Download Your Results
After the above cell has finished writing there should be an image in generated_images/ for each image in aligned_images/.
You can right-click and download each of these images to see your final latent self.
Latent Representation
You can also download the npy files in the latent_representations/ directory. Each of those is a serialized numpy array which contains the (18, 512) array encoding the point in latent space which corresponds to the generated image. Which you can open with latent = np.load('filename.npy')
Change your Smile, Gender, or Age
Once your latent representation has been generated and saved you can explore the volume around it through latent vectors. Puzer has provided vectors for Smile, Gender and Age so you can see what you look like as your latent self varies along those axes.
Run the following cells.
Can you write Python code for it?

import os
import pickle
import PIL.Image
import numpy as np
import dnnlib
import dnnlib.tflib as tflib
import config
from encoder.generator_model import Generator

import matplotlib.pyplot as plt
%matplotlib inline

URL_FFHQ = 'https://drive.google.com/uc?id=1MEGjdvVpUsu1jB4zrXZN7Y4kBBOzizDQ'

tflib.init_tf()
with dnnlib.util.open_url(URL_FFHQ, cache_dir=config.cache_dir) as f:
    generator_network, discriminator_network, Gs_network = pickle.load(f)

generator = Generator(Gs_network, batch_size=1, randomize_noise=False)

def generate_image(latent_vector):
    latent_vector = latent_vector.reshape((1, 18, 512))
    generator.set_dlatents(latent_vector)
    img_array = generator.generate_images()[0]
    img = PIL.Image.fromarray(img_array, 'RGB')
    return img.resize((256, 256))

def move_and_show(latent_vector, direction, coeffs):
    fig,ax = plt.subplots(1, len(coeffs), figsize=(15, 10), dpi=80)
    for i, coeff in enumerate(coeffs):
        new_latent_vector = latent_vector.copy()
        new_latent_vector[:8] = (latent_vector + coeff*direction)[:8]
        ax[i].imshow(generate_image(new_latent_vector))
        ax[i].set_title('Coeff: %0.1f' % coeff)
    [x.axis('off') for x in ax]
    plt.show()

# Loading already learned representations
me = np.load('latent_representations/<YOUR FILENAME>.npy')

# Loading already learned latent directions
smile_direction = np.load('ffhq_dataset/latent_directions/smile.npy')
gender_direction = np.load('ffhq_dataset/latent_directions/gender.npy')
age_direction = np.load('ffhq_dataset/latent_directions/age.npy')

# In general it's possible to find directions of almost any face attributes: position, hair style or color ...
# Additional scripts for doing so will be realised soon


I am working on the file "Latent_Me.ipynb".
The first task is:
Smile transformation
Can you write Python code for it?

move_and_show(me, smile_direction, [-0.5, 0, 0.5])


I am working on the file "Latex/ConvertFromTeX.ipynb".
The first task is:
Binding energy
Can you write Python code for it?

%%bash --out tmp
perl -nlw \
    -e 's/\\begin\{eqnarray\}//g; s/\\end\{eqnarray\}//g; ' `# remove environment for Mathematica` \
    -e 's/\\label\{eB6PN\}//;' `# remove equation label` \
    -e 's/\\nonumber//g; s/\\biggl//g; s/\\Biggl//g; s/\\left//g; s/\\right//g;' `# remove irrelevant formatting` \
    -e 's/\&//g; s/\\\\//g;' `# remove alignments and newlines` \
    -e 's/\\\{/(/g; s/\\\}/)/g; s/\[/(/g; s/\]/)/g;' `# convert braces and brackets to parentheses` \
    -e 's/\),/\)/;' `# remove final punctuation` \
    -e 'print if /\S/;' `# only print line if nonempty` \
    BindingEnergy.tex > BindingEnergy_Simplified.tex
cat BindingEnergy_Simplified.tex

tmp

from __future__ import print_function
Terms = ['E_2', 'E_4', 'E_6', 'E_8', 'E_lnv_8', 'E_10', 'E_lnv_10', 'E_11', 'E_12', 'E_lnv_12']
Expressions = [s[(s.index('=')+1):].replace('\n','') for s in tmp.split('\ne')]
for t,e in zip(Terms,Expressions):
    print(e, file=open('/tmp/'+t+'.tex','w+'))

import re
MathKernel='/Applications/Local/Mathematica.app/Contents/MacOS/MathKernel'
BindingEnergyCommand = r"""
\[Gamma] = EulerGamma;
HornerForm[ToExpression[Import[
    "/tmp/{0}.tex",
    "Text"], TeXForm]] >> /tmp/{0}.cpp
Exit[];
"""
for t in Terms:
    ! {MathKernel} -run '{BindingEnergyCommand.format(t)}' >& /dev/null
    BindingEnergy = !cat /tmp/{t}.cpp
    BindingEnergy = ''.join(BindingEnergy).replace(' ','').replace('Pi','pi').replace('Log','log').replace('Nu','nu')
    BindingEnergy = BindingEnergy.replace('[','(').replace(']',')').replace('Power','Pow').replace('^','**').replace('\\','')
    BindingEnergy = re.sub(r'Subscript\((.*?),([0-9])[.5]*\)\*\*(c|ln)', r'\1_\2__\g<3>1', BindingEnergy)
    BindingEnergy = sympify(BindingEnergy)

    logv = symbols('logv')
    BindingEnergy = BindingEnergy.subs('log(v)', logv)

    print("BindingEnergy_NoSpin_B.AddDerivedConstant('{0}', {1})".format(t, BindingEnergy))
    print()

import re
from sympy import sympify, symbols
for i in [1]:
    BindingEnergy = !cat /tmp/E_lnv_12.cpp
   ...


I am working on the file "Latex/ConvertFromTeX.ipynb".
The first task is:
PN angular momentum
Can you write Python code for it?

from sympy import *
ellHat= Symbol('ellHat')
nHat= Symbol('nHat')
lambdaHat= Symbol('lambdaHat')
var('v, m, nu, G, c, x');

def MathematicaToSympy(L):
    import re
    L = ''.join(L).replace(' ','')
    L = L.replace(r'\[ScriptL]','ell')
    MathematicaCapitalGreek = re.compile(r'\\ \[ Capital(.*?) \]', re.VERBOSE)
    L = MathematicaCapitalGreek.sub(r'\1',L)
    MathematicaGreek = re.compile(r'\\ \[ (.*?) \]', re.VERBOSE)
    L = MathematicaGreek.sub(lambda m: m.group(1).lower(),L)
    OverHat = re.compile(r'OverHat\[ (.*?) \]', re.VERBOSE)
    L = OverHat.sub(r'\1Hat',L)
    Subscript = re.compile(r'Subscript\[ (.*?), (.*?) \]', re.VERBOSE)
    L = Subscript.sub(r'\1_\2',L)
    Sqrt = re.compile(r'Sqrt\[ (.*?) \]', re.VERBOSE)
    L = Sqrt.sub(r'sqrt(\1)',L)
    L = L.replace('Pi','pi').replace('Log','log').replace('Zeta','zeta').replace('Power','Pow')
    L = L.replace('^','**')
    return L

MathKernel='/Applications/Local/Mathematica.app/Contents/MacOS/MathKernel'
MCommand = r"""
\[Gamma] = EulerGamma;
\[Zeta] = Zeta;
HornerForm[ToExpression[Import[
    "AngularMomentum.tex",
    "Text"], TeXForm],x] >> /tmp/AngularMomentum.cpp
Exit[];
"""
! {MathKernel} -run '{MCommand}' >& /dev/null
L = !cat /tmp/AngularMomentum.cpp
L = MathematicaToSympy(L)
L = sympify(L).subs('sqrt(x)',v).subs('x',v**2).subs('G',1).subs('c',1).simplify()

L_ellHat = horner( (v*L).simplify().expand().coeff(ellHat) )/v
L_nHat = horner( (v*L).simplify().expand().coeff(nHat) )/v
L_lambdaHat = horner( (v*L).simplify().expand().coeff(lambdaHat) )/v

L_set = [L_ellHat, L_nHat, L_lambdaHat]

for n in range(3,9,2):
    print("""AngularMomentum_Spin.AddDerivedVariable('L_SO_{0}',
    ({1})*ellHat
    + ({2})*nHat
    + ({3})*lambdaHat,
    datatype=ellHat.datatype)""".format(n,
                                        (v*L_ellHat/(m**2*nu)).expand().coeff(v**n).simplify(),
                                        (v*L_nHat/(m**2*nu)).expand().coeff(v**n).simplify(),
                                       ...


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Higher relative TFP to the farm with market power
Can you write Python code for it?


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Let's recalculate the new equilibria under the different scenarios.
Can you write Python code for it?

(Xrc,Xr,wc,wr) = scene_print(E,10,detail=True)

factor_plot(E,Xrc,Xr)

E.s[-1]

E.s[-1] = 1
E.GAMMA = 0.90

(Xrc,Xr,wc,wr) = scene_print(E,10,prnt=False, detail=True)
factor_plot(E,Xrc,Xr)
E.s[-1] = 1.5
(Xrc2,Xr2,wc2,wr2) = scene_print(E,10,prnt=False, detail=True)
factor_plot(E,Xrc2,Xr2)


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Most of the code for this project is in the geqfarm module (which has Economy, MirEconomy and CESEconomy class definitions).  For the code and documentation type:
geqfarm??
Can you write Python code for it?


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Now create an economy and change a few parameters from their default.  The $\gamma$ parameter which measures the degree of homogeneity in production is purposefully set very high.. We are very close to assuming constant returns to scale (but setting it just below 1 is needed to make sure the size-distribution remains determinate).
Can you write Python code for it?

E = Economy(N)    # an instance takes N length as parameter
s = np.ones(N)    # skill distribution in the economy
E.ALPHA = 0.5
E.GAMMA = 0.98

E.s

E.smallhold_eq([E.TBAR,E.LBAR],E.s)

E.cartel_eq(0.5)


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Simulating Latifundia Economics
The following contrived example helps to starkly highlight the mechanisms behind the factor-market power distorted equilibrium.
TO start simple assume that there just N=5 farmers each with a skill level normalized to 1.
Can you write Python code for it?


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
So as expected the efficient (competitive) resource allocation has every farmer operating a farm of equal unit size.  The smallhold_eq method gives the competitive (efficient) allocation for a subeconomy with XE = (TE,LE) endowments and skill distribution s. It returns a named tuple with equilibrium facor prices and X=[T,L] allocations.
If XE and s are those of the entire economy then we've found the economywide competitive allocation.
Can you write Python code for it?

Xc = E.smallhold_eq([E.TBAR,E.LBAR],s)
Xc

(Xrc,Xr,wc,wr) = scene_print(E,10, detail=True)


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
The Economy has default resource endowment:
Can you write Python code for it?


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
This describes models and code implementation for several variations on a neo-classical model. The framework allows us to explore how factor endowments, the initial distribution of property rights and skills in the population interact with production technologies to shape equilibrium patterns of agrarian production organization and the size distribution of farms. The most original part of the focus is on endogenous market power distortions that may arise when land ownership becomes concentrated.
To understand the main arguments, consider the simplest case of a single landlord (or a cartel of landlords) surrounded by a fringe of small landowning or landless agricultural households.  If the lanlord owns a large fraction of the land endowment a standard partial-equilibrium analysis of non-price discriminating monopoly suggests the landlord would drive up the rental price of land by withholding land from the lease market. In a general equilibrium setting however there is another effect: by restricting other farmers' access to land landlords also lower the marginal product of labor on those farms. This increases the supply of labor to landlord estates at any given wage increasing landlords' potential income from monopsony rents.  This can lead to equilibria where landlords increase the size of their production estates scale well above efficient scale in a competitive economy.  A Latifundia-Minifundia type economy can emerge in which landlords operate large estates employing overly land-intensive production techniques while a large mass of farmers operate inefficiently and labor-intensive small parcels of land and sell labor to the landlord estate(s).
The model
The following is a close adaptation of earlier statement of this problem(Conning, 2004, 2010).  We start with a simplified statement of the problem with just two types of households -- landlords and `peasants' and a very simple discrete distribution of farming skills.  We later generalize.
Preliminaries
The economy has $\overline{T}$ units of cultivable land...
Can you write Python code for it?

%load_ext autoreload
%autoreload 2

import numpy as np
from matplotlib import pyplot as plt
from ipywidgets import interact, fixed

plt.rcParams["figure.figsize"] = (10, 8)
np.set_printoptions(precision=4)
%matplotlib inline


I am working on the file "LatifundiaEconomics.ipynb".
The first task is:
Thus far we've said nothing of the ownership of land or labor.  Let's assume every household has one unit of labor but that the 'landlord' class (which WLOG we index to be the last skill group s[-1]) owns fraction $\theta$ of the land.  Assuming a uniform distribution of households across skills every skill group has Lbar/N households, and so there are that many landlords who act as a single cartel.
The following code is useful for printing and plotting out equilibria as a function of initial landlords' land ownership share.
We pass our economy instance to the scenarios function and it solves for competitive and market-power equilibria (at different levels of theta) and prints out the results.
Let's plot the results.. THis is the classic diagram from Latifundia Economics..
Can you write Python code for it?


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Build the network
Now we can put all the pieces together and build a class for the network. To actually run data through the LSTM cells, we will use tf.nn.dynamic_rnn. This function will pass the hidden and cell states across LSTM cells appropriately for us. It returns the outputs for each LSTM cell at each step for each sequence in the mini-batch. It also gives us the final LSTM state. We want to save this state as final_state so we can pass it to the first LSTM cell in the the next mini-batch run. For tf.nn.dynamic_rnn, we pass in the cell and initial state we get from build_lstm, as well as our input sequences. Also, we need to one-hot encode the inputs before going into the RNN.

Exercise: Use the functions you've implemented previously and tf.nn.dynamic_rnn to build the network.
Can you write Python code for it?

class CharRNN:

    def __init__(self, num_classes, batch_size=64, num_steps=50,
                       lstm_size=128, num_layers=2, learning_rate=0.001,
                       grad_clip=5, sampling=False):

        # When we're using this network for sampling later, we'll be passing in
        # one character at a time, so providing an option for that
        if sampling == True:
            batch_size, num_steps = 1, 1
        else:
            batch_size, num_steps = batch_size, num_steps

        tf.reset_default_graph()

        # Build the input placeholder tensors
        self.inputs, self.targets, self.keep_prob = build_inputs(batch_size, num_steps)

        # Build the LSTM cell
        cell, self.initial_state = build_lstm(lstm_size, num_layers, batch_size, self.keep_prob)

        ### Run the data through the RNN layers
        # First, one-hot encode the input tokens
        x_one_hot = tf.one_hot(self.inputs, num_classes)

        # Run each sequence step through the RNN with tf.nn.dynamic_rnn
        outputs, state = tf.nn.dynamic_rnn(cell, x_one_hot, initial_state=self.initial_state)
        self.final_state = state

        # Get softmax predictions and logits
        self.prediction, self.logits = build_output(outputs, lstm_size, num_classes)

        # Loss and optimizer (with gradient clipping)
        self.loss = build_loss(self.logits, self.targets, lstm_size, num_classes)
        self.optimizer = build_optimizer(self.loss, learning_rate, grad_clip)


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Here, pass in the path to a checkpoint and sample from the network.
Can you write Python code for it?


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Hyperparameters
Here are the hyperparameters for the network.

batch_size - Number of sequences running through the network in one pass.
num_steps - Number of characters in the sequence the network is trained on. Larger is better typically, the network will learn more long range dependencies. But it takes longer to train. 100 is typically a good number here.
lstm_size - The number of units in the hidden layers.
num_layers - Number of hidden LSTM layers to use
learning_rate - Learning rate for training
keep_prob - The dropout keep probability when training. If you're network is overfitting, try decreasing this.

Here's some good advice from Andrej Karpathy on training the network. I'm going to copy it in here for your benefit, but also link to where it originally came from.

Tips and Tricks
Monitoring Validation Loss vs. Training Loss
If you're somewhat new to Machine Learning or Neural Networks it can take a bit of expertise to get good models. The most important quantity to keep track of is the difference between your training loss (printed during training) and the validation loss (printed once in a while when the RNN is run on the validation data (by default every 1000 iterations)). In particular:

If your training loss is much lower than validation loss then this means the network might be overfitting. Solutions to this are to decrease your network size, or to increase dropout. For example you could try dropout of 0.5 and so on.
If your training/validation loss are about equal then your model is underfitting. Increase the size of your model (either number of layers or the raw number of neurons per layer)

Approximate number of parameters
The two most important parameters that control the model are lstm_size and num_layers. I would advise that you always use num_layers of either 2/3. The lstm_size can be adjusted based on how much data you have. The two important quantities to keep track of here are:

The number of parameters in your model. This is printed when you start training.
The size of your dataset....
Can you write Python code for it?

batch_size = 100        # Sequences per batch
num_steps = 100         # Number of sequence steps per batch
lstm_size = 512         # Size of hidden layers in LSTMs
num_layers = 2          # Number of LSTM layers
learning_rate = 0.001   # Learning rate
keep_prob = 0.5         # Dropout keep probability


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
If you implemented get_batches correctly, the above output should look something like
```
x
 [[55 63 69 22  6 76 45  5 16 35]
 [ 5 69  1  5 12 52  6  5 56 52]
 [48 29 12 61 35 35  8 64 76 78]
 [12  5 24 39 45 29 12 56  5 63]
 [ 5 29  6  5 29 78 28  5 78 29]
 [ 5 13  6  5 36 69 78 35 52 12]
 [63 76 12  5 18 52  1 76  5 58]
 [34  5 73 39  6  5 12 52 36  5]
 [ 6  5 29 78 12 79  6 61  5 59]
 [ 5 78 69 29 24  5  6 52  5 63]]
y
 [[63 69 22  6 76 45  5 16 35 35]
 [69  1  5 12 52  6  5 56 52 29]
 [29 12 61 35 35  8 64 76 78 28]
 [ 5 24 39 45 29 12 56  5 63 29]
 [29  6  5 29 78 28  5 78 29 45]
 [13  6  5 36 69 78 35 52 12 43]
 [76 12  5 18 52  1 76  5 58 52]
 [ 5 73 39  6  5 12 52 36  5 78]
 [ 5 29 78 12 79  6 61  5 59 63]
 [78 69 29 24  5  6 52  5 63 76]]
 ``
 although the exact numbers will be different. Check to make sure the data is shifted over one step fory`.
Building the model
Below is where you'll build the network. We'll break it up into parts so it's easier to reason about each bit. Then we can connect them up into the whole network.
<img src="assets/charRNN.png" width=500px>
Inputs
First off we'll create our input placeholders. As usual we need placeholders for the training data and the targets. We'll also create a placeholder for dropout layers called keep_prob. This will be a scalar, that is a 0-D tensor. To make a scalar, you create a placeholder without giving it a size.

Exercise: Create the input placeholders in the function below.
Can you write Python code for it?

def build_inputs(batch_size, num_steps):
    ''' Define placeholders for inputs, targets, and dropout

        Arguments
        ---------
        batch_size: Batch size, number of sequences per batch
        num_steps: Number of sequence steps in a batch

    '''
    # Declare placeholders we'll feed into the graph
    inputs = tf.placeholder(tf.int32, [batch_size, num_steps], name='inputs')
    targets = tf.placeholder(tf.int32, [batch_size, num_steps], name='labels')

    # Keep probability placeholder for drop out layers
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')

    return inputs, targets, keep_prob


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
LSTM Cell
Here we will create the LSTM cell we'll use in the hidden layer. We'll use this cell as a building block for the RNN. So we aren't actually defining the RNN here, just the type of cell we'll use in the hidden layer.
We first create a basic LSTM cell with
python
lstm = tf.contrib.rnn.BasicLSTMCell(num_units)
where num_units is the number of units in the hidden layers in the cell. Then we can add dropout by wrapping it with
python
tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
You pass in a cell and it will automatically add dropout to the inputs or outputs. Finally, we can stack up the LSTM cells into layers with tf.contrib.rnn.MultiRNNCell. With this, you pass in a list of cells and it will send the output of one cell into the next cell. Previously with TensorFlow 1.0, you could do this
python
tf.contrib.rnn.MultiRNNCell([cell]*num_layers)
This might look a little weird if you know Python well because this will create a list of the same cell object. However, TensorFlow 1.0 will create different weight matrices for all cell objects. But, starting with TensorFlow 1.1 you actually need to create new cell objects in the list. To get it to work in TensorFlow 1.1, it should look like
```python
def build_cell(num_units, keep_prob):
    lstm = tf.contrib.rnn.BasicLSTMCell(num_units)
    drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
return drop

tf.contrib.rnn.MultiRNNCell([build_cell(num_units, keep_prob) for _ in range(num_layers)])
```
Even though this is actually multiple LSTM cells stacked on each other, you can treat the multiple layers as one cell.
We also need to create an initial cell state of all zeros. This can be done like so
python
initial_state = cell.zero_state(batch_size, tf.float32)
Below, we implement the build_lstm function to create these LSTM cells and the initial state.
Can you write Python code for it?

print(tf.__version__)

def build_lstm(lstm_size, num_layers, batch_size, keep_prob):
    ''' Build LSTM cell.

        Arguments
        ---------
        keep_prob: Scalar tensor (tf.placeholder) for the dropout keep probability
        lstm_size: Size of the hidden layers in the LSTM cells
        num_layers: Number of LSTM layers
        batch_size: Batch size

    '''
    def build_cell(num_units, keep_prob):
        lstm = tf.contrib.rnn.BasicLSTMCell(num_units)
        drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)
        return drop

    if tf.__version__ == '1.0.0':
        ### Build the LSTM Cell
        # Use a basic LSTM cell
        lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)

        # Add dropout to the cell outputs
        drop = tf.contrib.rnn.DropoutWrapper(lstm, output_keep_prob=keep_prob)

        # Stack up multiple LSTM layers, for deep learning
        cell = tf.contrib.rnn.MultiRNNCell([drop] * num_layers)
    else:
        cell = tf.contrib.rnn.MultiRNNCell([build_cell(lstm_size, keep_prob) for _ in range(num_layers)])

    initial_state = cell.zero_state(batch_size, tf.float32)

    return cell, initial_state


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Latin text generation
First we'll load the text file and convert it into integers for our network to use. Here I'm creating a couple dictionaries to convert the characters to and from integers. Encoding the characters as integers makes it easier to use as input in the network.
I am using this notebook to evaluate the use for latin language generation, based on a set of classical texts taken from the Latin Library.
This will eventually go into my LatinLearner project:
https://github.com/CarstenIsert/LatinLearner
Right now, this is just a small test.
Can you write Python code for it?

import os

input_dir = 'clean_library'
list_of_files = os.listdir(input_dir)
latin_text = ""
for file_name in list_of_files:
    print("Processing file: ", file_name)
    infile = open(input_dir + '/' + file_name, 'r')
    file_text = infile.read()
    infile.close()
    latin_text = latin_text + " " + file_text

print(len(latin_text))
vocab = sorted(set(latin_text))
vocab_to_int = {character: idx for idx, character in enumerate(vocab)}
int_to_vocab = dict(enumerate(vocab))
encoded = np.array([vocab_to_int[character] for character in latin_text], dtype=np.int32)


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Making training mini-batches
Here is where we'll make our mini-batches for training. Remember that we want our batches to be multiple sequences of some desired number of sequence steps. Considering a simple example, our batches would look like this:

We have our text encoded as integers as one long array in encoded. Let's create a function that will give us an iterator for our batches. I like using generator functions to do this. Then we can pass encoded into this function and get our batch generator.
The first thing we need to do is discard some of the text so we only have completely full batches. Each batch contains $N \times M$ characters, where $N$ is the batch size (the number of sequences) and $M$ is the number of steps. Then, to get the number of batches we can make from some array arr, you divide the length of arr by the batch size. Once you know the number of batches and the batch size, you can get the total number of characters to keep.
After that, we need to split arr into $N$ sequences. You can do this using arr.reshape(size) where size is a tuple containing the dimensions sizes of the reshaped array. We know we want $N$ sequences (n_seqs below), let's make that the size of the first dimension. For the second dimension, you can use -1 as a placeholder in the size, it'll fill up the array with the appropriate data for you. After this, you should have an array that is $N \times (M * K)$ where $K$ is the number of batches.
Now that we have this array, we can iterate through it to get our batches. The idea is each batch is a $N \times M$ window on the array. For each subsequent batch, the window moves over by n_steps. We also want to create both the input and target arrays. Remember that the targets are the inputs shifted over one character. You'll usually see the first input character used as the last target character, so something like this:
python
y[:, :-1], y[:, -1] = x[:, 1:], x[:, 0]
where x is the input batch and y is the target...
Can you write Python code for it?

def get_batches(arr, n_seqs, n_steps):
    '''Create a generator that returns batches of size
       n_seqs x n_steps from arr.

       Arguments
       ---------
       arr: Array you want to make batches from
       n_seqs: Batch size, the number of sequences per batch
       n_steps: Number of sequence steps per batch
    '''
    # Get the number of characters per batch and number of batches we can make
    characters_per_batch = n_seqs * n_steps
    n_batches = len(arr)//characters_per_batch

    # Keep only enough characters to make full batches
    arr = arr[:n_batches * characters_per_batch]

    # Reshape into n_seqs rows
    arr = arr.reshape((n_seqs, -1))

    for n in range(0, arr.shape[1], n_steps):
        # The features
        x = arr[:, n:n+n_steps]
        # The targets, shifted by one
        y = np.zeros_like(x)
        y[:, :-1], y[:, -1] = x[:, 1:], x[:, 0]
        yield x, y


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
RNN Output
Here we'll create the output layer. We need to connect the output of the RNN cells to a full connected layer with a softmax output. The softmax output gives us a probability distribution we can use to predict the next character, so we want this layer to have size $C$, the number of classes/characters we have in our text.
If our input has batch size $N$, number of steps $M$, and the hidden layer has $L$ hidden units, then the output is a 3D tensor with size $N \times M \times L$. The output of each LSTM cell has size $L$, we have $M$ of them, one for each sequence step, and we have $N$ sequences. So the total size is $N \times M \times L$.
We are using the same fully connected layer, the same weights, for each of the outputs. Then, to make things easier, we should reshape the outputs into a 2D tensor with shape $(M * N) \times L$. That is, one row for each sequence and step, where the values of each row are the output from the LSTM cells. We get the LSTM output as a list, lstm_output. First we need to concatenate this whole list into one array with tf.concat. Then, reshape it (with tf.reshape) to size $(M * N) \times L$.
One we have the outputs reshaped, we can do the matrix multiplication with the weights. We need to wrap the weight and bias variables in a variable scope with tf.variable_scope(scope_name) because there are weights being created in the LSTM cells. TensorFlow will throw an error if the weights created here have the same names as the weights created in the LSTM cells, which they will be default. To avoid this, we wrap the variables in a variable scope so we can give them unique names.

Exercise: Implement the output layer in the function below.
Can you write Python code for it?

def build_output(lstm_output, in_size, out_size):
    ''' Build a softmax layer, return the softmax output and logits.

        Arguments
        ---------

        lstm_output: List of output tensors from the LSTM layer
        in_size: Size of the input tensor, for example, size of the LSTM cells
        out_size: Size of this softmax layer

    '''

    # Reshape output so it's a bunch of rows, one row for each step for each sequence.
    # Concatenate lstm_output over axis 1 (the columns)
    seq_output = tf.concat(lstm_output, axis = 1)
    # Reshape seq_output to a 2D tensor with lstm_size columns
    x = tf.reshape(seq_output, [-1, in_size])

    # Connect the RNN outputs to a softmax layer
    with tf.variable_scope('softmax'):
        # Create the weight and bias variables here
        softmax_w = tf.Variable(tf.truncated_normal((in_size, out_size), stddev=0.1))
        softmax_b = tf.Variable(tf.zeros(out_size))

    # Since output is a bunch of rows of RNN cell outputs, logits will be a bunch
    # of rows of logit outputs, one for each step and sequence
    logits = tf.matmul(x, softmax_w) + softmax_b

    # Use softmax to get the probabilities for predicted characters
    out = tf.nn.softmax(logits, name='predictions')

    return out, logits


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Sampling
Now that the network is trained, we'll can use it to generate new text. The idea is that we pass in a character, then the network will predict the next character. We can use the new one, to predict the next one. And we keep doing this to generate all new text. I also included some functionality to prime the network with some text by passing in a string and building up a state from that.
The network gives us predictions for each character. To reduce noise and make things a little less random, I'm going to only choose a new character from the top N most likely characters.
Can you write Python code for it?

def pick_top_n(preds, vocab_size, top_n=5):
    p = np.squeeze(preds)
    p[np.argsort(p)[:-top_n]] = 0
    p = p / np.sum(p)
    c = np.random.choice(vocab_size, 1, p=p)[0]
    return c

def sample(checkpoint, n_samples, lstm_size, vocab_size, prime="The "):
    samples = [c for c in prime]
    model = CharRNN(len(vocab), lstm_size=lstm_size, sampling=True)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        saver.restore(sess, checkpoint)
        new_state = sess.run(model.initial_state)
        for c in prime:
            x = np.zeros((1, 1))
            x[0,0] = vocab_to_int[c]
            feed = {model.inputs: x,
                    model.keep_prob: 1.,
                    model.initial_state: new_state}
            preds, new_state = sess.run([model.prediction, model.final_state],
                                         feed_dict=feed)

        c = pick_top_n(preds, len(vocab))
        samples.append(int_to_vocab[c])

        for i in range(n_samples):
            x[0,0] = c
            feed = {model.inputs: x,
                    model.keep_prob: 1.,
                    model.initial_state: new_state}
            preds, new_state = sess.run([model.prediction, model.final_state],
                                         feed_dict=feed)

            c = pick_top_n(preds, len(vocab))
            samples.append(int_to_vocab[c])

    return ''.join(samples)


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Saved checkpoints
Read up on saving and loading checkpoints here: https://www.tensorflow.org/programmers_guide/variables
Can you write Python code for it?


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Time for training
This is typical training code, passing inputs and targets into the network, then running the optimizer. Here we also get back the final LSTM state for the mini-batch. Then, we pass that state back into the network so the next batch can continue the state from the previous batch. And every so often (set by save_every_n) I save a checkpoint.
Here I'm saving checkpoints with the format
i{iteration number}_l{# hidden layer units}.ckpt

Exercise: Set the hyperparameters above to train the network. Watch the training loss, it should be consistently dropping. Also, I highly advise running this on a GPU.
Can you write Python code for it?

epochs = 10
# Save every N iterations
save_every_n = 500

model = CharRNN(len(vocab), batch_size=batch_size, num_steps=num_steps,
                lstm_size=lstm_size, num_layers=num_layers,
                learning_rate=learning_rate)

saver = tf.train.Saver(max_to_keep=100)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    # Use the line below to load a checkpoint and resume training
    saver.restore(sess, 'checkpoints/i5400_l512.ckpt')
    counter = 0
    print('Started training...')
    for e in range(epochs):
        # Train network
        new_state = sess.run(model.initial_state)
        loss = 0
        for x, y in get_batches(encoded, batch_size, num_steps):
            counter += 1
            start = time.time()
            feed = {model.inputs: x,
                    model.targets: y,
                    model.keep_prob: keep_prob,
                    model.initial_state: new_state}
            batch_loss, new_state, _ = sess.run([model.loss,
                                                 model.final_state,
                                                 model.optimizer],
                                                 feed_dict=feed)

            end = time.time()

            if (counter % save_every_n == 0):
                print('Epoch: {}/{}... '.format(e+1, epochs),
                      'Training Step: {}... '.format(counter),
                      'Training loss: {:.4f}... '.format(batch_loss),
                      '{:.4f} sec/batch'.format((end-start)))

                saver.save(sess, "checkpoints/i{}_l{}.ckpt".format(counter, lstm_size))

    saver.save(sess, "checkpoints/i{}_l{}.ckpt".format(counter, lstm_size))


I am working on the file "LatinLeaRNNr.ipynb".
The first task is:
Training loss
Next up is the training loss. We get the logits and targets and calculate the softmax cross-entropy loss. First we need to one-hot encode the targets, we're getting them as encoded characters. Then, reshape the one-hot targets so it's a 2D tensor with size $(MN) \times C$ where $C$ is the number of classes/characters we have. Remember that we reshaped the LSTM outputs and ran them through a fully connected layer with $C$ units. So our logits will also have size $(MN) \times C$.
Then we run the logits and targets through tf.nn.softmax_cross_entropy_with_logits and find the mean to get the loss.

Exercise: Implement the loss calculation in the function below.
Can you write Python code for it?

def build_loss(logits, targets, lstm_size, num_classes):
    ''' Calculate the loss from the logits and the targets.

        Arguments
        ---------
        logits: Logits from final fully connected layer
        targets: Targets for supervised learning
        lstm_size: Number of LSTM hidden units
        num_classes: Number of classes in targets

    '''

    # One-hot encode targets and reshape to match logits, one row per sequence per step
    y_one_hot = tf.one_hot(targets, num_classes)
    y_reshaped =  tf.reshape(y_one_hot, logits.get_shape())

    # Softmax cross entropy loss
    loss = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=y_reshaped)
    loss = tf.reduce_mean(loss)

    return loss


I am working on the file "LatticeBoltzmann/LatticeBoltzmannMethod.ipynb".
The first task is:
$d2q9$
cf.Jonas Tölke.  Implementation of a Lattice Boltzmann kernel using the Compute
Unified Device Architecture developed by nVIDIA. Comput. Visual Sci. DOI 10.1007/s00791-008-0120-2
Affine Spaces
Lattices, that are "sufficiently" Galilean invariant, through non-perturbative algebraic theory
cf. http://staff.polito.it/pietro.asinari/publications/preprint_Asinari_PA_2010a.pdf, I. Karlin and P. Asinari, Factorization symmetry in the lattice Boltzmann method.  Physica A 389, 1530 (2010).  The prepaper that this seemd to be based upon and had some more calculation details is
Maxwell Lattices in 1-dim.
Maxwell's (M) moment relations
Can you write Python code for it?

#u = Symbol("u",assume="real")
u = Symbol("u",real=True)
#T_0 =Symbol("T_0",assume="positive")
T_0 =Symbol("T_0",real=True,positive=True)

#v = Symbol("v",assume="real")
v = Symbol("v",real=True)

#phi_v = sqrt( pi/(Rat(2)*T_0))*exp( - (v-u)**2/(Rat(2)*T_0))
phi_v = sqrt( pi/(2*T_0))*exp( - (v-u)**2/(2*T_0))

integrate(phi_v,v)

integrate(phi_v,(v,-oo,oo))

(integrate(phi_v,v).subs(u,oo)- integrate(phi_v,v).subs(u,-oo)).expand()

integrate(phi_v,(v,-oo,oo),conds='none')

integrate(phi_v,(v,-oo,oo),conds='separate')


I am working on the file "Lauttasaari_tms_averages.ipynb".
The first task is:
Since the time series is at uneven intervals some reductions have to be made.
Can you write Python code for it?

laru['tunti'] = (laru['aika'] / 100).apply(np.floor)
laru


I am working on the file "Layers of the Earth-Updated.ipynb".
The first task is:
Questions

Where are earthquakes most likely to occur?
What is the name for movements between plates?
Using the data above, which earthquake caused had a stronger impact?
Using the link provided above, analyze the table given and identify each locations magnitude type along with its range.

Temperature and Pressure
Earth's internal temperature increases with depth.  This increase rate is not linear, though. As shown in the data below, the temperature increases quickly through the crust at about 20°C per kilometer. The temperature then increases more slowly as we descend through the mantle, sharply increases at the base of the mantle, and then increases slowly through the core. The temperature is around 1000°C at the base of the crust, around 3500°C at the base of the mantle, and around 5,000°C at Earth’s center.
Can you write Python code for it?

# Read the data that will be used for answering the questions below.
data = pd.read_csv("./temp.csv")

# Observe the first 20 rows and plot provided for you below.
data.head(20)

fig = plt.figure(figsize=(10,3))
plt.plot(data['Depth in km'],data['Temperature in C'])
plt.ylabel('Temperature in C')
plt.xlabel('Depth in km')

plt.annotate('Lithosphere', xy=(0,0), xytext=(90,0))
plt.annotate('Asthenosphere', xy=(100,1100), xytext=(500,1100),
            arrowprops=dict(facecolor='yellow', shrink=0.02))
plt.annotate('Mantle', xy=(400,1900), xytext=(1000,1900),
             arrowprops=dict(facecolor='green',shrink=0.1))
plt.annotate('Outer Core', xy=(2800,3840), xytext=(3200,3840),
            arrowprops=dict(facecolor='orange'))
plt.annotate('Inner Core', xy=(5100,5400), xytext=(5100,4300),
            arrowprops=dict(facecolor='red'))

# Note: the arrows on the graph point out where the specific layer begins

# Observe the following graph, it will also be needed to answer the questions below.
data = pd.read_csv("./Pressure_Data.csv")

# Note: 1 on the y-axis is equal to 1 x 10^6.
fig = plt.figure(figsize=(10,3))
plt.plot(data['Depth in km'],data['Pressure in bar'], 'r--')
plt.ylabel('Pressure [10^6bar]')
plt.xlabel('Depth [km]')

plt.annotate('Mantle', xy=(1500,1.5))
plt.annotate('Core', xy=(4800,1.0))


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
Evaluate the Model
Once you are completely satisfied with your model, evaluate the performance of the model on the test set.
Be sure to only do this once!
If you were to measure the performance of your trained model on the test set, then improve your model, and then measure the performance of your model on the test set again, that would invalidate your test results. You wouldn't get a true measure of how well your model would perform against real data.
You do not need to modify this section.
Can you write Python code for it?

with tf.Session() as sess:
    saver.restore(sess, tf.train.latest_checkpoint('.'))

    test_accuracy = evaluate(X_test, y_test)
    print("Test Accuracy = {:.3f}".format(test_accuracy))


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
Features and Labels
Train LeNet to classify MNIST data.
x is a placeholder for a batch of input images.
y is a placeholder for a batch of output labels.
You do not need to modify this section.
Can you write Python code for it?

x = tf.placeholder(tf.float32, (None, 32, 32, 1))
y = tf.placeholder(tf.int32, (None))
one_hot_y = tf.one_hot(y, 10)


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
Preprocess Data
Shuffle the training data.
You do not need to modify this section.
Can you write Python code for it?

from sklearn.utils import shuffle

X_train, y_train = shuffle(X_train, y_train)


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
SOLUTION: Implement LeNet-5
Implement the LeNet-5 neural network architecture.
This is the only cell you need to edit.
Input
The LeNet architecture accepts a 32x32xC image as input, where C is the number of color channels. Since MNIST images are grayscale, C is 1 in this case.
Architecture
Layer 1: Convolutional. The output shape should be 28x28x6.
Activation. Your choice of activation function.
Pooling. The output shape should be 14x14x6.
Layer 2: Convolutional. The output shape should be 10x10x16.
Activation. Your choice of activation function.
Pooling. The output shape should be 5x5x16.
Flatten. Flatten the output shape of the final pooling layer such that it's 1D instead of 3D. The easiest way to do is by using tf.contrib.layers.flatten, which is already imported for you.
Layer 3: Fully Connected. This should have 120 outputs.
Activation. Your choice of activation function.
Layer 4: Fully Connected. This should have 84 outputs.
Activation. Your choice of activation function.
Layer 5: Fully Connected (Logits). This should have 10 outputs.
Output
Return the result of the 2nd fully connected layer.
Can you write Python code for it?

from tensorflow.contrib.layers import flatten

def LeNet(x):
    # Arguments used for tf.truncated_normal, randomly defines variables for the weights and biases for each layer
    mu = 0
    sigma = 0.1

    # SOLUTION: Layer 1: Convolutional. Input = 32x32x1. Output = 28x28x6.
    conv1_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 1, 6), mean = mu, stddev = sigma))
    conv1_b = tf.Variable(tf.zeros(6))
    conv1   = tf.nn.conv2d(x, conv1_W, strides=[1, 1, 1, 1], padding='VALID') + conv1_b

    # SOLUTION: Activation.
    conv1 = tf.nn.relu(conv1)

    # SOLUTION: Pooling. Input = 28x28x6. Output = 14x14x6.
    conv1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    # SOLUTION: Layer 2: Convolutional. Output = 10x10x16.
    conv2_W = tf.Variable(tf.truncated_normal(shape=(5, 5, 6, 16), mean = mu, stddev = sigma))
    conv2_b = tf.Variable(tf.zeros(16))
    conv2   = tf.nn.conv2d(conv1, conv2_W, strides=[1, 1, 1, 1], padding='VALID') + conv2_b

    # SOLUTION: Activation.
    conv2 = tf.nn.relu(conv2)

    # SOLUTION: Pooling. Input = 10x10x16. Output = 5x5x16.
    conv2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')

    # SOLUTION: Flatten. Input = 5x5x16. Output = 400.
    fc0   = flatten(conv2)

    # SOLUTION: Layer 3: Fully Connected. Input = 400. Output = 120.
    fc1_W = tf.Variable(tf.truncated_normal(shape=(400, 120), mean = mu, stddev = sigma))
    fc1_b = tf.Variable(tf.zeros(120))
    fc1   = tf.matmul(fc0, fc1_W) + fc1_b

    # SOLUTION: Activation.
    fc1    = tf.nn.relu(fc1)

    # SOLUTION: Layer 4: Fully Connected. Input = 120. Output = 84.
    fc2_W  = tf.Variable(tf.truncated_normal(shape=(120, 84), mean = mu, stddev = sigma))
    fc2_b  = tf.Variable(tf.zeros(84))
    fc2    = tf.matmul(fc1, fc2_W) + fc2_b

    # SOLUTION: Activation.
    fc2    = tf.nn.relu(fc2)

    # SOLUTION: Layer 5: Fully Connected. Input = 84. Output = 10.
    fc3_W  = tf.Variable(tf.truncated_normal(shape=(84,...


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
The MNIST data that TensorFlow pre-loads comes as 28x28x1 images.
However, the LeNet architecture only accepts 32x32xC images, where C is the number of color channels.
In order to reformat the MNIST data into a shape that LeNet will accept, we pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).
You do not need to modify this section.
Can you write Python code for it?

import numpy as np

# Pad images with 0s
X_train      = np.pad(X_train, ((0,0),(2,2),(2,2),(0,0)), 'constant')
X_validation = np.pad(X_validation, ((0,0),(2,2),(2,2),(0,0)), 'constant')
X_test       = np.pad(X_test, ((0,0),(2,2),(2,2),(0,0)), 'constant')

print("Updated Image Shape: {}".format(X_train[0].shape))


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
Train the Model
Run the training data through the training pipeline to train the model.
Before each epoch, shuffle the training set.
After each epoch, measure the loss and accuracy of the validation set.
Save the model after training.
You do not need to modify this section.
Can you write Python code for it?

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(X_train)

    print("Training...")
    print()
    for i in range(EPOCHS):
        X_train, y_train = shuffle(X_train, y_train)
        for offset in range(0, num_examples, BATCH_SIZE):
            end = offset + BATCH_SIZE
            batch_x, batch_y = X_train[offset:end], y_train[offset:end]
            sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})

        validation_accuracy = evaluate(X_validation, y_validation)
        print("EPOCH {} ...".format(i+1))
        print("Validation Accuracy = {:.3f}".format(validation_accuracy))
        print()

    saver.save(sess, './lenet')
    print("Model saved")


I am working on the file "LeNet-Lab-Solution.ipynb".
The first task is:
Visualize Data
View a sample from the dataset.
You do not need to modify this section.
Can you write Python code for it?

import random
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

index = random.randint(0, len(X_train))
image = X_train[index].squeeze()

plt.figure(figsize=(1,1))
plt.imshow(image, cmap="gray")
print(y_train[index])


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Evaluate the Model
Once you are completely satisfied with your model, evaluate the performance of the model on the test set.
Be sure to only do this once!
If you were to measure the performance of your trained model on the test set, then improve your model, and then measure the performance of your model on the test set again, that would invalidate your test results. You wouldn't get a true measure of how well your model would perform against real data.
You do not need to modify this section.
Can you write Python code for it?

with tf.Session() as sess:
    saver.restore(sess, tf.train.latest_checkpoint('./models'))

    test_accuracy = evaluate(X_test, y_test)
    print("Test Accuracy = {:.3f}".format(test_accuracy))


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Features and Labels
Train LeNet to classify MNIST data.
x is a placeholder for a batch of input images.
y is a placeholder for a batch of output labels.
You do not need to modify this section.
Can you write Python code for it?


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Model Evaluation
Evaluate how well the loss and accuracy of the model for a given dataset.
You do not need to modify this section.
Can you write Python code for it?

correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(one_hot_y, 1))
accuracy_operation = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()

def evaluate(X_data, y_data):
    num_examples = len(X_data)
    total_accuracy = 0
    sess = tf.get_default_session()
    for offset in range(0, num_examples, BATCH_SIZE):
        batch_x, batch_y = X_data[offset:offset+BATCH_SIZE], y_data[offset:offset+BATCH_SIZE]
        accuracy = sess.run(accuracy_operation, feed_dict={x: batch_x, y: batch_y})
        total_accuracy += (accuracy * len(batch_x))
    return total_accuracy / num_examples


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Preprocess Data
Shuffle the training data.
You do not need to modify this section.
Can you write Python code for it?


I am working on the file "LeNet-Lab.ipynb".
The first task is:
The MNIST data that TensorFlow pre-loads comes as 28x28x1 images.
However, the LeNet architecture only accepts 32x32xC images, where C is the number of color channels.
In order to reformat the MNIST data into a shape that LeNet will accept, we pad the data with two rows of zeros on the top and bottom, and two columns of zeros on the left and right (28+2+2 = 32).
You do not need to modify this section.
Can you write Python code for it?


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Train the Model
Run the training data through the training pipeline to train the model.
Before each epoch, shuffle the training set.
After each epoch, measure the loss and accuracy of the validation set.
Save the model after training.
You do not need to modify this section.
Can you write Python code for it?

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    num_examples = len(X_train)

    print("Training...")
    print()
    for i in range(EPOCHS):
        X_train, y_train = shuffle(X_train, y_train)
        for offset in range(0, num_examples, BATCH_SIZE):
            end = offset + BATCH_SIZE
            batch_x, batch_y = X_train[offset:end], y_train[offset:end]
            sess.run(training_operation, feed_dict={x: batch_x, y: batch_y})

        validation_accuracy = evaluate(X_validation, y_validation)
        print("EPOCH {} ...".format(i+1))
        print("Validation Accuracy = {:.3f}".format(validation_accuracy))
        print()

    saver.save(sess, './models/lenet')
    print("Model saved")


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Training Pipeline
Create a training pipeline that uses the model to classify MNIST data.
You do not need to modify this section.
Can you write Python code for it?

rate = 0.001

logits = LeNet(x)
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=one_hot_y, logits=logits)
loss_operation = tf.reduce_mean(cross_entropy)
optimizer = tf.train.AdamOptimizer(learning_rate = rate)
training_operation = optimizer.minimize(loss_operation)


I am working on the file "LeNet-Lab.ipynb".
The first task is:
Visualize Data
View a sample from the dataset.
You do not need to modify this section.
Can you write Python code for it?


I am working on the file "LeNet5.ipynb".
The first task is:
Building The Model
Activation Function & Regularization
We decided to use the Exponential Linear Unit (ELU) as described in <u>Fast and accurate deep network learning by exponetial lienar units</u> by Clevert et al., since this non-linear function displayed quicker convergence and yielded a higher validation score relative to a sigmoid, tanh, or ReLu function. Furthermore, to reduce overfitting we used $l_2$ regularization which acts by penalizing our model by adding a complexity term that would yields a higher loss for more complex models. Both the activation function and regularization method or defined below.
Can you write Python code for it?

def elu(x, alpha=1.0):
    return T.switch(x > 0, x, T.exp(x)-1)

def l2_reg(x, lmbd=0.05):
    """
    L_2 regularization
    """

    l2 = 0
    for elements in x:
        l2 += T.sum(elements[0]**2)

    return lmbd / 2 * l2


I am working on the file "LeNet5.ipynb".
The first task is:
Convolutional Neural Network
Can you write Python code for it?

X = T.tensor4(name='X', dtype=theano.config.floatX)
Y = T.imatrix(name='Y')
y = T.ivector(name='y')
lr = T.scalar(name='learning_rate', dtype=theano.config.floatX)

nkerns = [8, 32]
batch_size = 256
act_f = elu

conv_layer1 = ConvLayer(input=X,
                        filter_shape=(nkerns[0], 1, 3, 3),
                        image_shape=(batch_size, 1, 28, 28),
                        activation_fn=None)
pool_layer1 = PoolingLayer(input=conv_layer1.output,
                           activation_fn=act_f)
conv_layer2 = ConvLayer(input=pool_layer1.output,
                        filter_shape=(nkerns[1], nkerns[0], 5, 5),
                        image_shape=(batch_size, nkerns[0], 13, 13),
                        activation_fn=None)
pool_layer2 = PoolingLayer(input=conv_layer2.output,
                           activation_fn=act_f)

# outputs from convolution network need to be flattend before being
# passed through to the the fully-connected layer
fc_layer_input = pool_layer2.output.flatten(2)

fc_layer1 = FC(input=fc_layer_input,
                n_in=nkerns[1] * 4 * 4,
                n_out=512,
                activation_fn=act_f)
fc_layer2 = FC(input=fc_layer1.output,
               n_in=512,
               n_out=10,
               activation_fn=act_f)


I am working on the file "LeNet5.ipynb".
The first task is:
Fully Connected Layer
The Fully Connected layer is a traditional Multi Layer Perceptron hence we need to take into account the following arguments:
input: The input feature map

n_in: The number of input neurons

n_out: The number of output neurons.

W: (Optional) Load pretrained weights

b: (Optional) Load pretrained biases.

activation_fn: A nonlinear function used to transform our input; generally representing the rate of action potential firing for a given pixel. The purpose of which is to ensure that the representation in the input is mapped to a different space in the output.
Can you write Python code for it?

class FC(object):

    def __init__(self, input, n_in, n_out, W=None, b=None, seed=35,
                 activation_fn=None):

        # rng = np.random.RandomState(seed)

        self.input = input

        if W is None:
            W_values = np.random.uniform(low=-np.sqrt(6./(n_in+n_out)),
                                   high=np.sqrt(6./(n_in+n_out)),
                                   size=(n_out, n_in)).astype(theano.config.floatX)

            if activation_fn == theano.tensor.nnet.sigmoid:
                W_values *= 4

            W = theano.shared(name='Weights', value=W_values, borrow=True)

        if b is None:
            b_values = np.zeros(n_out, dtype=theano.config.floatX)
            b = theano.shared(name='bias', value=b_values, borrow=True)

        self.W = W
        self.b = b

        l_output = (T.dot(self.W, input.T)).T + self.b
        self.output = (l_output if activation_fn is None
                       else activation_fn(l_output))

        self.params = [self.W, self.b]


I am working on the file "LeNet5.ipynb".
The first task is:
Momentum
Momentum is a varient of Vanilla SGD that helps accelerate SGD in the relevant direction and dampens oscillations. It does this by adding a fraction $\gamma$ of the update vector of the past time step to the current update vector. Nevertheless, like Vanilla SGD it still has difficulty escaping a saddle point.
The update rule is given by:
<h5 align="center">
$\begin{align}
v_{t+1} &= \gamma v_t + \eta \nabla C\\
x_{t+1} &=  x_t- v_{t+1}
\end{align}$
</h5>

where $v_t$ can be considered as the velocity.
Can you write Python code for it?

def momentum(l_rate, parameters, grads, momentum=0.9):

    def update_rule(param, velocity, df):
        v_next = momentum * velocity - l_rate * df
        updates = (param, param+v_next), (velocity, v_next)

        return updates

    assert momentum <=1 and momentum >= 0

    velocities = [theano.shared(name='v_%s' % param,
                                value=param.get_value() * 0.,
                                broadcastable=param.broadcastable)
                  for param in parameters]

    updates = []
    for p, v, g in zip(parameters, velocities, grads):
        param_updates, vel_updates = update_rule(p, v, g)
        updates.append(param_updates)
        updates.append(vel_updates)

    return updates


I am working on the file "LeNet5.ipynb".
The first task is:
Stochastic Gradient Descent Methods (SGD)
We present three (3) different SGD algorithm used to minimize our cost function. For more option we encourage the reader to see Theano_gradient_descent.
Vanilla SGD
This is the simplest form of update, it changes the parameters along the negative gradient direction. Vanilla SGD suffers from a slow convergence and displays a difficutly escaping from a saddle point.
The update rule is given by:

$
x_{t+1} = x_{t} - \eta \nabla C
$

Can you write Python code for it?

def sgd(l_rate, parameters, grads):

    updates = []
    for param, grad in zip(parameters, grads):
        updates.append((param, param - l_rate * grad))

    return updates


I am working on the file "LeNet5.ipynb".
The first task is:
Training and Validating
To train the network we need to define a training and validation function using theano.function(). Using RMSProp as our SGD method we create the necessary functions below.  All the functions are defined in a similar manner, both validation function and a prediction functions omit the update argument, since they are used for pushing the data forward.
Can you write Python code for it?

train = theano.function(inputs=[X, Y, lr], outputs=cost,
                        updates=rmsprop(l_rate=lr, parameters=params,
                                     grads=grads),
                        allow_input_downcast=True)

# Validation results
pred_result = cost_input.argmax(axis=1)
accu = theano.function(inputs=[X, y], outputs=T.sum(T.eq(pred_result, y)),
                       allow_input_downcast=True)

pred = theano.function(inputs=[X], outputs=pred_result,
                       allow_input_downcast=True)


I am working on the file "LeNet5.ipynb".
The first task is:
Training the Model
The last stage involves creating a function that shuffles and partitions into appropriate sized batches before being processed further during each epoch.
Can you write Python code for it?

def train_model(training_data, validation_data, test_data=None,
                learning_rate=1e-4, epochs=100):

    print('---Training Model---')
    predicted_results = []

    total_values, total_val_values = len(training_data), len(validation_data)

    for epoch in range(epochs):
        print('Currently on epoch {}'.format(epoch+1))
        np.random.shuffle(training_data)

        mini_batches = [training_data[k: k+batch_size]
                        for k in range(0, total_values, batch_size)]
        validation_batches = [validation_data[m: m+batch_size]
                              for m in range(0, total_val_values, batch_size)]

        training_cost, accuracy = 0, 0
        training_cost_list, accuracy_list = [], []

        for mini_batch in mini_batches:
            labels = mini_batch[:, 0]
            label_matrix = np.zeros(shape=(256, 10), dtype=theano.config.floatX)

            for i, label in enumerate(labels):
                vec = scalar_to_vec(int(label), 10)
                label_matrix[i] = vec

            digits = mini_batch[:, 1:]/255
            digits = digits.reshape(-1, 1, 28, 28)
            cost_ij = train(digits, label_matrix, learning_rate)
            training_cost += cost_ij

        for val_batch in validation_batches:
            labels = mini_batch[:, 0]
            label_matrix = np.zeros(shape=(256, 10), dtype=theano.config.floatX)

            for i, label in enumerate(labels):
                vec = scalar_to_vec(int(label), 10)
                label_matrix[i] = vec

            digits = mini_batch[:, 1:]/255
            digits = digits.reshape(-1, 1, 28, 28)
            accuracy += accu(digits, labels)

        training_cost_list.append(training_cost/total_values)
        accuracy_list.append(accuracy/total_val_values)

        print('The accuracy is: {}'.format(accuracy/total_val_values))
        print('The loss is: {}'.format(training_cost/total_values))
        print('--------------------------')

    if np.any(test_data):
       ...


I am working on the file "Leaderboard.ipynb".
The first task is:
ALFA + fo-MAML (alfa_maml)
Not included in the global table as it performs worse than ALFA + fo-Proto-MAML overall, but provided here for reference.
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
ALFA + fo-Proto-MAML (alfa_protomaml)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Add rankings
Can you write Python code for it?

def is_difference_significant(best_stats, candidate_stats):
  # compute a 95% confidence for the difference of means.
  ci = 1.96 * np.sqrt((best_stats['stddev'] ** 2) / best_stats['# episodes'] +
                      (candidate_stats['stddev'] ** 2) / candidate_stats['# episodes'])
  diff_of_means = best_stats['mean (%)'] - candidate_stats['mean (%)']
  return np.abs(diff_of_means) > ci

def compute_ranks(dataset_series):
  dataset_df = dataset_series.unstack()
  n_models = len(dataset_df.index)
  remaining_models = list(dataset_df.index)
  next_available_rank = 1
  ranks = {}
  # Iteratively pick the best models, then all the ones statistically equivalent
  while remaining_models:
    accuracies = dataset_df.loc[remaining_models]['mean (%)'].astype('d')
    # best_model = accuracies.idxmax(axis=1)  #previous
    best_model = accuracies.idxmax(axis=0)
    best_stats = dataset_df.loc[best_model]
    tied_models = [best_model]
    potential_tied_models = [model for model in remaining_models
                             if model != best_model]
    for candidate in potential_tied_models:
      candidate_stats = dataset_df.loc[candidate]
      if not is_difference_significant(best_stats, candidate_stats):
        tied_models.append(candidate)

    n_ties = len(tied_models)
    # All tied models share the same rank, which is the average of the next
    # `n_ties` available ranks (the ranks they would have without the ties), or
    # next_available_rank + (1 + ... + (n_ties - 1)) / n_ties, which gives:
    shared_rank = next_available_rank + (n_ties - 1) / 2
    next_available_rank += n_ties
    for model in tied_models:
      ranks[model] = shared_rank

    # Remove picked models for next iteration
    remaining_models = [model for model in remaining_models
                        if model not in tied_models]
  return pd.Series(ranks, name='rank')

def add_ranks(df):
  # Get ranks as a data frame (ignore "ILSVRC (valid)")
  ranks = df[1:].apply(compute_ranks, axis=1)
  # Set the columns as (model, 'rank')...


I am working on the file "Leaderboard.ipynb".
The first task is:
Add stddev
Can you write Python code for it?

def add_stddev(df):
  # Extract original order of labels
  datasets = df.index
  models = df.columns.levels[0]
  # Have only one result (mean, CI, ...) per row
  stacked_df = df.stack(0)
  # Add 'stddev' as column
  stacked_df['stddev'] = stacked_df['95% CI'] * np.sqrt(stacked_df['# episodes']) / 1.96
  # Reshape and put back in original order
  new_df = stacked_df.unstack().swaplevel(0, 1, axis=1)
  new_df = new_df.loc[datasets][models]
  return new_df

imagenet_df = add_stddev(imagenet_df)
imagenet_df

all_df = add_stddev(all_df)
all_df


I am working on the file "Leaderboard.ipynb".
The first task is:
BOHB (bohb)
Validated on S1 (ImageNet) only, nearest-centroid classifier (NC).
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
CNAPs (cnaps)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
CrossTransformers (ctx)
CrossTransformers network with:
- 224x224 input size
- ResNet-34 backbone
- SimCLR episodes
- 14x14 feature grid
- BOHB-inspired data augmentation
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Display in MarkDown
At least, in GitHub-flavored MarkDown.
Can you write Python code for it?

def md_render(series):
  # Summarize each (episode, model) by a single cell containing MarkDown
  nbsp = ' '
  md_string = '%(bold)s%(acc)5.2f%(bold)s±%(ci)4.2f%(nbsp)s(%(rank)g)' % {
      'acc': series['mean (%)'],
      'ci': series['95% CI'],
      'rank': series['rank'],
      'bold': '**' if series['best_rank'] else '',
      'nbsp': nbsp
  }
  return md_string

def md_table(df, models=None):
  # Whether a model has the best rank on a given dataset
  rank_df = df.xs('rank', axis=1, level=1).loc[df.index[1:]]
  best_rank = pd.concat([rank_df.T == rank_df.min(axis=1)], axis=1,
                        keys=['best_rank']).swaplevel(0, 1, axis=1)
  accuracies_df = df[1:].T.unstack(1)
  accuracies_df = pd.concat([accuracies_df, best_rank], axis=1)
  accuracies_md = accuracies_df.stack(0).apply(md_render, axis=1).unstack(1)

  # Average rank (and whether it's the best)
  avg_rank_df = pd.DataFrame(rank_df.mean(), columns=['Avg rank'])
  best_avg_rank = (avg_rank_df == avg_rank_df.min()).rename(
      columns={'Avg rank': 'best_rank'})
  avg_rank_md = pd.concat([avg_rank_df, best_avg_rank], axis=1).apply(
      lambda s: '%(bold)s%(avg_rank)g%(bold)s' % {
          'avg_rank': s['Avg rank'],
          'bold': '**' if s['best_rank'] else ''
      },
      axis=1).rename('Avg rank')

  # Display method name with a pointer to the reference, defined later.
  ref_to_link = {ref[0]: "[[%i]]" % i for i, ref in enumerate(references, 1)}
  method_md = models_df.apply(lambda r: '['+','.join([ref_to_link[item][1:-1] for item in r['ref']])+']'
                              if (type(r['ref']) is list) else ref_to_link[r['ref']],
                              axis='columns')

  display_md = pd.concat([avg_rank_md, accuracies_md[df.index[1:]]], axis=1)
  if models:
    # Try and force a particular order of models
    display_md = display_md.loc[list(models)]

  # Pad all cells so they align well, 27 chars should be enough
  header_str = '|'.join(['%-27s' % c
                         for c in ['Method'] +...


I am working on the file "Leaderboard.ipynb".
The first task is:
Full section
Can you write Python code for it?

def export_md():
  begin_line = '<!-- Beginning of content generated by `Leaderboard.ipynb` -->'
  end_line = '<!-- End of content generated by `Leaderboard.ipynb` -->'

  parts = [
      begin_line,
      '## Training on ImageNet only',
      md_table(imagenet_df, models=imagenet_dfs.keys()),
      '## Training on all datasets',
      md_table(all_df, models=all_dfs.keys()),
      '## References',
      ref_list(),
      end_line
  ]
  return '\n\n'.join(parts)

print(export_md())


I am working on the file "Leaderboard.ipynb".
The first task is:
Meta-Dataset leaderboard
This notebook computes leaderboard tables for different models on Meta-Dataset.
Results for each model in each training setting (ImageNet-only or all datasets) are defined in a different DataFrame. This script aggregates the data in one DataFrame, ranks the models in each setting (using a statistical test for equality), and produces the final tables.
Can you write Python code for it?

import re
import textwrap

import numpy as np
import pandas as pd
from IPython import display

# Explicit list of evaluation datasets.
# ILSVRC (valid) is included for completeness, but does not have to be reported.
datasets = [
    "ILSVRC (valid)",
    "ILSVRC (test)",
    "Omniglot",
    "Aircraft",
    "Birds",
    "Textures",
    "QuickDraw",
    "Fungi",
    "VGG Flower",
    "Traffic signs",
    "MSCOCO"
]

# Explicit list of articles and references, filled in throughout the notebook.
references = []


I am working on the file "Leaderboard.ipynb".
The first task is:
ProtoNet (prototypical)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
ProtoNet large (protonet_large)
Larger-scale prototypical networks, including:
- 224x224 input size
- ResNet-34 backbone
- SimCLR Episodes
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
RelationNet (relationnet)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Baik et al. (2020)
Can you write Python code for it?

ref = ("Baik et al. (2020)",
       "Sungyong Baik, Myungsub Choi, Janghoon Choi, Heewon Kim, Kyoung Mu Lee; "
       "[_Meta-Learning with Adaptive Hyperparameters_]"
       "(https://papers.nips.cc/paper/2020/hash/ee89223a2b625b5152132ed77abbcc79-Abstract.html); "
       "NeurIPS 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Bateni et al. (2020)
Can you write Python code for it?

ref = ("Bateni et al. (2020)",
       "Peyman Bateni, Raghav Goyal, Vaden Masrani, Frank Wood, Leonid Sigal; "
       "[_Improved Few-Shot Visual Classification_]"
       "(https://openaccess.thecvf.com/content_CVPR_2020/html/Bateni_Improved_Few-Shot_Visual_Classification_CVPR_2020_paper.html); "
       "CVPR 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Doersch et al. (2020)
Carl Doersch, Ankush Gupta, Andrew Zisserman,
CrossTransformers: spatially-aware few-shot transfer,
NeurIPS 2020
Can you write Python code for it?

ref = ("Doersch et al. (2020)",
       "Carl Doersch, Ankush Gupta, Andrew Zisserman; "
       "[_CrossTransformers: spatially-aware few-shot transfer_]"
       "(https://arxiv.org/abs/2007.11498); "
       "NeurIPS 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Dvornik et al. (2020)
Can you write Python code for it?

ref = ("Dvornik et al. (2020)",
       "Nikita Dvornik, Cordelia Schmid, Julien Mairal; "
       "[_Selecting Relevant Features from a Multi-domain Representation for "
       "Few-shot Classification_](https://arxiv.org/abs/2003.09338); "
       "ECCV 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Li et al. (2021a)
Wei-Hong Li, Xialei Liu, Hakan Bilen. Universal Representation Learning from Multiple Domains for Few-shot Classification. ICCV 2021.
Can you write Python code for it?

ref = ("Li et al. (2021a)",
       "Wei-Hong Li, Xialei Liu, Hakan Bilen; "
       "[_Universal Representation Learning from Multiple Domains for Few-shot Classification_]"
       "(https://arxiv.org/pdf/2103.13841.pdf); "
       "ICCV 2021.")
references.append(ref)
# display.display(display.Markdown(ref[1]))

url_all_df = pd.DataFrame(
    columns=['mean (%)', '95% CI', '# episodes'],
    index=datasets
)
url_all_df['# episodes'] = 600
url_all_df.loc[datasets[1:], ['mean (%)', '95% CI']] = [
    [57.51, 1.08],
    [94.51, 0.41],
    [88.59, 0.46],
    [80.54, 0.69],
    [76.17, 0.67],
    [81.94, 0.56],
    [68.75, 0.95],
    [92.11, 0.48],
    [63.34, 1.19],
    [54.03, 0.96],
]
url_all_df


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Liu et al. (2021)
Lu Liu, William Hamilton, Guodong Long, Jing Jiang, Hugo Larochelle,
A Universal Representation Transformer Layer for Few-Shot Image Classification, ICLR 2021
Can you write Python code for it?

ref = ("Liu et al. (2021a)",
       "Lu Liu, William Hamilton, Guodong Long, Jing Jiang, Hugo Larochelle; "
       "[_Universal Representation Transformer Layer for Few-Shot Image "
       "Classification_](https://arxiv.org/abs/2006.11702); "
       "ICLR 2021.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Liu et al. (2021)
Yanbin Liu, Juho Lee, Linchao Zhu, Ling Chen, Humphrey Shi and Yi Yang. A Multi-Mode Modulator for Multi-Domain Few-Shot Classification. ICCV 2021.
Can you write Python code for it?

ref = ("Liu et al. (2021b)",
       "Yanbin Liu, Juho Lee, Linchao Zhu, Ling Chen, Humphrey Shi, Yi Yang; "
       "[_A Multi-Mode Modulator for Multi-Domain Few-Shot Classification_]"
       "(https://openaccess.thecvf.com/content/ICCV2021/papers/Liu_A_Multi-Mode_Modulator_for_Multi-Domain_Few-Shot_Classification_ICCV_2021_paper.pdf); "
       "ICCV 2021.")
references.append(ref)

triM_all_df = pd.DataFrame(
    columns=['mean (%)', '95% CI', '# episodes'],
    index=datasets
)
triM_all_df['# episodes'] = 600
triM_all_df.loc[1:, ['mean (%)', '95% CI']] = [
    [58.6, 1.0],
    [92.0, 0.6],
    [82.8, 0.7],
    [75.3, 0.8],
    [71.2, 0.8],
    [77.3, 0.7],
    [48.5, 1.0],
    [90.5, 0.5],
    [63.0, 1.0],
    [52.8, 1.1]
]
triM_all_df


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Saikia et al. (2020)
Tonmoy Saikia, Thomas Brox, Cordelia Schmid, Optimized Generic Feature Learning for Few-shot Classification across Domains, arXiv 2020
Can you write Python code for it?

ref = ("Saikia et al. (2020)",
       "Tonmoy Saikia, Thomas Brox, Cordelia Schmid; "
       "[_Optimized Generic Feature Learning for Few-shot Classification "
       "across Domains_]"
       "(https://arxiv.org/abs/2001.07926); "
       "arXiv 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
Results from Triantafillou et al. (2020)
Can you write Python code for it?

ref = ("Triantafillou et al. (2020)",
       "Eleni Triantafillou, Tyler Zhu, Vincent Dumoulin, Pascal Lamblin, "
       "Utku Evci, Kelvin Xu, Ross Goroshin, Carles Gelada, Kevin Swersky, "
       "Pierre-Antoine Manzagol, Hugo Larochelle; "
       "[_Meta-Dataset: A Dataset of Datasets for Learning to Learn from Few "
       "Examples_](https://arxiv.org/abs/1903.03096); ICLR 2020.")
references.append(ref)
# display.display(display.Markdown(ref[1]))


I am working on the file "Leaderboard.ipynb".
The first task is:
SUR-pnf (sur_pnf)
SUR with parametric network family, also referred as "SUR-pf".
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Task-specific Adapters (tsa)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Task-specific Adapters with ResNet18 (tsa_resnet18)
Task-specific Adapters with:

84x84 input size
ResNet-18 backbone
adapters in matrix form with residual connections
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Task-specific Adapters with ResNet34 (tsa_resnet34)
Task-specific Adapters with:
224x224 input size
ResNet-34 backbone
adapters in matrix form with residual connections
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
Template to add a new paper
```
ref = ("Author et al. (year)",
       "First Author, Second Author, Last Author; "
       "Title of Paper; "
       "Venue year.")
references.append(ref)
display.display(display.Markdown(ref[1]))
```
Template to add a new model
<model_name>_<train_source>_df = pd.DataFrame(
    columns=['mean (%)', '95% CI', '# episodes'],
    index=datasets
)
<model_name>_<train_source>_df['# episodes'] = ...
<model_name>_<train_source>_df.loc[datasets[1:], ['mean (%)', '95% CI']] = [...]
Aggregate in table
Can you write Python code for it?

imagenet_dfs = {
    'k-NN': baseline_imagenet_df,
    'Finetune': baselineft_imagenet_df,
    'MatchingNet': matching_imagenet_df,
    'ProtoNet': prototypical_imagenet_df,
    'fo-MAML': maml_imagenet_df,
    'RelationNet': relationnet_imagenet_df,
    'fo-Proto-MAML': protomaml_imagenet_df,
    'ALFA+fo-Proto-MAML': alfa_protomaml_imagenet_df,
    'ProtoNet (large)': protonet_large_imagenet_df,
    'CTX': ctx_imagenet_df,
    'BOHB': bohb_imagenet_df,
    'SimpleCNAPS': simple_cnaps_imagenet_df,
    'TransductiveCNAPS': transductive_cnaps_imagenet_df,
    'TSA_resnet18': tsa_resnet18_imagenet_df,
    'TSA_resnet34': tsa_resnet34_imagenet_df,
}

imagenet_df = pd.concat(
    imagenet_dfs.values(),
    axis=1,
    keys=imagenet_dfs.keys())
imagenet_df

all_dfs = {
    'k-NN': baseline_all_df,
    'Finetune': baselineft_all_df,
    'MatchingNet': matching_all_df,
    'ProtoNet': prototypical_all_df,
    'fo-MAML': maml_all_df,
    'RelationNet': relationnet_all_df,
    'fo-Proto-MAML': protomaml_all_df,
    'CNAPs': cnaps_all_df,
    'SUR': sur_all_df,
    'SUR-pnf': sur_pnf_all_df,
    'SimpleCNAPS': simple_cnaps_all_df,
    'TransductiveCNAPS': transductive_cnaps_all_df,
    'URT': urt_all_df,
    'URT-pf': urt_pf_all_df,
    'FLUTE': flute_all_df,
    'URL': url_all_df,
    'TSA': tsa_all_df,
    'TriM': triM_all_df,
}
all_df = pd.concat(
    all_dfs.values(),
    axis=1,
    keys=all_dfs.keys())
all_df

models_df = pd.DataFrame.from_dict(
    orient='index',
    columns=["ref"],
    data={
        'k-NN': 'Triantafillou et al. (2020)',
        'Finetune': 'Triantafillou et al. (2020)',
        'MatchingNet': 'Triantafillou et al. (2020)',
        'ProtoNet': 'Triantafillou et al. (2020)',
        'fo-MAML': 'Triantafillou et al. (2020)',
        'RelationNet': 'Triantafillou et al. (2020)',
        'fo-Proto-MAML': 'Triantafillou et al. (2020)',
        'CNAPs': 'Requeima et al. (2019)',
        'ALFA+fo-Proto-MAML': 'Baik et al. (2020)',
        'ProtoNet (large)': 'Doersch et al. (2020)',
        'CTX':...


I am working on the file "Leaderboard.ipynb".
The first task is:
Transductive CNAPS (transductive_cnaps)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
URT (urt)
Can you write Python code for it?


I am working on the file "Leaderboard.ipynb".
The first task is:
URT-pf (urt-pf)
Can you write Python code for it?


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
2. Fetch most frequently played champions
Since we already had a dictionary of all user ids mapping to all categories of ranks, we can now use those user ids to get the stats data of their most frequently used champions.
Riot gives us the API to get all champions that a user had used during the season. And the response will be in JSON format. After parsing the JSON response, what we need to do is to get the most frequently used champions which can represent a player's level. So we sort the champions list by the number of games that the player used this champioin (totalSessionsPlayed) in descending order. Notice that the first element in the list will always be the champion with id 0, which represents the stats data of all champions that the player used in the season. So we need to skip that.
After we filter out the top frequently used champions of a user, we need to save the stats data with the player's tier as the training label into a csv file. In this project, each champion has a corresponding csv file which records all the stats data of this hero with the tier of the player as the training data. Since there are hundreds of champions in League of Legend, we will have hundreds of csv files for training and each file uses the id of champions as the file name. If the file is already created, we will append more stats of this champion to the csv file.
Can you write Python code for it?

class TopChampion:

    FIELD_NAMES = ['totalSessionsPlayed', 'totalSessionsLost', 'totalSessionsWon',
                   'totalChampionKills', 'totalDamageDealt', 'totalDamageTaken',
                   'mostChampionKillsPerSession', 'totalMinionKills', 'totalDoubleKills',
                   'totalTripleKills', 'totalQuadraKills', 'totalPentaKills',
                   'totalUnrealKills', 'totalDeathsPerSession', 'totalGoldEarned',
                   'mostSpellsCast', 'totalTurretsKilled', 'totalPhysicalDamageDealt',
                   'totalMagicDamageDealt', 'totalFirstBlood', 'totalAssists',
                   'maxChampionsKilled', 'maxNumDeaths', 'label']

    def __init__(self, key, player_id, label, n):
        self.label = label
        self.player_id = player_id
        self.key = key
        self.n = n
        self.top_champions = []
        pass

    def get_top_champions(self):
        self.top_champions[:] = []
        data = urllib2.urlopen(
            'https://na.api.pvp.net/api/lol/na/v1.3/stats/by-summoner/' +
            self.player_id +
            '/ranked?season=SEASON2016&api_key=' +
            self.key
        ).read()
        json_data = json.loads(data)
        champions = json_data['champions']
        champion_stats = []
        for champion in champions:
            champion_stat = champion['stats']
            champion_stat['id'] = champion['id']
            champion_stat['label'] = self.label
            champion_stats.append(champion_stat)
            pass
        self.top_champions = sorted(champion_stats,
                                    key=lambda x: x['totalSessionsPlayed'],
                                    reverse=True)[1:self.n + 1]
        return self.top_champions
        pass

    def save_top_champions(self, champion_id=None):
        for champion in self.top_champions:
            if not champion_id:
                champion_id = champion['id']
            file_name = './data/{}.csv'.format(champion['id'])
            if os.path.isfile(file_name):
              ...


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
Summoner Analysis
Definition of KDA (Kill/Death/Assit)
KDA is a ratio factor revealing the survival and game ability of a summoner. The formula could be described as:
KDA = (#Kill + #Assist) / #Death
As a result, the higher KDA is, the better the summoner would be.
Analysis and Suggestions
The radar chart above displays five ability in all the game the summoner played in Season 2016. The red chart represents the summoner's data, and the blue one is the representation of average player in the same predicted tier (in this chart is tier Diamond, and all the data refers to ADC role).
We have already pre-processing the data from per-session to per-game. As we can see in the chart, the summoner's total CS (minion kills) per game is higher than average, and so are the damage and gold earned. However, the summoner's KDA and win rate is lower than average.
The comparison shows that the summoner is able to obtain map resources fast, and always take advantage in lane confrontation and lane pushing. However, when it refers to group battle, especially for ADC role, the summoner should be more aware of the damage dealing position and survival.
Lower KDA and lower winning rate than average might also reveals that the number of team fight engagement in one game is insufficient. League of Legends is a team strategic game, and if the summoner wants to win the game, he/she should group more and try to engage in team fight as much as possible.
Champion Popularity
With the data crawled, we can also visualize the popularities of all champions in LOL. Here, we used the word cloud library which is a very popular data visualization library. The code below will measure how frequently each champion is used in the games from the data we crawled, and it will draw a word cloud image to display the result.
Can you write Python code for it?

def count_champion(api_key):
    res = dict()
    file_names = glob.glob('./data/*.csv')
    for file_name in file_names:
        m = re.search(r'/(\d+)\.csv', file_name)
        if m:
            champion_id = m.group(1)
            req_url = 'https://global.api.pvp.net/api/lol/static-data/na/v1.2/champion/' \
                      + champion_id \
                      + '?api_key=' \
                      + api_key
            time.sleep(1)
            # print req_url
            data = urllib2.urlopen(req_url).read()
            json_data = json.loads(data)
            champion_name = json_data['name']
            with open(file_name) as f:
                champion_count = len(f.readlines())
                res[champion_name] = champion_count
            pass
        pass
    return res
    pass

if os.path.isfile('champion_popularity.pkl'):
    with open('champion_popularity.pkl', 'r+') as f:
        champion_counts = pickle.load(f)
        pass
    pass
else:
    champion_counts = count_champion(config['key'])
    with open('champion_popularity.pkl', 'w+') as f:
        pickle.dump(champion_counts, f, pickle.HIGHEST_PROTOCOL)
    pass

word_cloud = WordCloud().generate_from_frequencies(champion_counts.iteritems())
plt.imshow(word_cloud)
plt.show()


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
Data process, training and Analysis
Process raw data
Given the data we crawled from the previous part, now we need to process them and make sure that they can be used for later training part. Although we did a little processing work in crawling part to store data into csv format, we still need to group champions with the same type together. Here we take the role of "ADC" as an example in our project. We get all "ADC" champions from this website.
Can you write Python code for it?

adc_idx = [202, 42, 67, 222, 29, 51, 96, 81, 21, 6, 119, 15, 22, 236, 110, 18, 429]
dfs = list()
for adc_id in adc_idx:
    df = pd.read_csv('./data/{}.csv'.format(adc_id))
    dfs.append(df)
    pass
res = pd.concat(dfs, ignore_index=True, verify_integrity=True)
res.to_csv('./data/adc.csv', index=False)


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
After predicting the tier, we fetch the average summoner's data from the predicted tier, and process the data for summoner analysis use (details would be explained later).
Can you write Python code for it?

data = {}
reader = csv.reader(open('./data/solo_summoner_70359816.csv'))

index = 0
title, content = [], []
for row in reader:
    if index == 0:
        title = row
        index += 1
    else:
        content = row

for i in xrange(len(title)):
    data[title[i]] = float(content[i])

total_played = data['totalSessionsPlayed']
for key, value in data.iteritems():
    data[key] = value / total_played

filtered_data = {}
mean_temp = find_average(predict, df_avg)

data['KDA'] = (data['totalChampionKills'] + data['totalAssists']) / float(data['totalDeathsPerSession'])

for key, value in mean_temp.iteritems():
    if key in data:
        filtered_data[key] = data[key]
print "Summoner's game data: {}".format(filtered_data)


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
KNN Analysis
From the figures above, we can see that the accuracy reaches its maximum when k is around 20. And after that the accuracy will go down. While the standard deviation across all folds is always going down with the value of k going up. This is an important property of KNN.
But the problem is that the accuracies are low no matter which k value we selected and no matter which fold we used. The maximum accuracy is around 50% with k = 20 in the KNN model.
We think this is probably caused by possible irrelavant features remaining and the noise in the training data and the KNN model is not good at dealing with such cases.
Support Vector Machine (SVM) Model
Why SVM model?
Since we failed using the KNN model in predicting results, we would like to try SVM model instead in this project. From the class we know that the key idea of the SVM model is maximizing the margins between different clusters of the data with different labels. It is a eager learner which may consume more time during training process compared with the KNN model. But it should be able to handle the irrelavant features and some noises in the training data.
Model Design
Here we used C-Support Vector Machine, which has an adjustable field 'C'. It is used to add penalty for misclassification. If the C value gets smaller the penalty will be smaller, but the margin will be larger. So it cannot be too large or too tiny. If it is too large, the margin will be too small which might lead to overfitting. And if it is too small, there will be too many misclassification which makes the accuracy lower. Here we make C to be 40, which is a fairly safe value to use.
Moreover, since there are a few kernel functions we can choose from for SVM, here we just tried all of them and compared their results.
Can you write Python code for it?

from sklearn.svm import SVC

kernels = ['linear', 'poly', 'rbf', 'sigmoid']
kernel_num = len(kernels)
svm_accuracies = np.zeros((k, kernel_num))
for i in range(k):
    X_tr, y_tr, X_te, y_te = cf_list[i]
    for j in range(kernel_num):
        svm = SVC(C=40, kernel=kernels[j])
        svm.fit(X_tr, y_tr)
        svm_accuracies[i, j] = svm.score(X_te, y_te)
        pass
    pass

svm_mean_accuracies = np.mean(svm_accuracies, axis=0)
svm_std_accuracies = np.std(svm_accuracies, axis=0)
plt.figure(3)
plt.bar(range(4), svm_mean_accuracies, align='center')
plt.xticks(range(4), kernels)
plt.ylabel('Mean accuracy')
plt.figure(4)
plt.bar(range(4), svm_std_accuracies, align='center')
plt.xticks(range(4), kernels)
plt.ylabel('Standard deviation across folds')
plt.show()


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
Now we generate a new adc.csv file of all data we collected for the "ADC" champions. The next thing we need to do is to group the data of "ADC" of different levels together. Because later we will use k-fold cross validation on the data and we need to sample the same ratio of data of different levels for testing.
For the labels in crawled data, they are now python strings of "bronze", "silver", "gold", "platinum", "diamond", "challenger", "master". We mapped them from 0 to 6.
And since different features here have different scales of their values. For example, the "totalPhysicalDamageDealt" can be more than a 1000,000 while "totalSessionsLost" are mostly less than 100. So we also implement a normalize function which is used to scale each attribute into the same scale. Although this normalize function may not affect the final results and accuracies, it will definitely make training processes much faster.
Also, since we only focus on "ADC" champioins, we can drop some obviously not relavant features in this step.
Can you write Python code for it?

def string_to_int(s):
    if s == 'bronze':
        return 0
    elif s == 'silver':
        return 1
    elif s == 'gold':
        return 2
    elif s == 'platinum':
        return 3
    elif s == 'diamond':
        return 4
    elif s == 'challenger':
        return 5
    else:
        return 6
    pass

def normalize(X):
    n, m = X.shape
    for i in range(m):
        col_max = np.max(X[:, i])
        col_min = np.min(X[:, i])
        col_delta = col_max - col_min + 1.0
        X[:, i] = (X[:, i] - col_min) / col_delta
        pass
    return X
    pass

def preprocess_adc(filename):
    df = pd.read_csv(filename, index_col=False)
    y = df['label'].map(lambda x: string_to_int(x)).as_matrix()
    df_t = df.drop(['totalDamageDealt', 'totalDamageTaken', 'totalDoubleKills', 'totalTripleKills',
                    'totalQuadraKills', 'totalPentaKills', 'totalUnrealKills', 'mostSpellsCast',
                    'totalTurretsKilled', 'totalMagicDamageDealt', 'totalFirstBlood'], axis=1)
    X = df_t.drop(['label'], axis=1).as_matrix().astype(float)
    X = normalize(X)
    uniques = np.unique(y)
    X_list = list()
    y_list = list()
    for unique in uniques:
        idxes = y == unique
        X_list.append(X[idxes])
        y_list.append(y[idxes])
        pass
    return X_list, y_list, df_t
    pass


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
Result
Here we use the adc.csv file as our dataset, and use cross validation to fetch our training and testing data. We set our training epoch as 500, using rmsprop as the optimizer.
The following code gives a method to compute the accuracy between the final result and the test result.
Can you write Python code for it?

def accuracy(training_result, y_te):
    return np.sum(training_result==y_te) / float(len(training_result))

X_tr, y_tr, X_te, y_te = cf_list[0]
predict_class = classify(X_tr, y_tr, X_te)
print accuracy(predict_class, y_te)


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
The training results of the training data (in the first iteration) is as follow (plot by matplotlib using results from keras):

The test result gives an accuracy of 0.87 on average.
Analysis
Our CNN model gives an accuracy around 87%, which could provide a pretty good prediction for summoners who have not played any rank games or want to know their true tiers. We think the good performance of CNN is because it could take care of both linear and non-linear features. And also the CNN model can extract pretty large and complex features with its multiple layers and learnable filters in each layer.
Visualization
Summoner prediction
Since it's now a new season (S7), many summoners have not completed their pre-rank game (10 games to initialize their S7 tier). Some of the summoners are new to the game, and are unaware of their actual game skill among all the summoners. However, the average S6 summoners' data could be used to predict the summoner's level, and give them a rough idea of their current tier level, and even suggestions on how they could improve.
The find_average method compute the average statistics of summoners in certain tiers, after predicting the tier of given summoner.
Can you write Python code for it?

def find_average(tier_id, df):
    tier = TIER[tier_id]
    df = df[df['label'] == tier]
    mean_df = df.mean(axis=0)

    mean_data = mean_df / mean_df['totalSessionsPlayed']
    mean_data = mean_data.drop(['totalSessionsPlayed', 'totalSessionsLost',
                    'mostChampionKillsPerSession', 'maxNumDeaths', 'maxChampionsKilled'])
    mean_dict = dict(mean_data)
    mean_dict['KDA'] = (mean_dict['totalChampionKills'] + mean_dict['totalAssists']) / float(mean_dict['totalDeathsPerSession'])
    del mean_dict['totalChampionKills']
    del mean_dict['totalAssists']
    del mean_dict['totalDeathsPerSession']
    return mean_dict

# 70359816 is a summoner's id
top_champion = TopChampion(config['key'], '70359816', 2, n=1)
top_champion.save_top_champions(champion_id='solo_summoner_{}'.format('70359816'))


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
Then we train our CNN model:
Can you write Python code for it?

predict = classify(X_tr, y_tr, X_te)[0]
TIER = {
    0: 'bronze',
    1: 'silver',
    2: 'gold',
    3: 'platinum',
    4: 'diamond',
    5: 'challenger',
    6: 'master',
}
print 'the predicted tier is: {}'.format(TIER[predict])


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
To use the Riot api, one more important thing to do is to get your own API key. API key can be obtained from here. Note that normal developr API key has a narrow request limit, whereas production API key for commercial use has a looser requirement of request limit.
After getting your own API key, put it in the config dictionary below:
Can you write Python code for it?


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
With the above class, now we can start crawling the stats data of all champions saving them to csv files by the following code. Notice that this process is pretty slow since we added the sleep methods in our code. Riot APIs have a limitation on the API calls rate. You cannot send more than 500 requests per 10 minutes. So everytime we send a request here, we sleep for 1 second to prevent error responses.
Can you write Python code for it?

def main():
    tiers = get_tier()
    for tier, rank_dict in tiers.iteritems():
        print 'starting tier: {}'.format(tier)
        for summoner_id in rank_dict:
            print 'tier: {}, summoner id: {}'.format(tier, summoner_id)
            top_champion = TopChampion(config['key'], summoner_id, tier, 3)
            top_champion.get_top_champions()
            top_champion.save_top_champions()
            time.sleep(2)
        print 'end tier: {}'.format(tier)
main()


I am working on the file "LeagueRank_notebook.ipynb".
The first task is:
get_tier will return a divisioin dictionary, whose keys are the tier name, and values are the summoner id list in each tier. The results are printed in a human-readable format, categorized by tier.
Can you write Python code for it?

def get_tier():
    # challenger: 77759242
    # platinum: 53381
    # gold: 70359816
    # silver: 65213225
    # bronze: 22309680
    # master: 22551130
    # diamond: 34570626
    player_ids = [70359816, 77759242, 53381, 65213225, 22309680, 22551130, 34570626]
    riot_crawler = RiotCrawler(config['key'])
    for player_id in player_ids:
        print 'start crawling id: {}'.format(player_id)
        try:
            riot_crawler.get_player_by_tier(player_id)
        except:
            continue
    return riot_crawler.tiers

tiers = get_tier()
for tier, rank_dict in tiers.iteritems():
    print '--- {} ---'.format(tier)
    for summoner in rank_dict:
        print 'summoner id: {}'.format(summoner)
    print '--- end of {} ---'.format(tier)


I am working on the file "Learn Jupyter.ipynb".
The first task is:
<kbd>?</kbd> 可以显示文档
Can you write Python code for it?

from sklearn.ensemble import RandomForestRegressor
RandomForestRegressor?

%%latex
$$ x^3 + C = \int{\frac{1}{3} x^2 \; dx} \quad (C \in \mathbb{R})$$

import sklearn
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt

from sklearn.datasets import california_housing
cal = california_housing.fetch_california_housing()
df = pd.DataFrame(data=cal.data, columns=cal.feature_names, index=cal.target)
df.head(10)

plt.scatter(df.Latitude, df.Longitude)

import seaborn as sns

sns.jointplot(df.Longitude, df.Latitude)

sns.set(style="ticks")

sns.jointplot(df.Longitude, df.Latitude, kind="hex", color="#4CB391")

from scipy.stats import kendalltau
kendalltau?

sns.set(style="ticks")

sns.jointplot(df.Longitude, df.Latitude, kind="hex",stat_func=kendalltau, color="#4CB391")


I am working on the file "Learn Jupyter.ipynb".
The first task is:
TAB 自动提示
Shift+TAB 参数提示
Can you write Python code for it?