I am working on the file "PySeidon_tuto_3.ipynb".
The first task is:
1.3. Object attributes, functions, methods & special methods
The ADCP object possesses 3 attributes and 3 methods. They would appear by typing adcp. Tab for instance.
An attribute is a quantity intrinsic to its object. A method is an intrinsic function which changes an attribute of its object. Contrarily a function will generate its own output:
The Station attributes are:
- History: history metadata that keeps track of the object changes
- Data: gathers the raw/unchanged data of the specified .mat file
- Variables*: gathers the hydrodynamics related data. Note that methods will generate new fields in this attribute
The Station methods & functions are:
- Utils: gathers utility methods and functions for use with 2D and 3D variables
- Plots: gathers plotting methods for use with 2D and 3D variables
- dump_profile_data: dumps profile data (x,y) in a *.csv file.
2. PySeidon - Hands-on (15 mins)
Utils
Exercise 3:
- Print the (lon,lat) coordinates of the adcp object (hint: look into Variables)
- Use Utils.velo_norm to compute the velocity norm over all time steps and all vertical levels...and accessorily plot the mean velocity vertical profile.
- Use Utils.ebb_flood_split function to get the ebb and flood time indices of the time series.
- Plot the flood flow speed hitogram
- Compute & Plot the ebb vertical shear
- Perform a harmonic analysis of the velocities and print out the result
- Reconstruction these velocities based on the harmonic results of the previous question
Answer:
Can you write Python code for it?

print "(lon, lat) coordinates: ("+str(adcp.Variables.lon)+", "+str(adcp.Variables.lat)+")"
vel = adcp.Utils.velo_norm()
fI, eI, pa, pav = adcp.Utils.ebb_flood_split()
adcp.Utils.speed_histogram(time_ind=fI)
dveldz = adcp.Utils.verti_shear(time_ind=eI)
harmo = adcp.Utils.Harmonic_analysis(elevation=False, velocity=True)
print harmo
velos = adcp.Utils.Harmonic_reconstruction(harmo)


