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

testing_data = [
    ['Green', 3, 'Apple'],
    ['Yellow', 4, 'Apple'],
    ['Red', 2, 'Grape'],
    ['Red', 1, 'Grape'],
    ['Yellow', 3, 'Lemon'],
]


I am working on the file "DecisionTree_Math_Fruits.ipynb".
The first task is:
Test the model with test data
Write a function to classify the test data
Can you write Python code for it?

def classify(row, node):

    # Base case: we've reached a leaf
    if isinstance(node, Leaf):
        return node.predictions

    # Decide whether to follow the true-branch or the false-branch.
    # Compare the feature / value stored in the node,
    # to the example we're considering.
    if node.question.match(row):
        return classify(row, node.true_branch)
    else:
        return classify(row, node.false_branch)


I am working on the file "DecisionTree_Math_Fruits.ipynb".
The first task is:
Useful data and Methods for our Dataset manipulation
Can you write Python code for it?

#Column names for our data
header = ["color","diameter","label"]

"""Find the unique values for a column in dataset"""
def unique_values(rows,col):
     return set([row[col] for row in rows])

"""count the no of examples for each label in a dataset"""
def class_counts(rows):
    counts = {}  # a dictionary of label -> count.
    for row in rows:
        # in our dataset format, the label is always the last column
        label = row[-1]
        if label not in counts:
            counts[label] = 0
        counts[label] += 1
    return counts

"""Check if the value is numeric"""
def is_numeric(value):
    return isinstance(value, int) or isinstance(value, float)


I am working on the file "Decision_Trees.ipynb".
The first task is:
Creating your first decision tree
You will use the scikit-learn and numpy libraries to build your first decision tree. scikit-learn can be used to create tree objects from the DecisionTreeClassifier class. The methods that we will use take numpy arrays as inputs and therefore we will need to create those from the DataFrame that we already have. We will need the following to build a decision tree
* target: A one-dimensional numpy array containing the target/response from the train data. (Survival in your case)
* features: A multidimensional numpy array containing the features/predictors from the train data. (ex. Sex, Age)
Take a look at the sample code below to see what this would look like:
target = train["Survived"].values

features = train[["Sex", "Age"]].values

my_tree = tree.DecisionTreeClassifier()

my_tree = my_tree.fit(features, target)

One way to quickly see the result of your decision tree is to see the importance of the features that are included. This is done by requesting the .feature_importances_ attribute of your tree object. Another quick metric is the mean accuracy that you can compute using the .score() function with features_one and target as arguments.
Ok, time for you to build your first decision tree in Python!
Can you write Python code for it?

# Create the target and features numpy arrays: target, features_one
target = train["Survived"].values
columns_one = ["Pclass", "Sex", "Age", "Fare"]
features_one = train[columns_one].values
features_one

# Fit your first decision tree: my_tree_one
my_tree_one = tree.DecisionTreeClassifier()
my_tree_one = my_tree_one.fit(features_one, target)

# Look at the importance and score of the included features
print(my_tree_one.feature_importances_)
print(my_tree_one.score(features_one, target))


I am working on the file "Decision_Trees.ipynb".
The first task is:
Does age play a role?
Another variable that could influence survival is age; since it's probable that children were saved first. You can test this by creating a new column with a categorical variable Child. Child will take the value 1 in cases where age is less than 18, and a value of 0 in cases where age is greater than or equal to 18.
To add this new variable you need to do two things (i) create a new column, and (ii) provide the values for each observation (i.e., row) based on the age of the passenger.
Adding a new column with Pandas in Python is easy and can be done via the following syntax:
your_data["new_var"] = 0

This code would create a new column in the train DataFrame titled new_var with 0 for each observation.
To set the values based on the age of the passenger, you make use of a boolean test inside the square bracket operator. With the []-operator you create a subset of rows and assign a value to a certain variable of that subset of observations. For example,
train["new_var"][train["Fare"] > 10] = 1

would give a value of 1 to the variable new_var for the subset of passengers whose fares greater than 10. Remember that new_var has a value of 0 for all other values (including missing values).
The data below shows that 54% of children survived, but only 38% of adults survived.  So yes, age does play a role.
Can you write Python code for it?

# Added a new column called Child in the train data frame that initially takes the value 0 for all observations
train["Child"] = float(0)

# Assign 1 to passengers under 18
train.loc[train["Age"] < 18, "Child"] = 1

# Print normalized Survival Rates for passengers under 18
print("Survival rates for children:\n{}".format(train["Survived"][train["Child"] == 1].value_counts(normalize = True)))

# Print normalized Survival Rates for passengers 18 or older
print("\nSurvival rates for adults:\n{}".format(train["Survived"][train["Child"] == 0].value_counts(normalize = True)))


I am working on the file "Decision_Trees.ipynb".
The first task is:
Female vs Male
How many people in your training set survived the disaster with the Titanic? To see this, you can use the value_counts() method in combination with standard bracket notation to select a single column of a DataFrame:
    # absolute numbers
    train["Survived"].value_counts()
# percentages
train["Survived"].value_counts(normalize = True)

If you run these commands in the console, you'll see that 549 individuals died (62%) and 342 survived (38%). A simple way to predict heuristically could be: "majority wins". This would mean that you will predict every unseen observation to not survive.
To dive in a little deeper we can perform similar counts and percentage calculations on subsets of the Survived column. For example, maybe gender could play a role as well? You can explore this using the .value_counts() method for a two-way comparison on the number of males and females that survived, with this syntax:
train["Survived"][train["Sex"] == 'male'].value_counts()
train["Survived"][train["Sex"] == 'female'].value_counts()

To get proportions, you can again pass in the argument normalize = True to the .value_counts() method.
The results below show that 81% of the men died, but only 26% of the women died.  So gender matters tremendously.
Can you write Python code for it?

# Passengers that survived vs passengers that passed away
print(train["Survived"].value_counts())

# As proportions
print(train["Survived"].value_counts(normalize=True))

# Males that survived vs males that passed away
print(train["Survived"][train["Sex"] == 'male'].value_counts())

# Females that survived vs Females that passed away
print(train["Survived"][train["Sex"] == 'female'].value_counts())

# Normalized male survival
print("\nMale survival rates:\n{}".format(train["Survived"][train["Sex"] == 'male'].value_counts(normalize=True)))

# Normalized female survival
print("\nFemale survival rates:\n{}".format(train["Survived"][train["Sex"] == 'female'].value_counts(normalize=True)))


I am working on the file "Decision_Trees.ipynb".
The first task is:
How well did our attempts at preventing overfitting help?
This second solution achieves a higher score of 0.76555 on the test set, even though it had a lower score of 0.906 on the training set. So it predicts approximately 76.6% of the values in the test set correctly.
This is a little bit better than before we attempted to prevent overfitting.  But it is still only par with a pure gender-based model.  So there is still a lot of room for improvement.
How can we do better?  One way is to spend a little bit of time on feature engineering ...
Feature-engineering for our Titanic data set
Data Science is an art that benefits from a human element. Enter feature engineering: creatively engineering your own features by combining the different existing variables.
While feature engineering is a discipline in itself, too broad to be covered here in detail, you will have a look at a simple example by creating your own new predictive attribute: family_size.
A valid assumption is that larger families need more time to get together on a sinking ship, and hence have lower probability of surviving. Family size is determined by the variables SibSp and Parch, which indicate the number of family members a certain passenger is traveling with. So when doing feature engineering, you add a new variable family_size, which is the sum of SibSp and Parch plus one (the observation itself), to the test and train set.
Can you write Python code for it?

# Create train_two with the newly defined feature
train_two = train.copy()
train_two["family_size"] = train_two["SibSp"] +  train_two["Parch"] + 1

cols_three = ["Pclass", "Sex", "Age", "Fare", "SibSp", "Parch", "Embarked", "family_size", "Child"]
# Create a new feature set and add the new feature
features_three = train_two[cols_three].values

#Control overfitting by setting "max_depth" to 9 and "min_samples_split" to 6
max_depth = 9
min_samples_split = 6

# Define the tree classifier, then fit the model
my_tree_three = tree.DecisionTreeClassifier(max_depth = max_depth, min_samples_split = min_samples_split, random_state = 1)
my_tree_three.fit(features_three, target)

# Print the score of this decision tree
print(my_tree_three.score(features_three, target))

# Make your prediction using the test set
test_two = test.copy()

test_two["Child"] = float(0)
test_two.loc[test_two["Age"] < 18, "Child"] = 1

test_two["family_size"] = test_two["SibSp"] +  test_two["Parch"] + 1
test_features_three = test_two[cols_three].values
predition_three = my_tree_three.predict(test_features_three)
predition_three.shape

# Create a data frame with two columns: PassengerId & Survived. Survived contains your predictions
solution_three = pd.DataFrame(predition_three, PassengerId, columns = ["Survived"])

# Check that your data frame has 418 entries
print(solution_three.shape)

# Write your solution to a csv file with the name my_solution.csv
solution_three.to_csv(os.path.join(solution_dir, "decision_tree_three.csv"), index_label = ["PassengerId"])


I am working on the file "Decision_Trees.ipynb".
The first task is:
Intro to Decision Trees
In the previous sections, you did all the slicing and dicing yourself to find subsets that have a higher chance of surviving. A decision tree automates this process for you and outputs a classification model or classifier.
Conceptually, the decision tree algorithm starts with all the data at the root node and scans all the variables for the best one to split on. Once a variable is chosen, you do the split and go down one level (or one node) and repeat. The final nodes at the bottom of the decision tree are known as terminal nodes, and the majority vote of the observations in that node determine how to predict for new observations that end up in that terminal node.
First, let's import the necessary libraries ...
Can you write Python code for it?

# Import the Numpy library
import numpy as np

# Import 'tree' from scikit-learn library
from sklearn import tree


I am working on the file "Decision_Trees.ipynb".
The first task is:
Understanding Your Data
Before starting with the actual analysis, it's important to understand the structure of your data. Both test and train are DataFrame objects, the way pandas represent datasets. You can easily explore a DataFrame using the .describe() method. .describe() summarizes the columns/features of the DataFrame, including the count of observations, mean, max and so on. Another useful trick is to look at the dimensions of the DataFrame. This is done by requesting the .shape attribute of your DataFrame object. (ex. your_data.shape)
The training and test set are already available in the workspace, as train and test. Apply .describe() method and print the .shape attribute of the training set.
Can you write Python code for it?

# DataFrame.describe() generates various various summary statistics, automatically excluding NaN or missing values
train.describe()

# This means the training set has 891 observations with 12 variables each
train.shape


I am working on the file "Decoder.ipynb".
The first task is:
2B1Q
Warning! 2B1Q is currently almost completely broken. Pull requests with correct solutions are welcome :)
Can you write Python code for it?

ans3 = print_nums(parse_code("Line_Code_PNGs/2B1Q.png", code2B1Q))

res2d3 = ""
for i in range(0, len(ans3)):
    res2d3 += chr(ans3[i])

