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

Neural Network Module and Function for Training

Create Neural Network Module using ModuleList()
Can you write Python code for it?

# Create dataset object

class Net(nn.Module):

    # Constructor
    def __init__(self, Layers):
        super(Net, self).__init__()
        self.hidden = nn.ModuleList()
        for input_size, output_size in zip(Layers, Layers[1:]):
            self.hidden.append(nn.Linear(input_size, output_size))

    # Prediction
    def forward(self, activation):
        L = len(self.hidden)
        for (l, linear_transform) in zip(range(L), self.hidden):
            if l < L - 1:
                activation = F.relu(linear_transform(activation))
            else:
                activation = linear_transform(activation)
        return activation


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

Compare Results of Different Momentum Terms

The plot below compares results of different momentum terms. We see that in general. The Cost decreases proportionally to the momentum term, but larger momentum terms lead to larger oscillations. While the momentum term decreases faster, it seems that a momentum term of 0.2 reaches the smallest value for the cost.
Can you write Python code for it?

# Plot the Loss result for each term

for key, value in Results.items():
    plt.plot(value['Loss'],label = key)
    plt.legend()
    plt.xlabel('epoch')
    plt.ylabel('Total Loss or Cost')


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Create a  network to classify three classes with 1 hidden layer with 50 neurons and a momentum value of zero.
Can you write Python code for it?

# Train a model with 1 hidden layer and 50 neurons

Layers = [2, 50, 3]
model = Net(Layers)
learning_rate = 0.10
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate)
train_loader = DataLoader(dataset = data_set, batch_size = 20)
criterion = nn.CrossEntropyLoss()
Results["momentum 0"] = train(data_set, model, criterion, train_loader, optimizer, epochs = 100)
plot_decision_regions_3class(model, data_set)


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Create a network to classify three classes with 1 hidden layer with 50 neurons and a momentum value of 0.2.
Can you write Python code for it?

# Train a model with 1 hidden layer and 50 neurons with 0.2 momentum

Layers = [2, 50, 3]
model = Net(Layers)
learning_rate = 0.10
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate, momentum = 0.2)
train_loader = DataLoader(dataset = data_set, batch_size = 20)
criterion = nn.CrossEntropyLoss()
Results["momentum 0.2"] = train(data_set, model, criterion, train_loader, optimizer, epochs = 100)
plot_decision_regions_3class(model, data_set)


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Create a network to classify three classes with 1 hidden layer with 50 neurons and a momentum value of 0.4.
Can you write Python code for it?

# Train a model with 1 hidden layer and 50 neurons with 0.4 momentum

Layers = [2, 50, 3]
model = Net(Layers)
learning_rate = 0.10
optimizer = torch.optim.SGD(model.parameters(), lr = learning_rate, momentum = 0.4)
train_loader = DataLoader(dataset = data_set, batch_size = 20)
criterion = nn.CrossEntropyLoss()
Results["momentum 0.4"] = train(data_set, model, criterion, train_loader, optimizer, epochs = 100)
plot_decision_regions_3class(model, data_set)


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

# Create the dataset class

class Data(Dataset):

    #  modified from: http://cs231n.github.io/neural-networks-case-study/
    # Constructor
    def __init__(self, K = 3, N = 500):
        D = 2
        X = np.zeros((N * K, D)) # data matrix (each row = single example)
        y = np.zeros(N * K, dtype = 'uint8') # class labels
        for j in range(K):
          ix = range(N * j, N * (j + 1))
          r = np.linspace(0.0, 1, N) # radius
          t = np.linspace(j * 4, (j + 1) * 4, N) + np.random.randn(N) * 0.2 # theta
          X[ix] = np.c_[r * np.sin(t), r * np.cos(t)]
          y[ix] = j

        self.y = torch.from_numpy(y).type(torch.LongTensor)
        self.x = torch.from_numpy(X).type(torch.FloatTensor)
        self.len = y.shape[0]

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

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

    # Plot the diagram
    def plot_data(self):
        plt.plot(self.x[self.y[:] == 0, 0].numpy(), self.x[self.y[:] == 0, 1].numpy(), 'o', label = "y=0")
        plt.plot(self.x[self.y[:] == 1, 0].numpy(), self.x[self.y[:] == 1, 1].numpy(), 'ro', label = "y=1")
        plt.plot(self.x[self.y[:] == 2, 0].numpy(),self.x[self.y[:] == 2, 1].numpy(), 'go',label = "y=2")
        plt.legend()


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Create the function for training the model.
Can you write Python code for it?

# Define the function for training the model

def train(data_set, model, criterion, train_loader, optimizer, epochs = 100):
    LOSS = []
    ACC = []
    for epoch in range(epochs):
        for x, y in train_loader:
            optimizer.zero_grad()
            yhat = model(x)
            loss = criterion(yhat, y)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
        LOSS.append(loss.item())
        ACC.append(accuracy(model,data_set))

    results ={"Loss":LOSS, "Accuracy":ACC}
    fig, ax1 = plt.subplots()
    color = 'tab:red'
    ax1.plot(LOSS,color = color)
    ax1.set_xlabel('epoch', color = color)
    ax1.set_ylabel('total loss', color = color)
    ax1.tick_params(axis = 'y', color = color)

    ax2 = ax1.twinx()
    color = 'tab:blue'
    ax2.set_ylabel('accuracy', color = color)  # we already handled the x-label with ax1
    ax2.plot(ACC, color = color)
    ax2.tick_params(axis = 'y', color = color)
    fig.tight_layout()  # otherwise the right y-label is slightly clipped

    plt.show()
    return results


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Dictionary to contain different cost and  accuracy values for each epoch  for different values of the momentum parameter.
Can you write Python code for it?

# Initialize a dictionary to contain the cost and accuracy

Results = {"momentum 0": {"Loss": 0, "Accuracy:": 0}, "momentum 0.1": {"Loss": 0, "Accuracy:": 0}}


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
Functions used to plot:
Can you write Python code for it?

# Define a function for plot the decision region

def plot_decision_regions_3class(model,data_set):
    cmap_light = ListedColormap(['#FFAAAA', '#AAFFAA','#00AAFF'])
    cmap_bold = ListedColormap(['#FF0000', '#00FF00','#00AAFF'])
    X=data_set.x.numpy()
    y=data_set.y.numpy()
    h = .02
    x_min, x_max = X[:, 0].min()-0.1 , X[:, 0].max()+0.1
    y_min, y_max = X[:, 1].min()-0.1 , X[:, 1].max() +0.1
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h),np.arange(y_min, y_max, h))
    XX=torch.torch.Tensor(np.c_[xx.ravel(), yy.ravel()])
    _,yhat=torch.max(model(XX),1)
    yhat=yhat.numpy().reshape(xx.shape)
    plt.pcolormesh(xx, yy, yhat, cmap=cmap_light)
    plt.plot(X[y[:]==0,0],X[y[:]==0,1],'ro',label='y=0')
    plt.plot(X[y[:]==1,0],X[y[:]==1,1],'go',label='y=1')
    plt.plot(X[y[:]==2,0],X[y[:]==2,1],'o',label='y=2')
    plt.title("decision region")
    plt.legend()


I am working on the file "DL0110EN/5.4.3_NeuralNetworkswithMomentum.ipynb".
The first task is:
The  accuracy seems to be proportional to the momentum term.
Can you write Python code for it?

# Plot the Accuracy result for each term

for key, value in Results.items():
    plt.plot(value['Accuracy'],label= key)
    plt.legend()
    plt.xlabel('epoch')
    plt.ylabel('Accuracy')


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:

What is Convolution?

Convolution is a linear operation similar to a linear equation, dot product, or matrix multiplication. Convolution has several advantages for analyzing images. As discussed in the video, convolution preserves the relationship between elements, and it requires fewer parameters than other methods.
You can see the relationship between the different methods that you learned:
$$linear \ equation :y=wx+b$$
$$linear\ equation\ with\ multiple \ variables \ where \ \mathbf{x} \ is \ a \ vector \ \mathbf{y}=\mathbf{wx}+b$$
$$ \ matrix\ multiplication \ where \ \mathbf{X} \ in \ a \ matrix \ \mathbf{y}=\mathbf{wX}+\mathbf{b} $$
$$\ convolution \ where \ \mathbf{X} \ and \ \mathbf{Y} \ is \ a \ tensor \  \mathbf{Y}=\mathbf{w}*\mathbf{X}+\mathbf{b}$$
In convolution, the parameter w is called a kernel. You can perform convolution on images where you let the variable image denote the variable X and w denote the parameter.

Create a two-dimensional convolution object by using the constructor Conv2d, the parameter in_channels and out_channels will be used for this section, and the parameter kernel_size will be three.
Can you write Python code for it?

conv = nn.Conv2d(in_channels=1, out_channels=1,kernel_size=3)
conv


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:

Stride parameter

The parameter stride changes the number of shifts the kernel moves per iteration. As a result, the output size also changes and is given by the following formula:
$$M_{new}=\dfrac{M-K}{stride}+1$$
Create a convolution object with a stride of 2:
Can you write Python code for it?

conv3 = nn.Conv2d(in_channels=1, out_channels=1,kernel_size=2,stride=2)

conv3.state_dict()['weight'][0][0]=torch.tensor([[1.0,1.0],[1.0,1.0]])
conv3.state_dict()['bias'][0]=0.0
conv3.state_dict()


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:

The following equation provides the output:
$$M_{new}=M-K+1$$
$$M_{new}=4-2+1$$
$$M_{new}=3$$
The following animation illustrates the process: The first iteration of the kernel overlay of the images produces one output. As the kernel is of size K, there are M-K  elements for the kernel to move in the horizontal direction. The same logic applies to the vertical direction.