I am working on the file "PySeidon_tuto_3.ipynb".
The first task is:
According to the documentation, in order to define a ADCP object, the only required input is a filename. This string input represents path to a file (e.g. testAdcp=ADCP('./path_to_matlab_file/filename') and whose file must be a matlab file (i.e. .mat).
Note that, at the current stage, the package only handle fully processed ADCP matlab data previously quality-controlled as well as formatted through "EnsembleData_FlowFile" matlab script at the mo. All the tool necessary for this processing and quality-control can be found in ./pyseidon/utilities/BP_tools.py and save_FlowFile_BPFormat.py. Additionally, a template for the ADCP file is provided in the package under data4tutorial
Exercise 2:
- define a adcp object named adcp from the following template: ./data4tutorial/adcp_GP_01aug2013.mat
- Tip: adapt the file's path to your local machine.
Answer:
Can you write Python code for it?


I am working on the file "PySeidon_tuto_3.ipynb".
The first task is:
Save functions
Exercise 5:
- Dump depth-averaged velocity and time step data in a .csv file
- Hint:* use numpy for averaging
Answer:
Can you write Python code for it?

import numpy as np
da_vel = np.nanmean(vel,axis=1)
station.dump_profile_data(adcp.Variables.matlabTime[:], da_vel, title='flow_speed_time_series', xlabel='matlab time', ylabel='speed')


I am working on the file "PySeidon_tuto_3.ipynb".
The first task is:
Star  here means all. Usually this form of statements would import the entire library. In the case of PySeidon, this statement will import the following object classes: FVCOM, Station, Validation, ADCP, Tidegauge and Drifter. Only the ADCP class will be tackle in this tutorial. However note should note that the architecture design and functioning between each classes are very similar.
1.2. Object definition
Python is by definition an object oriented language...and so is matlab. PySeidon is based on this notion of object, so let us define our first "ADCP" object.
Exercise 1:
- Unravel ADCP documentation with Ipython shortcuts
Answer:
Can you write Python code for it?


I am working on the file "PySeidon_tuto_4.ipynb".
The first task is:
1. PySeidon - TideGauge object initialisation
Similarly to the "ADCP class" and the "Drifter class", the "TideGauge class" is a measurement-based object.
1.1. Package importation
As any other library in Python, PySeidon has to be first imported before to be used. Here we will use an alternative import statement compared to the one previoulsy presented:
Can you write Python code for it?


I am working on the file "PySeidon_tuto_4.ipynb".
The first task is:
According to the documentation, in order to define a TideGauge object, the only required input is a filename. This string input represents path to a file (e.g. testAdcp=TideGauge('./path_to_matlab_file/filename') and whose file must be a matlab file (i.e. .mat).
Note that, at the current stage, the package only handle a certain type of file and data format. A template for the TideGauge file/data format is provided in the package under data4tutorial
Exercise 2:
- define a tide gauge object named tg from the following template: ./data4tutorial/tidegauge_GP_01aug2013.mat
- Tip: adapt the file's path to your local machine.
Answer:
Can you write Python code for it?


I am working on the file "PySeidon_tuto_4.ipynb".
The first task is:
Star  here means all. Usually this form of statements would import the entire library. In the case of PySeidon, this statement will import the following object classes: FVCOM, Station, Validation, ADCP, Tidegauge and Drifter. Only the TideGauge class will be tackle in this tutorial. However note should note that the architecture design and functioning between each classes are very similar.
1.2. Object definition
Python is by definition an object oriented language...and so is matlab. PySeidon is based on this notion of object, so let us define our first "Tidegauge" object.
Exercise 1:
- Unravel TideGauge documentation with Ipython shortcuts
Answer:
Can you write Python code for it?


I am working on the file "PySolExExample.ipynb".
The first task is:
And the function to call is pysolex.pyex:
Can you write Python code for it?

output = pysolex.pyex(H2O,CO2,PPMS0,PPMCl0,Si,Al,Fe,Ca,Mg,Na,K,pi,SiO2,pisol,piswitch,flagout,T,P,NNO)
output


I am working on the file "PySolExExample.ipynb".
The first task is:
Below is the wt% of SiO2 that was used for the SiO2 only parameterisation. From Fred: "I think (I should check but dont have the code available now) that this is no-longer used as the SiO2 wt% is calculated from Si mol%."
Can you write Python code for it?

SiO2 = 52.12 #should match the good value


I am working on the file "PySolExExample.ipynb".
The first task is:
Done! Let's do a nice graph for those results:
Can you write Python code for it?

plt.plot(rev_Pint[:],results[:,0])
plt.xlabel("Pressure, bars", fontsize = 14)
plt.ylabel("Water content in melt, wt%", fontsize = 14)
plt.title("Fig. 1: Water concentration vs pressure, closed system",fontsize = 14,fontweight = "bold")
plt.text(2000,2,("T ="+str(T)+"\nNNO ="+str(NNO)),fontsize = 14)


I am working on the file "PySolExExample.ipynb".
The first task is:
Let's do the same thing for the CO2 now:
Can you write Python code for it?

plt.plot(rev_Pint[:],results[:,1])
plt.xlabel("Pressure, bars", fontsize = 14)
plt.ylabel("CO$_2$ content in melt, ppm", fontsize = 14)
plt.title("Fig. 2: CO$_2$ concentration vs pressure, closed system",fontsize = 14,fontweight = "bold")
plt.text(1000,800,("T ="+str(T)+"\nNNO ="+str(NNO)),fontsize = 14)


I am working on the file "PySolExExample.ipynb".
The first task is:
Now a second switch to determine if pisol is given or should be calculated: if 1 then the value pi is used for solubility calculations, and if 0 pi is calculated from the composition of the melt.
Can you write Python code for it?


I am working on the file "PySolExExample.ipynb".
The first task is:
Now let's fix the other parameters of our system: temperature, pressure, and oxygen fugacity! We will start with a fixed oxygen fugacity, pressure and temperature:
Can you write Python code for it?


I am working on the file "PySolExExample.ipynb".
The first task is:
Now we define the value of pi to use, which is the parameterisation described in Dixon(1997). This value is not used unless piswitch is set to 1.
Can you write Python code for it?


I am working on the file "PySolExExample.ipynb".
The first task is:
Oh... Here is the result contained in output => a SWIG Object containing double number... To read it, I only found one way online: reading directly the memory block allocated to this Ojbect using ctypes:
Can you write Python code for it?

rawPointer = output.__long__() # we're going to read the "address"
pC = ctypes.cast(rawPointer, ctypes.POINTER( ctypes.c_double )) # and we read the array stored at this address
print(("wt% H2O = "+str(pC[0])))
print(("PPM CO2 = "+str(pC[1])))
print(("PPM S = "+str(pC[2])))
print(("PPM Cl = "+str(pC[3])))
print(("Vol% Exsolve = "+str(pC[4])))
print(("XV H2O (mass) = "+str(pC[5])))
print(("XV CO2 (mass) = "+str(pC[6])))
print(("XV S (mass) = "+str(pC[7])))
print(("XV Cl (mass) = "+str(pC[8])))
print(("molV H2O (mass) = "+str(pC[9])))
print(("molV CO2 (mass) = "+str(pC[10])))
print(("molV S (mass) = "+str(pC[11])))
print(("molV Cl (mass) = "+str(pC[12])))


I am working on the file "PySolExExample.ipynb".
The first task is:
Ok, for this single calculation, SolEx has a flag for terminal output, but it is not working in the Notebook. So let's put the flag to 0:
Can you write Python code for it?

flagout = bool(0) #has to be a bool value


I am working on the file "PySolExExample.ipynb".
The first task is:
Ok, it's working. Now let's complicate the case. Let's imagine that we have a closed-system degassing, going from P = 4000 to 100 bar, as you can do in SolEx. You will write something like that to reproduce the calculation in Python:
Can you write Python code for it?

Pint = np.arange(100,4000,100) #start, stop, step
rev_Pint = Pint[::-1] # To have the first values being the highest ones
results = np.zeros((len(Pint),13)) # For storing the results


I am working on the file "PySolExExample.ipynb".
The first task is:
Ok, now we start by defining a starting chemical composition of interest (warning to put dots so that Python interpretes the numbers as float and not int!):
Can you write Python code for it?


I am working on the file "PySolExExample.ipynb".
The first task is:
We can now plot the results as we did for the closed system case:
Can you write Python code for it?

plt.plot(rev_Pint[:],results[:,0])
plt.xlabel("Pressure, bars", fontsize = 14)
plt.ylabel("Water content in melt, wt%", fontsize = 14)
plt.title("Fig. 3: Open system ",fontsize = 14,fontweight = "bold")
plt.text(2000,2,("T ="+str(T)+"\nNNO ="+str(NNO)))

plt.plot(rev_Pint[:],results[:,1])
plt.xlabel("Pressure, bars", fontsize = 14)
plt.ylabel("CO$_2$ content in melt, ppm", fontsize = 14)
plt.title("Fig. 4: Open system ",fontsize = 14,fontweight = "bold")
#plt.text(1000,800,("T ="+str(T)+"\nNNO ="+str(NNO)))

results[0,0]

rev_Pint[0]


I am working on the file "PySparkTutorial.ipynb".
The first task is:
Actions

returns anything but rdd.
may return nothing as well (foreach).
like aggregtions (N to few).
return value(s) to driver code.
Can you write Python code for it?

rdd.reduce(lambda acc, x: acc+x) # reduce; operation must satisfy associative and communtative property

rdd.count() # count

rdd.take(4) # take (returns as a list; selects data from one partition, then moves to another partition as required to satisfy the limit)

rdd.takeSample(False, 5, seed = 13) # takeSample

rdd.takeOrdered(4, key = lambda x:x) # takeOrdered

rdd.collect()

rdd.first() # first

rdd.top(4, key = int) # top ; returns top n items in descending order

rdd.countApprox(1000, 0.5) # countApprox

rdd.countApproxDistinct(0.7) # number of distinct elements

def showValues(x):
    print "hello: " + str(x)

rdd.foreach(showValues) # foreach (Applies a function to every element of rdd)
# useful to communicate to external services, accumulate values in a queue, logging info, ...
# NOTE: verify results in stderr file of the working dir

def showValuesPartition(iterator):
    vals = []
    for item in iterator:
        vals.append("hello: " + str(item))
    print vals

rdd.foreachPartition(showValuesPartition) # foreachPartition (Applies a function per partition of rdd)

rdd.max()

rdd.min()

rdd.stats()

rdd.sum()

rdd.mean()

rdd.stdev()

# must be an absolute path to directory name; default is hdfs namespace
# creates a part-xxxx file for each partition
rdd.saveAsTextFile("file:///" + os.path.realpath("./textfiles")) # saveAsTextFile

# using compression
# compresses part-xxxx file of each partition
rdd.saveAsTextFile("file:///" + os.path.realpath("./textfileszip"),
                   compressionCodecClass = "org.apache.hadoop.io.compress.GzipCodec") # saveAsTextFile

rdd.saveAsPickleFile("file:///" + os.path.realpath("./textfiles-pickled")) # saveAsPickleFile (faster reads, writes)

rdd.countByValue() # countByValue - returns as dict of value: count

rdd.isEmpty() # isEmpty

print rdd.getStorageLevel() # getStorageLevel

rdd.getNumPartitions() # getNumPartitions

rdd.persist(StorageLevel.DISK_ONLY)
print rdd.is_cached
print rdd.getStorageLevel()
rdd.unpersist()
print rdd.is_cached
print...


I am working on the file "PySparkTutorial.ipynb".
The first task is:
Key-Value RDD Actions
All  actions  available for normal RDD are also  available for  Key-Value RDD.
Can you write Python code for it?

krdd.collect()

krdd.count()

krdd.take(3)

krdd_dup = sc.parallelize([("a", 1), ("a", 1)])
krdd_dup.distinct().collect()

krdd.countByKey() # countByKey - number of times a key appears in the k-v rdd

krdd.lookup("a") # lookup

krdd.toDebugString() # toDebugString (identifies recursive dependencies of this rdd for debugging purposes)

krdd.collectAsMap() # collectAsMap -> return key-value RDD as a dictionary


I am working on the file "PySparkTutorial.ipynb".
The first task is:
Key-Value RDD Transformations
Can you write Python code for it?

krdd = sc.parallelize([("a", 1), ("a", 2), ("b", 1), ("b", 2), ("c", 1)], 2)
krdd2 = sc.parallelize([("a", 3), ("b", 3), ("d", 1)], 2)

print krdd.glom().collect()
print krdd2.glom().collect()

krdd.groupByKey().collect() # groupByKey

list(krdd.groupByKey().collect()[0][1])

krdd.reduceByKey(lambda acc, x: acc + x, numPartitions = 1).collect() # reduceByKey
# does a groupByKey, followed by reduction
# operation must obey associative and commutative properties
# numPartitions controls the level of parallelism

# http://www.learnbymarketing.com/618/pyspark-rdd-basics-examples/
# does a groupByKey, followed by custom reduce function that doesn't have to obey commutative and associative property

# define a resultset template (any data structure) with initial values
init_state_template = [0]

def mergeValuesWithinPartition(template, val):
    template[0] = template[0] + val
    return template

def mergePartitions(template1, template2):
    template = template1[0] + template2[0]
    return template

krdd.aggregateByKey(init_state_template,
                    mergeValuesWithinPartition,
                    mergePartitions).collect() # aggregateByKey

krdd.sortByKey(ascending = False, numPartitions = 1, keyfunc = lambda x: x).collect() # sortByKey (can also use sortBy)

krdd.join(krdd2).collect() # join (inner-join in SQL; returns all-pair combinations)

krdd.leftOuterJoin(krdd2).collect() # leftOuterJoin (left join in SQL)

krdd.rightOuterJoin(krdd2).collect() # rightOuterJoin (right join in SQL)

krdd.fullOuterJoin(krdd2).collect() # fullOuterJoin (full join in SQL)

krdd.cogroup(krdd2).collect() # cogroup (returns iterator one for each rdd)

print list(krdd.cogroup(krdd2).collect()[0][1][0])
print list(krdd.cogroup(krdd2).collect()[0][1][1])

print list(krdd.cogroup(krdd2).collect()[2][1][0])
print list(krdd.cogroup(krdd2).collect()[2][1][1])

krdd.mapValues(lambda x: x**2).collect() # mapValues

krdd_val_iter = sc.parallelize([("a", [1,2,3]), ("b", [4,5,6])])
krdd_val_iter.flatMapValues(lambda x: [y**2...


I am working on the file "PySparkTutorial.ipynb".
The first task is:
Spark  does  lazy  evaluation.  If we  have a chain of transformations, Spark won't  execute them untill an action is invoked.
A RDD  has  several levels of persistence (Memory, Disk, Memory-DISK, Serialized, ...).  If we use a RDD  for subsequent transformations, it is good to persist it to avoid expensive computations.
rdd
rdd.persist()
rdd.is_cached
rdd.trans1().action()
rdd.trans1().trans2().action()
rdd.unpersist()
No. of partitions = No. of output files for output actions.
No. of  partitions = No. of executors. Spark will start & stop an executor to work on each partition of data. Starting & Stopping many executors has an overhead for small size of data with many partitions.
No. of partitions = Level of parallelism
Default partitioner = Hash  partitioner ( evenly distribute all the elements of RDD across all the workers )
2 - 4 partitions  per CPU core is recommended
Transformations
An RDD can contain heterogenuous elements.
Ex. [1,2, "abc", (1,2), {4,5,6}]
Can you write Python code for it?

sc.parallelize([1,2, "abc", (1,2), {4,5,6}]).collect()

rdd = sc.parallelize([1,2,3,4,5,6,7,8,9,10], 3)
rdd2 = sc.parallelize(xrange(10, 20), 3)

print rdd.glom().collect() # shows data grouped by partitions
print rdd2.glom().collect()

rdd.map(lambda x: x**2).collect() # map 1-to-1 transformation, operates on every element of rdd

# functions must be self-contained, no states or access global variables
def trans1(x):
    return x**2

rdd.map(trans1).collect()

rdd.filter(lambda x: x > 5).collect() # filter

datasets = "../../../Machine_Learning/WPI_ML/datasets"
print os.path.exists(datasets)
#print os.path.realpath(datasets)

# default is hdfs filesystem, to access local files, use namespace -> file:///
textrdd = sc.textFile("file:///" + os.path.realpath(datasets) + "/Audio_Standardization_ Sentences.txt",
                      use_unicode = False, minPartitions = 3)

textrdd.glom().collect()

textrdd.flatMap(lambda x: x.split(" ")).take(11) # 1-to-many transformation, puts in a global list

def countWordsInPartition(iterator):
    """
    @params:
        iterator: a partition of the rdd
    """
    count = 0
    for x in iterator:
        count += len(x.split(" "))
    yield count

textrdd.mapPartitions(countWordsInPartition).collect() # same as map but operates on each chunk/partition of the rdd

rdd.sortBy(keyfunc = lambda x: x, ascending = False, numPartitions = 1).collect() # sorting
# numPartitions controls the level of parallelism

rdd.sample(withReplacement = False, fraction = 0.5, seed = 13).collect() # sampling

rdd.coalesce(1).glom().collect() # reduce no. of partitions by combining partitions from each worker, thereby minimizing network traffic

rdd.repartition(2).glom().collect()
# increases or decreases the no. of partitions, but at the cost of more network traffic,
# because Spark has to shuffle the data across the workers.
# Use coalesce when intent to decrease the partitions.

rdd.repartition(5).glom().collect()

rdd.union(rdd2).collect() # combines two rdds -> A u...


I am working on the file "PySummary1.ipynb".
The first task is:
@wraps forwards the __doctstring__ and __name__ of the incoming f argument to the proxy being wrapped.
LAB:
Try commenting out the line with @wraps on it and checking the __doctstring__ and __name__ of Sqr again.

Comment out @wraps
re-run the cell containing the two function definitions
re-run the cell below, and not changes
change it back
Can you write Python code for it?


I am working on the file "PySummary1.ipynb".
The first task is:
We also learned about decorator syntax.  Using a decorator, we're able to use a callable as an input to an object that provides a proxy output, likewise callable by the same name.
Can you write Python code for it?

from functools import wraps

def decorator(f):
    @wraps(f)
    def proxy(x):
        # proxy
        print("Look at me!")
        return f(x)
    return proxy

@decorator
def Sqr(x):
    """Square Dancer"""
    return x * x

Sqr(10)


I am working on the file "PySummary2.ipynb".
The first task is:
Expected:
<pre>
b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06'
</pre>
Can you write Python code for it?

result = hashlib.sha256(b"Nobody inspects the spammish repetition").hexdigest()
result

print("Digest size", m.digest_size)
print("Block size ", m.block_size)


I am working on the file "PySummary2.ipynb".
The first task is:
In class, we looked at a passwords database that doesn't save actual passwords, only hashes thereof.  Even system administrators with the keys to the database, have no means to force a hash to run backwards to regain the phrase which was behind it.  A hash is a one way street.
LAB:
Check the Python docs and run the above example with sha224 instead.  Do you get past this assertion (unit test)?
Can you write Python code for it?

# Uncomment me to check your result
# assert result == 'a4337bc45a8fc544c03f52dc550cd6e1e87021bc896588bd79e901e2'


I am working on the file "PyTorch Tutorials/2. Learning PyTorch with Examples.ipynb".
The first task is:
.clamp()
clamp表示夹紧，夹住的意思，torch.clamp(input,min,max,out=None)-> Tensor
将input中的元素限制在[min,max]范围内并返回一个Tensor
Can you write Python code for it?

q = torch.randn(2,4)
print(q)
p = q.clamp(min=0)
print(p)

# -*- coding: utf-8 -*-
import torch

dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold input and outputs.
# Setting requires_grad=False indicates that we do not need to compute gradients
# with respect to these Tensors during the backward pass.
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)

# Create random Tensors for weights.
# Setting requires_grad=True indicates that we want to compute gradients with
# respect to these Tensors during the backward pass.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)

learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y using operations on Tensors; these
    # are exactly the same operations we used to compute the forward pass using
    # Tensors, but we do not need to keep references to intermediate values since
    # we are not implementing the backward pass by hand.
    y_pred = x.mm(w1).clamp(min=0).mm(w2)

    # Compute and print loss using operations on Tensors.
    # Now loss is a Tensor of shape (1,)
    # loss.item() gets the a scalar value held in the loss.
    loss = (y_pred - y).pow(2).sum()
    print(t, loss.item())

    # Use autograd to compute the backward pass. This call will compute the
    # gradient of loss with respect to all Tensors with requires_grad=True.
    # After this call w1.grad and w2.grad will be Tensors holding the gradient
    # of the loss with respect to w1 and w2 respectively.
    loss.backward()

    # Manually update weights using gradient descent. Wrap in torch.no_grad()
    # because weights have requires_grad=True, but we don't need to track this
    # in...


I am working on the file "PyTorch Tutorials/2. Learning PyTorch with Examples.ipynb".
The first task is:
.mm()
Can you write Python code for it?

import torch
a = torch.randn(2,3)
w = torch.randn(3,4)
h = a.mm(w)
print(h.size())


I am working on the file "PyTorch Tutorials/2. Learning PyTorch with Examples.ipynb".
The first task is:
PyTorch: Defining new autograd functions
Can you write Python code for it?

# -*- coding: utf-8 -*-
import torch

class MyReLU(torch.autograd.Function):
    """
    We can implement our own custom autograd Functions by subclassing
    torch.autograd.Function and implementing the forward and backward passes
    which operate on Tensors.
    """

    @staticmethod
    def forward(ctx, input):
        """
        In the forward pass we receive a Tensor containing the input and return
        a Tensor containing the output. ctx is a context object that can be used
        to stash information for backward computation. You can cache arbitrary
        objects for use in the backward pass using the ctx.save_for_backward method.
        """
        ctx.save_for_backward(input)
        return input.clamp(min=0)

    @staticmethod
    def backward(ctx, grad_output):
        """
        In the backward pass we receive a Tensor containing the gradient of the loss
        with respect to the output, and we need to compute the gradient of the loss
        with respect to the input.
        """
        input, = ctx.saved_tensors
        grad_input = grad_output.clone()
        grad_input[input < 0] = 0
        return grad_input

dtype = torch.float
device = torch.device("cpu")
# device = torch.device("cuda:0") # Uncomment this to run on GPU

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)

# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)

learning_rate = 1e-6
for t in range(500):
    # To apply our Function, we use Function.apply method. We alias this as 'relu'.
    relu = MyReLU.apply

    # Forward pass: compute predicted y using operations; we compute
    # ReLU using our custom autograd operation.
    y_pred =...


I am working on the file "PyTorch Tutorials/2. Learning PyTorch with Examples.ipynb".
The first task is:
PyTorch: nn
Can you write Python code for it?

# -*- coding: utf-8 -*-
import torch

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random Tensors to hold inputs and outputs
x = torch.randn(N, D_in)
y = torch.randn(N, D_out)

# Use the nn package to define our model as a sequence of layers. nn.Sequential
# is a Module which contains other Modules, and applies them in sequence to
# produce its output. Each Linear Module computes output from input using a
# linear function, and holds internal Tensors for its weight and bias.
model = torch.nn.Sequential(
    torch.nn.Linear(D_in, H),
    torch.nn.ReLU(),
    torch.nn.Linear(H, D_out),
)

# The nn package also contains definitions of popular loss functions; in this
# case we will use Mean Squared Error (MSE) as our loss function.
loss_fn = torch.nn.MSELoss(reduction='sum')

learning_rate = 1e-4
for t in range(500):
    # Forward pass: compute predicted y by passing x to the model. Module objects
    # override the __call__ operator so you can call them like functions. When
    # doing so you pass a Tensor of input data to the Module and it produces
    # a Tensor of output data.
    y_pred = model(x)

    # Compute and print loss. We pass Tensors containing the predicted and true
    # values of y, and the loss function returns a Tensor containing the
    # loss.
    loss = loss_fn(y_pred, y)
    print(t, loss.item())

    # Zero the gradients before running the backward pass.
    model.zero_grad()

    # Backward pass: compute gradient of the loss with respect to all the learnable
    # parameters of the model. Internally, the parameters of each Module are stored
    # in Tensors with requires_grad=True, so this call will compute gradients for
    # all learnable parameters in the model.
    loss.backward()

    # Update the weights using gradient descent. Each parameter is a Tensor, so
    # we can access its gradients like we did before.
    with torch.no_grad():
        for param in model.parameters():
       ...


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
ConvNet as fixed feature extractor
Here, we need to freeze all the network except the final layer. We need to set
requires_grad == False to freeze the parameters so that the gradients are not computed in backward().
Can you write Python code for it?

model_conv = torchvision.models.resnet18(pretrained=True)
for param in model_conv.parameters():
    param.requires_grad = False

# Parameters of newly constructed modules have requires_grad=True by default
num_ftrs = model_conv.fc.in_features
model_conv.fc = nn.Linear(num_ftrs, 2)

model_conv = model_conv.to(device)

criterion = nn.CrossEntropyLoss()

# Observe that only parameters of final layer are being optimized as
# opoosed to before.
optimizer_conv = optim.SGD(model_conv.fc.parameters(), lr=0.001, momentum=0.9)

# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_conv, step_size=7, gamma=0.1)


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
Finetuning the convnet
Load a pretrained model and reset final fully connected layer.
Can you write Python code for it?

model_ft = models.resnet18(pretrained=True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 2)

model_ft = model_ft.to(device)

criterion = nn.CrossEntropyLoss()

# Observe that all parameters are being optimized
optimizer_ft = optim.SGD(model_ft.parameters(), lr=0.001, momentum=0.9)

# Decay LR by a factor of 0.1 every 7 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=7, gamma=0.1)


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
Train and evaluate
Can you write Python code for it?

model_conv = train_model(model_conv, criterion, optimizer_conv,
                         exp_lr_scheduler, num_epochs=25)

visualize_model(model_conv)

plt.ioff()
plt.show()


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
Training the model
Scheduling the learning rate
Saving the best model
Can you write Python code for it?

def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
    since = time.time()

    best_model_wts = copy.deepcopy(model.state_dict())
    best_acc = 0.0

    for epoch in range(num_epochs):
        print('Epoch{}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        #Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                scheduler.step()
                model.train()    # Set model to training mode
            else:
                model.eval()     # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0

            #iterate over data
            for inputs, labels in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                #zero the parameter gradients
                optimizer.zero_grad()

                #forward
                #tack history if only in train
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model(inputs)
                    _, preds = torch.max(outputs, 1)
                    loss = criterion(outputs, labels)

                    #backward + optimize only if in training phase
                    if phase == 'train':
                        loss.backward()
                        optimizer.step()

                #statistics
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / dataset_sizes[phase]
            epoch_acc = running_corrects.double() / dataset_sizes[phase]

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                phase, epoch_loss, epoch_acc))

            # deep copy the model
            if phase == 'val' and epoch_acc > best_acc:
       ...


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
Visualize a few images
Can you write Python code for it?

def imshow(inp, title=None):
    """Imshow for Tensor."""
    inp = inp.numpy().transpose((1, 2, 0))
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    inp = std * inp + mean
    inp = np.clip(inp, 0, 1)
    plt.imshow(inp)
    if title is not None:
        plt.title(title)
    plt.pause(0.001)  # pause a bit so that plots are updated

# Get a batch of training data
inputs, classes = next(iter(dataloaders['train']))

# Make a grid from batch
out = torchvision.utils.make_grid(inputs)

imshow(out, title=[class_names[x] for x in classes])


I am working on the file "PyTorch Tutorials/3. Transfer Learning tutorial.ipynb".
The first task is:
Visualizing the model predictions
Can you write Python code for it?

def visualize_model(model, num_images=6):
    was_training = model.training
    model.eval()
    images_so_far = 0
    fig = plt.figure()

    with torch.no_grad():
        for i, (inputs, labels) in enumerate(dataloaders['val']):
            inputs = inputs.to(device)
            labels = labels.to(device)

            outputs = model(inputs)
            _, preds = torch.max(outputs, 1)

            for j in range(inputs.size()[0]):
                images_so_far += 1
                ax = plt.subplot(num_images//2, 2, images_so_far)
                ax.axis('off')
                ax.set_title('predicted: {}'.format(class_names[preds[j]]))
                imshow(inputs.cpu().data[j])

                if images_so_far == num_images:
                    model.train(mode=was_training)
                    return
        model.train(mode=was_training)


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Compose transforms
Can you write Python code for it?

scale = Rescale(256)
crop = RandomCrop(128)
composed = transforms.Compose([Rescale(256),
                               RandomCrop(224)])

# Apply each of the above transforms on sample.
fig = plt.figure()
sample = face_dataset[65]
for i, tsfrm in enumerate([scale, crop, composed]):
    transformed_sample = tsfrm(sample)

    ax = plt.subplot(1, 3, i + 1)
    plt.tight_layout()
    ax.set_title(type(tsfrm).__name__)
    show_landmarks(**transformed_sample)

plt.show()


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Dataset class
torch.utils.data.Dataset is an abstract class representing a dataset. Your custom dataset should inherit Dataset and override the following methods:

len so that len(dataset) returns the size of the dataset.
getitem to support the indexing such that dataset[i] can be used to get ith sample

Let’s create a dataset class for our face landmarks dataset. We will read the csv in init but leave the reading of images to getitem. This is memory efficient because all the images are not stored in the memory at once but read as required.
Sample of our dataset will be a dict {'image': image, 'landmarks': landmarks}. Our datset will take an optional argument transform so that any required processing can be applied on the sample. We will see the usefulness of transform in the next section.
Can you write Python code for it?

class FaceLandmarksDataset(Dataset):
    """Face Landmarks dataset."""

    def __init__(self, csv_file, root_dir, transform=None):
        """
        Args:
            csv_file (string): Path to the csv file with annotations.
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.landmarks_frame = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

    def __len__(self):
        return len(self.landmarks_frame)

    def __getitem__(self, idx):
        img_name = os.path.join(self.root_dir,
                                self.landmarks_frame.iloc[idx, 0])
        image = io.imread(img_name)
        landmarks = self.landmarks_frame.iloc[idx, 1:].as_matrix()
        landmarks = landmarks.astype('float').reshape(-1, 2)
        sample = {'image': image, 'landmarks': landmarks}

        if self.transform:
            sample = self.transform(sample)

        return sample


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
However, we are losing a lot of features by using a simple for loop to iterate over the data. In particular, we are missing out on:

Batching the data
Shuffling the data
Load the data in parallel using multiprocessing workers.

torch.utils.data.DataLoader is an iterator which provides all these features. Parameters used below should be clear. One parameter of interest is collate_fn. You can specify how exactly the samples need to be batched using collate_fn. However, default collate should work fine for most use cases.
Can you write Python code for it?

dataloader = DataLoader(transformed_dataset, batch_size=4,
                        shuffle=True, num_workers=4)

# Helper function to show a batch
def show_landmarks_batch(sample_batched):
    """Show image with landmarks for a batch of samples."""
    images_batch, landmarks_batch = \
            sample_batched['image'], sample_batched['landmarks']
    batch_size = len(images_batch)
    im_size = images_batch.size(2)

    grid = utils.make_grid(images_batch)
    plt.imshow(grid.numpy().transpose((1, 2, 0)))

    for i in range(batch_size):
        plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size,
                    landmarks_batch[i, :, 1].numpy(),
                    s=10, marker='.', c='r')

        plt.title('Batch from dataloader')

for i_batch, sample_batched in enumerate(dataloader):
    print(i_batch, sample_batched['image'].size(),
          sample_batched['landmarks'].size())

    # observe 4th batch and stop.
    if i_batch == 3:
        plt.figure()
        show_landmarks_batch(sample_batched)
        plt.axis('off')
        plt.ioff()
        plt.show()
        break


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Iterating through the dataset
Can you write Python code for it?

transformed_dataset = FaceLandmarksDataset(csv_file='faces/face_landmarks.csv',
                                           root_dir='faces/',
                                           transform=transforms.Compose([
                                               Rescale(256),
                                               RandomCrop(224),
                                               ToTensor()
                                           ]))

for i in range(len(transformed_dataset)):
    sample = transformed_dataset[i]

    print(i, sample['image'].size(), sample['landmarks'].size())

    if i == 3:
        break


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Let’s instantiate this class and iterate through the data samples. We will print the sizes of first 4 samples and show their landmarks.
Can you write Python code for it?

face_dataset = FaceLandmarksDataset(csv_file='faces/face_landmarks.csv',
                                    root_dir='faces/')

fig = plt.figure()

for i in range(len(face_dataset)):
    sample = face_dataset[i]

    print(i, sample['image'].shape, sample['landmarks'].shape)

    ax = plt.subplot(1, 4, i + 1)
    plt.tight_layout()
    ax.set_title('Sample #{}'.format(i))
    ax.axis('off')
    show_landmarks(**sample)

    if i == 3:
        plt.show()
        break


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Let’s quickly read the CSV and get the annotations in an (N, 2) array where N is the number of landmarks.
Can you write Python code for it?

landmarks_frame = pd.read_csv('faces/face_landmarks.csv')

n = 65
img_name = landmarks_frame.iloc[n, 0]
landmarks = landmarks_frame.iloc[n, 1:].as_matrix()
landmarks = landmarks.astype('float').reshape(-1, 2)

print('Image name: {}'.format(img_name))
print('Landmarks shape: {}'.format(landmarks.shape))
print('First 4 Landmarks: {}'.format(landmarks[:4]))

def show_landmarks(image, landmarks):
    """Show image with landmarks"""
    plt.imshow(image)
    plt.scatter(landmarks[:, 0], landmarks[:, 1], s=10, marker='.', c='r')
    plt.pause(0.001)  # pause a bit so that plots are updated

plt.figure()
show_landmarks(io.imread(os.path.join('faces/', img_name)),
               landmarks)
plt.show()


I am working on the file "PyTorch Tutorials/4. Data Loading and Processing Tutorial.ipynb".
The first task is:
Transforms
One issue we can see from the above is that the samples are not of the same size. Most neural networks expect the images of a fixed size. Therefore, we will need to write some prepocessing code. Let’s create three transforms:

Rescale: to scale the image
RandomCrop: to crop from image randomly. This is data augmentation.
ToTensor: to convert the numpy images to torch images (we need to swap axes).

We will write them as callable classes instead of simple functions so that parameters of the transform need not be passed everytime it’s called. For this, we just need to implement call method and if required, init method. We can then use a transform like this:
Can you write Python code for it?

class Rescale(object):
    """Rescale the image in a sample to a given size.

    Args:
        output_size (tuple or int): Desired output size. If tuple, output is
            matched to output_size. If int, smaller of image edges is matched
            to output_size keeping aspect ratio the same.
    """

    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple))
        self.output_size = output_size

    def __call__(self, sample):
        image, landmarks = sample['image'], sample['landmarks']

        h, w = image.shape[:2]
        if isinstance(self.output_size, int):
            if h > w:
                new_h, new_w = self.output_size * h / w, self.output_size
            else:
                new_h, new_w = self.output_size, self.output_size * w / h
        else:
            new_h, new_w = self.output_size

        new_h, new_w = int(new_h), int(new_w)

        img = transform.resize(image, (new_h, new_w))

        # h and w are swapped for landmarks because for images,
        # x and y axes are axis 1 and 0 respectively
        landmarks = landmarks * [new_w / w, new_h / h]

        return {'image': img, 'landmarks': landmarks}

class RandomCrop(object):
    """Crop randomly the image in a sample.

    Args:
        output_size (tuple or int): Desired output size. If int, square crop
            is made.
    """

    def __init__(self, output_size):
        assert isinstance(output_size, (int, tuple))
        if isinstance(output_size, int):
            self.output_size = (output_size, output_size)
        else:
            assert len(output_size) == 2
            self.output_size = output_size

    def __call__(self, sample):
        image, landmarks = sample['image'], sample['landmarks']

        h, w = image.shape[:2]
        new_h, new_w = self.output_size

        top = np.random.randint(0, h - new_h)
        left = np.random.randint(0, w - new_w)

        image = image[top: top + new_h,
                      left: left + new_w]

        landmarks = landmarks...


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Addition:
Can you write Python code for it?

#syntax 1
y = torch.rand(5,3)
print(x + y)

#syntax 2
print(torch.add(x,y))

#providing an output tensor as argument
result = torch.empty(5,3)
torch.add(x,y,out=result)
print(result)

#in-place
#add x to y
y.add_(x)
print(y)
#Any operation that mutates a tensor in-place is post-fixed with an _.
#For example: x.copy_(y), x.t_(), will change x.

#You can use standard NumPy-like indexing with all bells and whistles!
print(x[:,1])

#Resizing: If you want to resize/reshape tensor, you can use torch.view:
x = torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)  # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

#If you have a one element tensor, use .item() to get the value as a Python number
x = torch.randn(1)
print(x)
print(x.item())


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
CUDA Tensors
Tensors can be moved onto any device using the .to method.
Can you write Python code for it?

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
    device = torch.device("cuda")          # a CUDA device object
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU
    x = x.to(device)                       # or just use strings ``.to("cuda")``
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Construct a 5x3 matrix, uninitialized:
Can you write Python code for it?


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Construct a matrix filled zeros and of dtype long:
Can you write Python code for it?

x = torch.zeros(5,3,dtype=torch.long)
print(x)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Construct a randomly initialized matrix:
Can you write Python code for it?


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Converting NumPy Array to Torch Tensor
Can you write Python code for it?

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out=a)
print(a)
print(b)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Define a Convolution Neural Network
Can you write Python code for it?

import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 5)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16*5*5,120)
        self.fc2 = nn.Linear(120,84)
        self.fc3 = nn.Linear(84,10)

    def forward(self,x):
        x = self.pool(F.relu(self.conv1(x)))
        x = self.pool(F.relu(self.conv2(x)))
        x = x.view(-1, 16*5*5)
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

net = Net()


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Gradients
Let’s backprop now Because out contains a single scalar, out.backward() is equivalent to out.backward(torch.tensor(1))
Can you write Python code for it?

out.backward()

print(x.grad)

x = torch.randn(3,requires_grad = True)
y = x*2
while y.data.norm() < 1000:
    y = y*2
print(y)

y

y.data.norm()

0.7772*0.7772+0.5340*0.5340+4.4317*4.4317

4.5309*4.5309

gradients = torch.tensor([0.1,1.0,0.001],dtype=torch.float)
y.backward(gradients)
print(x.grad)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Hmmm, what are the classes that performed well, and the classes that did not perform well:
Can you write Python code for it?

class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))

with torch.no_grad():
    for data in testloader:
        iamges, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1

for i in range(10):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Let us look at how the network performs on the whole dataset.
Can you write Python code for it?

correct = 0
total = 0
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(output.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (100 * correct / total))


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Neural Networks
Neural networks can be constructed using the torch.nn package.
Now that you had a glimpse of autograd, nn depends on autograd to define models and
differentiate them. An nn.Module contains layers, and a method forward(input)that returns the output.
Can you write Python code for it?

import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution kernel
        self.conv1 = nn.Conv2d(1, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16*5*5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2,2))
        x = F.max_pool2d(F.relu(self.conv2(x)),(2))# If the size is a square you can only specify a single number
        x = x.view(-1,self.num_flat_features(x))
        x = F.relu((self.fc1(x)))
        x = F.relu((self.fc2(x)))
        x = self.fc3(x)
        return x

    def num_flat_features(self,x):
        size = x.size()[1:]# all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net = Net()
print(net)

params = list(net.parameters())
print(len(params))
print(params[0].size())# conv1's .weight

for i in range(len(params)):
    print(i,params[i].size())

input = torch.randn(1,1,32,32)
out = net(input)
print(out)

net.zero_grad()
out.backward(torch.randn(1,10))


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Note
torch.nn only supports mini-batches. The entire torch.nn package only supports inputs that are a mini-batch of samples, and not a single sample.
For example, nn.Conv2d will take in a 4D Tensor of nSamples x nChannels x Height x Width.
If you have a single sample, just use input.unsqueeze(0) to add a fake batch dimension.
Can you write Python code for it?

output = net(input)
target = torch.randn(10)
target = target.view(1,-1)
criterion = nn.MSELoss()

loss = criterion(output,target)
print(loss)

print(loss.grad_fn)# MSELoss
print(loss.grad_fn.next_functions[0][0])# Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0]) # ReLU

net.zero_grad()# zeroes the gradient buffers of all parameters

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Remember that you will have to send the inputs and targets at every step to the GPU too:
Can you write Python code for it?

inputs, labels = inputs.to(device), labels.to(device)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Test the network on the test data
We will check this by predicting the class label that the neural network outputs,
and checking it against the ground-truth. If the prediction is correct, we add the
sample to the list of correct predictions.
Okay, first step. Let us display an image from the test set to get familiar.
Can you write Python code for it?

dataiter = iter(testloader)
images, labels = dataiter.next()

#print images
imshow(torchvision.utils.make_grid(images))
print('GroundTruth:', ' '.join('%5s'%classes[labels[j]] for j in range(4)))

#Okay, now let us see what the neural network thinks these examples above are:
outputs = net(images)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
The outputs are energies for the 10 classes. Higher the energy for a class,
the more the network thinks that the image is of the particular class. So, let’s get the index of the highest energy:
Can you write Python code for it?

_, predicted = torch.max(outputs, 1)

print('Predicted: ', ' '.join('%5s' % classes[predicted[j]] for j in range(4)))


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Then these methods will recursively go over all modules and convert their parameters and buffers to CUDA tensors:
Can you write Python code for it?


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Training a classifier
Loading and normalizing CIFAR10
Can you write Python code for it?

import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
    [transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download = True, transform = transform)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform = transform)

testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'forg', 'horse', 'ship', 'truck')

import matplotlib.pyplot as plt
import numpy as np

def imshow(img):
    img = img/2 + 0.5   #unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1,2,0)))

#get some random traning images
dataiter = iter(trainloader)
images, labels = dataiter.next()

#show images
imshow(torchvision.utils.make_grid(images))
#print labels
print(''.join('%5s' % classes[labels[j]] for j in range(4)))


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
Training on GPU
Can you write Python code for it?

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Assume that we are on a CUDA machine, then this should print a CUDA device:
print(device)


I am working on the file "PyTorch Tutorials/Deep Learning with PyTorch A 60 Minute Blitz.ipynb".
The first task is:
or create a tensor based on an existing tensor. These methods will reuse properties of the input tensor, e.g. dtype, unless new values are provided by user
Can you write Python code for it?

x = x.new_ones(5,3,dtype=torch.double) #new_* methods take in sizes
print(x)

x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x)                                # result has the same size

#Get its size:
print(x.size())


I am working on the file "PyTorch Tutorials/detach.ipynb".
The first task is:
deatch():  https://discuss.pytorch.org/t/clone-and-detach-in-v0-4-0/16861
tensor.detach() creates a tensor that shares storage with tensor that does not require grad.
Can you write Python code for it?

# a is a tensor with require grad
a = torch.tensor(2., requires_grad=True);a

b = a.detach();b # with deatch() no grad.


I am working on the file "PyTorch Tutorials/detach.ipynb".
The first task is:
tensor.data returns a new tensor that shares storage with tensor. However, it always has requires_grad=False (even if the original tensor had requires_grad=True
Can you write Python code for it?


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

x = torch.ones(2,2, requires_grad=True)
x

y = x + 2
y

y.grad_fn ### y was created as a result of an operation, hence it has a grad_fn

## more operation on y

z = y*y*3
out = z.mean()

print(z, out)

### .requires_grad_( ... ) changes an existing Tensor’s requires_grad flag in-place. The input flag defaults to False if not given.

a = torch.randn(2,2)
a = ((a*2)/(a-1))
print(a.requires_grad)

a.requires_grad_(True) ## changing the grad inplace

print(a.requires_grad)

b = (a * a).sum()
print(b.grad_fn)


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

out ## out contains a single scalar, out.backward() is equivalent to out.backward(torch.tensor(1.)).

out.backward()

print(x.grad) ### Print gradients d(out)/dx

## another example

t1 = torch.ones(1, requires_grad= True)
t2 = torch.ones(1, requires_grad=True)

print(t1, t2)

s = t1+t2
print(s)

s.grad_fn

s.backward()

t1.grad


I am working on the file "PyTorchStuff.ipynb".
The first task is:
Operation on Tensors
Can you write Python code for it?

x = torch.randn(5,2)
x

y = torch.ones(5,2)
y

x + y ## sum of two tensors

torch.add(x,y) ## alternative: sum of two tensors

## In Place addition : Any operation that mutates a tensor in-place is post-fixed with an _. For example: x.copy_(y), x.t_(), will change x.

y.add_(x)

### Standard numpy operations on tensors

x[:1]

type(x[:1])

### Resize the tensor

x = torch.randn(4,4)
x

y = x.view(16)
y

z = x.view([-1,8])
z

## transpose an array

torch.transpose(x, 0,1)

## to get the value of a tensor

x = torch.randn(1)
print(x)
print(x.item())


I am working on the file "PyTorchStuff.ipynb".
The first task is:
Tensors
Tensors are similar to NumPy’s ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing.
Can you write Python code for it?

x = torch.empty(5,3) ## empty
x

x = torch.randn(5,3) ## random initialized
x

x = torch.zeros(5,3, dtype=torch.long)
x

type(x)

x = torch.ones(5,2)
x

myarr = [[10,20.2],[30,40]] ## sample data

x = torch.tensor(myarr)
x

## create a tensor from an existing tensor

x = torch.tensor([[1,2],[3,4]], dtype=torch.int16)
print(f"X tensor: {x}")

y = torch.tensor(x, dtype=torch.float16)
print(f"Y tensor: {y}")

## size of tensor

x.size()


I am working on the file "PyTorchStuff.ipynb".
The first task is:
Vector Jacobian Product
Can you write Python code for it?

x = torch.randn(3, requires_grad=True)

y = x * 2
while y.data.norm() < 1000:
    y = y * 2

print(y)

v = torch.tensor([0.1, 1.0, 0.0001], dtype=torch.float)
y.backward(v)

print(x.grad)

print(x.requires_grad)
y = x.detach()
print(y.requires_grad)
print(x.eq(y).all())


I am working on the file "PyWCPS.ipynb".
The first task is:
Another simple example, from the same notebook:
```
for c in ( CCI_V2_release_chlor_a ), d in (CCI_V2_monthly_chlor_a_rmsd)
return
encode((float)
 avg(
 c[Long(0:10), Lat(45:55), ansi("2010-01-31T23:59:00")] *
  (d[Long(0:10), Lat(45:55), ansi("2010-01-31T23:59:00")] < 0.1 )
 ), "csv"
)
```
Translates to:
Can you write Python code for it?

@wcps
def test_cloro2():
    return For(c="CCI_V2_release_chlor_a",
               d="CCI_V2_monthly_chlor_a_rmsd")[
        encode(
          cast('float',
             avg(c[axis('Long',0,10),
                   axis('Lat', 45,55),
                   axis('ansi', '2010-01-31T23:59:00')] *
                 (d[axis('Long',0,10),
                    axis('Lat', 45,55),
                    axis('ansi', '2010-01-31T23:59:00')] < 0.45))
        ), "csv")]

float(eo.get_str(test_cloro2))


I am working on the file "PyWCPS.ipynb".
The first task is:
The following examples are from the Domain Examples Notebook of GeoPython 2017 (mine is at: https://jupyter.eofrom.space/user/jarnaldich/notebooks/jupyter_notebooks/geopython_workshop_2017/Domain_examples.ipynb)
This query:
```
for  d in (CCI_V2_monthly_chlor_a_rmsd)
return
encode((float)
count(d[ ansi("2010-01-31T23:59:00")] < 0.2 )
, "csv"
)
```
Is equivalent to the following Python snippet:
Can you write Python code for it?

@wcps
def test_cloro():
    return For(c="CCI_V2_monthly_chlor_a_rmsd")[
         encode(cast('float',
                    count(c[ansi("2010-01-31T23:59:00")] < 0.201)),
               "csv")]

float(eo.get_str(test_cloro))


I am working on the file "PyWCPS.ipynb".
The first task is:
The mechanics of the above should be fairly obvious. The user needs to define a function and decorate it with wcps. The decorator performs some magic so that WCPS code can be obtained from the function definition. The generated code can always be inspected, by the way:
Can you write Python code for it?


I am working on the file "PyWCPS.ipynb".
The first task is:
which looks kinda cute. Notice that the helper functions do not need to be inner functions. They could just as well be outside for reuse, if needed. For example, it would be entirely possible to write a function that automatically generates the switch statement for a colorscale like the above from a list of value cuts, which would make the above function even shorter.
Before you ask, I got the coverage constructor, too... I called it "New"
Can you write Python code for it?

@wcps
def test_coverage_constructor():
    l = 100000
    def roi(cov, time):
        return cov[axis('Long', -50, -40),
                   axis('Lat', 45,55),
                   axis('ansi', time, crs="CRS:1")]

    def term(cov, time):
        return (add((roi(cov, time) < l) * roi(cov, time))
                /
                count(roi(cov, time) < l))

    return For(c="CCI_V2_release_daily_chlor_a")[
            encode(cast('float',
                        New('histogram',
                            px=axis('x', 0, 0),
                            py=axis('y', 0, 0),
                            pt=axis('t', 0, 360))[
                                term(c, pt) #+ term(c, pt+1) + term(c, pt+2)
                            ]), "csv")]

res_txt = eo.get_str(test_coverage_constructor)
plt.plot([float(x) for x in res_txt[2:-2].split(",")])


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
... It was too slow!
Chudnovsky brothers' formula (hard):
$$\frac{1}{\pi} \simeq 12 \sum_{n=0}^N \frac {(-1)^{n}(6n)!(545140134n+13591409)}{(3n)!(n!)^{3}640320^{{3n+3/2}}}. $$
In 2015, the best method is still the Chudnovsky brothers's formula.

Be careful when you use these formulae, check carefully the constants you wrote (545140134 will work well, 545140135 might not work as well!).
This formula is used as the logo of the French agrégation of Mathematics website agreg.org :
Can you write Python code for it?

from math import factorial

def chudnovsky(max_step):
    """ Computing an approximation of pi with Bellard series."""
    my_pi = Decimal(0)
    for k in range(max_step):
        my_pi += (Decimal(-1)**k)*(Decimal(factorial(6*k))/((factorial(k)**3)*(factorial(3*k)))* (13591409+545140134*k)/(640320**(3*k)))
    my_pi = my_pi * Decimal(10005).sqrt()/4270934400
    my_pi = my_pi**(-1)
    return my_pi


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
A simple Monte-Carlo method
A simple Monte Carlo method for computing $\pi$ is to draw a circle inscribed in a square, and randomly place dots in the square.
The ratio of dots inside the circle to the total number of dots will approximately equal $\pi / 4$, which is also the ratio of the area of the circle by the area of the square:

In Python, such simulation can basically be implemented like this:
Can you write Python code for it?

from random import uniform

def montecarlo_pi(nbPoints=10000):
    """Returns a probabilist estimate of pi, as a float number."""
    nbInside = 0
    # we pick a certain number of points (nbPoints)
    for i in range(nbPoints):
        x = uniform(0, 1)
        y = uniform(0, 1)
        # (x, y) is now a random point in the square [0, 1] × [0, 1]
        if (x**2 + y**2) < 1:
            # This point (x, y) is inside the circle C(0, 1)
            nbInside += 1

    return 4 * float(nbInside) / floor(nbPoints)

print("The simple Monte-Carlo method with {} random points gave pi = {}".format(10000, montecarlo_pi(10000)))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
And it will most probably be the quickest method presented here:
Can you write Python code for it?


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Applying Machin's formula
Finally, the main function uses Machin's formula to compute $\pi$ using the necessary level of precision, by using this high precision $\mathrm{arccot}$ function:
$$\pi = 16 \;\mathrm{arccot}(5) - 4 \;\mathrm{arccot}(239).$$
python
def machin(digits):
    unity = 10**(digits + 10)
    pi = 4 * (4*arccot(5, unity) - arccot(239, unity))
    return pi / unity
To avoid rounding errors in the result, we use 10 guard digits internally during the calculation.
We may now reproduce the historical result obtained by Machin in 1706.
Can you write Python code for it?

def machin(digits):
    """Compute pi with Machin's formula, with precision at least digits."""
    unity = 10**(digits + 10)
    my_pi = Decimal(4) * (Decimal(4)*arccot(5, unity) - arccot(239, unity))
    return my_pi / Decimal(unity)

getcontext().prec = 10000  # trick to improve precision
my_pi = machin(100)

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Asking for $10$ times more digits take about $100$ more of time (that's a bad news).
Can you write Python code for it?


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Bellard's formula (hard):
It is a more efficient formula.
$$\pi \simeq \frac1{2^6} \sum_{n=0}^N \frac{(-1)^n}{2^{10n}} \, \left(-\frac{2^5}{4n+1} - \frac1{4n+3} + \frac{2^8}{10n+1} - \frac{2^6}{10n+3} - \frac{2^2}{10n+5} - \frac{2^2}{10n+7} + \frac1{10n+9} \right). $$
Can you write Python code for it?

def bellard(max_step):
    """ Computing an approximation of pi with Bellard series."""
    my_pi = Decimal(0)
    for k in range(max_step):
        my_pi += (Decimal(-1)**k/(1024**k))*( Decimal(256)/(10*k+1) + Decimal(1)/(10*k+9) - Decimal(64)/(10*k+3) - Decimal(32)/(4*k+1) - Decimal(4)/(10*k+5) - Decimal(4)/(10*k+7) -Decimal(1)/(4*k+3))
    return my_pi * Decimal(1.0/(2**6))

getcontext().prec = 40  # trick to improve precision
my_pi = bellard(10)
my_pi

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
If we know the digits, we can directly print them:
Can you write Python code for it?


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
It does not use Decimal numbers.
Can you write Python code for it?

getcontext().prec = 50  # trick to improve precision
generator_pi(1000)

getcontext().prec = 5000  # trick to improve precision
generator_pi(1000)


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
It gets $2834$ correct numbers in $200$ steps!
It is more efficient that Ramanujan's formula, but slower to compute.
Can you write Python code for it?

getcontext().prec = 6000  # trick to improve precision
my_pi = chudnovsky(400)

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))

getcontext().prec = 3000  # trick to improve precision
%timeit chudnovsky(200)

getcontext().prec = 6000  # trick to improve precision
%timeit chudnovsky(400)


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
It is very efficient, as Ramanujan's formula.
Can you write Python code for it?

getcontext().prec = 3000  # trick to improve precision
my_pi = chudnovsky(200)
my_pi

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
It is, of course, slower than the optimized algorithm from mpmath.
And it does not scale well with the precision:
Can you write Python code for it?

getcontext().prec = 10 + 1000  # trick to improve precision
%timeit bellard(1000)

getcontext().prec = 10 + 2000  # trick to improve precision
%timeit bellard(2000)


getcontext().prec = 10 + 1000  # trick to improve precision
%timeit bbp(1000)

getcontext().prec = 10 + 2000  # trick to improve precision
%timeit bbp(2000)


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Let save it for further comparison of simpler methods.
Can you write Python code for it?

mp.dps = 100000  # number of digits
len(str(mp.pi))
mpmath_pi = Decimal(str(mp.pi))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Let's try to answer my initial question, using this naive implementation.
Can you write Python code for it?

%%time
getcontext().prec = 140350  # trick to improve precision
i = 140317
my_pi = ramanujan(10000)
print(str(my_pi)[2:][i:i+10])

mp.dps=140330
print(str(mp.pi)[2:][i:i+10])


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
Methods based on a convergent series
For the following formulae, you can try to write a program that computes the partial sum of the series, up to a certain number of term ($N \geq 1$).
Basically, the bigger the $N$, the more digits you get (but the longer the program will run).
Some methods might be really efficient, only needing a few number of steps (a small $N$) for computing many digits.
Try to implement and compare at least two of these methods.
You can use the function sum (or math.fsum) to compute the sum, or a simple while/for loop.
All these partial sums are written up to an integer $N \geq 1$.
A Leibniz formula (easy):
It has a number of digits sub-linear in the number $N$ of terms in the sum: we need $10$ times more terms to win just one digit.
$$\pi \simeq 4\sum_{n=0}^{N} \cfrac {(-1)^n}{2n+1}. $$
Can you write Python code for it?

def leibniz(max_step):
    """ Computing an approximation of pi with Leibniz series."""
    my_pi = Decimal(0)
    for k in range(max_step):
        my_pi += Decimal((-1)**k) / Decimal(2*k+1)
    return Decimal(4) * my_pi

getcontext().prec = 20  # trick to improve precision
my_pi = leibniz(1000)
my_pi

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))