ans2d3 = []
for i in range(0, len(res2d3), 8):
    print(int('0b'+res2d3[i:i+8], 2))


I am working on the file "Decoder.ipynb".
The first task is:
Manchester Code
(a rather tricky example)
Here is a tricky example of Manchester code - where we have ASCII '0's and '1's with which a 3-letter "word" is encoded.
Can you write Python code for it?

ans1 = print_nums(parse_code("Line_Code_PNGs/Manchester.png", manchester))

res2d = ""
for i in range(0, len(ans1)):
    res2d += chr(ans1[i])

ans2d = []
for i in range(0, len(res2d), 8):
    print(int('0b'+res2d[i:i+8], 2))


I am working on the file "Decoder.ipynb".
The first task is:
Now the information transfer signal itself is ~"black", so we need to find the black colour range as well!
Can you write Python code for it?

# Let's do just that

black = colourlist([[0, 0, 0], [0, 1, 0], [7, 2, 8]])
# black.add_deviations(60) # experimentally it is somewhere around that
# experimentally the max deviation is somewhere around 60
print(black)


I am working on the file "Decoder.ipynb".
The first task is:
Outline
The outline of the idea is:

Find the red lines that represent parallel synchronization signal above
Calculate their size
"Synchromize with rows below" (according to the rules of the code)
...
PROFIT!

!!! Things to keep in mind:

deviations of red
deviations of black
noise - it might just break everything!
beginning and end of image...
...

A rather simple PNG we'll work with first:
Can you write Python code for it?

# Let us first define colour red
# We'll work with RGB for colours
# So for accepted variants we'll make a list of 3-lists.

class colourlist(list):
    """Just lists of 3-lists with some fancy methods to work with RGB colours
    """

    def add_deviations(self, d=8): # Magical numbers are so magical!
        """Adds deviations for RGB colours to a given list.
           Warning! Too huge - it takes forever.

           Input: list of 3-lists
           Output: None (side-effects - changes the list)
        """

        #l = l[:] Nah, let's make it a method
        l = self

        v = len(l)
        max_deviation = d

        for i in range(v): # Iterate through the list of colours

            for j in range(-max_deviation, max_deviation+1):
                # Actually it is the deviation.

                #for k in range(3): # RGB! (no "a"s here)

                newcolour = self[i][:] # Take one of the original colours
                newcolour[0] = abs(newcolour[0]+j) # Create a deviation
                l.append(newcolour)
                # Append new colour to the end of the list.
                # <- Here it is changed!
                for j in range(-max_deviation, max_deviation+1):
                    # Work with all the possibilities with this d
                    newcolour1 = newcolour[:]
                    newcolour1[1] = abs(newcolour1[1]+j)
                    l.append(newcolour1)
                    # Append new colour to the end of the list. Yeah!
                    # <- Here it is changed!

                    for j in range(-max_deviation, max_deviation+1):
                        # Work with all the possibilities with this d
                        newcolour2 = newcolour1[:]
                        newcolour2[2] = abs(newcolour2[2]+j)
                        l.append(newcolour2) # Append new colour to the end of the list. Yeah!
                        # <- Here it is changed!

       ...


I am working on the file "Decoder.ipynb".
The first task is:
The signal we are currently interested in is Manchester code (as per G.E. Thomas).
It is a self-clocking signal, but since we do have a clock with it - we use it)
Let us find the height of the Manchester signal in our PNG - just because...
Can you write Python code for it?

fb = find_first_pixel_of_colour(img, black)

def signal_height(pxls, fib):
    signal_height = 1
    # if ([img[fb[0]+1][fb[1]], img[fb[0]+1][fb[1]+1], img[fb[0]+1][fb[1]+2]] in black):
    if withinDeviation([pxls[fib[0]+1][fib[1]], pxls[fib[0]+1][fib[1]+1]
                        , pxls[fib[0]+1][fib[1]+2]], black, 60):
        signal_height += 1
        i = 2
        rows = len(pxls)
        # while([img[fb[0]+i][fb[1]], img[fb[0]+i][fb[1]+1], img[fb[0]+i][fb[1]+2]] in black):
        while(withinDeviation([pxls[fib[0]+i][fib[1]]
                               , pxls[fib[0]+i][fib[1]+1]
                               , pxls[fib[0]+i][fib[1]+2]], black, 60)):
            signal_height += 1
            i += 1
            if (i >= rows):
                break
    else:
        print("") # TO DO
    return signal_height

sheight = signal_height(img, fb)-1

print(sheight)

# Let's quickly find the last red line
...