Perform the convolution and verify the size is correct:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:
Because the parameters in nn.Conv2d are randomly initialized and learned through training, give them some values.
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:
Create an image of size 2:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:
For an image with a size of 4, calculate the output size:
$$M_{new}=\dfrac{M-K}{stride}+1$$
$$M_{new}=\dfrac{4-2}{2}+1$$
$$M_{new}=2$$
The following animation illustrates the process: The first iteration of the kernel overlay of the images produces one output. Because the kernel is of size K, there are M-K=2 elements. The stride is 2 because it will move 2 elements at a time. As a result, you divide M-K by the stride value 2:
<img src = "https://ibm.box.com/shared/static/wq8wbqhm4824y1oxpdbol55q645gykg9.gif" width = 500, align = "center">
Perform the convolution and verify the size is correct:
Can you write Python code for it?

z3=conv3(image1)

print("z3:",z3)
print("shape:",z3.shape[2:4])


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:
Question: Without using the function, determine what the outputs values are as each element:
Double-click here for the solution.

Question: Use the following convolution object to perform convolution on the tensor   Image:
Can you write Python code for it?

conv = nn.Conv2d(in_channels=1, out_channels=1,kernel_size=3)
conv.state_dict()['weight'][0][0]=torch.tensor([[0,0,0],[0,0,0],[0,0.0,0]])
conv.state_dict()['bias'][0]=0.0


I am working on the file "DL0110EN/6.1.1What is Convolution.ipynb".
The first task is:
The process is summarized in the following  animation:

Practice Question

A kernel of zeros with a kernel size=3  is applied to the following image:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Create a Conv2d object with two inputs:
Can you write Python code for it?

conv3 = nn.Conv2d(in_channels=2, out_channels=1,kernel_size=3)


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Create a two-channel image and plot the results:
Can you write Python code for it?

image4=torch.zeros(1,2,5,5)
image4[0][0]=torch.ones(5,5)
image4[0][1][2][2]=1
for channel,image in enumerate(image4[0]):
    plt.imshow(image.detach().numpy(), interpolation='nearest', cmap=plt.cm.gray)
    print(image)
    plt.title("channel {}".format(channel))
    plt.colorbar()
    plt.show()


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Create an input image to represent the input X:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Different kernels can be used to detect various features in an image. You can see that the first channel fluctuates, and the second two channels produce a constant value. The following figure summarizes the process:
<img src = "https://ibm.box.com/shared/static/rcldenz1cwak2ss3ixo61b7alqzqoudy.png" width = 750, align = "center">
If you use a different image, the result will be different:
Can you write Python code for it?

image1=torch.zeros(1,1,5,5)
image1[0,0,2,:]=1
print(image1)
plt.imshow(image1[0,0,:,:].detach().numpy(), interpolation='nearest', cmap=plt.cm.gray)
plt.show()


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Each kernel has its own bias, so set them all to zero:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
In this case, the second channel fluctuates, and the first and the third channels produce a constant value.
Can you write Python code for it?

out1=conv1(image1)
for channel,image in enumerate(out1[0]):
    plt.imshow(image.detach().numpy(), interpolation='nearest', cmap=plt.cm.gray)
    print(image)
    plt.title("channel {}".format(channel))
    plt.colorbar()
    plt.show()


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Perform convolution using each channel:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Perform the convolution:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Plot it as an image:
Can you write Python code for it?

plt.imshow(image[0,0,:,:].numpy(), interpolation='nearest', cmap=plt.cm.gray)
plt.colorbar()
plt.show()


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Plot out each image:
Can you write Python code for it?

for channel,image in enumerate(image2[0]):
    plt.imshow(image.detach().numpy(), interpolation='nearest', cmap=plt.cm.gray)
    print(image)
    plt.title("channel {}".format(channel))
    plt.colorbar()
    plt.show()


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Print out each kernel:
Can you write Python code for it?

for x in conv1.state_dict()['weight']:
    print(x)


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
Pytorch randomly assigns values to each kernel. However, use kernels that have  been developed to detect edges:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
The following figure summarizes the process:

Multiple Input Channels

For two inputs, you can create two kernels. Each kernel performs a convolution on its associated input channel. The resulting output is added together as shown:

Create an input with two channels:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
The following images summarize the process. The object performs Convolution.

Then, it adds the result:

Multiple Input and Multiple Output Channels

When using multiple inputs and outputs, a kernel is created for each input, and the process is repeated for each output. The process is summarized in the following image.
There are two input channels and 3 output channels. For each channel, the input in red and purple is convolved with an individual kernel that is colored differently. As a result, there are three outputs.

Create an example with two inputs and three outputs and assign the kernel values to make the math a little easier:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
The output of the first channel is given by:

The output of the second channel is given by:

The output of the third channel is given by:

Practice Questions

Use the following two convolution objects to produce the same result as two input channel convolution on imageA and imageB as shown in the following image:
Can you write Python code for it?

imageA=torch.zeros(1,1,5,5)
imageB=torch.zeros(1,1,5,5)
imageA[0,0,2,:]=-2
imageB[0,0,2,:]=1

conv5 = nn.Conv2d(in_channels=1, out_channels=1,kernel_size=3)
conv6 = nn.Conv2d(in_channels=1, out_channels=1,kernel_size=3)

Gx1=torch.tensor([[0.0,0.0,0.0],[0,1.0,0],[0.0,0.0,0.0]])
conv5.state_dict()['weight'][0][0]=1*Gx1
conv6.state_dict()['weight'][0][0]=-2*Gx1
conv5.state_dict()['bias'][:]=torch.tensor([0.0])
conv6.state_dict()['bias'][:]=torch.tensor([0.0])


I am working on the file "DL0110EN/6.1.2Multiple Channel Convolution.ipynb".
The first task is:
The result is a 1x3x3x3 tensor. This represents one sample with three channels, and each channel contains a 3x3 image.  The same rules that govern the shape of each image were discussed in the last section.
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.3.Activation max pooling .ipynb".
The first task is:
If the stride is set to None (its defaults setting), the process will simply take the maximum in a prescribed area and shift over accordingly as shown in the following figure:
<img src = "https://ibm.box.com/shared/static/cenhef82q5kxzvzdqmjyuvbxo6j3c2ej.gif" width = 500, align = "center">
Here's the code in Pytorch:
Can you write Python code for it?


I am working on the file "DL0110EN/6.1.3.Activation max pooling .ipynb".
The first task is:
Max pooling simply takes the maximum value in each region. Consider the following image. For the first region, max pooling simply takes the largest element in a yellow region.

The region shifts, and the process is repeated. The process is similar to convolution and is demonstrated in the following figure:

Create a maxpooling object in 2d as follows and perform max pooling as follows:
Can you write Python code for it?

max3=torch.nn.MaxPool2d(2,stride=1)
max3(image1)

max1=torch.nn.MaxPool2d(2)
max1(image1)


I am working on the file "DL0110EN/6.1.3.Activation max pooling .ipynb".
The first task is:
The following image shows the image and kernel:
<img src = "https://ibm.box.com/shared/static/e0xc2oqtolg4p6nfsumcbpix1q5yq2kr.png" width = 500, align = "center">
Apply convolution to the image:
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:

Prepare Data

Load the training dataset with 10000 samples
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:

Analyse Results

Plot the loss and accuracy on the validation data:
Can you write Python code for it?

fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.plot(loss_list,color=color)
ax1.set_xlabel('epoch',color=color)
ax1.set_ylabel('total loss',color=color)
ax1.tick_params(axis='y', color=color)

ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('accuracy', color=color)
ax2.plot( accuracy_list, color=color)
ax2.tick_params(axis='y', labelcolor=color)
fig.tight_layout()


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
plot_activation: plot out the activations of the Convolutional layers
Can you write Python code for it?

