I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
Now let's try using KFold to train and test our model on 10 different subsets of our data. Below we set our cross-validator as 'cv'. We then loop through the various splits in our data that cv creates and use it to make our training and test sets. We then use our training set to fit a Logistic Regression model and generate predictions from our test set, which we compare to the actual outcomes we observed.
Can you write Python code for it?

## Define function
cv = KFold(n_splits=10, shuffle=True, random_state=0)

## Create for-loop
for train_index, test_index in cv.split(data):

    ## Define training and test sets
    X_train = data.loc[train_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_train = data.loc[train_index]['activity']
    X_test = data.loc[test_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_test = data.loc[test_index]['activity']

    ## Fit model
    clf = LogisticRegression()
    clf.fit(X_train, y_train)

    ## Generate predictions
    predicted = clf.predict(X_test)

    ## Compare to actual outcomes and return precision
    print('Precision: '+str(100 * round(precision_score(y_test, predicted),3)))


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
Our model seems to be performing even better when we cross-validate over months, possibly because we're structuring the cross-validation such that inspections in some of the same blocks appear consistently over time.
Exercise 2
Try re-creating this cross-validation, but with the training set restricted to only the 3 months prior to the test set. Now do the same with the last 1 and 2 months. Do the results change?
Can you write Python code for it?

months = np.sort(data.month.unique())

for month in range(2,13):
    test = data[data.month==month]
    train = data[data.month>=month-3]
    train = train.drop[train.month>=month]
    print('Test Month: '+str(test.month.unique()), 'Training Months: '+str(train.month.unique()))


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
The next important decision we need to make when cross-validating our models is how we will define our "folds." Folds are the independent subsamples on which we train and test the data. Keep in mind that it is important that our folds are INDEPENDENT, which means we must guarantee that there's no overlap between our training and test set (i.e., no observation is in both the training and test set). Independence can also have other implications for how we slice the data, which we will discuss as we progress through this lesson.
One of the most common approaches to cross-validation is to make random splits in the data. This is often referred to as k-fold cross-validation, in which the only thing we define is the number of folds (k) that want to split our sample into. Here, I'll use the KFold function from scikit-learn's model_selection library. Let's begin by importing the library and then taking a look at how it splits our data.
Can you write Python code for it?

from sklearn.model_selection import KFold


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
Today we'll be looking at 311 service requests for rodent inspection and abatement aggregated at the Census block level. The data set is already prepared for you and available in the same folder as this assignment. Census blocks are a good geographic level to analyze rodent infestations because they are drawn along natural and human-made boundaries, like rivers and roads, that rats tend not to cross.
We will look at the 'activity' variable, which indicates whether inspectors found rat burrows during an inspection (1) or not (0). Here we are looking only at inpsections in 2016. About 43 percent on inspections in 2016 led to inspectors finding and treating rat burrows, as you can see below.
Can you write Python code for it?

data = pd.read_csv('rat_data_2016.csv')

data.columns

data.describe().T


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
Ward's 5-8 have different active-not active ratios. They're most out of whack in 6-8, the least accurate of our predictions. Ratio of not active/to active is about 1 (give or take .25) for every value.
Things are about 3 times as inactive and 5-6 times as inactive in wards 6-8. Ward 3, the sample we could be overfitting to is about 1.5 times more inactive. A bit weird, but more reasonable. This could be a source of our issue?
Exercise 4
Now try running some cross-validations with the data from your project. What are some different ways you might slice the data you're using for your project? Try them out here. This will be a good way to begin making progress toward your final submission.
PLEASE REMEMBER TO SUBMIT THIS HOMEWORK BY CLASS TIME ON THURSDAY.
Can you write Python code for it?

data.describe().T

df1 = pd.read_csv('https://opendata.arcgis.com/datasets/82ab09c9541b4eb8ba4b537e131998ce_22.csv')

df2 = pd.read_csv('https://opendata.arcgis.com/datasets/f2e1c2ef9eb44f2899f4a310a80ecec9_2.csv')

df2.describe().T

df1 = pd.read_csv('https://opendata.arcgis.com/datasets/82ab09c9541b4eb8ba4b537e131998ce_22.csv')

df2 = pd.read_csv('https://opendata.arcgis.com/datasets/f2e1c2ef9eb44f2899f4a310a80ecec9_2.csv')

DF = df1.merge(df2, on ='X', how = 'outer')

from sklearn.metrics import precision_score
from sklearn.model_selection import KFold

cv = KFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in cv.split(data):
    print("TRAIN:", train_index, "TEST:", test_index)

cv = KFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in cv.split(data):
    print("TRAIN:", len(train_index), "TEST:", len(test_index))

## Define function
cv = KFold(n_splits=10, shuffle=True, random_state=0)

## Create for-loop
for train_index, test_index in cv.split(data):

## Define training and test sets
    X_train = DF.loc[train_index].drop(['BBL_LICENSE_FACT_ID'], axis=1)
    y_train = DF.loc[train_index]['BBL_LICENSE_FACT_ID']
    X_test = DF.loc[test_index].drop(['BBL_LICENSE_FACT_ID'], axis=1)
    y_test = DF.loc[test_index]['BBL_LICENSE_FACT_ID']

    ## Fit model
    clf = LogisticRegression()
    clf.fit(X_train, y_train)

    ## Generate predictions
    predicted = clf.predict(X_test)

    ## Compare to actual outcomes and return precision
    print('Precision: '+str(100 * round(precision_score(y_test, predicted),3)))

DF

from sklearn.metrics import precision_score
from sklearn.model_selection import KFold

cv = KFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in cv.split(data):
    print("TRAIN:", train_index, "TEST:", test_index)

cv = KFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in cv.split(data):
    print("TRAIN:", len(train_index), "TEST:", len(test_index))

## Define...


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
We can see that, for the most part, about 50 to 60 percent of the inspections our model predicts will lead our inspectors to rat burrows actually do. This is a modest improvement over our inspectors' current performance in the field. Based on these results, if we used our models to determine which locations our inspectors go to in the field, we'd probably see a 10 to 20 point increase in their likelihood of finding rat burrows.
Exercise 1
Try running the k-fold cross-validation a few times with the same random state. Then try running it a few times with different random states. How do the results change?
Can you write Python code for it?

## Define function
cv = KFold(n_splits=10, shuffle=True, random_state=0)

## Create for-loop
for train_index, test_index in cv.split(data):

    ## Define training and test sets
    X_train = data.loc[train_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_train = data.loc[train_index]['activity']
    X_test = data.loc[test_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_test = data.loc[test_index]['activity']

    ## Fit model
    clf = LogisticRegression()
    clf.fit(X_train, y_train)

    ## Generate predictions
    predicted = clf.predict(X_test)

    ## Compare to actual outcomes and return precision
    print('Precision: '+str(100 * round(precision_score(y_test, predicted),3)))

## Define function
cv = KFold(n_splits=10, shuffle=True, random_state=1)

## Create for-loop
for train_index, test_index in cv.split(data):

    ## Define training and test sets
    X_train = data.loc[train_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_train = data.loc[train_index]['activity']
    X_test = data.loc[test_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_test = data.loc[test_index]['activity']

    ## Fit model
    clf = LogisticRegression()
    clf.fit(X_train, y_train)

    ## Generate predictions
    predicted = clf.predict(X_test)

    ## Compare to actual outcomes and return precision
    print('Precision: '+str(100 * round(precision_score(y_test, predicted),3)))

## Define function
cv = KFold(n_splits=10, shuffle=True, random_state=17)

## Create for-loop
for train_index, test_index in cv.split(data):

    ## Define training and test sets
    X_train = data.loc[train_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_train = data.loc[train_index]['activity']
    X_test = data.loc[test_index].drop(['activity', 'month', 'WARD'], axis=1)
    y_test = data.loc[test_index]['activity']

    ## Fit model
    clf = LogisticRegression()
    clf.fit(X_train, y_train)

    ## Generate predictions
    predicted = clf.predict(X_test)

    ##...


I am working on the file "DG/lesson_08DG.ipynb".
The first task is:
You can see that ShuffleSplit has selected a random set of observations from the index of our data set for each fold of our cross-validation. Let's look at the size of our training and test set for each fold.
Can you write Python code for it?

cv = KFold(n_splits=10, shuffle=True, random_state=0)
for train_index, test_index in cv.split(data):
    print("TRAIN:", len(train_index), "TEST:", len(test_index))


I am working on the file "DICOM-Image-viz.ipynb.ipynb".
The first task is:
Here we plot two DICOM images. Since the second one has a poor contrast we process it through histogram equalization:
Can you write Python code for it?

import plotly.plotly as py
py.sign_in('empet', '')

from plotly.offline import download_plotlyjs, init_notebook_mode,  iplot, plot
init_notebook_mode(connected=True)

title1="Plotly viz of a DICOM medical image
"+\
"Data set: [1]"
pl_img1=get_pl_image('MR-MONO2-16-head')#this DICOM file has no extension
fig1=DICOM_heatmap(pl_img1, title1)
py.iplot(fig1, filename='DICOM-img-MRIs')

title2="Plotly viz of a DICOM medical image"+\
"
Data set: [1]"
pl_img2=get_pl_image('MRBRAIN.DCM', hist_equal=True, no_bins=36)
fig2=DICOM_heatmap(pl_img2, title2)

py.iplot(fig2, filename='DICOM-MRBRAIN')

from IPython.core.display import HTML
def  css_styling():
    styles = open("./custom.css", "r").read()
    return HTML(styles)
css_styling()


I am working on the file "DICOM-Image-viz.ipynb.ipynb".
The first task is:
In this notebook we define functions to read  DICOM files, transform the corresponding image to get the z-value for a heatmap and plot the  heatmap, using the mpl bone colormap/colorscale.
If necessary we  increase the image contrast  applying the   histogram equalization, defined by the following function:
Can you write Python code for it?

def histogram_equalization(img, no_bins):

    #img- the image as a numpy.array
    #the appropriate number of bins, `no_bins` in the histogram is chosen by experiments,
    #until the contrast is convenient

    image_hist, bins = np.histogram(img.flatten(), no_bins, normed=True)
    csum = image_hist.cumsum()
    cdf_mult = np.max(img) * csum / csum[-1] # cdf multiplied by a factor

    #  linear interpolation of cdf_mult to get new pixel values
    im_new = np.interp(img.flatten(), bins[:-1],  cdf_mult)

    return im_new.reshape(img.shape), cdf_mult


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
Dans la partie de code suivante, nous supprimons les fichiers générés par l'algorithme précédent pour pouvoir les regénérer une deuxième fois.
Can you write Python code for it?

cl.delete_blob(bs, "hdblobstorage", 'imdd/dom/matrix_all.txt')
cl.delete_blob(bs, "hdblobstorage", 'imdd/dom/similarities.txt')
df = %blob_ls hdblobstorage/imdd/dom/matrix_all.txt/
df
for name in df["name"]:
    cl.delete_blob(bs, "hdblobstorage", name)
df = %blob_ls hdblobstorage/imdd/dom/similarities.txt/
df
for name in df["name"]:
    cl.delete_blob(bs, "hdblobstorage", name)


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
Et nous affichons enfin les résultats :
Can you write Python code for it?

 with open('recom.txt', 'r') as f:
    ids = f.read()
    print(ids)


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
Nous supprimons d'abord le fichier généré par la dernière exécution, ensuite nous lançons le script PIG afin de récupérer les ids des films similaires. Pour cet exemple, nous souhaitons récupérer les 20 films les plus proches à celui dont l'id est 1610.
Can you write Python code for it?

if cl.exists(bs, cl.account_name, "$PSEUDO/imdd/dom/recom.txt"):
    r = cl.delete_folder (bs, cl.account_name, "$PSEUDO/imdd/dom/recom.txt")
jid = cl.pig_submit(bs, blobstorage, "load_results.pig",params={"MvID":'1610',"size":"20"})
jid

st = %hd_job_status jid["id"]
(st["id"],st["percentComplete"],st["completed"],
st["status"]["jobComplete"],st["status"]["state"])


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
On code l'algorithme en PIG. La difficulté est que PIG gère très mal l'imbrication de FOREACH, absolument nécessaire à l'algorithme. Notre solution s'est portée sur la mise à plat totale des données. D'où le FLATTEN puis nous avons effectué un JOIN pour la deuxième boucle. Puis nous avons appliqué les règles définies par l'algorithme.
En sortie nous obtenons l'ensemble des paires semblables, avec leur mesure de similarité.
On stocke dans un fichier pour l'exploiter ensuite avec un autre script PIG.
Pour réduire le nombre de résultats, nous ne retiendrons que les paires de films qui ont une similarité de plus de 0.5 .
Can you write Python code for it?

%%PIG_azure dimsum.pig

-- Macro de calcul des normes par colonne (movieID)
DEFINE computeMatrixNorms(cData,sqrt_gamma) RETURNS Matrix_Norms {
    cData_grp = GROUP $cData BY MovieID;
    -- On calcule la norme et le gamma sur la norme
    $Matrix_Norms = FOREACH cData_grp {
        tmp_out = FOREACH $cData GENERATE Rating*Rating;
        out = SUM(tmp_out);
        GENERATE group as MovieID, SQRT(out) as Norm, ($sqrt_gamma.$0/SQRT(out)>1?1:$sqrt_gamma.$0/SQRT(out)) as Prob_j;
    }
}

cData =  LOAD '$CONTAINER/imdd/ratings_mean.csv'
        using PigStorage (',')
        AS (UserID:int, MovieID:int, Rating:double) ;

-- On calcule le gamma
users = GROUP cData all ;
total= FOREACH users GENERATE MAX($1.UserID) as m, MAX($1.MovieID) as n;
sqrt_gamma = FOREACH total GENERATE SQRT(4*LOG(n)/0.7) as a;

-- On calcule la norme et le gamma sur la norme
Matrix_Norms = computeMatrixNorms(cData,sqrt_gamma);

-- On ajoute la colonne Norm et probabilite dans cData
C = JOIN cData BY MovieID,Matrix_Norms BY MovieID;
D = FOREACH C GENERATE cData::UserID as UserID_f,cData::MovieID as MovieID_f,cData::Rating as Rating_f,
                        Matrix_Norms::Norm as Norm_f,Matrix_Norms::Prob_j as Prob_j_f;

Matrix_data = GROUP D BY UserID_f;
FF = FOREACH Matrix_data GENERATE group as UID, FLATTEN(D.MovieID_f) as MV1;

-- Ajout des informations de MV1
FFF = JOIN FF BY (UID,MV1), D BY (UserID_f,MovieID_f);

-- Condition de validite premier IF
FFD = FILTER FFF BY RANDOM()<Prob_j_f;

-- Ajout de la seconde loop
GG = JOIN FFD BY UID, D BY UserID_f;

-- Cleaning du tableau
GGG = FOREACH GG GENERATE FFD::FF::UID as UserID,FFD::FF::MV1 as MV_1,FFD::D::Rating_f as Rating_1,FFD::D::Norm_f as Norm_1,
                FFD::D::Prob_j_f as Proba_1,D::MovieID_f as MV_2,D::Rating_f as Rating_2,
                D::Norm_f as Norm_2,D::Prob_j_f as Proba_2;

-- Ajout de la deuxieme boucle
-- Condition de validite second IF
GGD = FILTER GGG BY RANDOM()<Proba_2;

-- Generation des...


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
On ouvre la connexion au cluster et au blob
Can you write Python code for it?


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
On upload les fichiers qui contient tous les ratings des users.
Can you write Python code for it?


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
On vérifie que tous les fichiers sont présents dans le blob
Can you write Python code for it?

#List files in blob storage
df=%blob_ls hdblobstorage/imdd/
df


I am working on the file "DIMSUM_FINAL_MACHRAOUI_DUONGPRUNIER.ipynb".
The first task is:
Upload du script dimsum.pig et lancement de son exécution :
Can you write Python code for it?

jid = %hd_pig_submit dimsum.pig
jid

st = %hd_job_status jid["id"]
st["id"],st["percentComplete"],st["completed"],st["status"]["jobComplete"],st["status"]["state"]

df=%blob_ls hdblobstorage/imdd/
list(df["name"])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Pandas Series can also be converted by using the numpy array that is stored in pandas_series.values. Note that pandas_series can be any pandas_series object.
Can you write Python code for it?

# Convert a panda series to a tensor

pandas_series=pd.Series([0.1, 2, 0.3, 10.1])
new_tensor=torch.from_numpy(pandas_series.values)
print("The new tensor from numpy array: ", new_tensor)
print("The dtype of new tensor: ", new_tensor.dtype)
print("The type of new tensor: ", new_tensor.type())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Types and Shape

You can find the type of the following list of integers [0, 1, 2, 3, 4] by applying the method torch.tensor():
Can you write Python code for it?

# Convert a integer list with length 5 to a tensor

ints_to_tensor = torch.tensor([0, 1, 2, 3, 4])
print("The dtype of tensor object after converting it to tensor: ", ints_to_tensor.dtype)
print("The type of tensor object after converting it to tensor: ", ints_to_tensor.type())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Max and Min

Now, you'll review another two useful methods: tensor_obj.max() and tensor_obj.min(). These two methods are used for finding the maximum value and the minimum value in the tensor.

Create a max_min_tensor:
Can you write Python code for it?

# Sample for introducing max and min methods

max_min_tensor = torch.tensor([1, 1, 3, 5, 5])
print("Tensor example: ", max_min_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Try

Implement the tensor subtraction with u and v as u-v.
Can you write Python code for it?

# Try by yourself to get a result of u-v

u = torch.tensor([1, 0])
v = torch.tensor([0, 1])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Assign steps with 9:
Can you write Python code for it?

# Second try on using linspace to create tensor

len_9_tensor = torch.linspace(-2, 2, steps = 9)
print ("Second Try on linspace", len_9_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
<!--Empty Space for separating topics-->

Here is the mean method:
Can you write Python code for it?

#Calculate the mean for math_tensor

mean = math_tensor.mean()
print("The mean of math_tensor: ", mean)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

The tensor_obj.size() helps you to find out the size of the tensor_obj.
The tensor_obj.ndimension() shows the dimension of the tensor object.
Can you write Python code for it?

# Introduce the tensor_obj.size() & tensor_ndimension.size() methods

print("The size of the new_float_tensor: ", new_float_tensor.size())
print("The dimension of the new_float_tensor: ",new_float_tensor.ndimension())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

The tensor_obj.view(row, column) is used for reshaping a tensor object.

What if you have a tensor object with torch.Size([5]) as a new_float_tensor as shown in the previous example?

After you execute new_float_tensor.view(5, 1), the size of new_float_tensor will be torch.Size([5, 1]).

This means that the tensor object new_float_tensor has been reshaped from a one-dimensional  tensor object with 5 elements to a two-dimensional tensor object with 5 rows and 1 column.
Can you write Python code for it?

# Introduce the tensor_obj.view(row, column) method

twoD_float_tensor = new_float_tensor.view(5, 1)
print("Original Size: ", new_float_tensor)
print("Size after view method", twoD_float_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

The standard deviation can also be calculated by using tensor_obj.std():
Can you write Python code for it?

#Calculate the standard deviation for math_tensor

standard_deviation = math_tensor.std()
print("The standard deviation of math_tensor: ", standard_deviation)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

Use both torch.linspace() and torch.sin() to construct a tensor that contains the 100 sin result in range from 0 (0 degree) to 2π (360 degree):
Can you write Python code for it?

# Construct the tensor within 0 to 360 degree

pi_tensor = torch.linspace(0, 2*np.pi, 100)
sin_result = torch.sin(pi_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

You can also assign one value to the selected indexes by using the variable. For example, assign 100,000 to all the selected_indexes:
Can you write Python code for it?

#Using variable to assign the value to the selected indexes

print("The inital tensor_sample", tensor_sample)
selected_indexes = [1, 3]
tensor_sample[selected_indexes] = 100000
print("Modified tensor with one value: ", tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:

You can also convert an existing tensor object (tensor_obj) to another tensor type. Convert the integer tensor to a float tensor:
Can you write Python code for it?

# Another method to convert the integer list to float tensor

old_int_tensor = torch.tensor([0, 1, 2, 3, 4])
new_float_tensor = old_int_tensor.type(torch.FloatTensor)
print("The type of the new_float_tensor:", new_float_tensor.type())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Note that the index_tensor[5] will create an error.

The index is shown in the following figure:

Now, you'll see how to change the values on certain indexes.
Suppose you have a tensor as shown here:
Can you write Python code for it?

# A tensor for showing how to change value according to the index

tensor_sample = torch.tensor([20, 1, 2, 3, 4])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
back_to_numpy and new_tensor still point to numpy_array. As a result if we change numpy_array both back_to_numpy and new_tensor will change. For example if we set all the elements in numpy_array to zeros, back_to_numpy and  new_tensor will follow suit.
Can you write Python code for it?

# Set all elements in numpy array to zero
numpy_array[:] = 0
print("The new tensor points to numpy_array : ", new_tensor)
print("and back to numpy array points to the tensor: ", back_to_numpy)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
As a result, the subset_tensor_sample returned only the values on index 1, index 2, and index 3. Then, it stored them in a subset_tensor_sample.
Note: The number on the left side of the colon represents the index of the first value. The number on the right side of the colon is always 1 larger than the index of the last value. For example, tensor_sample[1:4] means you get values from the index 1 to index 3 (4-1).

As for assigning values to the certain index, you can also assign the value to the slices:
Change the value of tensor_sample from index 3 to index 4:
Can you write Python code for it?

# Change the values on index 3 and index 4

print("Inital value on index 3 and index 4:", tensor_sample[3:5])
tensor_sample[3:5] = torch.tensor([300.0, 400.0])
print("Modified tensor:", tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
As a result, the integer list has been converted to a long tensor.

You can find the type of this float list [0.0, 1.0, 2.0, 3.0, 4.0] by applying the method torch.tensor():
Can you write Python code for it?

# Convert a float list with length 5 to a tensor

floats_to_tensor = torch.tensor([0.0, 1.0, 2.0, 3.0, 4.0])
print("The dtype of tensor object after converting it to tensor: ", floats_to_tensor.dtype)
print("The type of tensor object after converting it to tensor: ", floats_to_tensor.type())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
As you can see, the value on index 0 changes. Change the value on index 4 to 0:
Can you write Python code for it?

# Change the value on the index 4 to 0

print("Inital value on index 4:", tensor_sample[4])
tensor_sample[4] = 0
print("Modified tensor:", tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Assign the value on index 0 as 100:
Can you write Python code for it?

# Change the value on the index 0 to 100

print("Inital value on index 0:", tensor_sample[0])
tensor_sample[0] = 100
print("Modified tensor:", tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Double-click here for the solution.

You can add a scalar to the tensor. Use u as the sample tensor:
Can you write Python code for it?

# tensor + scalar

u = torch.tensor([1, 2, 3, -1])
v = u + 1
print ("Addition Result: ", v)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Double-click here for the solution.

Tensor Operations

In the following section, you'll work with operations that you can apply to a tensor.

Tensor Addition

You can perform addition between two tensors.
Create a tensor u with 1 dimension and 2 elements. Then, create another tensor v with the same number of dimensions and the same number of elements:
Can you write Python code for it?

# Create two sample tensors

u = torch.tensor([1, 0])
v = torch.tensor([0, 1])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Double-click here for the solution.

Tensor Functions

For this section, you'll work with some methods that you can apply to tensor objects.

Mean and Standard Deviation

You'll review the mean and standard deviation methods first. They are two basic statistical methods.

Create a tensor with values [1.0, -1, 1, -1]:
Can you write Python code for it?

# Sample tensor for mathmatic calculation methods on tensor

math_tensor = torch.tensor([1.0, -1.0, 1, -1])
print("Tensor example: ", math_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Double-click here for the solution.

Indexing and Slicing

In Python, the index starts with 0. Therefore, the last index will always be 1 less than the length of the tensor object.
You can access the value on a certain index by using the square bracket, for example:
Can you write Python code for it?

# A tensor for showing how the indexs work on tensors

index_tensor = torch.tensor([0, 1, 2, 3, 4])
print("The value on index 0:",index_tensor[0])
print("The value on index 1:",index_tensor[1])
print("The value on index 2:",index_tensor[2])
print("The value on index 3:",index_tensor[3])
print("The value on index 4:",index_tensor[4])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
If you know the trigonometric function, you will notice this is the diagram of the sin result in the range 0 to 360 degrees.

Practice

Construct a tensor with 25 steps in the range 0 to π/2. Print out the Maximum and Minimum number. Also, plot  a graph showing the diagram that shows the result.
Can you write Python code for it?

# Practice: Create your tensor, print max and min number, plot the sin result diagram

# Type your code here


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
Plot the result to get a clearer picture. You must cast the tensor to a numpy array before plotting it.
Can you write Python code for it?

# Plot sin_result

plt.plot(pi_tensor.numpy(), sin_result.numpy())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The answer is tensor(1). Therefore, the method tensor_obj.min() is grabbing the minimum value but not the elements that contain the minimum value in the tensor.

Sin

Sin is the trigonometric function of an angle. Again, you will not be introducedvto any mathematic functions. You'll focus on Python.

Create a tensor with 0, π/2 and π. Then, apply the sin function on the tensor. Notice here that the sin() is not a method of tensor object but is a function of torch:
Can you write Python code for it?

# Method for calculating the sin result of each element in the tensor

pi_tensor = torch.tensor([0, np.pi/2, np.pi])
sin = torch.sin(pi_tensor)
print("The sin result of pi_tensor: ", sin)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The answer is tensor(5). Therefore, the method tensor_obj.max() is grabbing the maximum value but not the elements that contain the maximum value in the tensor.

Use tensor_obj.min() on max_min_tensor:
Can you write Python code for it?

# Method for finding the minimum value in the tensor

min_val = max_min_tensor.min()
print("Minimum number in the tensor: ", min_val)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The float list is converted to a float tensor.
Note: The elements in the list that will be converted to tensor must have the same type.

From the previous examples, you see that torch.tensor() converts the list to the tensor type, which is similar to the original list type. However, what if you want to convert the list to a certain tensor type? torch contains the methods required to do this conversion. The following code  converts an integer list to float tensor:
Can you write Python code for it?

# Convert a integer list with length 5 to float tensor

new_float_tensor = torch.FloatTensor([0, 1, 2, 3, 4])
new_float_tensor.type()
print("The type of the new_float_tensor:", new_float_tensor.type())


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The result is simply tensor([3, 4]). This result is achieved by multiplying every element in u with the corresponding element in the same position v, which is similar to [1 * 3, 2 * 2].

Dot Product

The dot product is a special operation for a vector that you can use in Torch.
Here is the dot product of the two tensors u and v:
Can you write Python code for it?

# Calculate dot product of u, v

u = torch.tensor([1, 2])
v = torch.tensor([3, 2])

print("Dot Product of u, v:", torch.dot(u,v))


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The resultant tensor sin contains the result of the sin function applied to each element in the pi_tensor.

This is different from the previous methods. For tensor_obj.mean(), tensor_obj.std(), tensor_obj.max(), and tensor_obj.min(), the result is a tensor with only one number because these are aggregate methods.

However, the torch.sin() is not. Therefore, the resultant tensors have the same length as the input tensor.

Create Tensor by torch.linspace()

A useful function for plotting mathematical functions is torch.linspace(). torch.linspace() returns evenly spaced numbers over a specified interval. You specify the starting point of the sequence and the ending point of the sequence. The parameter steps indicates the number of samples to generate. Now, you'll work with steps = 5.
Can you write Python code for it?

# First try on using linspace to create tensor

len_5_tensor = torch.linspace(-2, 2, steps = 5)
print ("First Try on linspace", len_5_tensor)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The value on index 4 turns to 0.

If you are familiar with Python, you know that there is a feature called slicing on a list. Tensors support the same feature.
Get the subset of tensor_sample. The subset should contain the values in tensor_sample from index 1 to index 3.
Can you write Python code for it?

# Slice tensor_sample

subset_tensor_sample = tensor_sample[1:4]
print("Original tensor sample: ", tensor_sample)
print("The subset of tensor sample:", subset_tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The values on both index 3 and index 4 were changed. The values on other indexes remain the same.
<!--Empty Space for separating topics-->

You can also use a variable to contain the selected indexes and pass that variable to a tensor slice operation as a parameter, for example:
Can you write Python code for it?

# Using variable to contain the selected index, and pass it to slice operation

selected_indexes = [3, 4]
subset_tensor_sample = tensor_sample[selected_indexes]
print("The inital tensor_sample", tensor_sample)
print("The subset of tensor_sample with the values on index 3 and 4: ", subset_tensor_sample)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
The values on index 1 and index 3 were changed to 100,000. Others remain the same.
Note: You can use only one value for the assignment.

Practice

Try to change the values on index 3, 4, 7 of the following tensor to 0.
Can you write Python code for it?

# Practice: Change the values on index 3, 4, 7 to 0

practice_tensor = torch.tensor([2, 7, 3, 4, 6, 2, 3, 1, 2])


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
This is the function for plotting diagrams. You will use this function to plot the vectors in Coordinate system.
Can you write Python code for it?

# Plot vecotrs, please keep the parameters in the same length
# @param: Vectors = [{"vector": vector variable, "name": name of vector, "color": color of the vector on diagram}]

def plotVec(vectors):
    ax = plt.axes()

    # For loop to draw the vectors
    for vec in vectors:
        ax.arrow(0, 0, *vec["vector"], head_width = 0.05,color = vec["color"], head_length = 0.1)
        plt.text(*(vec["vector"] + 0.1), vec["name"])

    plt.ylim(-2,2)
    plt.xlim(-2,2)


I am working on the file "DL0110EN/1.1_1Dtensors_v2.ipynb".
The first task is:
You get the same result as the previous example. The -1 can represent any size. However, be careful because you can set only one argument as -1.

You can also convert a numpy array to a tensor, for example:
Can you write Python code for it?

# Convert a numpy array to a tensor

numpy_array = np.array([0.0, 1.0, 2.0, 3.0, 4.0])
new_tensor = torch.from_numpy(numpy_array)

print("The dtype of new tensor: ", new_tensor.dtype)
print("The type of new tensor: ", new_tensor.type())


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:

Practice

Try to convert the following Pandas Dataframe  to a tensor
Can you write Python code for it?

# Practice: try to convert Pandas Series to tensor

df = pd.DataFrame({'A':[11, 33, 22],'B':[3, 3, 2]})


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:

Element-wise Product/Hadamard Product

Multiplication of two tensors corresponds to an element-wise product or Hadamard product.  Consider matrix the X and Y with the same size. The Hadamard product corresponds to multiplying each of the elements at the same position, that is, multiplying elements with the same color together. The result is a new matrix that is the same size as matrix X and Y as shown in the following figure:

The code below calculates the element-wise product of the tensor X and Y:
Can you write Python code for it?


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:

Practice

Try to create your own two tensors (X and Y) with different sizes, and multiply them.
Can you write Python code for it?

# Practice: Calculate the product of two tensors (X and Y) with different sizes

# Type your code here


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
As we can see, both methods return the true value (the same value as the picture above). Therefore, both of the methods work.

Consider the elements shown in the following figure:

Use the method above, we can access the 1st-row 1st-column by tensor_example[0][0]
But what if we want to get the value on both 1st-row 1st-column and 1st-row 2nd-column?
You can also use slicing in a tensor. Consider the following figure. You want to obtain the 1st two columns in the 1st row:

Let us see how  we use slicing with 2D tensors to get the values in the above picture.
Can you write Python code for it?

# Use tensor_obj[begin_row_number: end_row_number, begin_column_number: end_column number]
# and tensor_obj[row][begin_column_number: end_column number] to do the slicing

tensor_example = torch.tensor([[11, 12, 13], [21, 22, 23], [31, 32, 33]])
print("What is the value on 1st-row first two columns? ", tensor_example[0, 0:2])
print("What is the value on 1st-row first two columns? ", tensor_example[0][0:2])


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
Bravo! The method torch.tensor() works perfectly.Now, let us try other functions we studied in the Previous Lab.

Let us try tensor_obj.ndimension() (tensor_obj: This can be any tensor object), tensor_obj.shape, and tensor_obj.size()
Can you write Python code for it?

# Try tensor_obj.ndimension(), tensor_obj.shape, tensor_obj.size()

print("The dimension of twoD_tensor: ", twoD_tensor.ndimension())
print("The shape of twoD_tensor: ", twoD_tensor.shape)
print("The shape of twoD_tensor: ", twoD_tensor.size())


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
Fortunately, the code tensor_obj[begin_row_number: end_row_number, begin_column_number: end_column number] is still works.

Practice

Try to change the values on the second column and the last two rows to 0. Basically, change the values on tensor_ques[1][1] and tensor_ques[1][2] to 0.
Can you write Python code for it?

# Practice: Use slice and index to change the values on the matrix tensor_ques.

tensor_ques = torch.tensor([[11, 12, 13], [21, 22, 23], [31, 32, 33]])


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
Like the result shown in the picture above. The result is [[3, 1], [1, 3]]

 Scalar Multiplication

Multiplying a tensor by a scalar is identical to multiplying a matrix by a scaler. If you multiply the matrix Y by the scalar 2, you simply multiply every element in the matrix by 2 as shown in the figure:

Let us try to calculate the product of 2Y.
Can you write Python code for it?

# Calculate 2 * [[2, 1], [1, 2]]

Y = torch.tensor([[2, 1], [1, 2]])
two_Y = 2 * Y
print("The result of 2Y: ", two_Y)


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
See the results and dimensions in 2 and 3 are the same. Both of them contains the 3rd row in the tensor_example, but not the last two values in the 3rd column.

So how can we get the elements in the 3rd column with the last two rows? As the below picture.

Let's see the code below.
Can you write Python code for it?

# Use tensor_obj[begin_row_number: end_row_number, begin_column_number: end_column number]

tensor_example = torch.tensor([[11, 12, 13], [21, 22, 23], [31, 32, 33]])
print("What is the value on 3rd-column last two rows? ", tensor_example[1:3, 2])


I am working on the file "DL0110EN/1.2 Two-Dimensional Tensors_v2.ipynb".
The first task is:
This is a simple calculation. The result from the code matches the result shown in the picture.

Matrix Multiplication

We can also apply matrix multiplication to two tensors, if you have learned linear algebra, you should know that in the multiplication of two matrices order matters. This means if X * Y is valid, it does not mean Y * X is valid. The number of columns of the matrix on the left side of the multiplication sign must equal to the number of rows of the matrix on the right side.
First, let us create a tensor X with size 2X3. Then, let us create another tensor Y with size 3X2. Since the number of columns of X is equal to the number of rows of Y. We are able to perform the multiplication.
We use torch.mm() for calculating the multiplication between tensors with different sizes.
Can you write Python code for it?


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:

Simple dataset

Let us try to create our own dataset class.
Can you write Python code for it?

# Define class for dataset

class toy_set(Dataset):

    # Constructor with defult values
    def __init__(self, length = 100, transform = None):
        self.len = length
        self.x = 2 * torch.ones(length, 2)
        self.y = torch.ones(length, 1)
        self.transform = transform

    # Getter
    def __getitem__(self, index):
        sample = self.x[index], self.y[index]
        if self.transform:
            sample = self.transform(sample)
        return sample

    # Get Length
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
<!--Empty Space for separating topics-->

Now, create a transform object:.
Can you write Python code for it?

# Create an add_mult transform object, and an toy_set object

a_m = add_mult()
data_set = toy_set()


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
As the result, x has been added by 1 and y has been multiplied by 2, as [2, 2] + 1 = [3, 3] and [1] x 2 = [2]

We can apply the transform object every time we create a new toy_set object? Remember, we have the constructor in toy_set class with the parameter transform = None.
When we create a new object using the constructor, we can assign the transform object to the parameter transform, as the following code demonstrates.
Can you write Python code for it?

# Create a new data_set object with add_mult object as transform

cust_data_set = toy_set(transform = a_m)


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
Assign the outputs of the original dataset to x and y. Then, apply the transform add_mult to the dataset and output the values as x_ and y_, respectively:
Can you write Python code for it?

# Use loop to print out first 10 elements in dataset

for i in range(10):
    x, y = data_set[i]
    print('Index: ', i, 'Original x: ', x, 'Original y: ', y)
    x_, y_ = a_m(data_set[i])
    print('Index: ', i, 'Transformed x_:', x_, 'Transformed y_:', y_)


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
Double-click here for the solution.

Transforms

You can also create a class for transforming the data. In this case, we will try to add 1 to x and multiply y by 2:
Can you write Python code for it?

# Create tranform class add_mult

class add_mult(object):

    # Constructor
    def __init__(self, addx = 1, muly = 2):
        self.addx = addx
        self.muly = muly

    # Executor
    def __call__(self, sample):
        x = sample[0]
        y = sample[1]
        x = x + self.addx
        y = y * self.muly
        sample = x, y
        return sample


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
Let us print out the first 3 elements in different toy_set datasets in order to compare the output after different transforms have been applied:
Can you write Python code for it?

# Use loop to print out first 3 elements in dataset

for i in range(3):
    x, y = data_set[i]
    print('Index: ', i, 'Original x: ', x, 'Original y: ', y)
    x_, y_ = cust_data_set[i]
    print('Index: ', i, 'Transformed x_:', x_, 'Transformed y_:', y_)
    x_co, y_co = compose_data_set[i]
    print('Index: ', i, 'Compose Transformed x_co: ', x_co ,'Compose Transformed y_co: ',y_co)


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
Now let us try to combine the transforms add_mult and mult
Can you write Python code for it?

# Combine the add_mult() and mult()

data_transform = transforms.Compose([add_mult(), mult()])
print("The combination of transforms (Compose): ", data_transform)


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
The new Compose object will perform each transform concurrently as shown in this figure:

Now we can pass the new Compose object (The combination of methods add_mult() and mult) to the constructor for creating toy_set object.
Can you write Python code for it?

# Create a new toy_set object with compose object as transform

compose_data_set = toy_set(transform = data_transform)


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
The result is the same as the previous method.

Practice

Try to construct your own my_add_mult class by adding x and y with 2 and multiply both x and y by 10. Apply it on a new toy_set object, and print out the first 3 elements from the transformed dataset.
Can you write Python code for it?

# Practice: Construct your own my_add_mult transform. Apply my_add_mult on a new toy_set object. Print out the first three elements from the transformed dataset.

# Type your code here.


I am working on the file "DL0110EN/1.3.1_simple_data_set_v2.ipynb".
The first task is:
This applied a_m object (a transform method) to every element in cust_data_set as initialized. Let us print out the first 10 elements in cust_data_set in order to see whether the a_m applied on cust_data_set
Can you write Python code for it?

# Use loop to print out first 10 elements in dataset

for i in range(10):
    x, y = data_set[i]
    print('Index: ', i, 'Original x: ', x, 'Original y: ', y)
    x_, y_ = cust_data_set[i]
    print('Index: ', i, 'Transformed x_:', x_, 'Transformed y_:', y_)


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:

 Torchvision Transforms

We can apply some image transform functions on the MNIST dataset.
As an example, the images in the MNIST dataset can be cropped and converted to a tensor. We can use transform.Compose we learned from the previous lab to combine the two transform functions.
Can you write Python code for it?

# Combine two transforms: crop and convert to tensor. Apply the compose to MNIST dataset

croptensor_data_transform = transforms.Compose([transforms.CenterCrop(20), transforms.ToTensor()])
dataset = dsets.MNIST(root = './data', train = False, download = True, transform = croptensor_data_transform)
print("The shape of the first element in the first tuple: ", dataset[0][0].shape)


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:
As shown in the output, the first element in the tuple is a cuboid tensor. As you can see, there is a dimension with only size 1, so basically, it is a rectangular tensor.

The second element in the tuple is a number tensor, which indicate the real number the image shows. As the second element in the tuple is tensor(7), the image should show a hand-written 7.

Let us plot the first element in the dataset:
Can you write Python code for it?

# Plot the first element in the dataset

show_data(dataset[0])


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:
As we can see, it is a 7.
Plot the second sample:
Can you write Python code for it?

# Plot the second element in the dataset

show_data(dataset[1])


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:
Each element of the dataset object contains a tuple. Let us see whether the first element in the dataset is a tuple and what is in it.
Can you write Python code for it?

# Examine whether the elements in dataset MNIST are tuples, and what is in the tuple?

print("Type of the first element: ", type(dataset[0]))
print("The length of the tuple: ", len(dataset[0]))
print("The shape of the first element in the tuple: ", dataset[0][0].shape)
print("The type of the first element in the tuple", type(dataset[0][0]))
print("The second element in the tuple: ", dataset[0][1])
print("The type of the second element in the tuple: ", type(dataset[0][1]))
print("As the result, the structure of the first element in the dataset is (tensor([1, 28, 28]), tensor(7)).")


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:
This is the function for displaying images.
Can you write Python code for it?

# Show data by diagram

def show_data(data_sample, shape = (28, 28)):
    plt.imshow(data_sample[0].numpy().reshape(shape), cmap='gray')
    plt.title('y = ' + str(data_sample[1].item()))


I am working on the file "DL0110EN/1.3.2_pre-Built Datasets_and_transforms_v2.ipynb".
The first task is:
We can import a prebuilt dataset. In this case, use MNIST. You'll work with several of these parameters later by placing a transform object in the argument transform.
Can you write Python code for it?

# Import the prebuilt dataset into variable dataset

dataset = dsets.MNIST(
    root = './data',
    train = False,
    download = True,
    transform = transforms.ToTensor()
)


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:

Derivatives

Let us create the tensor x and set the parameter requires_grad to true because you are going to take the derivative of the tensor.
Can you write Python code for it?

# Create a tensor x

x = torch.tensor(2.0, requires_grad = True)
print("The tensor x: ", x)


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:

Practice

Try to determine partial derivative  $u$ of the following function where $u=2$ and $v=1$: $ f=uv+(uv)^2$
Can you write Python code for it?

# Practice: Calculate the derivative of f = u * v + (u * v) ** 2 at u = 2, v = 1

# Type the code here


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:
Double-click here for the solution.

Partial Derivatives

We can also calculate Partial Derivatives. Consider the function: $f(u,v)=vu+u^{2}$
Let us create u tensor, v tensor and  f tensor
Can you write Python code for it?

# Calculate f(u, v) = v * u + u^2 at u = 1, v = 2

u = torch.tensor(1.0,requires_grad=True)
v = torch.tensor(2.0,requires_grad=True)
f = u * v + u ** 2
print("The result of v * u + u^2: ", f)


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:
The equation is given by:
$\frac{\mathrm{\partial f(u,v)}}{\partial {v}}=u$
$\frac{\mathrm{\partial f(u=1,v=2)}}{\partial {v}}=1$
<!--Empty Space for separating topics-->

Calculate the derivative with respect to a function with multiple values as follows. You use the sum trick to produce a scalar valued function and then take the gradient:
Can you write Python code for it?

# Calculate the derivative with multiple values

x = torch.linspace(-10, 10, 10, requires_grad = True)
Y = x ** 2
y = torch.sum(x ** 2)


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:
The function is in the following form:
$y=x^{2}+2x+1$
The derivative is given by:
$\frac{\mathrm{dy(x)}}{\mathrm{dx}}=2x+2$
$\frac{\mathrm{dy(x=2)}}{\mathrm{dx}}=2(2)+2=6$

Practice

Determine the derivative of $ y = 2x^3+x $ at $x=1$
Can you write Python code for it?

# Practice: Calculate the derivative of y = 2x^3 + x at x = 1

# Type your code here


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:
We can plot the function  and its derivative
Can you write Python code for it?

# Take the derivative with respect to multiple value. Plot out the function and its derivative

y.backward()

plt.plot(x.detach().numpy(), Y.detach().numpy(), label = 'function')
plt.plot(x.detach().numpy(), x.grad.detach().numpy(), label = 'derivative')
plt.xlabel('x')
plt.legend()
plt.show()


I am working on the file "DL0110EN/1.3derivativesandGraphsinPytorch_v2.ipynb".
The first task is:
the expression is given by:
$\frac{\mathrm{\partial f(u,v)}}{\partial {u}}=v+2u$
$\frac{\mathrm{\partial f(u=1,v=2)}}{\partial {u}}=2+2(1)=4$

Now, take the derivative with respect to v:
Can you write Python code for it?

# Calculate the derivative with respect to v

print("The partial derivative with respect to u: ", v.grad)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:

Prediction

Let us create the following expressions:

First, define the parameters:
Can you write Python code for it?

# Define w = 2 and b = -1 for y = wx + b

w = torch.tensor(2.0, requires_grad = True)
b = torch.tensor(-1.0, requires_grad = True)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:

Practice

Create an object lr1 from the class we created before and make a prediction by using the following tensor:
Can you write Python code for it?

# Practice: Use the LR class to create a model and make a prediction of the following tensor.

x = torch.tensor([[1.0], [2.0], [3.0]])


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
<!--Empty Space for separating topics-->

Let us try to make a prediction of a single input sample.
Can you write Python code for it?

# Try our customize linear regression model with single input

x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
<!--Empty Space for separating topics-->

Now, let us try another example with multiple samples.
Can you write Python code for it?

# Try our customize linear regression model with multiple input

x = torch.tensor([[1.0], [2.0]])
yhat = lr(x)
print("The prediction: ", yhat)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:

Now, let us try to make the prediction for multiple inputs:

Let us construct the x tensor first. Check the shape of x.
Can you write Python code for it?

# Create x Tensor and check the shape of x tensor

x = torch.tensor([[1.0], [2.0]])
print("The shape of x: ", x.shape)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
Create an object by using the constructor. Print out the parameters we get and the model.
Can you write Python code for it?

# Create the linear regression model. Print out the parameters.

lr = LR(1, 1)
print("The parameters: ", list(lr.parameters()))
print("Linear model: ", lr.linear)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
Double-click here for the solution.

Build Custom Modules

Now, let's build a custom module. We can make more complex models by using this method later on.
First, import the following library.
Can you write Python code for it?

# Library for this section

from torch import nn


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
Now make the prediction:
Can you write Python code for it?

# Make the prediction of y = 2x - 1 at x = [1, 2]

yhat = forward(x)
print("The prediction: ", yhat)


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
The result is the same as what it is in the image above.

Practice

Make a prediction of the following x tensor using the w and b from above.
Can you write Python code for it?


I am working on the file "DL0110EN/2.1Prediction1Dregression_v2.ipynb".
The first task is:
This is equivalent to the following expression:

Now let us make a single prediction at x = [[1.0]].
Can you write Python code for it?

# Make the prediction at x = [[1.0]]

x = torch.tensor([[1.0]])
yhat = lr(x)
print("The prediction: ", yhat)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:

Make Some Data

Import PyTorch library:
Can you write Python code for it?

# Import the library PyTorch

import torch


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:

Create the Model and Cost Function (Total Loss)

In this section, let us create the model and the cost function (total loss) we are going to use to train the model and evaluate the result.
First, define the forward function $y=w*x$. (We will add the bias in the next lab.)
Can you write Python code for it?

# Create forward function for prediction

def forward(x):
    return w * x


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:

Train the Model

Let us define a function for training the model. The steps will be described in the comments.
Can you write Python code for it?

# Define a function for train the model

def train_model(iter):
    for epoch in range (iter):

        # make the prediction as we learned in the last lab
        Yhat = forward(X)

        # calculate the iteration
        loss = criterion(Yhat,Y)

        # plot the diagram for us to have a better idea
        gradient_plot(Yhat, w, loss.item(), epoch)

        # store the loss into list
        LOSS.append(loss)

        # backward pass: compute gradient of the loss with respect to all the learnable parameters
        loss.backward()

        # updata parameters
        w.data = w.data - lr * w.grad.data

        # zero the gradients before running the backward pass
        w.grad.data.zero_()


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:

Practice

Create a new learnable parameter w with an initial value of -15.0.
Can you write Python code for it?

# Practice: Create w with the inital value of -15.0

# Type your code here


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Create a plot_diagram object to visualize the data space and the parameter space for each iteration during training:
Can you write Python code for it?

gradient_plot = plot_diagram(X, Y, w, stop = 5)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Define the cost or criterion function using MSE (Mean Square Error):
Can you write Python code for it?

# Create the MSE function for evaluate the result.

def criterion(yhat, y):
    return torch.mean((yhat - y) ** 2)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Define the learning rate lr and an empty list LOSS to record the loss for each iteration:
Can you write Python code for it?

# Create Learning Rate and an empty list to record the loss for each iteration

lr = 0.1
LOSS = []


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Double-click here for the solution.

Write your own my_train_model function with loss list LOSS2. And run it with 4 iterations.
Can you write Python code for it?

# Practice: Create your own my_train_model

gradient_plot1 = plot_diagram(X, Y, w, stop = 15)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Double-click here for the solution.

Create an empty list LOSS2:
Can you write Python code for it?

# Practice: Create LOSS2 list

# Type your code here


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Generate values from -3 to 3 that create a line with a slope of -3. This is the line you will estimate.
Can you write Python code for it?

# Create the f(X) with a slope of -3

X = torch.arange(-3, 3, 0.1).view(-1, 1)
f = -3 * X


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Let us plot the line.
Can you write Python code for it?

# Plot the line with blue

plt.plot(X.numpy(), f.numpy(), label = 'f')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Let us try to run 4 iterations of gradient descent:
Can you write Python code for it?

# Give 4 iterations for training the model here.

train_model(4)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Now, we create a model parameter by setting the argument requires_grad to  True because the system must learn it.
Can you write Python code for it?

w = torch.tensor(-10.0, requires_grad = True)


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
Plot the cost for each iteration:
Can you write Python code for it?

# Plot the loss for each iteration

plt.plot(LOSS)
plt.tight_layout()
plt.xlabel("Epoch/Iterations")
plt.ylabel("Cost")


I am working on the file "DL0110EN/2.2.1linear_regression_one_parameter_v2.ipynb".
The first task is:
The class plot_diagram helps us to visualize the data space and the parameter space during training and has nothing to do with PyTorch.
Can you write Python code for it?

# The class for plotting

class plot_diagram():

    # Constructor
    def __init__(self, X, Y, w, stop, go = False):
        start = w.data
        self.error = []
        self.parameter = []
        self.X = X.numpy()
        self.Y = Y.numpy()
        self.parameter_values = torch.arange(start, stop)
        self.Loss_function = [criterion(forward(X), Y) for w.data in self.parameter_values]
        w.data = start

    # Executor
    def __call__(self, Yhat, w, error, n):
        self.error.append(error)
        self.parameter.append(w.data)
        plt.subplot(212)
        plt.plot(self.X, Yhat.detach().numpy())
        plt.plot(self.X, self.Y,'ro')
        plt.xlabel("A")
        plt.ylim(-20, 20)
        plt.subplot(211)
        plt.title("Data Space (top) Estimated Line (bottom) Iteration " + str(n))
        plt.plot(self.parameter_values.numpy(), self.Loss_function)
        plt.plot(self.parameter, self.error, 'ro')
        plt.xlabel("B")
        plt.figure()

    # Destructor
    def __del__(self):
        plt.close('all')


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:

Make Some Data

Import PyTorch:
Can you write Python code for it?


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:

Create the Model and Cost Function (Total Loss)

Define the forward function:
Can you write Python code for it?

# Define the forward function

def forward(x):
    return w * x + b


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Create a <code> plot_error_surfaces</code> object to visualize the data space and the parameter space during training:
Can you write Python code for it?

# Create plot_error_surfaces for viewing the data

get_surface = plot_error_surfaces(15, 15, X, Y, 30)


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Double-click here for the solution.

Plot the LOSS and LOSS2
Can you write Python code for it?

# Practice: Plot the LOSS and LOSS2 in order to compare the Total Loss

# Type your code here


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Now, add some noise to the data:
Can you write Python code for it?

# Add noise

Y = f + 0.1 * torch.randn(X.size())


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Plot the line and Y with noise:
Can you write Python code for it?

# Plot out the line and the points with noise

plt.plot(X.numpy(), Y.numpy(), 'rx', label = 'y')
plt.plot(X.numpy(), f.numpy(), label = 'f')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Plot total loss/cost surface with loss values for different parameters in red:
Can you write Python code for it?

# Plot out the Loss Result

get_surface.final_plot()
plt.plot(LOSS)
plt.tight_layout()
plt.xlabel("Epoch/Iterations")
plt.ylabel("Cost")


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Run 15 iterations of gradient descent: <b>bug</b> data space is 1 iteration ahead of parameter space
Can you write Python code for it?

# Train the model with 15 iterations

train_model(15)


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Set the learning rate to 0.1 and create an empty list LOSS for storing the loss for each iteration.
Can you write Python code for it?

# Define learning rate and create an empty list for containing the loss for each iteration.

lr = 0.1
LOSS = []


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
Start with generating values from -3 to 3 that create a line with a slope of 1 and a bias of -1. This is the line that you need to estimate.
Can you write Python code for it?

# Create f(X) with a slope of 1 and a bias of -1

X = torch.arange(-3, 3, 0.1).view(-1, 1)
f = 1 * X - 1


I am working on the file "DL0110EN/2.2.2_training_slope_and_bias_v2.ipynb".
The first task is:
The class plot_error_surfaces is just to help you visualize the data space and the parameter space during training and has nothing to do with PyTorch.
Can you write Python code for it?

# The class for plot the diagram

class plot_error_surfaces(object):

    # Constructor
    def __init__(self, w_range, b_range, X, Y, n_samples = 30, go = True):
        W = np.linspace(-w_range, w_range, n_samples)
        B = np.linspace(-b_range, b_range, n_samples)
        w, b = np.meshgrid(W, B)
        Z = np.zeros((30,30))
        count1 = 0
        self.y = Y.numpy()
        self.x = X.numpy()
        for w1, b1 in zip(w, b):
            count2 = 0
            for w2, b2 in zip(w1, b1):
                Z[count1, count2] = np.mean((self.y - w2 * self.x + b2) ** 2)
                count2 += 1
            count1 += 1
        self.Z = Z
        self.w = w
        self.b = b
        self.W = []
        self.B = []
        self.LOSS = []
        self.n = 0
        if go == True:
            plt.figure()
            plt.figure(figsize = (7.5, 5))
            plt.axes(projection='3d').plot_surface(self.w, self.b, self.Z, rstride = 1, cstride = 1,cmap = 'viridis', edgecolor = 'none')
            plt.title('Cost/Total Loss Surface')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.show()
            plt.figure()
            plt.title('Cost/Total Loss Surface Contour')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.contour(self.w, self.b, self.Z)
            plt.show()

    # Setter
    def set_para_loss(self, W, B, loss):
        self.n = self.n + 1
        self.W.append(W)
        self.B.append(B)
        self.LOSS.append(loss)

    # Plot diagram
    def final_plot(self):
        ax = plt.axes(projection = '3d')
        ax.plot_wireframe(self.w, self.b, self.Z)
        ax.scatter(self.W,self.B, self.LOSS, c = 'r', marker = 'x', s = 200, alpha = 1)
        plt.figure()
        plt.contour(self.w,self.b, self.Z)
        plt.scatter(self.W, self.B, c = 'r', marker = 'x')
        plt.xlabel('w')
        plt.ylabel('b')
        plt.show()

    # Plot diagram
    def plot_ps(self):
        plt.subplot(121)
        plt.ylim
       ...


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

Train the Model: Batch Gradient Descent

Create model parameters w, b by setting the argument requires_grad to True because the system must learn it.
Can you write Python code for it?

# Define the parameters w, b for y = wx + b

w = torch.tensor(-15.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

Make Some Data

Set random seed:
Can you write Python code for it?


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

Create the Model and Cost Function (Total Loss)

Define the forward function:
Can you write Python code for it?


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

Train the Model: Stochastic Gradient Descent

Create a plot_error_surfaces object to visualize the data space and the parameter space during training:
Can you write Python code for it?

# Create plot_error_surfaces for viewing the data

get_surface = plot_error_surfaces(15, 13, X, Y, 30, go = False)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

SGD with Dataset DataLoader

Import the module for building a dataset class:
Can you write Python code for it?

# Import the library for DataLoader

from torch.utils.data import Dataset, DataLoader


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:

Practice

For practice, try to use SGD with DataLoader to train model with 10 iterations. Store the total loss in LOSS. We are going to use it in the next question.
Can you write Python code for it?

# Practice: Use SGD with trainloader to train model and store the total loss in LOSS

LOSS = []
w = torch.tensor(-12.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Compare the loss of both batch gradient decent as SGD. Note that SGD converges to a minimum faster, that is, it decreases faster.
Can you write Python code for it?

# Plot the LOSS_BGD and LOSS_Loader

plt.plot(LOSS_BGD,label="Batch Gradient Descent")
plt.plot(LOSS_Loader,label="Stochastic Gradient Descent with DataLoader")
plt.xlabel('epoch')
plt.ylabel('Cost/ total loss')
plt.legend()
plt.show()


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Create a <code> plot_error_surfaces</code> object to visualize the data space and the parameter space during training:
Can you write Python code for it?

# Create plot_error_surfaces for viewing the data

get_surface = plot_error_surfaces(15, 13, X, Y, 30)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Create a DataLoader object by using the constructor:
Can you write Python code for it?

# Create DataLoader

trainloader = DataLoader(dataset = dataset, batch_size = 1)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Create a plot_error_surfaces object to visualize the data space and the parameter space during training:
Can you write Python code for it?


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Create a dataset class:
Can you write Python code for it?

# Dataset Class

class Data(Dataset):

    # Constructor
    def __init__(self):
        self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
        self.y = 1 * self.x - 1
        self.len = self.x.shape[0]

    # Getter
    def __getitem__(self,index):
        return self.x[index], self.y[index]

    # Return the length
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Create a dataset object and check the length of the dataset.
Can you write Python code for it?

# Create the dataset and check the length

dataset = Data()
print("The length of dataset: ", len(dataset))


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Define train_model function for train the model.
Can you write Python code for it?

# The function for training the model

def train_model(iter):

    # Loop
    for epoch in range(iter):

        # make a prediction
        Yhat = forward(X)

        # calculate the loss
        loss = criterion(Yhat, Y)

        # Section for plotting
        get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())
        get_surface.plot_ps()

        # store the loss in the list LOSS_BGD
        LOSS_BGD.append(loss)

        # backward pass: compute gradient of the loss with respect to all the learnable parameters
        loss.backward()

        # update parameters slope and bias
        w.data = w.data - lr * w.grad.data
        b.data = b.data - lr * b.grad.data

        # zero the gradients before running the backward pass
        w.grad.data.zero_()
        b.grad.data.zero_()


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Define train_model_SGD function for training the model.
Can you write Python code for it?

# The function for training the model

LOSS_SGD = []
w = torch.tensor(-15.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)

def train_model_SGD(iter):

    # Loop
    for epoch in range(iter):

        # SGD is an approximation of out true total loss/cost, in this line of code we calculate our true loss/cost and store it
        Yhat = forward(X)

        # store the loss
        LOSS_SGD.append(criterion(Yhat, Y).tolist())

        for x, y in zip(X, Y):

            # make a pridiction
            yhat = forward(x)

            # calculate the loss
            loss = criterion(yhat, y)

            # Section for plotting
            get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())

            # backward pass: compute gradient of the loss with respect to all the learnable parameters
            loss.backward()

            # update parameters slope and bias
            w.data = w.data - lr * w.grad.data
            b.data = b.data - lr * b.grad.data

            # zero the gradients before running the backward pass
            w.grad.data.zero_()
            b.grad.data.zero_()

        #plot surface and data space after each epoch
        get_surface.plot_ps()


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Define the cost or criterion function (MSE):
Can you write Python code for it?

# Define the MSE Loss function

def criterion(yhat, y):
    return torch.mean((yhat - y) ** 2)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Double-click <b>here</b> for the solution.
<!--
def my_train_model(epochs):
    for epoch in range(epochs):
        Yhat = forward(X)
        LOSS.append(criterion(Yhat, X))
        for x, y in trainloader:
            yhat = forward(x)
            loss = criterion(yhat, y)
            get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())
            loss.backward()
            w.data = w.data - lr * w.grad.data
            b.data = b.data - lr * b.grad.data
            w.grad.data.zero_()
            b.grad.data.zero_()
        get_surface.plot_ps()
my_train_model(10)
-->

Plot the total loss
Can you write Python code for it?

# Practice: Plot the total loss using LOSS

# Type your code here


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Generate values from -3 to 3 that create a line with a slope of 1 and a bias of -1. This is the line that you need to estimate. Add some noise to the data:
Can you write Python code for it?

# Setup the actual data and simulated data

X = torch.arange(-3, 3, 0.1).view(-1, 1)
f = 1 * X - 1
Y = f + 0.1 * torch.randn(X.size())


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Obtain the first training point:
Can you write Python code for it?

# Print the first point

x, y = dataset[0]
print("(", x, ", ", y, ")")


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Plot the results:
Can you write Python code for it?

# Plot out the data dots and line

plt.plot(X.numpy(), Y.numpy(), 'rx', label = 'y')
plt.plot(X.numpy(), f.numpy(), label = 'f')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Run 10 epochs of batch gradient descent: <b>bug</b> data space is 1 iteration ahead of parameter space.
Can you write Python code for it?

# Train the model with 10 iterations

train_model(10)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Run 10 epochs of stochastic gradient descent: <b>bug</b> data space is 1 iteration ahead of parameter space.
Can you write Python code for it?

# Run 10 iterations

train_model_DataLoader(10)


I am working on the file "DL0110EN/2.3.1stochastic_gradient_descent_v2.ipynb".
The first task is:
Similarly, obtain the first three training points:
Can you write Python code for it?

# Print the first 3 point

x, y = dataset[0:3]
print("The first 3 x: ", x)
print("The first 3 y: ", y)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
<!--Empty Space for separating topics-->

<h2 id="Makeup_Data">Make Some Data </h2>

Import PyTorch and set random seed:
Can you write Python code for it?

# Import PyTorch library

import torch
torch.manual_seed(1)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:

Mini Batch Gradient Descent: Batch Size Equals 5

Create a  plot_error_surfaces object to visualize the data space and the parameter space during training:
Can you write Python code for it?

# Create a plot_error_surfaces object.

get_surface = plot_error_surfaces(15, 13, X, Y, 30, go = False)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:

Create the Model and Cost Function (Total Loss)

Define the forward function:
Can you write Python code for it?

# Define the prediction function

def forward(x):
    return w * x + b


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:

 Stochastic Gradient Descent (SGD) with Dataset DataLoader

Create a plot_error_surfaces object to visualize the data space and the parameter space during training:
Can you write Python code for it?


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:

Train the Model: Batch Gradient Descent (BGD)

Define train_model_BGD function.
Can you write Python code for it?

# Define the function for training model

w = torch.tensor(-15.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)
lr = 0.1
LOSS_BGD = []

def train_model_BGD(epochs):
    for epoch in range(epochs):
        Yhat = forward(X)
        loss = criterion(Yhat, Y)
        LOSS_BGD.append(loss)
        get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())
        get_surface.plot_ps()
        loss.backward()
        w.data = w.data - lr * w.grad.data
        b.data = b.data - lr * b.grad.data
        w.grad.data.zero_()
        b.grad.data.zero_()


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:

Practice

Perform mini batch gradient descent with a batch size of 20. Store the total loss for each epoch in the list LOSS20.
Can you write Python code for it?

# Practice: Perform mini batch gradient descent with a batch size of 20.

dataset = Data()


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Create Data class
Can you write Python code for it?

# Create class Data

class Data(Dataset):

    # Constructor
    def __init__(self):
        self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
        self.y = 1 * X - 1
        self.len = self.x.shape[0]

    # Getter
    def __getitem__(self, index):
        return self.x[index], self.y[index]

    # Get length
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Create Data object and create a Dataloader object where the batch size equals 5:
Can you write Python code for it?

# Create DataLoader object and Data object

dataset = Data()
trainloader = DataLoader(dataset = dataset, batch_size = 5)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Create a <code> plot_error_surfaces</code> object to visualize the data space and the parameter space during training:
Can you write Python code for it?

# Create a plot_error_surfaces object.

get_surface = plot_error_surfaces(15, 13, X, Y, 30)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Create a dataset object and a dataloader object:
Can you write Python code for it?

# Create Data object and DataLoader object

dataset = Data()
trainloader = DataLoader(dataset = dataset, batch_size = 1)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Define train_model_Mini5 function to train the model.
Can you write Python code for it?

# Define train_model_Mini5 function

w = torch.tensor(-15.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)
LOSS_MINI5 = []
lr = 0.1

def train_model_Mini5(epochs):
    for epoch in range(epochs):
        Yhat = forward(X)
        get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), criterion(Yhat, Y).tolist())
        get_surface.plot_ps()
        LOSS_MINI5.append(criterion(forward(X), Y).tolist())
        for x, y in trainloader:
            yhat = forward(x)
            loss = criterion(yhat, y)
            get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())
            loss.backward()
            w.data = w.data - lr * w.grad.data
            b.data = b.data - lr * b.grad.data
            w.grad.data.zero_()
            b.grad.data.zero_()


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Define the cost or criterion function:
Can you write Python code for it?

# Define the cost function

def criterion(yhat, y):
    return torch.mean((yhat - y) ** 2)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Double-click <b>here</b> for the solution.
<!--
trainloader = DataLoader(dataset = dataset, batch_size = 20)
w = torch.tensor(-15.0, requires_grad = True)
b = torch.tensor(-10.0, requires_grad = True)

LOSS_MINI20 = []
lr = 0.1

def my_train_model(epochs):
    for epoch in range(epochs):
        Yhat = forward(X)
        get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), criterion(Yhat, Y).tolist())
        get_surface.plot_ps()
        LOSS_MINI20.append(criterion(forward(X), Y).tolist())
        for x, y in trainloader:
            yhat = forward(x)
            loss = criterion(yhat, y)
            get_surface.set_para_loss(w.data.tolist(), b.data.tolist(), loss.tolist())
            loss.backward()
            w.data = w.data - lr * w.grad.data
            b.data = b.data - lr * b.grad.data
            w.grad.data.zero_()
            b.grad.data.zero_()

my_train_model(10)
-->

Plot a graph that shows the LOSS results for all the methods.
Can you write Python code for it?

# Practice: Plot a graph to show all the LOSS functions

# Type your code here


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Import Dataset and DataLoader libraries
Can you write Python code for it?

# Import libraries

from torch.utils.data import Dataset, DataLoader


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Plot the loss for each epoch:
Can you write Python code for it?

# Plot out the LOSS for each method

plt.plot(LOSS_BGD,label = "Batch Gradient Descent")
plt.plot(LOSS_SGD,label = "Stochastic Gradient Descent")
plt.plot(LOSS_MINI5,label = "Mini-Batch Gradient Descent, Batch size: 5")
plt.plot(LOSS_MINI10,label = "Mini-Batch Gradient Descent, Batch size: 10")
plt.legend()


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Plot the results:
Can you write Python code for it?

# Plot the line and the data

plt.plot(X.numpy(), Y.numpy(), 'rx', label = 'y')
plt.plot(X.numpy(), f.numpy(), label = 'f')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Run 10 epochs of batch gradient descent: <b>bug</b> data space is 1 iteration ahead of parameter space.
Can you write Python code for it?

# Run train_model_BGD with 10 iterations

train_model_BGD(10)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
Run 10 epochs of mini-batch gradient descent: <b>bug</b> data space is 1 iteration ahead of parameter space.
Can you write Python code for it?

# Run train_model_Mini5 with 10 iterations.

train_model_Mini5(10)


# Run train_model_Mini5 with 10 iterations.

train_model_Mini10(10)


I am working on the file "DL0110EN/2.3.2mini-batch gradient descent_v2.ipynb".
The first task is:
The class plot_error_surfaces is just to help you visualize the data space and the parameter space during training and has nothing to do with PyTorch.
Can you write Python code for it?

# The class for plotting the diagrams

class plot_error_surfaces(object):

    # Constructor
    def __init__(self, w_range, b_range, X, Y, n_samples = 30, go = True):
        W = np.linspace(-w_range, w_range, n_samples)
        B = np.linspace(-b_range, b_range, n_samples)
        w, b = np.meshgrid(W, B)
        Z = np.zeros((30, 30))
        count1 = 0
        self.y = Y.numpy()
        self.x = X.numpy()
        for w1, b1 in zip(w, b):
            count2 = 0
            for w2, b2 in zip(w1, b1):
                Z[count1, count2] = np.mean((self.y - w2 * self.x + b2) ** 2)
                count2 += 1
            count1 += 1
        self.Z = Z
        self.w = w
        self.b = b
        self.W = []
        self.B = []
        self.LOSS = []
        self.n = 0
        if go == True:
            plt.figure()
            plt.figure(figsize = (7.5, 5))
            plt.axes(projection = '3d').plot_surface(self.w, self.b, self.Z, rstride = 1, cstride = 1, cmap = 'viridis', edgecolor = 'none')
            plt.title('Loss Surface')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.show()
            plt.figure()
            plt.title('Loss Surface Contour')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.contour(self.w, self.b, self.Z)
            plt.show()

     # Setter
    def set_para_loss(self, W, B, loss):
        self.n = self.n + 1
        self.W.append(W)
        self.B.append(B)
        self.LOSS.append(loss)

    # Plot diagram
    def final_plot(self):
        ax = plt.axes(projection = '3d')
        ax.plot_wireframe(self.w, self.b, self.Z)
        ax.scatter(self.W, self.B, self.LOSS, c = 'r', marker = 'x', s = 200, alpha = 1)
        plt.figure()
        plt.contour(self.w, self.b, self.Z)
        plt.scatter(self.W, self.B, c = 'r', marker = 'x')
        plt.xlabel('w')
        plt.ylabel('b')
        plt.show()

    # Plot diagram
    def plot_ps(self):
        plt.subplot(121)
        plt.ylim()
       ...


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:

Create the Model and Total Loss Function (Cost)

Create a linear regression class
Can you write Python code for it?

# Create a linear regression model class

from torch import nn, optim

class linear_regression(nn.Module):

    # Constructor
    def __init__(self, input_size, output_size):
        super(linear_regression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)

    # Prediction
    def forward(self, x):
        yhat = self.linear(x)
        return yhat


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
Create a dataset object:
Can you write Python code for it?

# Create dataset object

dataset = Data()


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
Plot out the data and the line.
Can you write Python code for it?

# Plot the data

plt.plot(dataset.x.numpy(), dataset.y.numpy(), 'rx', label = 'y')
plt.plot(dataset.x.numpy(), dataset.f.numpy(), label = 'f')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
PyTorch randomly initialises your model parameters. If we use those parameters, the result will not be very insightful as convergence will be extremely fast. So we will initialise the parameters such that they will take longer to converge, i.e. look cool
Can you write Python code for it?

# Customize the weight and bias

model.state_dict()['linear.weight'][0] = -15
model.state_dict()['linear.bias'][0] = -10


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
Remember to construct an optimizer you have to give it an iterable containing the parameters i.e. provide  model.parameters() as an input to the object constructor

Create a Dataloader object:
Can you write Python code for it?

# Create Dataloader object

trainloader = DataLoader(dataset = dataset, batch_size = 1)


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
The class plot_error_surfaces is just to help you visualize the data space and the parameter space during training and has nothing to do with PyTorch.
Can you write Python code for it?

# class for ploting

class plot_error_surfaces(object):

    # Constructor
    def __init__(self, w_range, b_range, X, Y, n_samples = 30, go = True):
        W = np.linspace(-w_range, w_range, n_samples)
        B = np.linspace(-b_range, b_range, n_samples)
        w, b = np.meshgrid(W, B)
        Z = np.zeros((30, 30))
        count1 = 0
        self.y = Y.numpy()
        self.x = X.numpy()
        for w1, b1 in zip(w, b):
            count2 = 0
            for w2, b2 in zip(w1, b1):
                Z[count1, count2] = np.mean((self.y - w2 * self.x + b2) ** 2)
                count2 += 1
            count1 += 1
        self.Z = Z
        self.w = w
        self.b = b
        self.W = []
        self.B = []
        self.LOSS = []
        self.n = 0
        if go == True:
            plt.figure()
            plt.figure(figsize = (7.5, 5))
            plt.axes(projection = '3d').plot_surface(self.w, self.b, self.Z, rstride = 1, cstride = 1, cmap = 'viridis', edgecolor = 'none')
            plt.title('Loss Surface')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.show()
            plt.figure()
            plt.title('Loss Surface Contour')
            plt.xlabel('w')
            plt.ylabel('b')
            plt.contour(self.w, self.b, self.Z)
            plt.show()

    # Setter
    def set_para_loss(self, model, loss):
        self.n = self.n + 1
        self.LOSS.append(loss)
        self.W.append(list(model.parameters())[0].item())
        self.B.append(list(model.parameters())[1].item())

    # Plot diagram
    def final_plot(self):
        ax = plt.axes(projection = '3d')
        ax.plot_wireframe(self.w, self.b, self.Z)
        ax.scatter(self.W, self.B, self.LOSS, c = 'r', marker = 'x', s = 200, alpha = 1)
        plt.figure()
        plt.contour(self.w, self.b, self.Z)
        plt.scatter(self.W, self.B, c = 'r', marker = 'x')
        plt.xlabel('w')
        plt.ylabel('b')
        plt.show()

    # Plot diagram
    def plot_ps(self):
       ...


I am working on the file "DL0110EN/2.4PyTorchway_v2.ipynb".
The first task is:
We will use PyTorch build-in functions to create a criterion function; this calculates the total loss or cost
Can you write Python code for it?

# Build in cost function

criterion = nn.MSELoss()


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:

Make Some Data

First, we'll create some artificial data in a dataset class. The class will include the option to produce training data or validation data. The training data will include outliers.
Can you write Python code for it?

# Create Data class

from torch.utils.data import Dataset, DataLoader

class Data(Dataset):

    # Constructor
    def __init__(self, train = True):
            self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
            self.f = -3 * self.x + 1
            self.y = self.f + 0.1 * torch.randn(self.x.size())
            self.len = self.x.shape[0]

            #outliers
            if train == True:
                self.y[0] = 0
                self.y[50:55] = 20
            else:
                pass

    # Getter
    def __getitem__(self, index):
        return self.x[index], self.y[index]

    # Get Length
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:

Train different models  for different Hyperparameters

Try different values of learning rates, perform stochastic gradient descent, and save the results on the training data and validation data. Finally, save each model in a list.
Can you write Python code for it?

# Define the train model function and train the model

def train_model_with_lr (iter, lr_list):

    # iterate through different learning rates
    for i, lr in enumerate(lr_list):
        model = linear_regression(1, 1)
        optimizer = optim.SGD(model.parameters(), lr = lr)
        for epoch in range(iter):
            for x, y in trainloader:
                yhat = model(x)
                loss = criterion(yhat, y)
                optimizer.zero_grad()
                loss.backward()
                optimizer.step()

        # train data
        Yhat = model(train_data.x)
        train_loss = criterion(Yhat, train_data.y)
        train_error[i] = train_loss.item()

        # validation data
        Yhat = model(val_data.x)
        val_loss = criterion(Yhat, val_data.y)
        validation_error[i] = val_loss.item()
        MODELS.append(model)

train_model_with_lr(10, learning_rates)


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:

Practice

The object good_model is the best performing model. Use the train loader to get the data samples x and y. Produce an estimate for yhat and print it out for every sample in a for a loop. Compare it to the actual prediction y.
Can you write Python code for it?

# Practice: Use the train loader to get the data samples x and y. Produce yhat. Compare y and yhat.

good_model = MODELS[2]


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:
Create the criterion function and a DataLoader object:
Can you write Python code for it?

# Create MSELoss function and DataLoader

criterion = nn.MSELoss()
trainloader = DataLoader(dataset = train_data, batch_size = 1)


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:
Create two objects: one that contains training data and a second that contains validation data. Assume that the training data has the outliers.
Can you write Python code for it?

# Create training dataset and validation dataset

train_data = Data()
val_data = Data(train = False)


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:
Overlay the training points in red over the function that generated the data. Notice the outliers at x=-3 and around x=2:
Can you write Python code for it?

# Plot out training points

plt.plot(train_data.x.numpy(), train_data.y.numpy(), 'xr')
plt.plot(train_data.x.numpy(), train_data.f.numpy())
plt.show()


I am working on the file "DL0110EN/2.5.1_training_and_validation_v2.ipynb".
The first task is:
Produce a prediction by using the validation data for each model:
Can you write Python code for it?

# Plot the predictions

i = 0
for model, learning_rate in zip(MODELS, learning_rates):
    yhat = model(val_data.x)
    plt.plot(val_data.x.numpy(), yhat.detach().numpy(), label = 'lr:' + str(learning_rate))
    print('i', yhat.detach().numpy()[0:3])
plt.plot(val_data.x.numpy(), val_data.f.numpy(), 'or', label = 'validation data')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()


I am working on the file "DL0110EN/2.5.2_early_stopping_v2.ipynb".
The first task is:

Make Some Data

First let's create some artificial data, in a dataset class.  The class will include the option to produce training data or validation data. The training data includes outliers.
Can you write Python code for it?

# Create Data Class

class Data(Dataset):

    # Constructor
    def __init__(self, train = True):
        if train == True:
            self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
            self.f = -3 * self.x + 1
            self.y = self.f + 0.1 * torch.randn(self.x.size())
            self.len = self.x.shape[0]
            if train == True:
                self.y[50:] = 20
        else:
            self.x = torch.arange(-3, 3, 0.1).view(-1, 1)
            self.y = -3 * self.x + 1
            self.len = self.x.shape[0]

    # Getter
    def __getitem__(self, index):
        return self.x[index], self.y[index]

    # Get Length
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/2.5.2_early_stopping_v2.ipynb".
The first task is:

Create a Linear Regression Class, Object, Data Loader, Criterion Function

Create linear regression model class.
Can you write Python code for it?

# Create linear regression model class

from torch import nn

class linear_regression(nn.Module):

    # Constructor
    def __init__(self, input_size, output_size):
        super(linear_regression, self).__init__()
        self.linear = nn.Linear(input_size, output_size)

    # Predition
    def forward(self, x):
        yhat = self.linear(x)
        return yhat


I am working on the file "DL0110EN/2.5.2_early_stopping_v2.ipynb".
The first task is:

View Results

View the  loss for every iteration on the training set and validation set.
Can you write Python code for it?

# Plot the loss

plt.plot(LOSS_TRAIN, label = 'training loss')
plt.plot(LOSS_VAL, label = 'validation loss')
plt.xlabel("epochs")
plt.ylabel("Loss")
plt.legend(loc = 'upper right')
plt.show()