def manchester(pixels, start, clock,
               line_colour, d=60, inv=False):
    """Decodes Manchester code (as per G. E. Thomas)
       (or with inv=True Manchester code
       (as per IEEE 802.4)).

       Input: array of int with len%3==0 (- PNG pixels),
             int, int, colourlist, int, bool (optional)
       Output: str (of '1' and '0') or None
    """

    res = ""

    cols = len(pixels[0])
    fb = find_first_pixel_of_colour(pixels, line_colour)
    m = 2*clock*3-2*3 # Here be dragons!
    # Hack: only check it using the upper line
    # (or lack thereof)

    if not(inv):
        for i in range(start, cols-2*3, m):
            fromUP = withinDeviation([pixels[fb[0]][i-6],
                              pixels[fb[0]][i-5],
                              pixels[fb[0]][i-4]],
                             line_colour, d)
            if fromUP:
                res = res + "1"
            else:
                res = res + "0"
    else:
        for i in range(start, cols-2*3, m):
            fromUP = withinDeviation([pixels[fb[0]][i-6],
                         ...


I am working on the file "Decoder.ipynb".
The first task is:
We found the sync (clock) line length in our graph!
Can you write Python code for it?


I am working on the file "DecomposingTestsOfMixtureModels.ipynb".
The first task is:
Corresponding to the two mixture models
Can you write Python code for it?

def F0(x):
    sum = 0.
    for w, f in zip(w0, fs):
        sum += w*f(x)
    return sum
def F1(x):
    sum = 0.
    for w, f in zip(w1, fs):
        sum+= w*f(x)
    return sum


I am working on the file "DecomposingTestsOfMixtureModels.ipynb".
The first task is:
Visualization of the models
Can you write Python code for it?

xarray = np.linspace(0,5,100)

plt.plot(xarray,f0(xarray), c='r', ls='--')
plt.plot(xarray,f1(xarray), c='y', ls='--')
plt.plot(xarray,f2(xarray), c='orange', ls='--')
plt.plot(xarray,F0(xarray), c='black', ls='-', lw=2)
plt.plot(xarray,F1(xarray), c='b', ls='-', lw=2)


I am working on the file "DecomposingTestsOfMixtureModels.ipynb".
The first task is:
Visualization of the target likleihood ratio
Can you write Python code for it?


I am working on the file "DecomposingTestsOfMixtureModels.ipynb".
The first task is:
with these mixture coefficients
Can you write Python code for it?


I am working on the file "Deconv/Deconvolution.ipynb".
The first task is:
$$argmin\frac{1}{2}(AX-B)^2+\lambda X \rightarrow X=(A^TA+\lambda I)^{-1}A^TB$$
$$A=\left[
 \begin{matrix}
   1/9 & 0 & 0 \
   ... & 1/9 & ... \
   0 & ... & 0
  \end{matrix}
  \right] $$
Can you write Python code for it?

import itertools
ta=[1,2,3]
tb=[4,5,6]
#tc=[(i,j) for i,j in zip(ta,tb)]
#print tc
#import itertools
#for i in itertools.product('ABCD', repeat = 2):
#    print i,

for i in itertools.product(range(1,4),range(4,7)):#dikaer product
    print(i,)
print(' ')
a=np.arange(10)
print(a)
a[ta]*=2
print(a)

from scipy.sparse import csc_matrix
from scipy.sparse import linalg as sppl
import numpy.linalg
test = csc_matrix((4,3))
test[0:2,0:2]=np.random.randn(test[0:2,0:2].shape[0],test[0:2,0:2].shape[1])
test[2:4,2:3]=np.random.randn(test[2:4,2:3].shape[0],test[2:4,2:3].shape[1])
print(test)
#testa = test.toarray()
#print testa
tt = csc_matrix(np.random.randn(3,4))
#tRes = np.mat(csc_matrix.dot(test,tt))
tRes = csc_matrix.dot(test,tt)
print(tRes)

A=csc_matrix(((img.shape[0]-3)*(img.shape[1]-3),img.shape[0]*img.shape[1]))
print(A.shape)
ind = -1
for j in range(0,np.int32(img.shape[1])-3):
    for i in range(0,np.int32(img.shape[0])-3):
        ind +=1
        #index=[]
        ravel = []
        for e in itertools.product(range(j,j+3),range(i,i+3)):
            #index.append(e)
            ravel.append(e[1]+e[0]*img.shape[0])
        A[ind,[ravel]]=1./9.
        #print ravel,A[ind,:]

from scipy import sparse
reg = 0.001
X = A.T.dot(A)
X=X+reg*sparse.eye(X.shape[0])
print(X.shape)
Xinv = sp.sparse.linalg.inv(X)
print(Xinv.shape)

X=csc_matrix.dot(csc_matrix(Xinv), A.T)
padBlur = csc_matrix(blurImg[0:np.int32(img.shape[0])-3,0:np.int32(img.shape[1])-3].ravel()).T
print(X.shape,padBlur.shape)
X=csc_matrix.dot(X,padBlur)
print(X.shape)

res=X.todense()
res = res.reshape(blurImg.shape)
fig = plt.figure()
ax = fig.add_subplot(131)
ax.imshow(img,cmap='gray')
ax = fig.add_subplot(132)
ax.imshow(blurImg,cmap='gray')
ax = fig.add_subplot(133)
ax.imshow(res,cmap='gray')
plt.show()

from scipy.sparse import csc_matrix,lil_matrix
from scipy.sparse import linalg as sppl
import numpy.linalg
from scipy import sparse
import cv2
import sys,time
from __future__ import division
from scipy import signal#warning
dia = 5
blurKernel =...


I am working on the file "Deconv/Deconvolution.ipynb".
The first task is:
$X_{k+1}=X_{k}-\mu[(A^TA+\lambda I)X_k-A^TB]$
Can you write Python code for it?

lr=0.7
reg = 0.01
rad = np.int32((dia-1) / 2)
print(rad,np.int32(img2.shape[0]-rad))
padBlur2 = csc_matrix(blurImg2[rad:np.int32(img2.shape[0])-rad-1,rad:np.int32(img2.shape[1])-rad-1].ravel()).T
print(A2.shape,padBlur2.shape,blurImg2.shape)
Xi = np.mat(np.random.randn(img2.shape[0],img2.shape[1]).ravel()).T
print(Xi.shape)
epoch = 200
oldloss = 100000000
newloss = 0
for i in range(0,epoch):
    delta = A2.T.dot(A2)
    delta=delta+reg*sparse.eye(delta.shape[0])
    delta=csc_matrix.dot(csc_matrix(delta), csc_matrix(Xi))
    delta-=A2.T.dot(padBlur2)
    delta *= lr
    Xi -= delta
    if (i > 0 and np.mod(i,25) == 0) or i==epoch - 1:
        newloss = np.sum(np.abs(Xi.reshape(img2.shape)-img2))
#         ratio = (oldloss - newloss)/newloss
#         if ratio < 0.:
#             lr *= 0.5
#         elif ratio < 0.2:
#             lr *= 1.3
#         elif ratio < 0.3:
#             lr *= 1.5
#         oldloss = newloss
        print(i,lr,newloss)
print('optimization complete')

res=Xi#.todense()
print(Xi.shape,img2.shape)
res = res.reshape(img2.shape)
# fig = plt.figure()
# plt.imshow(img2,cmap='gray')
# fig = plt.figure()
# plt.imshow(blurImg2,cmap='gray')
# fig = plt.figure()
# plt.imshow(res,cmap='gray')
fig = plt.figure()
ax = fig.add_subplot(131)
ax.imshow(img2,cmap='gray')
ax = fig.add_subplot(132)
ax.imshow(blurImg2,cmap='gray')
ax = fig.add_subplot(133)
ax.imshow(res,cmap='gray')
plt.show()

oImg = np.random.random(blurImg.shape)+10
for i in range(200):
    oImg = oImg - 1.0*( 0.01*oImg+signal.convolve2d( -blurImg + signal.convolve2d( oImg, blurKernel, 'same','symm'), blurKernel, 'same','symm'))

plt.imshow(blurImg,cmap='gray')
fig = plt.figure()
plt.imshow(oImg,cmap='gray')
fig = plt.figure()
plt.imshow(img,cmap='gray')
#fig = plt.figure()
#ax = fig.add_subplot(111)
#ax.imshow(img,cmap='gray')
#ax = fig.add_subplot(112)
#ax.imshow(blurImg,cmap='gray')
#ax = fig.add_subplot(113)
#ax.imshow(oImg,cmap='gray')
#fig.show()


I am working on the file "Deconvolution/convolution1.ipynb".
The first task is:
Setup a convolution data set and try and doconvolve it
Can you write Python code for it?

np.random.seed(8675309)

dat_len = 100
xval = np.arange(dat_len)
realdat = np.zeros(dat_len, dtype=int)
realdat[40:60] = 50
noisemean = 2
real_n = np.zeros_like(realdat)
for i in range(len(realdat)):
    real_n[i] = np.random.poisson(realdat[i]+noisemean)

# make a detector
# triangular with FWFM 5 and is square
det = np.array([1,1,1,1,1])

# the numpy convolve I don't understand the normalization
obs = np.convolve(real_n, det, mode='same')
obs = tb.normalize(obs)
obs *= real_n.max()

# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(4, sharex=True)
axarr[0].plot(xval, realdat)
axarr[0].set_ylabel('Truth')
axarr[0].set_ylim((0,60))

axarr[1].plot(xval, real_n)
axarr[1].set_ylabel('T+N')

axarr[2].plot(np.arange(len(det)), det)
axarr[2].set_ylabel('Det')

axarr[3].plot(xval, obs)
axarr[3].set_ylabel('Obs')


I am working on the file "Deconvolution/convolution1.ipynb".
The first task is:
So the det provides a point spread function that is U(0,5)
Can you write Python code for it?

# generate some data
with pm.Model() as model:
    truth_mc = pm.Uniform('truth', 0, 100, shape=dat_len)
    noisemean_mc = pm.Uniform('noisemean', 0, 100)
    noise_mc = pm.Poisson('noise', noisemean_mc, observed=obs[1:20])
    real_n_mc = pm.Poisson('real_n', truth_mc+noisemean_mc, shape=dat_len)
    psf = pm.Uniform('psf', 0, 5, observed=det)
    obs_mc = pm.Normal('obs', (truth_mc+noisemean_mc)*psf.max(), 1/5**2, observed=obs, shape=dat_len)

    trace = pm.sample(5000)

pm.traceplot(trace)

pm.summary(trace)

# plt.plot(trace['truth'][0:5,:].T)
trace['truth'].shape
iqr = np.zeros((dat_len,3))
for i in range(dat_len):
    iqr[i] = np.percentile(trace['truth'].T[i], (25,50,75), axis=0)
plt.plot(xval, iqr[:,1],  label='recovered')
plt.fill_between(xval, iqr[:,0], iqr[:,2], alpha=0.2)
plt.plot(xval, real_n, c='r', label='pre psf')
plt.plot(xval, realdat, c='g',  label='truth')
plt.plot(xval, obs, c='k',  label='observed', lw=3)

plt.legend()
plt.figure()
snr = iqr[:,1]/(iqr[:,2], iqr[:,0])
perixval.shape, snr.shape
plt.plot(xval, snr)

print(np.percentile(trace['noisemean'], (25,50,75)), noisemean)

obs[1:20]


I am working on the file "Deconvolution/convolution2.ipynb".
The first task is:
Setup a simulated data to see if we can make it look right
Can you write Python code for it?

np.random.seed(8675309)

sim_pa = np.arange(20,175)
sim_c = 890*np.sin(np.deg2rad(sim_pa))**0.8

# at each point draw a poisson variable with that mean

sim_c_n = np.asarray([np.random.poisson(v) for v in sim_c ])

prob=0.1
sim_c_n2 = np.asarray([np.random.negative_binomial((v*prob)/(1-prob), prob) for v in sim_c ])

# Two subplots, the axes array is 1-d
f, axarr = plt.subplots(3, sharex=True, sharey=True, figsize=(7,9))
axarr[0].plot(sim_pa, sim_c, lw=2)
axarr[0].set_ylabel('Truth')
axarr[0].set_xlim((0,180))
axarr[0].set_yscale('log')

axarr[1].plot(sim_pa, sim_c_n, lw=2)
axarr[1].set_ylabel('Measured\nPoisson')
axarr[1].set_xlim((0,180))
axarr[1].set_yscale('log')

axarr[2].plot(sim_pa, sim_c_n2, lw=2)
axarr[2].set_ylabel('Measured\nNegBin')
axarr[2].set_xlim((0,180))
axarr[2].set_yscale('log')
axarr[2].set_ylim(axarr[1].get_ylim())


I am working on the file "Deconvolution/convolution2.ipynb".
The first task is:
This will then serve as the background
Can the simplest model get started?
Can you write Python code for it?

# generate some data
with pm.Model() as model:
    bkg = pm.NegativeBinomial('bkg', mu=pm.Uniform('m_bkg', 0, 1e5,shape=len(sim_pa), testval=1e3),
                              alpha=0.1,
                              observed=sim_c_n2, shape=len(sim_pa))

#     truth_mc = pm.Uniform('truth', 0, 100, shape=dat_len)
#     noisemean_mc = pm.Uniform('noisemean', 0, 100)
#     noise_mc = pm.Poisson('noise', noisemean_mc, observed=obs[1:20])
#     real_n_mc = pm.Poisson('real_n', truth_mc+noisemean_mc, shape=dat_len)
#     psf = pm.Uniform('psf', 0, 5, observed=det)
#     obs_mc = pm.Normal('obs', (truth_mc+noisemean_mc)*psf.max(), 1/5**2, observed=obs, shape=dat_len)

    trace = pm.sample(50000)

pm.traceplot(trace)

pm.summary(trace)


I am working on the file "Decorators.ipynb".
The first task is:
Chaining Decorators in Python
Multiple decorators can be chained in python.
Can you write Python code for it?

def star(func):
    def inner(*args, **kwargs):
        print('*'* 30)
        func(*args, **kwargs)
        print('*'* 30)
    return inner

def percent(func):
    def inner(*args, **kwargs):
        print('%'*30)
        func(*args,**kwargs)
        print('%'*30)
    return inner

@star
@percent
def printer(msg):
    print(msg)

printer("Hello")


I am working on the file "Decorators.ipynb".
The first task is:
Decorating Functions with Parameters
The above decorator was simple and it only worked with functions that did not have any parameters. What if we had functions that took in parameters like below?
python
def divide(a,b):
    return a/b
The function has 2 parameters a and b. We know, it will give error if we pass in b as 0.
Can you write Python code for it?

def divide(a,b):
    return a/b

divide(2,5)
divide(2,0)


I am working on the file "Decorators.ipynb".
The first task is:
Functions as Parameters
Functions can be passed as arguments to another function. Such functions that take other functions as arguments are called Higher Order Functions.
Can you write Python code for it?

def inc(x):
    return x + 1
def dec(x):
    return x - 1
def operate(func,x):
    result = func(x)
    return result

operate(inc,1)

operate(dec,2)


I am working on the file "Decorators.ipynb".
The first task is:
Functions within functions
Can you write Python code for it?

def f():

    def g():
        print("Hi, it's me 'g'")
        print("Thanks for calling me")

    print("This is the function 'f'")
    print("I am calling 'g' now:")
    g()

f()

def temperature(t):
    def celsiusToFarhenheit(x):
        return 9 * x / 5 + 32
    result = "It's " + str(celsiusToFarhenheit(t)) + " degrees"
    return result

temperature(10)


I am working on the file "Decorators.ipynb".
The first task is:
If you observered closely the number of parameters of the nested inner() function inside the decorator is same as the parameters of functions it decorates. Taking this into account, now we can make general decorators that work with any number of parameter.
This magic is done as function(*args, **kwargs). In this way, args will be the tuple of positional arguments and kwargs will be the dictionary of keyword arguments. An example of such decorator will be.
Can you write Python code for it?

def works_for_all(func):
    def inner(*args, **kwargs):
        print('I can decorate any function...')
        return func(*args, **kwargs)
    return inner


I am working on the file "Decorators.ipynb".
The first task is:
Please note the order in which we chain the decorators also matter.
Can you write Python code for it?

@percent
@star
def printer(msg):
    print(msg)

printer('Hello')


I am working on the file "Decorators.ipynb".
The first task is:
This is equivalent to:
Can you write Python code for it?

def printer(msg):
    print(msg)

printer = star(percent(printer))
printer('Hello')


I am working on the file "Decorators.ipynb".
The first task is:
To avoid this error lets make a decorator to check for this case.
Can you write Python code for it?

def smart_divide(func):
    def inner(a,b):
        if b == 0:
            print('Cannot divide by zero')
            return
        return func(a,b)
    return inner

@smart_divide
def divide(a,b):
    return a / b

divide(5,2)

divide(5,0)


I am working on the file "Decorators/Decorators.ipynb".
The first task is:
Multiple Decorators
A function can have multiple decorators, and thus be altered quite a few times. Let's try an example where be implement bold, italics, and underline tags (that don't actually work), but look like they would have.
Can you write Python code for it?

def bold_decorator(func):
    def func_wrapper(some_text):
        return "[b]{}[/b]".format(func(some_text))
    return func_wrapper

def italic_decorator(func):
    def func_wrapper(some_text):
        return "[i]{}[/i]".format(func(some_text))
    return func_wrapper

def underline_decorator(func):
    def func_wrapper(some_text):
        return "[u]{}[/u]".format(func(some_text))
    return func_wrapper

@bold_decorator
@italic_decorator
@underline_decorator
def text(some_text):
    return some_text

print(text("Modify me!"))


I am working on the file "DecoratorsTalk.ipynb".
The first task is:
... not done yet.
Can you write Python code for it?

import functools

def better_float_wrapper(func):
    @functools.wraps(func)       # this line is the only difference
    def wrapper(num):
        return func(float(num))
    return wrapper

@better_float_wrapper
def halver3(num):
    """Returns half of the 'num' argument.
    This is a re-reimplementation of halve()."""
    return num / 2

print halver3(5)
print "halver3's name:", halver3.__name__
print "halver3's docstring:", halver3.__doc__

halver3?


I am working on the file "DecoratorsTalk.ipynb".
The first task is:
Dust off your hands and kick back.  We're completely, totally...
Can you write Python code for it?

print "halver's name:", halver.__name__
print "halver's docstring:", halver.__doc__

print "halver2's name:", halver2.__name__
print "halver2's docstring:", halver2.__doc__

halver?


I am working on the file "DecoratorsTalk.ipynb".
The first task is:
Uh-oh...
Can you write Python code for it?

print halver(5)

print [i/2 for i in range(10)]

print [i/2.0 for i in range(10)]

print [float(i)/2 for i in range(10)]


I am working on the file "DecoratorsTalk.ipynb".
The first task is:
Usage of @property
Can you write Python code for it?

class StrictAttributeHolder(object):
    def __init__(self):
        self._int_val = None

    @property
    def int_val(self):
        if self._int_val is not None:
            return self._int_val
        else:
            raise Exception("Can't read what isn't written!")

    @int_val.setter
    def int_val(self, value):
        if isinstance(value, int):
            self._int_val = value
        else:
            raise TypeError("Can't set int_val to a non-int value!")

sah = StrictAttributeHolder()

print sah.int_val

sah.int_val = 5

print sah.int_val

sah.int_val = 5.0

sah.int_val = [5]


I am working on the file "DecoratorsTalk.ipynb".
The first task is:
and then...
Avoid exceptions when running the three cells below.  If you run into problems, try adjusting your decorators above.
Can you write Python code for it?

# Execute this cell without modifying it.
picky_eater_food = "You can now write your own decorators!".split(' ')

@secured_function
@timed_function
@case_mod
def picky_eater(food):
    if food.islower():
        time.sleep(0.1 * len(food))
        return food
    else:
        raise Exception("I don't wanna eat this!")

# Change ONLY the assigned value of PASSWORD in this cell, then
# execute it.
global PASSWORD
PASSWORD = ''

# Run this cell as-is without any exceptions cropping up and with
# an execution time printed out for each morsel in picky_eater_food.
for morsel in picky_eater_food:
    print picky_eater(morsel)


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

@float_wrapper
def halver2(num):
    """Returns half of the 'num' argument.
    This is a re-reimplementation of halver()."""
    return num / 2

print halver2(5)


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

def halver(num):
    """Returns half of the 'num' argument."""  # docstring
    return num / 2


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
Go back to a setup where you are using the salary to predict the bonus, and rerun the code to remind yourself of what the data look like. You might notice a few data points that fall outside the main trend, someone who gets a high salary (over a million dollars!) but a relatively small bonus. This is an example of an outlier, and we’ll spend lots of time on them in the next lesson.
A point like this can have a big effect on a regression: if it falls in the training set, it can have a significant effect on the slope/intercept if it falls in the test set, it can make the score much lower than it would otherwise be as things stand right now, this point falls into the test set (and probably hurting the score on our test data as a result). Let’s add a little hack to see what happens if it falls in the training set instead. Add these two lines near the bottom right before plt.xlabel(features_list[1]):
python
reg.fit(feature_test, target_test)
plt.plot(feature_train, reg.predict(feature_train), color="b")
Now we’ll be drawing two regression lines, one fit on the test data (with outlier) and one fit on the training data (no outlier). Look at the plot now--big difference, huh? That single outlier is driving most of the difference. What’s the slope of the new regression line?
(That’s a big difference, and it’s mostly driven by the outliers.)
Can you write Python code for it?

import sys
import pickle
sys.path.append("../tools/")
from feature_format import featureFormat, targetFeatureSplit
dictionary = pickle.load( open("../final_project/final_project_dataset_modified.pkl", "r") )

### list the features you want to look at--first item in the
### list will be the "target" feature

print "\n ********Regression on salary to predict exercised_stock_options, cleaned for Outliers ********\n"

features_list = ["salary", "exercised_stock_options"]
data = featureFormat( dictionary, features_list, remove_any_zeroes=True)
target, features = targetFeatureSplit( data )

### training-testing split needed in regression, just like classification
from sklearn.cross_validation import train_test_split
feature_train, feature_test, target_train, target_test = train_test_split(
    features, target, test_size=0.5, random_state=42)
train_color = "b"
test_color = "r"

### Your regression goes here!
### Please name it reg, so that the plotting code below picks it up and
### plots it correctly. Don't forget to change the test_color above from "b" to
### "r" to differentiate training points from test points.

from sklearn import linear_model
reg = linear_model.LinearRegression()
reg.fit(feature_train, target_train)

### draw the scatterplot, with color-coded training and testing points
import matplotlib.pyplot as plt
for feature, target in zip(feature_test, target_test):
    plt.scatter( feature, target, color=test_color )
for feature, target in zip(feature_train, target_train):
    plt.scatter( feature, target, color=train_color )

### labels for the legend
plt.scatter(feature_test[0], target_test[0], color=test_color, label="test")
plt.scatter(feature_test[0], target_test[0], color=train_color, label="train")

### draw the regression line, once it's coded
try:
    plt.plot( feature_test, reg.predict(feature_test) )
except NameError:
    pass

reg.fit(feature_test, target_test)
plt.plot(feature_train, reg.predict(feature_train), color="b")...


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
How many POI’s were there total?
We compiled a list of all POI names (in ../final_project/poi_names.txt) and associated email addresses (in ../final_project/poi_email_addresses.py).
How many POI’s were there total? (Use the names file, not the email addresses, since many folks have more than one address and a few didn’t work for Enron, so we don’t have their emails.)
Can you write Python code for it?

import re
poiCount = 0

f = open('../final_project/poi_names.txt', 'r')
try:
    for line in f:
        m = re.search('^\(y\)', line )
        if m:
            print line
            poiCount += 1

finally:
    f.close()

print "There are %d POIs in the dataset" % poiCount


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
How many email messages do we have from Wesley Colwell to persons of interest?
Can you write Python code for it?

eD.enron_data['COLWELL WESLEY']['from_this_person_to_poi']

eD.enron_data['SKILLING JEFFREY K']['exercised_stock_options']


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
How many people in the E+F dataset (as it currently exists) have “NaN” for their total payments? What percentage of people in the dataset as a whole is this?
Can you write Python code for it?

pCount = 0
for person in eD.enron_data:
    if eD.enron_data[person]['total_payments'] == "NaN":
        pCount += 1
        print person, eD.enron_data[person]['total_payments']

print "There are %d persons in have “NaN” for their total payments" % pCount

import pandas as pd
import random
import csv

csvfile = 'adult.data'
df = pd.read_csv(csvfile, header=None,skipinitialspace = True)
columns = ['age','workclass','fnlwgt','education','education_num','marital_status',
           'occupation','relationship','race','sex','capital_gain','capital_loss',
           'hours_per_week','native_country','salary']
df.columns = columns

# generate data for regression with line equation
# y = (6.25)x + 0

def makeSalary():
    m = 6.25
    return (m + random.random()) * df['age']

X = df['age']
y = df['salary'].replace(['<=50K','>50K'],[makeSalary(),makeSalary()])

from sklearn import cross_validation

ages_train, ages_test, net_worths_train, net_worths_test = cross_validation.train_test_split(X, y, test_size=0.3)

ages_test = ages_test.reshape(-1, 1)
net_worths_test = net_worths_test.reshape(-1, 1)
ages_train =  ages_train.reshape(-1, 1)
net_worths_train = net_worths_train.reshape(-1, 1)

import numpy
import matplotlib

import matplotlib.pyplot as plt
from class_vis import prettyPicture, output_image

%matplotlib inline

def studentReg(ages_train, net_worths_train):
    from sklearn import linear_model
    reg = linear_model.LinearRegression()

    # Reshape data using X.reshape(-1, 1) since data has a single feature or X.
    return reg.fit(ages_train.reshape(-1, 1), net_worths_train.reshape(-1, 1))

reg = studentReg(ages_train, net_worths_train)
print "Omoju's net worth prediction $%.2fk" % reg.predict([[36]])
print "slope of regression is %.2f" % reg.coef_
print "intercepts of regression is %.2f" % reg.intercept_

plt.clf()
plt.scatter(ages_train, net_worths_train, color="b", label="train data")
plt.scatter(ages_test, net_worths_test, color="r", label="test data")
plt.plot(ages_test,...


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
Of these three individuals (Lay, Skilling and Fastow), who took home the most money (largest value of “total_payments” feature)?
How much money did that person get?
Can you write Python code for it?

print eD.enron_data['SKILLING JEFFREY K']['total_payments']
print eD.enron_data['LAY KENNETH L']['total_payments']
print eD.enron_data['FASTOW ANDREW S']['total_payments']

pCount = 0
for person in eD.enron_data:
    if eD.enron_data[person]['salary'] != "NaN":
        pCount += 1
        #print person, eD.enron_data[person]['salary']

print "There are %d persons in the dataset with a quantified salary" % pCount

pCount = 0
for person in eD.enron_data:
    if eD.enron_data[person]['email_address'] != "NaN":
        pCount += 1
        #print person, eD.enron_data[person]['email_address']

print "There are %d persons in the dataset with a known email address" % pCount


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
The “poi” feature records whether the person is a person of interest, according to our definition.
How many POIs are there in the E+F dataset?
Can you write Python code for it?

eD.enron_data['METTS MARK']['poi']

poiCount = 0
for person in eD.enron_data:
    if eD.enron_data[person]['poi']:
        poiCount += 1
        print person

print "There are %d POIs in the dataset" % poiCount


I am working on the file "Deep Dive into Enron Dataset.ipynb".
The first task is:
What is the total value of the stock belonging to James Prentice?
Can you write Python code for it?


I am working on the file "Deep Features for Image Retrieval.ipynb".
The first task is:
Just for fun, let's create a lambda to find and show nearest neighbor images
Can you write Python code for it?

show_neighbors = lambda i: get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()

show_neighbors(8)

show_neighbors(26)

image_train['label'].sketch_summary()

show_neighbors(0)

cat_images = image_train[image_train['label']=='cat']

dog_images = image_train[image_train['label']=='dog']

car_images = image_train[image_train['label']=='automobile']

bird_images = image_train[image_train['label']=='bird']

bird_images.head()

cat_model = graphlab.nearest_neighbors.create(cat_images,features=['deep_features'], label='id')

dog_model = graphlab.nearest_neighbors.create(dog_images,features=['deep_features'], label='id')

car_model = graphlab.nearest_neighbors.create(car_images,features=['deep_features'], label='id')

bird_model = graphlab.nearest_neighbors.create(bird_images,features=['deep_features'], label='id')

image_test = graphlab.SFrame('image_test_data/')

cat_model.query(image_test[0:1])[0:5]['distance'].mean()

dog_model.query(image_test[0:1])[0:5]['distance'].mean()

dog_images.filter_by(16976, 'id').show()

dog_test_images = image_test.filter_by('dog', 'label')

dog_cat = cat_model.query(dog_test_images, k=1)

dog_dog = dog_model.query(dog_test_images, k=1)

dog_car = car_model.query(dog_test_images, k=1)

dog_bird = bird_model.query(dog_test_images, k=1)

dog_distances = graphlab.SFrame({'dog':dog_dog['distance'], 'cat':dog_cat['distance'], 'car':dog_car['distance'], 'bird':dog_bird['distance']})

dog_distances.head()

dog_distances.apply(lambda row: row['dog'] == min(row.values())).sum()

min(dog_distances[0].values())


I am working on the file "Deep Features for Image Retrieval.ipynb".
The first task is:
Load the CIFAR-10 dataset
We will use a popular benchmark dataset in computer vision called CIFAR-10.
(We've reduced the data to just 4 categories = {'cat','bird','automobile','dog'}.)
This dataset is already split into a training set and test set. In this simple retrieval example, there is no notion of "testing", so we will only use the training data.
Can you write Python code for it?


I am working on the file "Deep Features for Image Retrieval.ipynb".
The first task is:
Train a nearest-neighbors model for retrieving images using deep features
We will now build a simple image retrieval system that finds the nearest neighbors for any image.
Can you write Python code for it?

knn_model = graphlab.nearest_neighbors.create(image_train,features=['deep_features'],
                                             label='id')


I am working on the file "Deep Features for Image Retrieval.ipynb".
The first task is:
Use image retrieval model with deep features to find similar images
Let's find similar images to this cat picture.
Can you write Python code for it?

graphlab.canvas.set_target('ipynb')
cat = image_train[18:19]
cat['image'].show()

knn_model.query(cat)


I am working on the file "Deep Features for Image Retrieval.ipynb".
The first task is:
We are going to create a simple function to view the nearest neighbors to save typing:
Can you write Python code for it?

def get_images_from_ids(query_result):
    return image_train.filter_by(query_result['reference_label'],'id')

cat_neighbors = get_images_from_ids(knn_model.query(cat))

cat_neighbors['image'].show()


I am working on the file "Deep Learning Nanodegree/Project_1/dlnd-your-first-neural-network.ipynb".
The first task is:
Checking out the data
This dataset has the number of riders for each hour of each day from January 1 2011 to December 31 2012. The number of riders is split between casual and registered, summed up in the cnt column. You can see the first few rows of the data above.
Below is a plot showing the number of bike riders over the first 10 days in the data set. You can see the hourly rentals here. This data is pretty complicated! The weekends have lower over all ridership and there are spikes when people are biking to and from work during the week. Looking at the data above, we also have information about temperature, humidity, and windspeed, all of these likely affecting the number of riders. You'll be trying to capture all this with your model.
Can you write Python code for it?


I am working on the file "Deep Learning Nanodegree/Project_1/dlnd-your-first-neural-network.ipynb".
The first task is:
Scaling target variables
To make training the network easier, we'll standardize each of the continuous variables. That is, we'll shift and scale the variables such that they have zero mean and a standard deviation of 1.
The scaling factors are saved so we can go backwards when we use the network for predictions.
Can you write Python code for it?

quant_features = ['casual', 'registered', 'cnt', 'temp', 'hum', 'windspeed']
# Store scalings in a dictionary so we can convert back later
scaled_features = {}
for each in quant_features:
    mean, std = data[each].mean(), data[each].std()
    scaled_features[each] = [mean, std]
    data.loc[:, each] = (data[each] - mean)/std


I am working on the file "Deep Learning Nanodegree/Project_1/dlnd-your-first-neural-network.ipynb".
The first task is:
Thinking about your results
Answer these questions about your results. How well does the model predict the data? Where does it fail? Why does it fail where it does?

Note: You can edit the text in this cell by double clicking on it. When you want to render the text, press control + enter

Your answer below
The model predicts well till Dec 21 and after that it is overfitting (Dec 22-Dec 31).
Unit tests
Run these unit tests to check the correctness of your network implementation. These tests must all be successful to pass the project.
Can you write Python code for it?

import unittest

inputs = [0.5, -0.2, 0.1]
targets = [0.4]
test_w_i_h = np.array([[0.1, 0.4, -0.3],
                       [-0.2, 0.5, 0.2]])
test_w_h_o = np.array([[0.3, -0.1]])

class TestMethods(unittest.TestCase):

    ##########
    # Unit tests for data loading
    ##########

    def test_data_path(self):
        # Test that file path to dataset has been unaltered
        self.assertTrue(data_path.lower() == 'bike-sharing-dataset/hour.csv')

    def test_data_loaded(self):
        # Test that data frame loaded
        self.assertTrue(isinstance(rides, pd.DataFrame))

    ##########
    # Unit tests for network functionality
    ##########

    def test_activation(self):
        network = NeuralNetwork(3, 2, 1, 0.5)
        # Test that the activation function is a sigmoid
        self.assertTrue(np.all(network.activation_function(0.5) == 1/(1+np.exp(-0.5))))

    def test_train(self):
        # Test that weights are updated correctly on training
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()

        network.train(inputs, targets)
        self.assertTrue(np.allclose(network.weights_hidden_to_output,
                                    np.array([[ 0.37275328, -0.03172939]])))
        self.assertTrue(np.allclose(network.weights_input_to_hidden,
                                    np.array([[ 0.10562014,  0.39775194, -0.29887597],
                                              [-0.20185996,  0.50074398,  0.19962801]])))

    def test_run(self):
        # Test correctness of run method
        network = NeuralNetwork(3, 2, 1, 0.5)
        network.weights_input_to_hidden = test_w_i_h.copy()
        network.weights_hidden_to_output = test_w_h_o.copy()

        self.assertTrue(np.allclose(network.run(inputs), 0.09998924))

suite = unittest.TestLoader().loadTestsFromModule(TestMethods())
unittest.TextTestRunner().run(suite)


I am working on the file "Deep Learning Nanodegree/Project_1/dlnd-your-first-neural-network.ipynb".
The first task is:
Time to build the network
Below you'll build your network. We've built out the structure and the backwards pass. You'll implement the forward pass through the network. You'll also set the hyperparameters: the learning rate, the number of hidden units, and the number of training passes.
The network has two layers, a hidden layer and an output layer. The hidden layer will use the sigmoid function for activations. The output layer has only one node and is used for the regression, the output of the node is the same as the input of the node. That is, the activation function is $f(x)=x$. A function that takes the input signal and generates an output signal, but takes into account the threshold, is called an activation function. We work through each layer of our network calculating the outputs for each neuron. All of the outputs from one layer become inputs to the neurons on the next layer. This process is called forward propagation.
We use the weights to propagate signals forward from the input to the output layers in a neural network. We use the weights to also propagate error backwards from the output back into the network to update our weights. This is called backpropagation.

Hint: You'll need the derivative of the output activation function ($f(x) = x$) for the backpropagation implementation. If you aren't familiar with calculus, this function is equivalent to the equation $y = x$. What is the slope of that equation? That is the derivative of $f(x)$.

Below, you have these tasks:
1. Implement the sigmoid function to use as the activation function. Set self.activation_function in __init__ to your sigmoid function.
2. Implement the forward pass in the train method.
3. Implement the backpropagation algorithm in the train method, including calculating the output error.
4. Implement the forward pass in the run method.
Can you write Python code for it?

class NeuralNetwork(object):

    def __init__(self, input_nodes, hidden_nodes, output_nodes, learning_rate):
        # Set number of nodes in input, hidden and output layers.
        self.input_nodes = input_nodes
        self.hidden_nodes = hidden_nodes
        self.output_nodes = output_nodes

        # Initialize weights
        self.weights_input_to_hidden = np.random.normal(0.0, self.hidden_nodes**-0.5,
                                       (self.hidden_nodes, self.input_nodes))

        self.weights_hidden_to_output = np.random.normal(0.0, self.output_nodes**-0.5,
                                       (self.output_nodes, self.hidden_nodes))
        self.lr = learning_rate

        #### Set this to your implemented sigmoid function ####
        # Activation function is the sigmoid function
        self.activation_function = lambda x: 1 / (1 + np.exp(-x))

    def train(self, inputs_list, targets_list):
        # Convert inputs list to 2d array
        inputs = np.array(inputs_list, ndmin=2).T
        targets = np.array(targets_list, ndmin=2).T

        #### Implement the forward pass here ####
        ### Forward pass ###
        # TODO: Hidden layer
        hidden_inputs = np.dot(self.weights_input_to_hidden, inputs)
        hidden_outputs = self.activation_function(hidden_inputs)

        # TODO: Output layer
        final_inputs = np.dot(self.weights_hidden_to_output,hidden_outputs)
        final_outputs = final_inputs

        #### Implement the backward pass here ####
        ### Backward pass ###

        # TODO: Output error
        #output_errors = # Output layer error is the difference between desired target and actual output.
        output_errors = targets_list-final_outputs

        # TODO: Backpropagated error
        hidden_errors = np.dot( self.weights_hidden_to_output.T, output_errors.T)
        hidden_grad = hidden_errors * (hidden_outputs * (1 - hidden_outputs))

        # TODO: Update the weights
       ...


I am working on the file "Deep Learning Nanodegree/Project_1/dlnd-your-first-neural-network.ipynb".
The first task is:
We'll split the data into two sets, one for training and one for validating as the network is being trained. Since this is time series data, we'll train on historical data, then try to predict on future data (the validation set).
Can you write Python code for it?

# Hold out the last 60 days of the remaining data as a validation set
train_features, train_targets = features[:-60*24], targets[:-60*24]
val_features, val_targets = features[-60*24:], targets[-60*24:]


I am working on the file "Deep Learning.ai/Numpy - Understanding Backprop.ipynb".
The first task is:
Compute the Class Scores
Can you write Python code for it?

# compute class scores for a linear classif
scores = np.dot(X,W) + b #scores will be 300 *3 and remember numpy broadcasting for b


I am working on the file "Deep Learning.ai/Numpy - Understanding Backprop.ipynb".
The first task is:
Compute the loss
Can you write Python code for it?

# Using cross - entropy loss
# Softmax loss = data loss + regularization loss

# Study about the Softmax loss and how it works before seeing how it works
#in this case it could be intuitive
num_examples = X.shape[0]
# get unnormalized probabilities
exp_scores = np.exp(scores)
# normalize them for each example
'''We now have an array probs of size [300 x 3], where each row now contains the class
probabilities. In particular, since we’ve normalized them every row now sums to one. We
can now query for the log probabilities assigned to the correct classes in each example:'''
probs = exp_scores / np.sum(exp_scores, axis=1 , keepdims=True)
# Calculating the correct_log probability
'''The array correct_logprobs is a 1D array of just the probabilities assigned to the
correct classes for each example. The full loss is then the average of these log
probabilities and the regularization loss:'''
correct_logprobs = -np.log(probs[range(num_examples),y])

# compute ths loss : average cross-entropy loss and regularization
data_loss = np.sum(correct_logprobs)/ num_examples
reg_loss = 0.5*reg*np.sum(W*W)

'''∂Li∂fk=pk−1(yi=k)

Notice how elegant and simple this expression is. Suppose the probabilities we
computed were p = [0.2, 0.3, 0.5], and that the correct class was the middle
one (with probability 0.3). According to this derivation the gradient on the
scores would be df = [0.2, -0.7, 0.5]. Recalling what the interpretation of the
gradient, we see that this result is highly intuitive: increasing the first or
last element of the score vector f (the scores of the incorrect classes) leads to
an increased loss (due to the positive signs +0.2 and +0.5) - and increasing the
loss is bad, as expected. However, increasing the score of the correct class has
negative influence on the loss. The gradient of -0.7 is telling us that increasing
the correct class score would lead to a decrease of the loss Li, which makes sense.'''
dscores = probs
dscores[range(num_examples),y]-=1
dscores/=num_examples

dW =...


I am working on the file "Deep Learning.ai/Numpy - Understanding Backprop.ipynb".
The first task is:
Putting it all Together
Till now we just looked at individual elements
Can you write Python code for it?

#Train a Linear Classifier

# initialize parameters randomly
W = 0.01 * np.random.randn(D,K)
b = np.zeros((1,K))

# some hyperparameters
step_size = 1e-0
reg = 1e-3 # regularization strength

# gradient descent loop
num_examples = X.shape[0]
for i in range(200):

  # evaluate class scores, [N x K]
  scores = np.dot(X, W) + b

  # compute the class probabilities
  exp_scores = np.exp(scores)
  probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True) # [N x K]

  # compute the loss: average cross-entropy loss and regularization
  corect_logprobs = -np.log(probs[range(num_examples),Y])
  data_loss = np.sum(corect_logprobs)/num_examples
  reg_loss = 0.5*reg*np.sum(W*W)
  loss = data_loss + reg_loss
  if i % 10 == 0:
    print ("iteration %d: loss %f" % (i, loss))

  # compute the gradient on scores
  dscores = probs
  dscores[range(num_examples),Y] -= 1
  dscores /= num_examples

  # backpropate the gradient to the parameters (W,b)
  dW = np.dot(X.T, dscores)
  db = np.sum(dscores, axis=0, keepdims=True)

  dW += reg*W # regularization gradient

  # perform a parameter update
  W += -step_size * dW
  b += -step_size * db

# evaluate training set Accuracy
scores = np.dot(X,W) + b
predicted_class = np.argmax(scores, axis=1)
print("training_accuracy: %.2f" % (np.mean(predicted_class==Y)))

# plot the resulting classifier
h = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
Z = np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b
Z = np.argmax(Z, axis=1)
Z = Z.reshape(xx.shape)

plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8)
plt.scatter(X[:, 0], X[:, 1], c=Y, s=40, cmap=plt.cm.Spectral)
plt.xlim(xx.min(), xx.max())
plt.ylim(yy.min(), yy.max())
plt.show()


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Backward pass
Implement the rest of the function. This will compute the gradient of the loss with respect to the variables W1, b1, W2, and b2. Now that you (hopefully!) have a correctly implemented forward pass, you can debug your backward pass using a numeric gradient check:
Can you write Python code for it?

from cs231n.gradient_check import eval_numerical_gradient

# Use numeric gradient checking to check your implementation of the backward pass.
# If your implementation is correct, the difference between the numeric and
# analytic gradients should be less than 1e-8 for each of W1, W2, b1, and b2.

loss, grads = two_layer_net(X, model, y, reg)

# these should all be less than 1e-8 or so
for param_name in grads:
  param_grad_num = eval_numerical_gradient(lambda W: two_layer_net(X, model, y, reg)[0], model[param_name], verbose=False)
  print '%s max relative error: %e' % (param_name, rel_error(param_grad_num, grads[param_name]))


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Forward pass: compute loss
In the same function, implement the second part that computes the data and regularizaion loss.
Can you write Python code for it?

reg = 0.1
loss, _ = two_layer_net(X, model, y, reg)
correct_loss = 1.38191946092

# should be very small, we get 5e-12
print 'Difference between your loss and correct loss:'
print np.sum(np.abs(loss - correct_loss))


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Load the data
Now that you have implemented a two-layer network that passes gradient checks, it's time to load up our favorite CIFAR-10 data so we can use it to train a classifier.
Can you write Python code for it?

from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]

    # Normalize the data: subtract the mean image
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image

    # Reshape data to rows
    X_train = X_train.reshape(num_training, -1)
    X_val = X_val.reshape(num_validation, -1)
    X_test = X_test.reshape(num_test, -1)

    return X_train, y_train, X_val, y_val, X_test, y_test

# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()
print 'Train data shape: ', X_train.shape
print 'Train labels shape: ', y_train.shape
print 'Validation data shape: ', X_val.shape
print 'Validation labels shape: ', y_val.shape
print 'Test data shape: ', X_test.shape
print 'Test labels shape: ', y_test.shape


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Now also implement the RMSProp update rule inside the train function and rerun the optimization:
Can you write Python code for it?

model = init_toy_model()
trainer = ClassifierTrainer()
# call the trainer to optimize the loss
# Notice that we're using sample_batches=False, so we're performing Gradient Descent (no sampled batches of data)
best_model, loss_history, _, _ = trainer.train(X, y, X, y,
                                             model, two_layer_net,
                                             reg=0.001,
                                             learning_rate=1e-1, momentum=0.9, learning_rate_decay=1,
                                             update='rmsprop', sample_batches=False,
                                             num_epochs=100,
                                             verbose=False)
correct_loss = 0.439368
print 'Final loss with RMSProp: %f. We get: %f' % (loss_history[-1], correct_loss)


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Run on the test set
When you are done experimenting, you should evaluate your final trained network on the test set.
We will give you extra bonus point for every 1% of accuracy above 56%.
Can you write Python code for it?

scores_test = two_layer_net(X_test, best_model)
print 'Test accuracy: ', np.mean(np.argmax(scores_test, axis=1) == y_test)


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
The neural network parameters will be stored in a dictionary (model below), where the keys are the parameter names and the values are numpy arrays. Below, we initialize toy data and a toy model that we will use to verify your implementations.
Can you write Python code for it?