def plot_activations(A,number_rows= 1,name=""):
    A=A[0,:,:,:].detach().numpy()
    n_activations=A.shape[0]

    print(n_activations)
    A_min=A.min().item()
    A_max=A.max().item()

    if n_activations==1:

        # Plot the image.
        plt.imshow(A[0,:], vmin=A_min, vmax=A_max, cmap='seismic')

    else:
        fig, axes = plt.subplots(number_rows, n_activations//number_rows)
        fig.subplots_adjust(hspace = 0.4)
        for i,ax in enumerate(axes.flat):
            if i< n_activations:
                # Set the label for the sub-plot.
                ax.set_xlabel( "activation:{0}".format(i+1))

                # Plot the image.
                ax.imshow(A[i,:], vmin=A_min, vmax=A_max, cmap='seismic')
                ax.set_xticks([])
                ax.set_yticks([])
    plt.show()


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
plot_channels: plot out the parameters of the Convolutional layers
Can you write Python code for it?

def plot_channels(W):
    #number of output channels
    n_out=W.shape[0]
    #number of input channels
    n_in=W.shape[1]
    w_min=W.min().item()
    w_max=W.max().item()
    fig, axes = plt.subplots(n_out,n_in)
    fig.subplots_adjust(hspace = 0.1)
    out_index=0
    in_index=0
    #plot outputs as rows inputs as columns
    for ax in axes.flat:

        if in_index>n_in-1:
            out_index=out_index+1
            in_index=0

        ax.imshow(W[out_index,in_index,:,:], vmin=w_min, vmax=w_max, cmap='seismic')
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        in_index=in_index+1

    plt.show()


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Create some toy data
Can you write Python code for it?

from torch.utils.data import Dataset, DataLoader
class Data(Dataset):
    def __init__(self,N_images=100,offset=0,p=0.9, train=False):
        """
        p:portability that pixel is wight
        N_images:number of images
        offset:set a random vertical and horizontal offset images by a sample should be less than 3
        """
        if train==True:
            np.random.seed(1)

        #make images multiple of 3
        N_images=2*(N_images//2)
        images=np.zeros((N_images,1,11,11))
        start1=3
        start2=1
        self.y=torch.zeros(N_images).type(torch.long)

        for n in range(N_images):
            if offset>0:

                low=int(np.random.randint(low=start1, high=start1+offset, size=1))
                high=int(np.random.randint(low=start2, high=start2+offset, size=1))
            else:
                low=4
                high=1

            if n<=N_images//2:
                self.y[n]=0
                images[n,0,high:high+9,low:low+3]= np.random.binomial(1, p, (9,3))
            elif  n>N_images//2:
                self.y[n]=1
                images[n,0,low:low+3,high:high+9] = np.random.binomial(1, p, (3,9))

        self.x=torch.from_numpy(images).type(torch.FloatTensor)
        self.len=self.x.shape[0]
        del(images)
        np.random.seed(0)
    def __getitem__(self,index):
        return self.x[index],self.y[index]
    def __len__(self):
        return self.len


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Define the data loader
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Define the loss function
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Define the optimizer class
Can you write Python code for it?

learning_rate=0.001

optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Determine the activations
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Plot maps out
Can you write Python code for it?

plot_activations(out[0],number_rows=1,name="first feature map")
plt.show()

plot_activations(out[2],number_rows=1,name="first feature map")
plt.show()

plot_activations(out[3],number_rows=1,name="first feature map")
plt.show()


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Plot the model parameters for the kernels before training the kernels. The kernels are initialized randomly.
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Plot the third sample

Build a Convolutional Neral Network Class
The input image is 11 x11, the following will change the size of the activations:

<il>convolutional layer</il>

<il>max pooling layer</il>

<il>convolutional layer </il>

<il>max pooling layer </il>

The following lines of code change the image before we get to the fully connected layer with the following parameters kernel_size, stride and  pad.
Can you write Python code for it?

out=conv_output_shape((11,11), kernel_size=2, stride=1, pad=0, dilation=1)
print(out)
out1=conv_output_shape(out, kernel_size=2, stride=1, pad=0, dilation=1)
print(out1)
out2=conv_output_shape(out1, kernel_size=2, stride=1, pad=0, dilation=1)
print(out2)

out3=conv_output_shape(out2, kernel_size=2, stride=1, pad=0, dilation=1)
print(out3)


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Print the model parameters with the object
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Save the output of the activation after flattening
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
The data type is long
Data Visualization
Each element in the rectangular  tensor corresponds to a number representing a pixel intensity  as demonstrated by  the following image.
Print out the third label
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Train the model and determine validation accuracy
Can you write Python code for it?

n_epochs=10
loss_list=[]
accuracy_list=[]
N_test=len(validation_dataset)
#n_epochs
for epoch in range(n_epochs):

    for x, y in train_loader:

        #clear gradient
        optimizer.zero_grad()
        #make a prediction
        z=model(x)
        # calculate loss
        loss=criterion(z,y)
        # calculate gradients of parameters
        loss.backward()
        # update parameters
        optimizer.step()

    correct=0
    #perform a prediction on the validation  data
    for x_test, y_test in validation_loader:

        z=model(x_test)
        _,yhat=torch.max(z.data,1)

        correct+=(yhat==y_test).sum().item()

    accuracy=correct/N_test

    accuracy_list.append(accuracy)

    loss_list.append(loss.data)


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Try the same thing for a sample where y=0
Can you write Python code for it?

out0=model.activations(train_dataset[100][0].view(1,1,11,11))[4][0].detach().numpy()
out0

plt.subplot(2, 1, 1)
plt.plot( out1, 'b')
plt.title('Flatted Activation Values  ')
plt.ylabel('Activation')
plt.xlabel('index')
plt.subplot(2, 1, 2)
plt.plot(out0, 'r')
plt.xlabel('index')
plt.ylabel('Activation')


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
Utility function for computing output of convolutions
takes a tuple of (h,w) and returns a tuple of (h,w)
Can you write Python code for it?

def conv_output_shape(h_w, kernel_size=1, stride=1, pad=0, dilation=1):
    #by Duane Nielsen
    from math import floor
    if type(kernel_size) is not tuple:
        kernel_size = (kernel_size, kernel_size)
    h = floor( ((h_w[0] + (2 * pad) - ( dilation * (kernel_size[0] - 1) ) - 1 )/ stride) + 1)
    w = floor( ((h_w[1] + (2 * pad) - ( dilation * (kernel_size[1] - 1) ) - 1 )/ stride) + 1)
    return h, w


I am working on the file "DL0110EN/6.2.1Convolutional Neral Network Simple example.ipynb".
The first task is:
View the results of the parameters for the Convolutional layers
Can you write Python code for it?

model.state_dict()['cnn1.weight']

plot_channels(model.state_dict()['cnn1.weight'])

model.state_dict()['cnn1.weight']

plot_channels(model.state_dict()['cnn2.weight'])


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

Prepare Data

Load the training dataset by setting the parameters train  to True. The parameter transform defines the method to convert image to tensor.
Can you write Python code for it?

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


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

Analyse Results

Plot the loss and accuracy on the validation data:
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
plot_activation: plot out the activations of the Convolutional layers
Can you write Python code for it?

def plot_activations(A,number_rows= 1,name="",i=0):
    A=A[0,:,:,:].detach().numpy()
    n_activations=A.shape[0]

    A_min=A.min().item()
    A_max=A.max().item()
    fig, axes = plt.subplots(number_rows, n_activations//number_rows)
    fig.subplots_adjust(hspace = 0.4)

    for i,ax in enumerate(axes.flat):
        if i< n_activations:
            # Set the label for the sub-plot.
            ax.set_xlabel( "activation:{0}".format(i+1))

            # Plot the image.
            ax.imshow(A[i,:], vmin=A_min, vmax=A_max, cmap='seismic')
            ax.set_xticks([])
            ax.set_yticks([])
    plt.show()


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
plot_channels:
plot out the kernel parameters of  each channel
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
plot_parameters: plot out the kernel parameters of  each channel
Can you write Python code for it?

def plot_parameters (W,number_rows =1,name="",i=0):
    W=W.data[:,i,:,:]
    n_filters=W.shape[0]

    w_min=W.min().item()
    w_max=W.max().item()
    fig, axes = plt.subplots(number_rows, n_filters//number_rows)
    fig.subplots_adjust(hspace = 0.4)

    for i,ax in enumerate(axes.flat):
        if i< n_filters:
            # Set the label for the sub-plot.
            ax.set_xlabel( "kernel:{0}".format(i+1))

            # Plot the image.
            ax.imshow(W[i,:], vmin=w_min, vmax=w_max, cmap='seismic')
            ax.set_xticks([])
            ax.set_yticks([])
    plt.suptitle(name, fontsize=10)
    plt.show()


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
show_data: plot out data sample
Can you write Python code for it?

def show_data(data_sample):

    plt.imshow(data_sample[0].numpy().reshape(28,28),cmap='gray')
    #print(data_sample[1].item())
    plt.title('y= '+ str(data_sample[1].item()))


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Data Visualization
Each element in the rectangular  tensor corresponds to a number representing a pixel intensity  as demonstrated by  the following image.

Print out the third label
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Define the dataset loader
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Define the loss function
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Define the optimizer class
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Determine the activations
Can you write Python code for it?

out=model.activations(train_dataset[1][0].view(1,1,28,28))

A=out[1]
A.shape


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Plot out the first set of activations
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Plot the fourth sample
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Plot the model parameters for the kernels before training the kernels. The kernels are initialized randomly.
Can you write Python code for it?

plot_parameters(model.state_dict()['cnn1.weight'],number_rows=4,name="1st layer kernels before training ")
plot_parameters(model.state_dict()['cnn2.weight'],number_rows=4,name='2nd layer kernels before training' )


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
The image below is the result after applying the relu activation function
Can you write Python code for it?

plot_activations(out[1],number_rows=4,name="first feature map after relu")


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
The image below is the result of the activation map after the second output layer.
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
Train the model and determine validation accuracy technically test accuracy (This may take a long time)
Can you write Python code for it?

n_epochs=10
loss_list=[]
accuracy_list=[]
N_test=len(validation_dataset)
#n_epochs
for epoch in range(n_epochs):
    for x, y in train_loader:

        #clear gradient
        optimizer.zero_grad()
        #make a prediction
        z=model(x)
        # calculate loss
        loss=criterion(z,y)
        # calculate gradients of parameters
        loss.backward()
        # update parameters
        optimizer.step()

    correct=0
    #perform a prediction on the validation  data
    for x_test, y_test in validation_loader:

        z=model(x_test)
        _,yhat=torch.max(z.data,1)

        correct+=(yhat==y_test).sum().item()

    accuracy=correct/N_test

    accuracy_list.append(accuracy)

    loss_list.append(loss.data)


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
View the results of the parameters for the Convolutional layers
Can you write Python code for it?


I am working on the file "DL0110EN/6.2.2.CNN.ipynb".
The first task is:
we can  see the result for the third sample
Can you write Python code for it?

show_data(train_dataset[2])

out=model.activations(train_dataset[2][0].view(1,1,28,28))

plot_activations(out[0],number_rows=4,name="first feature map")

plot_activations(out[1],number_rows=4,name="first feature map ")

plot_activations(out[2],number_rows=4,name="second feature map ")

plot_activations(out[3],number_rows=4,name="second feature map ")


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

Get the Data

We create a transform to resize the image and convert it to a tensor .
Can you write Python code for it?

# The composer for resizeing the image

IMAGE_SIZE = 8
composed = transforms.Compose([transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)), transforms.ToTensor()])


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

Analyze Results

Plot the loss and accuracy on the validation data:
Can you write Python code for it?

# Plot the loss and accuracy

fig, ax1 = plt.subplots()
color = 'tab:red'
ax1.plot(loss_list, color=color)
ax1.set_xlabel('epoch', color=color)
ax1.set_ylabel('total loss', color=color)
ax1.tick_params(axis='y', color=color)

ax2 = ax1.twinx()
color = 'tab:blue'
ax2.set_ylabel('accuracy', color=color)
ax2.plot( accuracy_list, color=color)
ax2.tick_params(axis='y', color=color)
fig.tight_layout()


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Define the function plot_channels to plot out the kernel parameters of  each channel
Can you write Python code for it?

# Define the function for plotting the channels

def plot_channels(W):
    n_out = W.shape[0]
    n_in = W.shape[1]
    w_min = W.min().item()
    w_max = W.max().item()
    fig, axes = plt.subplots(n_out, n_in)
    fig.subplots_adjust(hspace=0.1)
    out_index = 0
    in_index = 0

    #plot outputs as rows inputs as columns
    for ax in axes.flat:
        if in_index > n_in-1:
            out_index = out_index + 1
            in_index = 0
        ax.imshow(W[out_index, in_index, :, :], vmin=w_min, vmax=w_max, cmap='seismic')
        ax.set_yticklabels([])
        ax.set_xticklabels([])
        in_index = in_index + 1

    plt.show()


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Define the function plot_parameters to plot out the kernel parameters of each channel with Multiple outputs .
Can you write Python code for it?

# Define the function for plotting the parameters

def plot_parameters(W, number_rows=1, name="", i=0):
    W = W.data[:, i, :, :]
    n_filters = W.shape[0]
    w_min = W.min().item()
    w_max = W.max().item()
    fig, axes = plt.subplots(number_rows, n_filters // number_rows)
    fig.subplots_adjust(hspace=0.4)

    for i, ax in enumerate(axes.flat):
        if i < n_filters:
            # Set the label for the sub-plot.
            ax.set_xlabel("kernel:{0}".format(i + 1))

            # Plot the image.
            ax.imshow(W[i, :], vmin=w_min, vmax=w_max, cmap='seismic')
            ax.set_xticks([])
            ax.set_yticks([])
    plt.suptitle(name, fontsize=10)
    plt.show()


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Define the loss function, the optimizer and the dataset loader
Can you write Python code for it?

criterion = nn.CrossEntropyLoss()
learning_rate = 0.1
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=100)
validation_loader = torch.utils.data.DataLoader(dataset=validation_dataset, batch_size=5000)


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Each element in the rectangular tensor corresponds to a number representing a pixel intensity as demonstrated by the following image.
<img src="https://ibm.box.com/shared/static/7024mnculm8w2oh0080y71cpa48cib2k.png" width="550" alt="MNIST data image">
Print out the fourth label
Can you write Python code for it?

# The label for the fourth data element

train_dataset[3][1]


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Plot out the first set of activations
Can you write Python code for it?

# Plot the outputs after the first CNN

plot_activations(out[0], number_rows=4, name="Output after the 1st CNN")


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Plot the first five mis-classified samples:
Can you write Python code for it?

# Plot the mis-classified samples

count = 0
for x, y in torch.utils.data.DataLoader(dataset=validation_dataset, batch_size=1):
    z = model(x)
    _, yhat = torch.max(z, 1)
    if yhat != y:
        show_data((x, y))
        plt.show()
        print("yhat: ",yhat)
        count += 1
    if count >= 5:
        break


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
The fourth sample is a "1".

Build a Convolutional Neural Network Class

Build a Convolutional Network class with two Convolutional layers and one fully connected layer. Pre-determine the size of the final output matrix. The parameters in the constructor are the number of output channels for the first and second layer.
Can you write Python code for it?

# Build the convolutional Neural Network Class

class CNN(nn.Module):

    # Contructor
    def __init__(self, out_1=16, out_2=32):
        super(CNN, self).__init__()
        self.cnn1 = nn.Conv2d(in_channels=1, out_channels=out_1, kernel_size=3, stride=1, padding=1)
        self.relu1 = nn.ReLU()
        self.maxpool1 = nn.MaxPool2d(kernel_size=2)
        self.cnn2 = nn.Conv2d(in_channels=out_1, out_channels=out_2, kernel_size=3, stride=1, padding=1)
        self.relu2 = nn.ReLU()
        self.maxpool2 = nn.MaxPool2d(kernel_size=2)
        self.fc1 = nn.Linear(out_2 * 2 * 2, 10)

    # Prediction
    def forward(self, x):
        out = self.cnn1(x)
        out = self.relu1(out)
        out = self.maxpool1(out)
        out = self.cnn2(out)
        out = self.relu2(out)
        out = self.maxpool2(out)
        out = out.view(out.size(0), -1)
        out = self.fc1(out)
        return out

    # Outputs in each steps
    def activations(self, x):
        #outputs activation this is not necessary
        z1 = self.cnn1(x)
        a1 = self.relu1(z1)
        out = self.maxpool1(a1)

        z2 = self.cnn2(out)
        a2 = self.relu2(z2)
        out = self.maxpool2(a2)
        out = out.view(out.size(0),-1)
        return z1, a1, z2, a2, out


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
The image below is the result of the activation map after applying the second relu
Can you write Python code for it?

# Plot the outputs after the second Relu

plot_activations(out[3], number_rows=4, name="Output after the 2nd Relu")


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
The image below is the result of the activation map after the second output layer.
Can you write Python code for it?

# Plot the outputs after the second CNN

plot_activations(out[2], number_rows=32 // 4, name="Output after the 2nd CNN")


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
Train the model and determine validation accuracy technically test accuracy (This may take a long time)
Can you write Python code for it?

# Train the model

n_epochs = 3
loss_list = []
accuracy_list = []
N_test = len(validation_dataset)

def train_model(n_epochs):
    for epoch in range(n_epochs):
        for x, y in train_loader:
            optimizer.zero_grad()
            z = model(x)
            loss = criterion(z, y)
            loss.backward()
            optimizer.step()

        correct=0
        #perform a prediction on the validation  data
        for x_test, y_test in validation_loader:
            z = model(x_test)
            _, yhat = torch.max(z.data, 1)
            correct += (yhat == y_test).sum().item()
        accuracy = correct / N_test
        accuracy_list.append(accuracy)
        loss_list.append(loss.data)

train_model(n_epochs)


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
We can  see the result for the third sample
Can you write Python code for it?

# Show the third image

show_data(train_dataset[2])

# Use the CNN activations class to see the steps

out = model.activations(train_dataset[2][0].view(1, 1, IMAGE_SIZE, IMAGE_SIZE))

# Plot the outputs after the first CNN

plot_activations(out[0], number_rows=4, name="Output after the 1st CNN")

# Plot the outputs after the first Relu

plot_activations(out[1], number_rows=4, name="Output after the 1st Relu")

# Plot the outputs after the second CNN

plot_activations(out[2], number_rows=32 // 4, name="Output after the 2nd CNN")

# Plot the outputs after the second Relu

plot_activations(out[3], number_rows=4, name="Output after the 2nd Relu")


I am working on the file "DL0110EN/6.2.2.CNN_Small_Image_v2.ipynb".
The first task is:
We can see the data type is long.
Can you write Python code for it?

# Show the data type for each element in dataset

train_dataset[0][1].type()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Assets
Assets are digital "things", like a video, an HTML page, an image, etc. It includes things like copyright, license, etc., but the actual digital bits are stored in Asset Contents.
For example, an asset might represent a video, but it might include assetContents for a transcript, an .mp4 version, a thumbnail, etc.
Asset
      |--AssetContents
To make this work with and save files to your filesystem (instead of GridFS), you need to include an assetContent record type.
Can you write Python code for it?

from dlkit.primordium.transport.objects import DataInputStream
from dlkit.primordium.id.primitives import Id

form = repo.get_asset_form_for_create([])
form.display_name = 'My Image'
asset = repo.create_asset(form)

asset_content_type_list = []
try:
    config = repo._catalog._runtime.get_configuration()
    parameter_id = Id('parameter:assetContentRecordTypeForFiles@filesystem')
    asset_content_type_list.append(
        config.get_value_by_parameter(parameter_id).get_type_value())
except (AttributeError, KeyError):
    pass

form = repo.get_asset_content_form_for_create(asset.ident, asset_content_type_list)
with open('/Users/cjshaw/Documents/Projects/CLIx/dlkit-tutorial/files/draggable.green.dot.png', 'r') as dot:
    form.set_data(DataInputStream(dot))
repo.create_asset_content(form)


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
At the moment, the results only include the Responses without regard to AssessmentSections, but you can easily link in the actual questions as well. Sample code is provided below.
Can you write Python code for it?

question_maps = []
for index, question in enumerate(assessment_section.get_questions()):
    question_map = question.object_map
    question_map.update({
            'itemId': assessment_section._my_map['questions'][index]['itemId'],
            'responses': []
        })
    question_maps.append(question_map)
for index, response in enumerate(responses):
    question_maps[index]['responses'].append(response.object_map)
print question_maps


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Banks
Now that we have an AssessmentManager, we can see what Banks exist in the system. Calling for "lists" of things returns a "thing list", like a BankList (a Python generator), and we can check the number of results with .available().
Can you write Python code for it?

print am.banks
print am.banks.available()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
If we're starting with a clean slate, we have no Assessments. Let's get a form, and create one. Note that with the current DLKit configuration, we need to include at least the simple-child-sequencing record for each Assessment.
Can you write Python code for it?

from records.registry import ASSESSMENT_RECORD_TYPES
SIMPLE_SEQUENCE_ASSESSMENT = Type(**ASSESSMENT_RECORD_TYPES['simple-child-sequencing'])

form = bank.get_assessment_form_for_create([SIMPLE_SEQUENCE_ASSESSMENT])
form.display_name = 'Homework #1'
form.description = 'Favorites'
assessment = bank.create_assessment(form)
print assessment
print bank.get_assessment_items(assessment.ident).available()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
No banks!! Okay, let's create one. In DLKit, CrUD operations are done with forms. So let's get a form to create a new assessment bank, and assign it a displayName and description that we'll recognize later.
Notice that in the get_bank_form_for_create() method, we pass an empty list as an argument. This can be used to extend the base functionality of the bank, via record extensions. This is a more advanced feature we'll touch on in a bit.
Can you write Python code for it?

form = am.get_bank_form_for_create([])
form.display_name = "Class 9"
form.description = "For learning about DLKit"
bank = am.create_bank(form)
print bank
print str(bank.display_name.script_type)
print bank.description.text
print str(bank.ident)
bank.get_items()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Note that both submissions are stored in the database, but currently only the most recent one can be retrieved. Time data in DLKit is stored as UTC, so the information printed above may differ from your local time.
At any time, instructors can get a record of all student Responses and questions.
Can you write Python code for it?

responses = bank.get_assessment_taken_responses(assessment_taken.ident)
print [r.object_map for r in responses]


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Note that this last method, is_correct(), is a non-OSID convenience method, that currently only works with multiple choice questions, but can easily be integrated into any record extension.
We can try submitting again, if the AssessmentOffered allows us to.
Can you write Python code for it?

form = bank.get_response_form(assessment_section.ident, question.ident)
form.add_choice_id(choices[0]['id'])
bank.submit_response(assessment_section.ident, question.ident, form)
response = bank.get_response(assessment_section.ident, question.ident)
print response.is_correct()
print response.get_submission_time()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Now we can find the Item Answers (if you run the above block multiple times, you'll get a varying number of Answer elements.
Can you write Python code for it?

item = bank.get_item(item.ident)
print item.get_answers().available()
print [a.object_map for a in item.get_answers()]


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Once we have an AssessmentOffered, we can provide a set of methods for students to take the assessment. Each "taker" generates an AssessmentTaken, which links a taker's user ID (which was provided through the Proxy when the AssessmentManager was initially set up) to the AssessmentOffered ID. The AssessmentTaken also links to one or more AssessmentSections, which maintains a list of questions takers have seen and thier responses.
Can you write Python code for it?

form = bank.get_assessment_taken_form_for_create(assessment_offered.ident, [])
assessment_taken = bank.create_assessment_taken(form)
print assessment_taken
print str(assessment_taken.get_taking_agent_id().identifier)


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Resource Service
In the Resource Service, the catalogs are called bins and the objects are called resources. While this sounds very generic, one specific use case is for representing users. Resources have a displayName and description, and also an avatar -- for a profile picture or something.  We'll re-use the green-dot asset from above, so make sure you run those cells in the Repository Service first.
Can you write Python code for it?

resm = RUNTIME.get_service_manager('RESOURCE', proxy=proxy)
print resm

form = resm.get_bin_form_for_create([])
form.display_name = 'My bin'
bin = resm.create_bin(form)

print bin

form = bin.get_resource_form_for_create([])
form.display_name = "Cole Shaw"
form.description = "Software Developer"
form.set_avatar(asset.ident)
resource = bin.create_resource(form)

print resource.get_avatar()
print resource.get_avatar().get_asset_contents().next().get_data().read()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
So we have now created an item, but where's the actual question that we want a student to respond to?? We'll create that separately, with its own records. Recall:
    Item
      |--Question
      |--Answers

Since Questions are distinct from Items, students can be sent only the Question object, without any danger of them seeing any answers . Note that the Question form requires an extra itemId argument.  This attaches the question to the Item created previously.
Can you write Python code for it?

from records.registry import QUESTION_RECORD_TYPES
MULTIPLE_CHOICE_QUESTION = Type(**QUESTION_RECORD_TYPES['multi-choice-text'])

form = bank.get_question_form_for_create(item.ident, [MULTIPLE_CHOICE_QUESTION])
form.set_text("Which color do you prefer?")
form.add_choice("blue")
form.add_choice("red")
form.add_choice("yellow")
question = bank.create_question(form)

print question.get_choices()
print question.get_text().text

form = bank.get_question_form_for_update(question.ident)
form.set_text('why is the sky blue?')
question = bank.update_question(form)

print question.get_text()
print question.get_text().text
print str(question.get_text().language_type)
print str(question.get_text().script_type)
print str(question.get_text().format_type)


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
The Item has a method that allows us to get access to the Question.
Can you write Python code for it?


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
There are several ways a taker can get the Assessment questions ... they are the same for our simple Assessment because there is only one question, but you can easily imagine how the behavior might change when multiple questions are present. For sequential Assessments, each question must be answered in order. Hence, you iterate through the questions using methods like get_first_question() & get_next_question() or get_first_unanswered_question() & get_next_unanswered_question(). Alternatively, if the Assessment does not specify that the questions must be answered sequentially, you can get all the questions in bulk, via get_questions(). In more complex and adaptive scenarios, even this method might be dynamically updated, as students navigate through the Assessment.
Assessments can be divided into multiple AssessmentSections, for example to provide UI separation. In our simple Assessment, there is only one AssessmentSection, and we'll use that ID to grab the questions.
Let's demonstrate the methods.
Can you write Python code for it?

assessment_section = bank.get_first_assessment_section(assessment_taken.ident)
print str(bank.get_first_question(assessment_section.ident).ident)
print str(bank.get_first_unanswered_question(assessment_section.ident).ident)
print [str(q.ident) for q in bank.get_questions(assessment_section.ident)]


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
There they are!
That's a basic overview of Items. They are very powerful, and to learn more, you can inspect the records directory that came along with this tutorial.
Assessments
Conceptually, Items are organized into Assessments -- which are then offered to students. An Assessment in a classroom might be a homework, a quiz, or an exam ... a tool that evaluates students' knowledge of a topic or outcome.
Let's see what Assessments exist in our Bank.
Can you write Python code for it?

print bank.get_assessments()
print bank.get_assessments().available()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
There you have the basics of the Assessment service! Much of the additional complexity appears in the record extensions for various Item types and Assessment / AssessmentOffered / AssessmentTaken settings.
For example, we have randomized multiple choice questions where the choices appear in different orders to each student, but when students return to the AssessmentTaken, they see the exact same order they've seen before -- randomized per student, not per view of the question.
Another example of a complex assessment is adaptive behavior, where the "next question" a student sees depends on their response to the previous question. So each students gets a different set of questions, depending on their knowledge and performance.
Object Patterns in DLKit
Now that we've gone through the Assessment service, you've hopefully learned the basics about catalogs, objects, forms, sessions, and managers. Luckily, these patterns appear across all the DLKit services. So you can easily pick up how to use the other services. http://osid.org/ is a good reference, and we've included a simple table below that reflects the services available in this DLKit build:
````
|   Service     |      Catalog     |                            Objects                                     |
| Assessment    | Bank             | Item, Assessment, AssessmentOffered, AssessmentTaken, AssessmentPart   |
| Authorization | Vault            | Authorization                                                          |
| Commenting    | Book             | Comment                                                                |
| Grading       | Gradebook        | GradeSystem, GradeEntry, GradebookColumn                               |
| Logging       | Log              | LogEntry                                                               |
| Repository    | Repository       | Asset, AssetContent, Composition                                       |
| Resource      | Bin              | Resource                                                              ...
Can you write Python code for it?

print rm

form = rm.get_repository_form_for_create([])
form.display_name = 'test repository'
repo = rm.create_repository(form)
print repo


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
This is because questions, when put into an AssessmentSection, generate new, unique IDs. This helps us manage more advanced Item and Question records that manipulate the ID attribute to provide adaptability or consistent randomization -- which is beyond the scope of this basic tutorial.
However, we will use this new question ID to submit a Response. Again, we'll use a form, and we'll supply it with a choice ID from earlier.
Can you write Python code for it?

question = bank.get_first_question(assessment_section.ident)
choices = question.get_choices()
print choices

form = bank.get_response_form(assessment_section.ident, question.ident)
form.add_choice_id(choices[1]['id'])
bank.submit_response(assessment_section.ident, question.ident, form)
response = bank.get_response(assessment_section.ident, question.ident)
print response.is_correct()
print response.get_submission_time()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
This is great for authoring Assessments, but DLKit defines another set of methods to offer the Assessment in "student mode". This is through the AssessmentSession methods which define actions for taking assessments -- but first, we need to create an AssessmentOffered. AssessmentsOffered wrap some other information around the canonical Assessment, like the start_time, deadline, duration, etc. -- all of these typically are optional. Leaving them out "opens" the AssessmentOffered to students immediately, and it never "closes".
Again, we need a form for this, and that method requires the Assessment ID.
Can you write Python code for it?

form = bank.get_assessment_offered_form_for_create(assessment.ident, [])
assessment_offered = bank.create_assessment_offered(form)
print assessment_offered


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
Wait ... where are all the wrong Answers? There should be 2x the number of times you ran the block, Answers ... and by looking at the genusTypeId attributes, it seems like all the Answers from get_answers() are only "right-answer" types.
This is because DLKit defines the get_answers() method to only return the "right" answers, so the "wrong" answers need to be retrieved with a different method, defined by the Item record extension for wrong answers.
Can you write Python code for it?

print item.get_wrong_answers().available()
print [a.object_map for a in item.get_wrong_answers()]


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
What?!?! Oh, wait ... because the Item object we have was initialized before the Question was actually created, we need to re-grab that Item to get it in the "newest" state. Note that this is a due to the particular service implementation we are using. Some implementations might actually keep objects up-to-date with the underlying persistence (like database or filespace) and never let an object get stale.  you can check if an object is known to be up-to-date with underlying data by calling it's is_current() method.  If it is not, you can refresh the object, in this case our Item by calling the get_item(item_id) method of the Bank.
Can you write Python code for it?

print item.is_current()
if not item.is_current():
    item = bank.get_item(item.ident)
print item.get_question()
print item.get_question().get_choices()


I am working on the file "DLKit Tutorial.ipynb".
The first task is:
You can see that our new Bank has some attributes -- some that we assigned (display_name and description, and others that were created by DLKit, like ident). display_name and description return DisplayText objects that include the text strings we created, but can also contain language, format, and script data, which is why we call display_name.text and description.text above.
Items
Now that we have a Bank, we can create assessment Items in it.
An Item is what you might think of as a basic assessment question with the associated answers (right or wrong). There are many types of Items, including multiple choice, fill in the blank, short answer, etc.
Again, we can inspect to see if any exist.
Can you write Python code for it?

print bank.items
print bank.items.available()


I am working on the file "DLND Your first neural network.ipynb".
The first task is:
Check out your predictions
Here, use the test data to view how well your network is modeling the data. If something is completely wrong here, make sure each step in your network is implemented correctly.
Can you write Python code for it?

fig, ax = plt.subplots(figsize=(8,4))

mean, std = scaled_features['cnt']
predictions = network.run(test_features).T*std + mean
ax.plot(predictions[0], label='Prediction')
ax.plot((test_targets['cnt']*std + mean).values, label='Data')
ax.set_xlim(right=len(predictions))
ax.legend()

dates = pd.to_datetime(rides.ix[test_data.index]['dteday'])
dates = dates.apply(lambda d: d.strftime('%b %d'))
ax.set_xticks(np.arange(len(dates))[12::24])
_ = ax.set_xticklabels(dates[12::24], rotation=45)


I am working on the file "DLND Your first neural network.ipynb".
The first task is:
Load and prepare the data
A critical step in working with neural networks is preparing the data correctly. Variables on different scales make it difficult for the network to efficiently learn the correct weights. Below, we've written the code to load and prepare the data. You'll learn more about this soon!
Can you write Python code for it?

data_path = 'Bike-Sharing-Dataset/hour.csv'

rides = pd.read_csv(data_path)

rides.head()


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

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


I am working on the file "DLND Your first neural network.ipynb".
The first task is:
Splitting the data into training, testing, and validation sets
We'll save the data for the last approximately 21 days to use as a test set after we've trained the network. We'll use this set to make predictions and compare them with the actual number of riders.
Can you write Python code for it?

# Save data for approximately the last 21 days
test_data = data[-21*24:]

# Now remove the test data from the data set
data = data[:-21*24]

# Separate the data into features and targets
target_fields = ['cnt', 'casual', 'registered']
features, targets = data.drop(target_fields, axis=1), data[target_fields]
test_features, test_targets = test_data.drop(target_fields, axis=1), test_data[target_fields]


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

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

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

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

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

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

        #### TODO: Set self.activation_function to your implemented sigmoid function ####
        #
        # Note: in Python, you can define a function with a lambda expression,
        # as shown below.
        #self.activation_function = lambda x : 0  # Replace 0 with your sigmoid calculation.

        ### If the lambda code above is not something you're familiar with,
        # You can uncomment out the following three lines and put your
        # implementation there instead.
        #
        def sigmoid(x):
            return 1. / (1. + np.exp(-x))  # Replace 0 with your sigmoid calculation here
        self.activation_function = sigmoid

    def train(self, features, targets):
        ''' Train the network on batch of features and targets.

            Arguments
            ---------

            features: 2D array, each row is one data record, each column is a feature
            targets: 1D array of target values

        '''
        n_records = features.shape[0]
        delta_weights_i_h = np.zeros(self.weights_input_to_hidden.shape)
        delta_weights_h_o = np.zeros(self.weights_hidden_to_output.shape)
        for X, y in zip(features, targets):
            #### Implement the forward pass here ####
            ### Forward pass ###
            #...


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

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


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
CelebA
The CelebFaces Attributes Dataset (CelebA) dataset contains over 200,000 celebrity images with annotations.  Since you're going to be generating faces, you won't need the annotations.  You can view the first number of examples by changing show_n_images.
Can you write Python code for it?

show_n_images = 25

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'img_align_celeba/*.jpg'))[:show_n_images], 28, 28, 'RGB')
pyplot.imshow(helper.images_square_grid(mnist_images, 'RGB'))


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Explore the Data
MNIST
As you're aware, the MNIST dataset contains images of handwritten digits. You can view the first number of examples by changing show_n_images.
Can you write Python code for it?

show_n_images = 25

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
%matplotlib inline
import os
from glob import glob
from matplotlib import pyplot

mnist_images = helper.get_batch(glob(os.path.join(data_dir, 'mnist/*.jpg'))[:show_n_images], 28, 28, 'L')
pyplot.imshow(helper.images_square_grid(mnist_images, 'L'), cmap='gray')


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Generator
Implement generator to generate an image using z. This function should be able to reuse the variabes in the neural network.  Use tf.variable_scope with a scope name of "generator" to allow the variables to be reused. The function should return the generated 28 x 28 x out_channel_dim images.
Can you write Python code for it?

def generator(z, out_channel_dim, is_train=True):
    """
    Create the generator network
    :param z: Input z
    :param out_channel_dim: The number of channels in the output image
    :param is_train: Boolean if generator is being used for training
    :return: The tensor output of the generator
    """
    with tf.variable_scope('generator', reuse=not is_train):
        # First fully connected layer
        x1 = tf.layers.dense(z, 7*7*64)

        # Reshape it to start the convolutional stack
        x1 = tf.reshape(x1, (-1, 7, 7, 64))
        x1 = tf.layers.batch_normalization(x1, training=is_train)
        x1 = tf.maximum(0.1 * x1, x1)
        # 7x7x64 now

        x2 = tf.layers.conv2d_transpose(x1, 32, 5, strides=2, padding='same')
        x2 = tf.layers.batch_normalization(x2, training=is_train)
        x2 = tf.maximum(0.1 * x2, x2)
        # 14x14x32 now

        x3 = tf.layers.conv2d_transpose(x2, 16, 5, strides=1, padding='same')
        x3 = tf.layers.batch_normalization(x3, training=is_train)
        x3 = tf.maximum(0.1 * x3, x3)
        # 14x14x16 now

        x4 = tf.layers.conv2d_transpose(x3, 8, 5, strides=2, padding='same')
        x4 = tf.layers.batch_normalization(x4, training=is_train)
        x4 = tf.maximum(0.1 * x4, x4)
        # 28x28x8 now

        # Output layer
        logits = tf.layers.conv2d_transpose(x4, out_channel_dim, 5, strides=1, padding='same')
        # 28x28xout_channel_dim now

        out = tf.tanh(logits)

        return out

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_generator(generator, tf)


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Input
Implement the model_inputs function to create TF Placeholders for the Neural Network. It should create the following placeholders:
- Real input images placeholder with rank 4 using image_width, image_height, and image_channels.
- Z input placeholder with rank 2 using z_dim.
- Learning rate placeholder with rank 0.
Return the placeholders in the following the tuple (tensor of real input images, tensor of z data)
Can you write Python code for it?

import problem_unittests as tests

def model_inputs(image_width, image_height, image_channels, z_dim):
    """
    Create the model inputs
    :param image_width: The input image width
    :param image_height: The input image height
    :param image_channels: The number of image channels
    :param z_dim: The dimension of Z
    :return: Tuple of (tensor of real input images, tensor of z data, learning rate)
    """
    real_input = tf.placeholder(tf.float32, [None, image_width, image_height, image_channels], name='real_input_image')
    tensor_z = tf.placeholder(tf.float32, [None, z_dim], name='tensor_z')
    lr = tf.placeholder(tf.float32, [], name='lr')

    return real_input, tensor_z, lr

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)

tf.reset_default_graph()


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Loss
Implement model_loss to build the GANs for training and calculate the loss.  The function should return a tuple of (discriminator loss, generator loss).  Use the following functions you implemented:
- discriminator(images, reuse=False)
- generator(z, out_channel_dim, is_train=True)
Can you write Python code for it?

def model_loss(input_real, input_z, out_channel_dim):
    """
    Get the loss for the discriminator and generator
    :param input_real: Images from the real dataset
    :param input_z: Z input
    :param out_channel_dim: The number of channels in the output image
    :return: A tuple of (discriminator loss, generator loss)
    """
    # TODO: Implement Function
    g_model = generator(input_z, out_channel_dim)
    d_model_real, d_logits_real = discriminator(input_real)
    d_model_fake, d_logits_fake = discriminator(g_model, reuse=True)

    # Calculate losses
    d_loss_real = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_real, labels=tf.ones_like(d_logits_real)*0.9))
    d_loss_fake = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.zeros_like(d_logits_real)))
    g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=d_logits_fake, labels=tf.ones_like(d_logits_fake)))
    d_loss = d_loss_real + d_loss_fake

    return d_loss, g_loss

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_loss(model_loss)


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
MNIST
Test your GANs architecture on MNIST.  After 2 epochs, the GANs should be able to generate images that look like handwritten digits.  Make sure the loss of the generator is lower than the loss of the discriminator or close to 0.
Can you write Python code for it?