getcontext().prec = 20  # trick to improve precision
my_pi = leibniz(10000)
my_pi

accuracy = 100*abs(mpmath_pi - my_pi)/mpmath_pi
print("Accuracy % with mpmath_pi: {:.4g}".format(accuracy))


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
So we got the first $9995$ digits correctly... in $45$ seconds.
That will still be too slow to have the digits at position $130317$.
Can you write Python code for it?

getcontext().prec = 5000  # trick to improve precision
%timeit machin(50)

getcontext().prec = 10000  # trick to improve precision
%timeit machin(100)


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
The program can be used to compute tens of thousands of digits in just a few seconds on a modern computer.
Typical implementation will be in highly efficient compiled language; like C or maybe Julia.
Many Machin-like formulas also exist, like:
$$\pi = 4\arctan\left(\frac{1}{2}\right) + 4 \arctan\left(\frac{1}{3}\right).$$
Trying to solve my question!
The important parameter to tune is not the precision given to machin() but the Decimal.prec precision.
Can you write Python code for it?

%%time
i = 14031
getcontext().prec = i + 20  # trick to improve precision
mp.dps = i + 20
print(str(mp.pi)[2:][i:i+10])

my_pi = machin(11)
print(str(my_pi)[2:][i:i+10])

%%time
i = 140317
getcontext().prec = i + 20  # trick to improve precision
mp.dps = i + 20
print(str(mp.pi)[2:][i:i+10])