# Create some toy data to check your implementations
input_size = 4
hidden_size = 10
num_classes = 3
num_inputs = 5

def init_toy_model():
  model = {}
  model['W1'] = np.linspace(-0.2, 0.6, num=input_size*hidden_size).reshape(input_size, hidden_size)
  model['b1'] = np.linspace(-0.3, 0.7, num=hidden_size)
  model['W2'] = np.linspace(-0.4, 0.1, num=hidden_size*num_classes).reshape(hidden_size, num_classes)
  model['b2'] = np.linspace(-0.5, 0.9, num=num_classes)
  return model

def init_toy_data():
  X = np.linspace(-0.2, 0.5, num=num_inputs*input_size).reshape(num_inputs, input_size)
  y = np.array([0, 1, 2, 2, 1])
  return X, y

model = init_toy_model()
X, y = init_toy_data()


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Train a network
To train our network we will use SGD with momentum. In addition, we will adjust the learning rate with an exponential learning rate schedule as optimization proceeds; after each epoch, we will reduce the learning rate by multiplying it by a decay rate.
Can you write Python code for it?

from cs231n.classifiers.neural_net import init_two_layer_model

model = init_two_layer_model(32*32*3, 50, 10) # input size, hidden size, number of classes
trainer = ClassifierTrainer()
best_model, loss_history, train_acc, val_acc = trainer.train(X_train, y_train, X_val, y_val,
                                             model, two_layer_net,
                                             num_epochs=5, reg=1.0,
                                             momentum=0.9, learning_rate_decay = 0.95,
                                             learning_rate=1e-5, verbose=True)