batch_size =64
z_dim = 64
learning_rate = 0.001
beta1 = 0.1

epochs = 2

mnist_dataset = helper.Dataset('mnist', glob(os.path.join(data_dir, 'mnist/*.jpg')))
with tf.Graph().as_default():
    train(epochs, batch_size, z_dim, learning_rate, beta1, mnist_dataset.get_batches,
          mnist_dataset.shape, mnist_dataset.image_mode)


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Neural Network Training
Show Output
Use this function to show the current output of the generator during training. It will help you determine how well the GANs is training.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np

def show_generator_output(sess, n_images, input_z, out_channel_dim, image_mode):
    """
    Show example output for the generator
    :param sess: TensorFlow session
    :param n_images: Number of Images to display
    :param input_z: Input Z Tensor
    :param out_channel_dim: The number of channels in the output image
    :param image_mode: The mode to use for images ("RGB" or "L")
    """
    cmap = None if image_mode == 'RGB' else 'gray'
    z_dim = input_z.get_shape().as_list()[-1]
    example_z = np.random.uniform(-1, 1, size=[n_images, z_dim])

    samples = sess.run(
        generator(input_z, out_channel_dim, False),
        feed_dict={input_z: example_z})

    images_grid = helper.images_square_grid(samples, image_mode)
    pyplot.imshow(images_grid, cmap=cmap)
    pyplot.show()