my_pi = machin(50)
print(str(my_pi)[2:][i:i+10])


I am working on the file "Py_Pi_Day_2017.ipynb".
The first task is:
We can solve the initial challenge easily:
Can you write Python code for it?


I am working on the file "PyconZA-2016.ipynb".
The first task is:
A better Autoencoder
Can you write Python code for it?

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout

def make_autoencoder(nb_input=nb_input, nb_hidden=nb_hidden,
                     activation='sigmoid', init='uniform'):
    ae = Sequential()
    # encoder
    ae.add(Dense(nb_hidden, input_dim=nb_input, init=init))
    ae.add(Activation(activation))
    # decoder
    ae.add(Dense(nb_input, input_dim=nb_hidden, init=init))
    ae.add(Activation(activation))
    return ae

nb_epoch = 1
ae2 = make_autoencoder(activation='sigmoid', init='glorot_uniform')
ae2.compile(loss='mse', optimizer='adam')
ae2.fit(X_train, X_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)

plot_10_by_20_images(images_test, to_images(ae2.predict(X_test)), figsize=(10,5))

def draw_ae2_prediction(j):
    X_plot = X_test[j:j+1]
    prediction = ae2.predict(X_plot, verbose=False)
    plot_1_by_2_images(to_images(X_plot)[0], to_images(prediction)[0])