I am working on the file "Deep Learning/Exercise 1/Q1.ipynb".
The first task is:
Tune your hyperparameters
What's wrong?. Looking at the visualizations above, we see that the loss is decreasing more or less linearly, which seems to suggest that the learning rate may be too low. Moreover, there is no gap between the training and validation accuracy, suggesting that the model we used has low capacity, and that we should increase its size. On the other hand, with a very large model we would expect to see more overfitting, which would manifest itself as a very large gap between the training and validation accuracy.
Tuning. Tuning the hyperparameters and developing intuition for how they affect the final performance is a large part of using Neural Networks, so we want you to get a lot of practice. Below, you should experiment with different values of the various hyperparameters, including hidden layer size, learning rate, numer of training epochs, and regularization strength. You might also consider tuning the momentum and learning rate decay parameters, but you should be able to get good performance using the default values.
Approximate results. You should be aim to achieve a classification accuracy of greater than 50% on the validation set. Our best network gets over 56% on the validation set.
Experiment: You goal in this exercise is to get as good of a result on CIFAR-10 as you can, with a fully-connected Neural Network. For every 1% above 56% on the Test set we will award you with one extra bonus point. Feel free implement your own techniques (e.g. PCA to reduce dimensionality, or adding dropout, or adding features to the solver, etc.).
Can you write Python code for it?

best_model = None # store the best model into this

#################################################################################
# TODO: Tune hyperparameters using the validation set. Store your best trained  #
# model in best_model.                                                          #
#                                                                               #
# To help debug your network, it may help to use visualizations similar to the  #
# ones we used above; these visualizations will have significant qualitative    #
# differences from the ones we saw above for the poorly tuned network.          #
#                                                                               #
# Tweaking hyperparameters by hand can be fun, but you might find it useful to  #
# write code to sweep through possible combinations of hyperparameters          #
# automatically like we did on the previous assignment.                         #
#################################################################################
model = init_two_layer_model(32*32*3, 50, 10) # input size, hidden size, number of classes
trainer = ClassifierTrainer()
best_model, loss_history, train_acc, val_acc = trainer.train(X_train, y_train, X_val, y_val,
                                             model, two_layer_net,
                                             num_epochs=20, reg=1.0,
                                             momentum=0.90, learning_rate_decay = 0.95, update='rmsprop',
                                             learning_rate=1e-4, verbose=True)