I am working on the file "DLND-face-generation/dlnd_face_generation.ipynb".
The first task is:
Optimization
Implement model_opt to create the optimization operations for the GANs. Use tf.trainable_variables to get all the trainable variables.  Filter the variables with names that are in the discriminator and generator scope names.  The function should return a tuple of (discriminator training operation, generator training operation).
Can you write Python code for it?

def model_opt(d_loss, g_loss, learning_rate, beta1):
    """
    Get optimization operations
    :param d_loss: Discriminator loss Tensor
    :param g_loss: Generator loss Tensor
    :param learning_rate: Learning Rate Placeholder
    :param beta1: The exponential decay rate for the 1st moment in the optimizer
    :return: A tuple of (discriminator training operation, generator training operation)
    """
    # TODO: Implement Function
    # Get weights and bias to update
    t_vars = tf.trainable_variables()
    d_vars = [var for var in t_vars if var.name.startswith('discriminator')]
    g_vars = [var for var in t_vars if var.name.startswith('generator')]

    # Optimize
    with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
        d_train_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(d_loss, var_list=d_vars)
        g_train_opt = tf.train.AdamOptimizer(learning_rate, beta1=beta1).minimize(g_loss, var_list=g_vars)

    return d_train_opt, g_train_opt

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_opt(model_opt, tf)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Check Point
This is your first checkpoint.  If you ever decide to come back to this notebook or have to restart the notebook, you can start from here.  The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import pickle
import problem_unittests as tests
import helper