interact(draw_ae2_prediction, j=(0, len(X_test)-1))
None


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Autoencoders
Hinton 2006 (local)
Can you write Python code for it?

from IPython.display import HTML
HTML('<iframe src="pdf/Hinton2006-science.pdf" width=800 height=400></iframe>')

from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout

print('nb_input =', nb_input)
print('nb_hidden =', nb_hidden)
ae = Sequential()
# encoder
ae.add(Dense(nb_hidden, input_dim=nb_input, init='uniform'))
ae.add(Activation('sigmoid'))
# decoder
ae.add(Dense(nb_input, input_dim=nb_hidden, init='uniform'))
ae.add(Activation('sigmoid'))

ae.compile(loss='mse', optimizer='SGD')

nb_epoch = 1
ae.fit(X_train, X_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)

plot_10_by_20_images(images_test, to_images(ae.predict(X_test)),
                     figsize=(10,5))

from keras.optimizers import SGD
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

ae.compile(loss='mse', optimizer=sgd)
nb_epoch = 1
ae.fit(X_train, X_train, batch_size=batch_size, nb_epoch=nb_epoch, verbose=1)

plot_10_by_20_images(images_test, to_images(ae.predict(X_test)),
                     figsize=(10,5))

def draw_ae_prediction(j):
    X_plot = X_test[j:j+1]
    prediction = ae.predict(X_plot, verbose=False)
    plot_1_by_2_images(to_images(X_plot)[0], to_images(prediction)[0])