#################################################################################
#                               END OF YOUR CODE                                #
#################################################################################

# visualize the weights
show_net_weights(best_model)


I am working on the file "Deep Learning/Exercise 1/Q2.ipynb".
The first task is:
Affine layer: forward
Open the file cs231n/layers.py and implement the affine_forward function.
Once you are done we will test your can test your implementation by running the following:
Can you write Python code for it?

# Test the affine_forward function

num_inputs = 2
input_shape = (4, 5, 6)
output_dim = 3

input_size = num_inputs * np.prod(input_shape)
weight_size = output_dim * np.prod(input_shape)

x = np.linspace(-0.1, 0.5, num=input_size).reshape(num_inputs, *input_shape)
w = np.linspace(-0.2, 0.3, num=weight_size).reshape(np.prod(input_shape), output_dim)
b = np.linspace(-0.3, 0.1, num=output_dim)

out, _ = affine_forward(x, w, b)
correct_out = np.array([[ 1.49834967,  1.70660132,  1.91485297],
                        [ 3.25553199,  3.5141327,   3.77273342]])

# Compare your output with ours. The error should be around 1e-9.
print 'Testing affine_forward function:'
print 'difference: ', rel_error(out, correct_out)


I am working on the file "Deep Learning/Exercise 1/Q2.ipynb".
The first task is:
Loss layers: Softmax and SVM
You implemented these loss functions in the last assignment, so we'll give them to you for free here. It's still a good idea to test them to make sure they work correctly.
Can you write Python code for it?

num_classes, num_inputs = 10, 50
x = 0.001 * np.random.randn(num_inputs, num_classes)
y = np.random.randint(num_classes, size=num_inputs)

dx_num = eval_numerical_gradient(lambda x: svm_loss(x, y)[0], x, verbose=False)
loss, dx = svm_loss(x, y)

# Test svm_loss function. Loss should be around 9 and dx error should be 1e-9
print 'Testing svm_loss:'
print 'loss: ', loss
print 'dx error: ', rel_error(dx_num, dx)

dx_num = eval_numerical_gradient(lambda x: softmax_loss(x, y)[0], x, verbose=False)
loss, dx = softmax_loss(x, y)

# Test softmax_loss function. Loss should be 2.3 and dx error should be 1e-8
print '\nTesting softmax_loss:'
print 'loss: ', loss
print 'dx error: ', rel_error(dx_num, dx)


I am working on the file "Deep Learning/Exercise 1/Q2.ipynb".
The first task is:
ReLU layer: backward
Implement the relu_backward function and test your implementation using numeric gradient checking:
Can you write Python code for it?

x = np.random.randn(10, 10)
dout = np.random.randn(*x.shape)

dx_num = eval_numerical_gradient_array(lambda x: relu_forward(x)[0], x, dout)

_, cache = relu_forward(x)
dx = relu_backward(dout, cache)

# The error should be around 1e-12
print 'Testing relu_backward function:'
print 'dx error: ', rel_error(dx_num, dx)


I am working on the file "Deep Learning/Exercise 2/Q1.ipynb".
The first task is:
Aside: Image processing via convolutions
As fun way to both check your implementation and gain a better understanding of the type of operation that convolutional layers can perform, we will set up an input containing two images and manually set up filters that perform common image processing operations (grayscale conversion and edge detection). The convolution forward pass will apply these operations to each of the input images. We can then visualize the results as a sanity check.
Can you write Python code for it?

from scipy.misc import imread, imresize

kitten, puppy = imread('kitten.jpg'), imread('puppy.jpg')
# kitten is wide, and puppy is already square
d = kitten.shape[1] - kitten.shape[0]
kitten_cropped = kitten[:, d/2:-d/2, :]

img_size = 200   # Make this smaller if it runs too slow
x = np.zeros((2, 3, img_size, img_size))
x[0, :, :, :] = imresize(puppy, (img_size, img_size)).transpose((2, 0, 1))
x[1, :, :, :] = imresize(kitten_cropped, (img_size, img_size)).transpose((2, 0, 1))

# Set up a convolutional weights holding 2 filters, each 3x3
w = np.zeros((2, 3, 3, 3))

# The first filter converts the image to grayscale.
# Set up the red, green, and blue channels of the filter.
w[0, 0, :, :] = [[0, 0, 0], [0, 0.3, 0], [0, 0, 0]]
w[0, 1, :, :] = [[0, 0, 0], [0, 0.6, 0], [0, 0, 0]]
w[0, 2, :, :] = [[0, 0, 0], [0, 0.1, 0], [0, 0, 0]]

# Second filter detects horizontal edges in the blue channel.
w[1, 2, :, :] = [[1, 2, 1], [0, 0, 0], [-1, -2, -1]]

# Vector of biases. We don't need any bias for the grayscale
# filter, but for the edge detection filter we want to add 128
# to each output so that nothing is negative.
b = np.array([0, 128])

# Compute the result of convolving each input in x with each filter in w,
# offsetting by b, and storing the results in out.
out, _ = conv_forward_naive(x, w, b, {'stride': 1, 'pad': 1})

def imshow_noax(img, normalize=True):
    """ Tiny helper to show images as uint8 and remove axis labels """
    if normalize:
        img_max, img_min = np.max(img), np.min(img)
        img = 255.0 * (img - img_min) / (img_max - img_min)
    plt.imshow(img.astype('uint8'))
    plt.gca().axis('off')

# Show the original images and the results of the conv operation
plt.subplot(2, 3, 1)
imshow_noax(puppy, normalize=False)
plt.title('Original image')
plt.subplot(2, 3, 2)
imshow_noax(out[0, 0])
plt.title('Grayscale')
plt.subplot(2, 3, 3)
imshow_noax(out[0, 1])
plt.title('Edges')
plt.subplot(2, 3, 4)
imshow_noax(kitten_cropped, normalize=False)
plt.subplot(2, 3, 5)
imshow_noax(out[1, 0])
plt.subplot(2,...


I am working on the file "Deep Learning/Exercise 2/Q1.ipynb".
The first task is:
Convolution layer: backward naive
Next you need to implement the function conv_backward_naive in the file cs231n/layers.py. As usual, we will check your implementation with numeric gradient checking.
Can you write Python code for it?

x = np.random.randn(4, 3, 5, 5)
w = np.random.randn(2, 3, 3, 3)
b = np.random.randn(2,)
dout = np.random.randn(4, 2, 5, 5)
conv_param = {'stride': 1, 'pad': 1}

dx_num = eval_numerical_gradient_array(lambda x: conv_forward_naive(x, w, b, conv_param)[0], x, dout)
dw_num = eval_numerical_gradient_array(lambda w: conv_forward_naive(x, w, b, conv_param)[0], w, dout)
db_num = eval_numerical_gradient_array(lambda b: conv_forward_naive(x, w, b, conv_param)[0], b, dout)

out, cache = conv_forward_naive(x, w, b, conv_param)
dx, dw, db = conv_backward_naive(dout, cache)

# Your errors should be around 1e-9'
print 'Testing conv_backward_naive function'
print 'dx error: ', rel_error(dx, dx_num)
print 'dw error: ', rel_error(dw, dw_num)
print 'db error: ', rel_error(db, db_num)


I am working on the file "Deep Learning/Exercise 2/Q1.ipynb".
The first task is:
Convolution layer: forward naive
We are now ready to implement the forward pass for a convolutional layer. Implement the function conv_forward_naive in the file cs231n/layers.py.
You don't have to worry too much about efficiency at this point; just write the code in whatever way you find most clear.
You can test your implementation by running the following:
Can you write Python code for it?


I am working on the file "Deep Learning/Exercise 2/Q1.ipynb".
The first task is:
Dropout layer: forward
Open the file cs231n/layers.py and implement the dropout_forward function. You should implement inverted dropout rather than regular dropout. We can check the forward pass by looking at the statistics of the outputs in train and test modes.
Can you write Python code for it?

# Check the dropout forward pass

x = np.random.randn(100, 100)
dropout_param_train = {'p': 0.25, 'mode': 'train'}
dropout_param_test = {'p': 0.25, 'mode': 'test'}

out_train, _ = dropout_forward(x, dropout_param_train)
out_test, _ = dropout_forward(x, dropout_param_test)

# Test dropout training mode; about 25% of the elements should be nonzero
print np.mean(out_train != 0) # expected to be ~0.25

# Test dropout test mode; all of the elements should be nonzero
print np.mean(out_test != 0) # expected to be = 1


I am working on the file "Deep Learning/Exercise 2/Q1.ipynb".
The first task is:
Max pooling layer: backward naive
Implement the backward pass for a max pooling layer in the function max_pool_backward_naive in the file cs231n/layers.py. As always we check the correctness of the backward pass using numerical gradient checking.
Can you write Python code for it?

x = np.random.randn(3, 2, 8, 8)
dout = np.random.randn(3, 2, 4, 4)
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

dx_num = eval_numerical_gradient_array(lambda x: max_pool_forward_naive(x, pool_param)[0], x, dout)

out, cache = max_pool_forward_naive(x, pool_param)
dx = max_pool_backward_naive(dout, cache)

# Your error should be around 1e-12
print 'Testing max_pool_backward_naive function:'
print 'dx error: ', rel_error(dx, dx_num)


I am working on the file "Deep Learning/Exercise 2/Q2.ipynb".
The first task is:
Dropout
The first way we will reduce overfitting is to use dropout.
You have already implemented this in Q1 of this exercise, but let's just check that it still works :-)
Can you write Python code for it?

# Check the dropout forward pass

x = np.random.randn(100, 100)
dropout_param_train = {'p': 0.25, 'mode': 'train'}
dropout_param_test = {'p': 0.25, 'mode': 'test'}

out_train, _ = dropout_forward(x, dropout_param_train)
out_test, _ = dropout_forward(x, dropout_param_test)

# Test dropout training mode; about 25% of the elements should be nonzero
print np.mean(out_train != 0)

# Test dropout test mode; all of the elements should be nonzero
print np.mean(out_test != 0)

from cs231n.gradient_check import eval_numerical_gradient_array

# Check the dropout backward pass

x = np.random.randn(5, 4)
dout = np.random.randn(*x.shape)
dropout_param = {'p': 0.8, 'mode': 'train', 'seed': 123}

dx_num = eval_numerical_gradient_array(lambda x: dropout_forward(x, dropout_param)[0], x, dout)

_, cache = dropout_forward(x, dropout_param)
dx = dropout_backward(dout, cache)

# The error should be around 1e-12
print 'Testing dropout_backward function:'
print 'dx error: ', rel_error(dx_num, dx)


I am working on the file "Deep Learning/Exercise 2/Q2.ipynb".
The first task is:
Load data
For this exercise our training set will contain 500 images and our validation and test sets will contain 1000 images as usual.
Can you write Python code for it?

from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=500, num_validation=1000, num_test=1000, normalize=True):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = range(num_training, num_training + num_validation)
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = range(num_training)
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = range(num_test)
    X_test = X_test[mask]
    y_test = y_test[mask]

    # Normalize the data: subtract the mean image
    if normalize:
        mean_image = np.mean(X_train, axis=0)
        X_train -= mean_image
        X_val -= mean_image
        X_test -= mean_image

    # Transpose so that channels come first
    X_train = X_train.transpose(0, 3, 1, 2).copy()
    X_val = X_val.transpose(0, 3, 1, 2).copy()
    X_test = X_test.transpose(0, 3, 1, 2).copy()

    return X_train, y_train, X_val, y_val, X_test, y_test

# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data(num_training=500)
print 'Train data shape: ', X_train.shape
print 'Train labels shape: ', y_train.shape
print 'Validation data shape: ', X_val.shape
print 'Validation labels shape: ', y_val.shape
print 'Test data shape: ', X_test.shape
print 'Test labels shape: ', y_test.shape


I am working on the file "Deep Learning/Exercise 2/Q2.ipynb".
The first task is:
Train again
We will now train a new network with the same training data and the same architecture, but using data augmentation and dropout.
If everything works, you should see a higher validation accuracy than above and a smaller gap between the training accuracy and the validation accuracy.
Networks with dropout usually take a bit longer to train, so we will use more training epochs this time.
Can you write Python code for it?

input_shape = (3, 28, 28)

def augment_fn(X):
    out = random_flips(random_crops(X, input_shape[1:]))
    out = random_tint(random_contrast(out))
    return out

def predict_fn(X):
    return fixed_crops(X, input_shape[1:], 'center')

model = init_three_layer_convnet(filter_size=5, input_shape=input_shape, num_filters=(32, 128))
trainer = ClassifierTrainer()

best_model, loss_history, train_acc_history, val_acc_history = trainer.train(
          X_train, y_train, X_val, y_val, model, three_layer_convnet,
          reg=0.05, learning_rate=0.00005, learning_rate_decay=1.0,
          batch_size=50, num_epochs=30, update='rmsprop', verbose=True, dropout=0.6,
          augment_fn=augment_fn, predict_fn=predict_fn)

# Visualize the loss and accuracy for our network trained with dropout and data augmentation.
# You should see less overfitting, and you may also see slightly better performance on the
# validation set.

plt.subplot(2, 1, 1)
plt.plot(train_acc_history)
plt.plot(val_acc_history)
plt.title('accuracy vs time')
plt.legend(['train', 'val'], loc=4)
plt.xlabel('epoch')
plt.ylabel('classification accuracy')

plt.subplot(2, 1, 2)
plt.plot(loss_history)
plt.title('loss vs time')
plt.xlabel('iteration')
plt.ylabel('loss')
plt.show()


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Cost and Optimization Functions
We'll use Tensorflow's built-in functions for this part (check out the documentation for a lot more options and discussion on this):
Can you write Python code for it?

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Data Format
The data is stored in a vector format, although the original data was a 2-dimensional matirx with values representing how much pigment was at a certain location. Let's explore this:
Can you write Python code for it?

type(mnist)

type(mnist.train.images)

#mnist.train.images[0]
mnist.train.images[2].shape

sample = mnist.train.images[2].reshape(28,28)

import matplotlib.pyplot as plt
%matplotlib inline

plt.imshow(sample)


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
In order to get a numerical value for our predictions we will need to use tf.cast to cast the Tensor of booleans back into a Tensor of Floating point values in order to take the mean of it.
Can you write Python code for it?

correct_predictions = tf.cast(correct_predictions, "float")

print(correct_predictions[0])


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Model Evaluations
Tensorflow comes with some built-in functions to help evaluate our model, including tf.equal and tf.cast with tf.reduce_mean.
tf.equal()
This is essentially just a check of predictions == y_test. In our case since we know the format of the labels is a 1 in an array of zeroes, we can compare argmax() location of that 1. Remember that y here is still that placeholder we created at the very beginning, we will perform a series of operations to get a Tensor that we can eventually fill in the test data for with an evaluation method. What we are currently running will still be empty of test data:
Can you write Python code for it?

# Test model
correct_predictions = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))

print(correct_predictions[0])


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
MultiLayer Model
It is time to create our model, let's review what we want to create here.
First we receive the input data array and then to send it to the first hidden layer. Then the data will begin to have a weight attached to it between layers (remember this is initially a random value) and then sent to a node to undergo an activation function (along with a Bias as mentioned in the lecture). Then it will continue on to the next hidden layer, and so on until the final output layer. In our case, we will just use two hidden layers, the more you use the longer the model will take to run (but it has more of an opportunity to possibly be more accurate on the training data).
Once the transformed "data" has reached the output layer we need to evaluate it. Here we will use a loss function (also called a cost function) to evaluate how far off we are from the desired result. In this case, how many of the classes we got correct.
Then we will apply an optimization function to minimize the cost (lower the error). This is done by adjusting weight values accordingly across the network. In out example, we will use the Adam Optimizer, which keep in mind, relative to other mathematical concepts, is an extremely recent development.
We can adjust how quickly to apply this optimization by changing our earlier learning rate parameter. The lower the rate the higher the possibility for accurate training results, but that comes at the cost of having to wait (physical time wise) for the results. Of course, after a certain point there is no benefit to lower the learning rate.
Now we will create our model, we'll start with 2 hidden layers, which use the [RELU](https://en.wikipedia.org/wiki/Rectifier_(neural_networks) activation function, which is a very simple rectifier function which essentially either returns x or zero. For our final output layer we will use a linear activation with matrix multiplication:
Can you write Python code for it?

def multilayer_perceptron(x, weights, biases):
    '''
    x : Place Holder for Data Input
    weights: Dictionary of weights
    biases: Dicitionary of biases
    '''

    # First Hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)

    # Second Hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)

    # Last Output layer with linear activation
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Network Parameters
Here we have parameters which will directly define our Neural Network, these would be adjusted depending on what your data looked like and what kind of a net you would want to build. Basically just some numbers we will eventually use to define some variables later on in our model:
Can you write Python code for it?

# Network Parameters
n_hidden_1 = 256 # 1st layer number of features
n_hidden_2 = 256 # 2nd layer number of features
n_input = 784 # MNIST data input (img shape: 28*28)
n_classes = 10 # MNIST total classes (0-9 digits)
n_samples = mnist.train.num_examples


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Parameters
We'll need to define 4 parameters, it is really (really) hard to know what good parameter values are on a data set for which you have no experience with, however since MNIST is pretty famous, we have some reasonable values for our data below. The parameters here are:

Learning Rate - How quickly to adjust the cost function.
Training Epochs - How many training cycles to go through
Batch Size - Size of the 'batches' of training data
Can you write Python code for it?

# Parameters
learning_rate = 0.001
training_epochs = 15
batch_size = 100


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Running the Session
Now it is time to run our session! Pay attention to how we have two loops, the outer loop which runs the epochs, and the inner loop which runs the batches for each epoch of training. Let's breakdown each step!
Can you write Python code for it?

# Launch the session
sess = tf.InteractiveSession()

# Intialize all the variables
sess.run(init)

# Training Epochs
# Essentially the max amount of loops possible before we stop
# May stop earlier if cost/loss limit was set
for epoch in range(training_epochs):

    # Start with cost = 0.0
    avg_cost = 0.0

    # Convert total number of batches to integer
    total_batch = int(n_samples/batch_size)

    # Loop over all batches
    for i in range(total_batch):

        # Grab the next batch of training data and labels
        batch_x, batch_y = mnist.train.next_batch(batch_size)

        # Feed dictionary for optimization and loss value
        # Returns a tuple, but we only need 'c' the cost
        # So we set an underscore as a "throwaway"
        _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})

        # Compute average loss
        avg_cost += c / total_batch

    print("Epoch: {} cost={:.4f}".format(epoch+1,avg_cost))

print("Model has completed {} Epochs of Training".format(training_epochs))


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
TensorFlow Graph Input
Can you write Python code for it?

x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
The eval() method allows you to directly evaluates this tensor in a Session without needing to call tf.sess():mm
Can you write Python code for it?

print("Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
This may seem a little strange, but this accuracy is still a Tensor object. Remember that we still need to pass in our actual test data! Now we can call the MNIST test labels and images and evaluate our accuracy!
Can you write Python code for it?


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Training the Model
next_batch()
Before we get started I want to cover one more convenience function in our mnist data object called next_batch. This returns a tuple in the form (X,y) with an array of the data and a y array indicating the class in the form of a binary array. For example:
Can you write Python code for it?

Xsamp,ysamp = mnist.train.next_batch(1)

plt.imshow(Xsamp.reshape(28,28))

# Remember indexing starts at zero!
print(ysamp)


I am working on the file "Deep Learning/MNIST with Multi-Layer Perceptron.ipynb".
The first task is:
Weights and Bias
In order for our tensorflow model to work we need to create two dictionaries containing our weight and bias objects for the model. We can use the tf.variable object type. This is different from a constant because TensorFlow's Graph Object becomes aware of the states of all the variables. A Variable is a modifiable tensor that lives in TensorFlow's graph of interacting operations. It can be used and even modified by the computation. We will generally have the model parameters be Variables. From the documentation string:
A variable maintains state in the graph across calls to `run()`. You add a variable to the graph by constructing an instance of the class `Variable`.

The `Variable()` constructor requires an initial value for the variable, which can be a `Tensor` of any type and shape. The initial value defines the type and shape of the variable. After construction, the type and shape of the variable are fixed. The value can be changed using one of the assign methods.

We'll use tf's built-in random_normal method to create the random values for our weights and biases (you could also just pass ones as the initial biases).
Can you write Python code for it?

weights = {
    'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, n_classes]))
}

biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
pred = multilayer_perceptron(x, weights, biases)


I am working on the file "Deep Learning/Naive Version of Neural Network.ipynb".
The first task is:
Basic Properties
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib inline

x = np.linspace(-5,5,201)
plt.plot(x,tanh(x),label='Tanh')
plt.plot(x,logistic(x),label='Logistic')
plt.plot(x,relu(x),label='Relu')
plt.xlim([x.min(),x.max()])
plt.legend(loc=2)
plt.show()

import matplotlib.pyplot as plt
%matplotlib inline

x = np.linspace(-5,5,201)
plt.plot(x,tanh_deriv(x),label='Tanh derivative')
plt.plot(x,logistic_deriv(x),label='Logistic derivative')
plt.plot(x,relu_deriv(x),label='Relu derivative')
plt.ylim([-0.1,1.1])
plt.xlim([x.min(),x.max()])
plt.legend(loc=2,fontsize=10)
plt.show()


I am working on the file "Deep Learning/Naive Version of Neural Network.ipynb".
The first task is:
Gradient Means

Stochastic Gradient
Batch Gradient
MiniBatch Gradient

Test Neural Network
Can you write Python code for it?