# Load the Preprocessed Validation data
valid_features, valid_labels = pickle.load(open('preprocess_validation.p', mode='rb'))


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Explore the Data
The dataset is broken into batches to prevent your machine from running out of memory.  The CIFAR-10 dataset consists of 5 batches, named data_batch_1, data_batch_2, etc.. Each batch contains the labels and images that are one of the following:
* airplane
* automobile
* bird
* cat
* deer
* dog
* frog
* horse
* ship
* truck
Understanding a dataset is part of making predictions on the data.  Play around with the code cell below by changing the batch_id and sample_id. The batch_id is the id for a batch (1-5). The sample_id is the id for a image and label pair in the batch.
Ask yourself "What are all possible labels?", "What is the range of values for the image data?", "Are the labels in order or random?".  Answers to questions like these will help you preprocess the data and end up with better predictions.
Can you write Python code for it?

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import helper
import numpy as np

# Explore the dataset
batch_id = 1
sample_id = 10
helper.display_stats(cifar10_dataset_folder_path, batch_id, sample_id)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Flatten Layer
Implement the flatten function to change the dimension of x_tensor from a 4-D tensor to a 2-D tensor.  The output should be the shape (Batch Size, Flattened Image Size). Shortcut option: you can use classes from the TensorFlow Layers or TensorFlow Layers (contrib) packages for this layer. For more of a challenge, only use other TensorFlow packages.
Can you write Python code for it?