interact(draw_ae_prediction, j=(0, len(X_test)-1))
None


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Did you notice anything about the accuracy? Let's train it some more.
Can you write Python code for it?

mlp2.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch,
         verbose=1)

mlp2.evaluate(X_test, y_test)


I am working on the file "PyconZA-2016.ipynb".
The first task is:
For example, let's inspect the first 2 labels:
Can you write Python code for it?

print('labels_train[:2]:\n', labels_train[:2][:, np.newaxis])
print('y_train[:2]\n', y_train[:2])


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Let's load some data
Can you write Python code for it?

from keras.datasets import mnist
#(images_train, labels_train), (images_test, labels_test) = mnist.load_data()
print("Data shapes:")
print('images',images_train.shape)
print('labels', labels_train.shape)


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Once the model is trained, we can evaluate its performance on the test data.
Can you write Python code for it?

mlp.evaluate(X_test, y_test)

#plot_10_by_10_images(images_test, figsize=(8,8))

def draw_mlp_prediction(j):
    plot_mnist_digit(to_images(X_test)[j])
    prediction = mlp.predict_classes(X_test[j:j+1], verbose=False)[0]
    print('predict:', prediction, '\tactual:', labels_test[j])

interact(draw_mlp_prediction, j=(0, len(X_test)-1))
None