def flatten(x_tensor):
    """
    Flatten x_tensor to (Batch Size, Flattened Image Size)
    : x_tensor: A tensor of size (Batch Size, ...), where ... are the image dimensions.
    : return: A tensor of size (Batch Size, Flattened Image Size).
    """
    # print(x_tensor.get_shape()[1:4].num_elements())
    return tf.reshape(x_tensor, [-1, x_tensor.get_shape()[1:4].num_elements()])

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_flatten(flatten)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Fully Train the Model
Now that you got a good accuracy with a single CIFAR-10 batch, try it with all five batches.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
save_model_path = './image_classification'

print('Training...')
with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(epochs):
        # Loop over all batches
        n_batches = 5
        for batch_i in range(1, n_batches + 1):
            for batch_features, batch_labels in helper.load_preprocess_training_batch(batch_i, batch_size):
                train_neural_network(sess, optimizer, keep_probability, batch_features, batch_labels)
            print('Epoch {:>2}, CIFAR-10 Batch {}:  '.format(epoch + 1, batch_i), end='')
            print_stats(sess, batch_features, batch_labels, cost, accuracy)

    # Save Model
    saver = tf.train.Saver()
    save_path = saver.save(sess, save_model_path)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Fully-Connected Layer
Implement the fully_conn function to apply a fully connected layer to x_tensor with the shape (Batch Size, num_outputs). Shortcut option: you can use classes from the TensorFlow Layers or TensorFlow Layers (contrib) packages for this layer. For more of a challenge, only use other TensorFlow packages.
Can you write Python code for it?

def fully_conn(x_tensor, num_outputs):
    """
    Apply a fully connected layer to x_tensor using weight and bias
    : x_tensor: A 2-D tensor where the first dimension is batch size.
    : num_outputs: The number of output that the new tensor should be.
    : return: A 2-D tensor where the second dimension is num_outputs.
    """
    batch_size = x_tensor.shape[1].value
    weights = tf.Variable(
        tf.truncated_normal(
            [batch_size, num_outputs],
            mean = 0,
            stddev=0.1)
    )
    bias = tf.Variable(tf.zeros(num_outputs))

    fully_conn_layer = tf.matmul(x_tensor, weights)
    fully_conn_layer = tf.nn.bias_add(fully_conn_layer, bias)
    fully_conn_layer = tf.nn.relu(fully_conn_layer)

    return fully_conn_layer

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_fully_conn(fully_conn)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
One-hot encode
Just like the previous code cell, you'll be implementing a function for preprocessing.  This time, you'll implement the one_hot_encode function. The input, x, are a list of labels.  Implement the function to return the list of labels as One-Hot encoded Numpy array.  The possible values for labels are 0 to 9. The one-hot encoding function should return the same encoding for each value between each call to one_hot_encode.  Make sure to save the map of encodings outside the function.
Hint: Don't reinvent the wheel.
Can you write Python code for it?

from sklearn.preprocessing import LabelBinarizer

def one_hot_encode(x):
    """
    One hot encode a list of sample labels. Return a one-hot encoded vector for each label.
    : x: List of sample Labels
    : return: Numpy array of one-hot encoded labels
    """
    # The possible values for labels are 0 to 9. 10 in total
    return np.eye(10)[x]

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_one_hot_encode(one_hot_encode)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Output Layer
Implement the output function to apply a fully connected layer to x_tensor with the shape (Batch Size, num_outputs). Shortcut option: you can use classes from the TensorFlow Layers or TensorFlow Layers (contrib) packages for this layer. For more of a challenge, only use other TensorFlow packages.
Note: Activation, softmax, or cross entropy should not be applied to this.
Can you write Python code for it?

def output(x_tensor, num_outputs):
    """
    Apply a output layer to x_tensor using weight and bias
    : x_tensor: A 2-D tensor where the first dimension is batch size.
    : num_outputs: The number of output that the new tensor should be.
    : return: A 2-D tensor where the second dimension is num_outputs.
    """
    batch_size = x_tensor.shape[1].value
    weights = tf.Variable(
        tf.truncated_normal(
            [batch_size, num_outputs],
            mean = 0,
            stddev=0.1)
    )
    bias = tf.Variable(tf.zeros(num_outputs))

    output_layer = tf.matmul(x_tensor, weights)
    output_layer = tf.nn.bias_add(output_layer, bias)

    return output_layer

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_output(output)


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Show Stats
Implement the function print_stats to print loss and validation accuracy.  Use the global variables valid_features and valid_labels to calculate validation accuracy.  Use a keep probability of 1.0 to calculate the loss and validation accuracy.
Can you write Python code for it?

def print_stats(session, feature_batch, label_batch, cost, accuracy):
    """
    Print information about loss and validation accuracy
    : session: Current TensorFlow session
    : feature_batch: Batch of Numpy image data
    : label_batch: Batch of Numpy label data
    : cost: TensorFlow cost function
    : accuracy: TensorFlow accuracy function
    """
    loss = session.run(cost, feed_dict={x: feature_batch, y: label_batch, keep_prob: 1.0})
    accuracy = session.run(accuracy, feed_dict={x: valid_features, y: valid_labels, keep_prob: 1.0})
    print("Loss: {} Accuracy: {}".format(loss, accuracy))


I am working on the file "DLND-image-classification/dlnd_image_classification.ipynb".
The first task is:
Train on a Single CIFAR-10 Batch
Instead of training the neural network on all the CIFAR-10 batches of data, let's use a single batch. This should save time while you iterate on the model to get a better accuracy.  Once the final validation accuracy is 50% or greater, run the model on all the data in the next section.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
print('Checking the Training on a Single Batch...')
with tf.Session() as sess:
    # Initializing the variables
    sess.run(tf.global_variables_initializer())

    # Training cycle
    for epoch in range(epochs):
        batch_i = 1
        for batch_features, batch_labels in helper.load_preprocess_training_batch(batch_i, batch_size):
            train_neural_network(sess, optimizer, keep_probability, batch_features, batch_labels)
        print('Epoch {:>2}, CIFAR-10 Batch {}:  '.format(epoch + 1, batch_i), end='')
        print_stats(sess, batch_features, batch_labels, cost, accuracy)


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Decoding Layer
Implement decoding_layer() to create a Decoder RNN layer.

Create RNN cell for decoding using rnn_size and num_layers.
Create the output fuction using lambda to transform it's input, logits, to class logits.
Use the your decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob) function to get the training logits.
Use your decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length, vocab_size, decoding_scope, output_fn, keep_prob) function to get the inference logits.

Note: You'll need to use tf.variable_scope to share variables between training and inference.
Can you write Python code for it?

def decoding_layer(dec_embed_input, dec_embeddings, encoder_state, vocab_size, sequence_length, rnn_size,
                   num_layers, target_vocab_to_int, keep_prob):
    """
    Create decoding layer
    :param dec_embed_input: Decoder embedded input
    :param dec_embeddings: Decoder embeddings
    :param encoder_state: The encoded state
    :param vocab_size: Size of vocabulary
    :param sequence_length: Sequence Length
    :param rnn_size: RNN Size
    :param num_layers: Number of layers
    :param target_vocab_to_int: Dictionary to go from the target words to an id
    :param keep_prob: Dropout keep probability
    :return: Tuple of (Training Logits, Inference Logits)
    """
    # refer to chc170's code
    with tf.variable_scope("decoding") as decoding_scope:
        dec_cell = tf.contrib.rnn.MultiRNNCell([tf.contrib.rnn.BasicLSTMCell(rnn_size)] * num_layers)
        output_fn = lambda x: tf.contrib.layers.fully_connected(x, vocab_size, None, scope=decoding_scope)
        train_logits = decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope, output_fn, keep_prob)
        decoding_scope.reuse_variables()
        infer_logits = decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, target_vocab_to_int['<GO>'], target_vocab_to_int['<EOS>'], sequence_length-1, vocab_size, decoding_scope, output_fn, keep_prob)

    return train_logits, infer_logits

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer(decoding_layer)


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Graph
Build the graph using the neural network you implemented.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
save_path = 'checkpoints/dev'
(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()
max_source_sentence_length = max([len(sentence) for sentence in source_int_text])

train_graph = tf.Graph()
with train_graph.as_default():
    input_data, targets, lr, keep_prob = model_inputs()
    sequence_length = tf.placeholder_with_default(max_source_sentence_length, None, name='sequence_length')
    input_shape = tf.shape(input_data)

    train_logits, inference_logits = seq2seq_model(
        tf.reverse(input_data, [-1]), targets, keep_prob, batch_size, sequence_length, len(source_vocab_to_int), len(target_vocab_to_int),
        encoding_embedding_size, decoding_embedding_size, rnn_size, num_layers, target_vocab_to_int)

    tf.identity(inference_logits, 'logits')
    with tf.name_scope("optimization"):
        # Loss function
        cost = tf.contrib.seq2seq.sequence_loss(
            train_logits,
            targets,
            tf.ones([input_shape[0], sequence_length]))

        # Optimizer
        optimizer = tf.train.AdamOptimizer(lr)

        # Gradient Clipping
        gradients = optimizer.compute_gradients(cost)
        capped_gradients = [(tf.clip_by_value(grad, -1., 1.), var) for grad, var in gradients if grad is not None]
        train_op = optimizer.apply_gradients(capped_gradients)


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Build the Neural Network
You'll build the components necessary to build a Sequence-to-Sequence model by implementing the following functions below:
- model_inputs
- process_decoding_input
- encoding_layer
- decoding_layer_train
- decoding_layer_infer
- decoding_layer
- seq2seq_model
Input
Implement the model_inputs() function to create TF Placeholders for the Neural Network. It should create the following placeholders:

Input text placeholder named "input" using the TF Placeholder name parameter with rank 2.
Targets placeholder with rank 2.
Learning rate placeholder with rank 0.
Keep probability placeholder named "keep_prob" using the TF Placeholder name parameter with rank 0.

Return the placeholders in the following the tuple (Input, Targets, Learing Rate, Keep Probability)
Can you write Python code for it?

def model_inputs():
    """
    Create TF Placeholders for input, targets, and learning rate.
    :return: Tuple (input, targets, learning rate, keep probability)
    """
    inputs = tf.placeholder(tf.int32, [None, None], name='input')
    targets = tf.placeholder(tf.int32, [None, None], name='targets')
    learning_rate = tf.placeholder(tf.float32, None, name='learning_rate')
    keep_prob = tf.placeholder(tf.float32, None, name='keep_prob')

    return inputs, targets, learning_rate, keep_prob

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_model_inputs(model_inputs)


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Check Point
This is your first checkpoint. If you ever decide to come back to this notebook or have to restart the notebook, you can start from here. The preprocessed data has been saved to disk.
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
import numpy as np
import helper

(source_int_text, target_int_text), (source_vocab_to_int, target_vocab_to_int), _ = helper.load_preprocess()


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Check the Version of TensorFlow and Access to GPU
This will check to make sure you have the correct version of TensorFlow and access to a GPU
Can you write Python code for it?

"""
DON'T MODIFY ANYTHING IN THIS CELL
"""
from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) in [LooseVersion('1.0.0'), LooseVersion('1.0.1')], 'This project requires TensorFlow version 1.0  You are using {}'.format(tf.__version__)
print('TensorFlow Version: {}'.format(tf.__version__))

# Check for a GPU
if not tf.test.gpu_device_name():
    warnings.warn('No GPU found. Please use a GPU to train your neural network.')
else:
    print('Default GPU Device: {}'.format(tf.test.gpu_device_name()))


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Decoding - Inference
Create inference logits using tf.contrib.seq2seq.simple_decoder_fn_inference() and tf.contrib.seq2seq.dynamic_rnn_decoder().
Can you write Python code for it?

def decoding_layer_infer(encoder_state, dec_cell, dec_embeddings, start_of_sequence_id, end_of_sequence_id,
                         maximum_length, vocab_size, decoding_scope, output_fn, keep_prob):
    """
    Create a decoding layer for inference
    :param encoder_state: Encoder state
    :param dec_cell: Decoder RNN Cell
    :param dec_embeddings: Decoder embeddings
    :param start_of_sequence_id: GO ID
    :param end_of_sequence_id: EOS Id
    :param maximum_length: The maximum allowed time steps to decode
    :param vocab_size: Size of vocabulary
    :param decoding_scope: TensorFlow Variable Scope for decoding
    :param output_fn: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: Inference Logits
    """
    inference_decoder_fn = tf.contrib.seq2seq.simple_decoder_fn_inference(output_fn, encoder_state, dec_embeddings, start_of_sequence_id, end_of_sequence_id, maximum_length - 1, vocab_size)
    inference_pred, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder(dec_cell, inference_decoder_fn, scope=decoding_scope)
    inference_logits = tf.nn.dropout(inference_pred, keep_prob)

    return inference_logits

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer_infer(decoding_layer_infer)


I am working on the file "DLND-language-translation/dlnd_language_translation.ipynb".
The first task is:
Decoding - Training
Create training logits using tf.contrib.seq2seq.simple_decoder_fn_train() and tf.contrib.seq2seq.dynamic_rnn_decoder().  Apply the output_fn to the tf.contrib.seq2seq.dynamic_rnn_decoder() outputs.
Can you write Python code for it?

def decoding_layer_train(encoder_state, dec_cell, dec_embed_input, sequence_length, decoding_scope,
                         output_fn, keep_prob):
    """
    Create a decoding layer for training
    :param encoder_state: Encoder State
    :param dec_cell: Decoder RNN Cell
    :param dec_embed_input: Decoder embedded input
    :param sequence_length: Sequence Length
    :param decoding_scope: TenorFlow Variable Scope for decoding
    :param output_fn: Function to apply the output layer
    :param keep_prob: Dropout keep probability
    :return: Train Logits
    """
    train_decoder_fn = tf.contrib.seq2seq.simple_decoder_fn_train(encoder_state)
    train_pred, _, _ = tf.contrib.seq2seq.dynamic_rnn_decoder( dec_cell, train_decoder_fn, dec_embed_input, sequence_length, scope=decoding_scope)
    train_logits = output_fn(tf.nn.dropout(train_pred, keep_prob))

    return train_logits

"""
DON'T MODIFY ANYTHING IN THIS CELL THAT IS BELOW THIS LINE
"""
tests.test_decoding_layer_train(decoding_layer_train)