I am working on the file "PyconZA-2016.ipynb".
The first task is:
So building and training Neural Networks in Python in simple!
But it is also powerful!
Neural Style Transfer: github.com/titu1994/Neural-Style-Transfer

+

=

Neural Networks

A neuron looks something like this

Symbolically we can represent the key parts we want to model as

In order to build an artifical "brain" we need to connect together many neurons in a "neural network"

We can model the response of each neuron with various activation functions

Training a Neural Network

Mathematically the activation of each neuron can be represented by

where $W$ and $b$ are the weights and bias respectively.
Loss Function

Neural Networks in Python
Keras

High level library for specifying and training neural networks
Can use Theano or TensorFlow as backend

Keras makes Neural Networks awesome!
Theano

Python library that provides efficient (low-level) tools for working with Neural Networks
In particular:
Automatic Differentiation (AD)
Compiled computation graphs
GPU accelerated computation

Tensorflow

Deep Learning framework by Google

The MNIST Dataset

70,000 handwritten digits
60,000 for training
10,000 for testing

As 28x28 pixel images
Can you write Python code for it?

from __future__ import absolute_import, print_function, division
from ipywidgets import interact, interactive, widgets
import numpy as np
np.random.seed(1337)  # for reproducibility


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Transform the labels to a "one-hot" encoding ...
Can you write Python code for it?

# The labels need to be transformed into class indicators
from keras.utils import np_utils
y_train = np_utils.to_categorical(labels_train, nb_classes=10)
y_test = np_utils.to_categorical(labels_test, nb_classes=10)
print('labels_train:', labels_train.shape, labels_train.dtype)
print('y_train:', y_test.shape, y_train.dtype)


I am working on the file "PyconZA-2016.ipynb".
The first task is:
Visualising the Filters
Can you write Python code for it?

def visualise_filter(model, layer_index, filter_index):
    from keras import backend as K

    # build a loss function that maximizes the activation
    # of the nth filter on the layer considered
    layer_output = model.layers[layer_index].get_output()
    loss = K.mean(layer_output[:, filter_index])

    # compute the gradient of the input picture wrt this loss
    input_img = model.layers[0].input
    grads = K.gradients(loss, input_img)[0]

    # normalization trick: we normalize the gradient
    grads /= (K.sqrt(K.mean(K.square(grads))) + 1e-5)

    # this function returns the loss and grads given the input picture
    iterate = K.function([input_img], [loss, grads])

    # we start from a gray image with some noise
    input_img_data = np.random.random((1,nb_input,))
    # run gradient ascent for 20 steps
    step = 1
    for i in range(100):
        loss_value, grads_value = iterate([input_img_data])
        input_img_data += grads_value * step

        #print("Current loss value:", loss_value)
        if loss_value <= 0.:
            # some filters get stuck to 0, we can skip them
            break
    print("Current loss value:", loss_value)

    # decode the resulting input image
    if loss_value>0:
        #return input_img_data[0]
        return input_img_data
    else:
        raise ValueError(loss_value)

def draw_filter(i):
    flt = visualise_filter(mlp, 3, 4)
    #print(flt)
    plot_mnist_digit(to_images(flt)[0])

interact(draw_filter, i=[0, 9])


I am working on the file "PyconZA-2016.ipynb".
The first task is:
and then visualise it
Can you write Python code for it?

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt

def plot_mnist_digit(image, figsize=None):
    """ Plot a single MNIST image."""
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    if figsize:
        ax.set_figsize(*figsize)
    ax.matshow(image, cmap = matplotlib.cm.binary)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.show()

def plot_1_by_2_images(image, reconstruction, figsize=None):
    fig = plt.figure(figsize=figsize)
    ax = fig.add_subplot(1, 2, 1)
    ax.matshow(image, cmap = matplotlib.cm.binary)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    ax = fig.add_subplot(1, 2, 2)
    ax.matshow(reconstruction, cmap = matplotlib.cm.binary)
    plt.xticks(np.array([]))
    plt.yticks(np.array([]))
    plt.show()

def plot_10_by_10_images(images, figsize=None):
    """ Plot 100 MNIST images in a 10 by 10 table. Note that we crop
    the images so that they appear reasonably close together.  The
    image is post-processed to give the appearance of being continued."""
    fig = plt.figure(figsize=figsize)
    #images = [image[3:25, 3:25] for image in images]
    #image = np.concatenate(images, axis=1)
    for x in range(10):
        for y in range(10):
            ax = fig.add_subplot(10, 10, 10*y+x+1)
            ax.matshow(images[10*y+x], cmap = matplotlib.cm.binary)
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))
    plt.show()

def plot_10_by_20_images(left, right, figsize=None):
    """ Plot 100 MNIST images next to their reconstructions"""
    fig = plt.figure(figsize=figsize)
    for x in range(10):
        for y in range(10):
            ax = fig.add_subplot(10, 21, 21*y+x+1)
            ax.matshow(left[10*y+x], cmap = matplotlib.cm.binary)
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))
            ax = fig.add_subplot(10, 21, 21*y+11+x+1)
            ax.matshow(right[10*y+x], cmap = matplotlib.cm.binary)
            plt.xticks(np.array([]))
            plt.yticks(np.array([]))
   ...


I am working on the file "PyconZA-2016.ipynb".
The first task is:
then we compile it. This takes the symbolic computational graph of the model and compiles it an efficient implementation which can then be used to train and evaluate the model.
Note that we have to specify what loss/objective function we want to use as well which optimisation algorithm to use. SGD stands for Stochastic Gradient Descent.
Can you write Python code for it?

mlp.compile(loss='categorical_crossentropy', optimizer='SGD',
            metrics=["accuracy"])


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
1.4 Creating cells
To create a new code cell, click "Insert > Insert Cell [Above or Below]". A code cell will automatically be created.
To create a new markdown cell, first follow the process above to create a code cell, then change the type from "Code" to "Markdown" using the dropdown next to the run, stop, and restart buttons.
You can also use the + button to create a cell and the scissors buttom to cut it out.
1.5 Re-running cells
If you find a bug in your code, you can always update the cell and re-run it. However, any cells that come afterward won't be automatically updated. Try it out below. First run each of the three cells. The first two don't have any output, but you will be able to tell they've run because a number will appear next to them, for example, "In [5]". The third cell should output the message "Intro to Data Analysis is awesome!"
Can you write Python code for it?

who = "Python"
who

message = "We love " + who + "!"

message


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
1.7 Final tips
If the output is too long click on the left part of the result:
* One click will confine the result to a box
* Double click will hide the result
One final thing to remember: if you shut down the kernel after saving your notebook, the cells' output will still show up as you left it at the end of your session when you start the notebook back up. However, the state of the kernel will be reset. If you are actively working on a notebook, remember to re-run your cells to set up your working environment to really pick up where you last left off.
2. WORKSHOP
Handy documentation:
Pandas Docs
Pandas API Reference
Pandas DataFrames
THE "ZIKA REPORTS"
2.1 Obtaining and Preparing data
For this exercise we are going to use a real data set of Zika infection cases. The original data is provided by the  Centers for Desease Control and Prevention (CDC) in raw HTML. You can find it in the CDC site from where it is scrapped and formatted as CSVs files.
The CSVs are available from:
* Epidemic Prediction Innitiative github: https://github.com/cdcepi/zika
* Our github: https://github.com/PyLadiesDC/python-for-journalists
You've downloaded already all the data so you should have all that you need to start right now.
2.2 Check that we have all necessary files in current directory
First of all, we have to ensure that all files we downloded are in the working directory and all data files are available.
In this exercice we are going to work with data stored in the data folder. Check that you have that folder running the following cell.
Can you write Python code for it?

import os
print os.getcwd()
print os.listdir('.')
print os.listdir('data')


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.4 Concatenate several DataFrames
If we want to have the historical data to see the evolution of the disease we need to create a new DataFrame containing the content of the different csv files. We use pd.concat() to concatenate one DataFrame per csv file.
Can you write Python code for it?

import glob

#glob finds all the pathnames matching a specified pattern
csv_list = glob.glob("data/*.csv")

df_list = []
for f in csv_list:
    df = pd.read_csv(f)
    df_list.append(df)
year_df = pd.concat(df_list, ignore_index=True)
# NOTE: a more pythonic way of doing the last five lines would be:
# year_df = pd.concat((pd.read_csv(f) for f in all_files), ignore_index=True)

year_df.shape

year_df.head(2)


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.5 Arrange columns
2.5.1 Processing dates
We want to process the report_date column, being able to extract the year or the month or any other part of the date:
* we need to import datetime library
* using strptime to create a datetime object according to a format (see the link for understand the format directives)
Can you write Python code for it?

import datetime as dt

#creates a datetime object from a string representing a date and time and a corresponding format string.
my_date = dt.datetime.strptime('2010-06-17', '%Y-%m-%d')

# from the datetime object we can extract the following attributes and functions
print my_date.year
print my_date.month
print my_date.day
print my_date.hour
print my_date.minute
print my_date.isocalendar() #returns a tuple (ISO year, ISO week number, ISO weekday)


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.5.2 New column -- 'week'
Now we are going to create a column week from the column report_date using the apply method:
* We first define a function that extracts the week number for each of the dates.
* Then we need to apply this function to all the elements in the report_date column.
Can you write Python code for it?

def get_week_number(any_date):
     return dt.datetime.strptime(any_date,'%Y-%m-%d').isocalendar()[1]

# we apply the function to each of the elements of the column "report_date"
year_df['week'] = year_df['report_date'].apply(get_week_number)

year_df.head()


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.5.4 Deleting columns
We also want to delete the columns that we don't need. We use the drop method with:
* axis = 1 specifying that we are deleting a column (0 for deleting rows)
* inplace = True specifies that we are deleting the column in our object, with inplace = False we are creating a new DataFrame without that column.
Can you write Python code for it?

year_df.drop('time_period', axis=1, inplace=True)
year_df.head()


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.5.6 Solution to the previous exercise
Can you write Python code for it?

# Adding "country" column
def get_country(location):
    return location.split("-")[0]
year_df['country'] = year_df['location'].apply(get_country)

# Deleting extra columns
year_df.drop('time_period_type', axis=1, inplace=True)
year_df.drop('unit', axis=1, inplace=True)
year_df.drop('data_field_code', axis=1, inplace=True)
year_df.drop('location', axis=1, inplace=True)

year_df.head()


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.6.2 Applying conditions
Which are the zika infection cases in the District of Columbia?
Can you write Python code for it?

year_df[(year_df["state"] == "District_of_Columbia") & (year_df["week"] == 30)]


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.6.3 Exercise - try it on your own!

Try to do this exercise without having a look to the solution in the next paragraph
Use year_df dataframe and conditions to create a new df called syear_df where the state column contains only state names, not territory names
To ensure that we are making a copy use the copy() method
Can you write Python code for it?


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.6.4 Solution to exercise 2.6.3
Can you write Python code for it?

syear_df = year_df[year_df['location_type'] == 'state'].copy()
syear_df.head()


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.7 Grouping and ordering
2.7.1 Which states have more zika cases?

First we selected the chunk that we want to use, the latest report data
Can you write Python code for it?

latest_df = syear_df[syear_df['week'] == max_week].copy()
latest_df.head()


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.8 Plotting Data
matplotlib is one of the most common libraries for making 2D plots of arrays in Python, Pandas library has a plot() method that wraps the plotting functionalities of matplotlib. We'll try to use this method when possible, for simplicity.
Even so, in order to see the plots inmediately in our script we need to use the matplotlib magic line as we'll see in the next paragraph. More about magic lines here.
2.8.1 Bar graph with the aggregated cases of zika in the US
Can you write Python code for it?

"""This is the matplotlib magic line, by default matplotlib defers drawing until the end
of the script. But we need matplotlib to works interactively and draw the plots right away"""
%matplotlib inline

# Seaborn is a library that makes your plots prettier
import seaborn as sns

# we are drawing a plot bar with values in axis Y
# by default the indexes of the DF (states) are used for axis X
sorted_df[:10].plot.bar(y='value', figsize=(8,4), title='zika cases')


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.8.6 Stacked bar plots (extra)
We want to show in the same bar plot which cases of zika were transmitted locally and which cases where due to travelling to affected areas.
In order to plot two different variables travel and local we need to create two new columns for these variables.
Can you write Python code for it?

# We'll use latest_df dataframe which contains the most recent values, already filtered only for states.

latest_df['travel'] = latest_df.loc[latest_df['data_field'] == 'zika_reported_travel','value']
latest_df['local'] = latest_df.loc[latest_df['data_field'] == 'zika_reported_local','value']
latest_df.head()

group_df = latest_df.groupby('state').sum()
group_df.sort_values(by="value", ascending = False, inplace = True)
group_df[:10].plot.bar(y=['travel','local'], stacked = True, figsize=(8,4), title='zika cases')


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.8.7 Arranging the axes for dates (extra)
This example is a little bit more complex, in this case we have to go down to the original matplotlib library and use its plot_data method that allows as to configure their axes according to our necessities.
Can you write Python code for it?

import matplotlib.pyplot as plt
import matplotlib.dates as dates

# group by date returns a df with report_date as indexes
bydate_df = syear_df.groupby('report_date').sum()

# Series with the values to plot
y_values = bydate_df['value']
# Get the values of the indexes (our dates) and returns a list of Datetime objects
my_date_index = pd.DatetimeIndex(bydate_df.index.values).to_pydatetime()

f = plt.figure(figsize=(8, 4))
ax = f.gca() # get current axes
# set ticks location in the X axis, in the 14th day of each month (to see Sept)
ax.xaxis.set_major_locator(dates.MonthLocator(bymonthday=14))
# set ticks format in the X axis => (%b is abbreviated month)+new line+ year
ax.xaxis.set_major_formatter(dates.DateFormatter('%b\n%Y'))
plt.plot_date(
            my_date_index, # x values
            y_values,      # y values
            fmt='-',     # format of the line
            xdate=True,  # x-axis will be labeled with dates
            ydate=False, # y-axis don't
            color='red')


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
2.9 Store data in a file
Again, consult Pandas Input/Output commands to check other file formats available.
Can you write Python code for it?


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
But we should store the value of the latest week, to know which are the latest results if we need them.
Can you write Python code for it?

max_week = year_df["week"].max()
print max_week


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
Hint: color names for plotting:
http://matplotlib.org/mpl_examples/color/named_colors.hires.png
2.8.4 Linegraph with the evolution of cases throught 2016 in the US
Can you write Python code for it?

# Remember that syear_df is our dataframe with the anual data for the states
weekly_df = syear_df.groupby('week').sum()
weekly_df.head()
# now the weeks are the new indexes

# again the X axis takes by default the indexes of the DF (the weeks)

weekly_df.plot.line(y='value',figsize=(8,4), title='zika evolution in the US', color='orange')


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
NOTE: If you try to execute the drop twice you'll get an error because that column doesn't exist anymore. If you want to reset the kernel and start from scratch, you can go to the meny "Kernel" and select any of the "Restart" options and afterwards going to the menu "Cell" and Run the cells above or below the focused cell.
2.5.5 Exercise - try it on your own!

Try to do this exercise without having a look to the solution in the next paragraph
Create a column country similarly as the one you created before
Delete the column time_period_type, unit, data_field_code and location that you are not using
Can you write Python code for it?


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
Now we group the data by state to sum both local and travel cases
Can you write Python code for it?

sum_df = latest_df.groupby('state').sum()
sum_df.head()
# see how all the numerical columns ar added up, although the **week** column added doesn't make any sense
# pay attention to how the resulted DF has states as indexes


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
Once you've run all three cells, try modifying the first one to set who to your anything else.
Rerun the first and third cells without rerunning the second and see what happens. It seems that you need to run the three cells to have the expected result.
Often, after changing a cell, you'll want to rerun all the cells below it.  You can do that quickly by clicking "Cell > Run All Below".
1.6 Autocompletion
Use TAB to automplete python methods and variables.
Can you write Python code for it?


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
The attribute columns returns a list of the column names.
Can you write Python code for it?


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
We create a Pandas DataFrame from a csv file using the read_csv method. The name of the columns are going to be those of the CSVs columns.
Check Pandas Input/Output commands to check other available read methods read_json or read_excel.
Can you write Python code for it?

"""shape is an attribute (invoked without parenthesis)
tells us how many rows and columns our DataFrame has"""
august_df = pd.read_csv("data/CDC_Report-2016-08-31.csv")
august_df.shape


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
We order the df by the value columns
Can you write Python code for it?

sorted_df = sum_df.sort_values(by='value', ascending=False)
sorted_df[:10]


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
Which states or territories have zika cases due to local infection?
Can you write Python code for it?

year_df[(year_df["value"] != 0) & (year_df["week"] == max_week) & (year_df["data_field"] == "zika_reported_local") ]


I am working on the file "Pyjournalists_zika.ipynb".
The first task is:
Wouldn't it be clearer if the highest value is at the top of the plot?

2.8.2 Exercise - try it on your own!

Try to do this exercise without having a look to the solution in the next paragraph
Draw the same horizontal bar graph that we've done before but ordered from the top to the bottom
You need to re-write the code we've done for the graph, read the instructions in the following cell
Can you write Python code for it?

# This is the code we used:
# sorted_df = sum_df.sort_values(by='value', ascending=False)
# sorted_df[:10].plot.barh(y='value', figsize=(8,4), title='zika cases')

# 1. sort the dataframe in an ascending way
# 2. get the last 10 positions of the dataframe instead of the first ten


I am working on the file "Pynq-Z1/notebooks/Video_PR/Box_Blur_Filter.ipynb".
The first task is:
2. Connect camera
Physically connect the camera to the HDMI-in port of the PYNQ. Run the following code to instruct the PYNQ to capture the video from the camera and to begin streaming video to your monitor (connected to the HDMI-out port).
Can you write Python code for it?

hdmi_in = HDMI('in')
hdmi_out = HDMI('out', frame_list=hdmi_in.frame_list)
hdmi_out.mode(3)
hdmi_out.start()
hdmi_in.start()


I am working on the file "Pynq-Z1/notebooks/Video_PR/Box_Blur_Filter.ipynb".
The first task is:
3. Program board
Run the following script to download the Box Blur Filter to the PYNQ. This applies the bluring filter to the video stream.
Can you write Python code for it?

Bitstream_Part("box_blur_p.bit").download()

import ipywidgets as widgets
from ipywidgets import Button, HBox, VBox, Label

words = ['HDMI Reset']
items = [Button(description=w) for w in words]

def on_hdmi_clicked(b):
    hdmi_out.stop()
    hdmi_in.stop()
    hdmi_out.start()
    hdmi_in.start()

items[0].on_click(on_hdmi_clicked)

widgets.VBox([items[0]])