I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
Maybe the NYPD deals with different issues at different times? Check the most popular complaints in July and August vs the month of May. Also check the most common complaints for the Housing Preservation Bureau (HPD) in winter vs. summer.
Can you write Python code for it?

nypd=df[df['Agency']=='NYPD']

nypd[(nypd.index.month==7) | (nypd.index.month==8)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)

nypd[nypd.index.month==5].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)

# seems like mostly noise complaints and bad parking to me

hpd=df[df['Agency']=='HPD']

hpd[(hpd.index.month>=6) & (hpd.index.month<=8)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)
# i would consider summer to be june to august.

hpd[(hpd.index.month==12) | (hpd.index.month<=2)].groupby('Complaint Type')['Complaint Type'].count().sort_values(ascending=False).head(5)

# pretty similar list, but people probably notice a draft from their bad window or door in the winter more easily than summer


I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
Noise complaints are a big deal. Use .str.contains to select noise complaints, and make an chart of when they show up annually. Then make a chart about when they show up every day (cyclic).
Can you write Python code for it?

noise_df=df[df['Complaint Type'].str.contains('Noise')]
noise_df.resample('M').count().plot(y='Unique Key')

noise_df.groupby(by=noise_df.index.hour).count().plot(y='Unique Key')


I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
So odd. What's the per-minute breakdown of complaints between 12am and 1am? You don't need to include 1am.
Can you write Python code for it?

midnight_df = df[df.index.hour==0]

midnight_df.groupby(midnight_df.index.minute)['Unique Key'].count().sort_values(ascending=False)


I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
What hour of the day are the most complaints? Graph a day of complaints.
Can you write Python code for it?

df['Unique Key'].groupby(by=df.index.hour).count().sort_values(ascending=False)

df['Unique Key'].groupby(df.index.hour).count().plot()


I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
What month has the most reports filed? How many? Graph it.
Can you write Python code for it?

df.resample('M')['Unique Key'].count().sort_values(ascending=False)

df.resample('M').count().plot(y='Unique Key')


I am working on the file "12/homework 12 - 311 time series homework.ipynb".
The first task is:
Which borough has the most complaints per capita? Since it's only 5 boroughs, you can do the math manually.
Can you write Python code for it?

df.groupby(by='Borough')['Borough'].count()

boro_pop={
    'BRONX': 1438159,
    'BROOKLYN': 2621793,
    'MANHATTAN': 1636268,
    'QUEENS': 2321580,
    'STATEN ISLAND': 473279}

boro_df=pd.Series.to_frame(df.groupby(by='Borough')['Borough'].count())
boro_df['Population']=pd.DataFrame.from_dict(boro_pop, orient='index')
boro_df['Complaints']=boro_df['Borough']
boro_df.drop('Borough', axis=1, inplace=True)
boro_df['Per Capita']=boro_df['Complaints']/boro_df['Population']
boro_df['Per Capita'].plot(kind='bar')


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

Результат выполненного запроса будет выглядеть как:

column name	row_number	driver_id	 start_timestamp	 status

data type	(Int32)	(String)	(String)	(String)

1	1	driver_id_1	2017-01-21 00:05	driving

2	2	driver_id_1	2017-01-21 00:09	waiting

...	...	...	...	...

k x n	k	driver_id_n	2017-01-21 23:49	transporting

Вопрос: как нужно переписать код, чтобы реализовать функцию LEAD(), т.е. добавить запись следующего статуса водителя в соседней колонке? Для выполнения задания требуется переписать код.
Type your code below
Can you write Python code for it?

def LEAD(driver_id, input_data):
    sorted_data = sorted(input_data, lambda x: x[0]) # сортируем список входных данных по дате
    result = []
    row_number = 0
    while row_number < len(input_data) - 1: # для всех состояний конкретного водителя, кроме финального, добавляем ещё одно значеник
        row_data = {'row_number': row_number
                    , 'driver_id': driver_id
                    , 'start_timestamp': sorted_data[row_number][0]
                    , 'status': sorted_data[row_number][1]
                    , 'status_next': sorted_data[row_number + 1][1]
                    }
        row_number += 1
        result.append(row_data)
    row_data = {'row_number': row_number
                , 'driver_id': driver_id
                , 'start_timestamp': sorted_data[row_number][0]
                , 'status': sorted_data[row_number][1]
                , 'status_next': None # если состояние водителя финальное, то ставим в следующий статус значение None
                }
    result.append(row_data)
    return result


I am working on the file "12042.ipynb".
The first task is:
По аналогии требуется написать функцию, которая будет возвращать текущее и следующее значения.
<i>Type your code below</i>
Can you write Python code for it?

def this_and_next(iterable): # немного изменил код по сравнению с предыдущей функцией, если следующего значения нет, возвращаем ноль
    iterator = iter(iterable)
    curr_item = next(iterator)
    for next_item in iterator:
        yield (curr_item, next_item)
        curr_item = next_item
    yield (curr_item, None)

for i,j in this_and_next( range(5) ):
    print(i,j)


I am working on the file "12_1D_SH_Spectral_Elements/3_se_homo_1d.ipynb".
The first task is:
1. Initialization of setup
Can you write Python code for it?

# Initialization of setup
# ---------------------------------------------------------------
nt    = 10000         # number of time steps
xmax  = 10000.        # Length of domain [m]
vs    = 2500.         # S velocity [m/s]
rho   = 2000          # Density [kg/m^3]
mu    = rho * vs**2   # Shear modulus mu
N     = 3             # Order of Lagrange polynomials
ne    = 250           # Number of elements
Tdom  = .2            # Dominant period of Ricker source wavelet
iplot = 20            # Plotting each iplot snapshot

# variables for elemental matrices
Me = np.zeros(N+1, dtype =  float)
Ke = np.zeros((N+1, N+1), dtype =  float)
# ----------------------------------------------------------------

# Initialization of GLL points integration weights
[xi, w] = gll(N)    # xi, N+1 coordinates [-1 1] of GLL points
                    # w Integration weights at GLL locations
# Space domain
le = xmax/ne        # Length of elements
# Vector with GLL points
k = 0
xg = np.zeros((N*ne)+1)
xg[k] = 0
for i in range(1,ne+1):
    for j in range(0,N):
        k = k+1
        xg[k] = (i-1)*le + .5*(xi[j+1]+1)*le

# ---------------------------------------------------------------
dxmin = min(np.diff(xg))
eps = 0.1           # Courant value
dt = eps*dxmin/vs   # Global time step

# Mapping - Jacobian
J = le/2
Ji = 1/J    # Inverse Jacobian

# 1st derivative of Lagrange polynomials
l1d = lagrange1st(N)   # Array with GLL as columns for each N+1 polynomial


I am working on the file "12_1D_SH_Spectral_Elements/3_se_homo_1d.ipynb".
The first task is:
2. The Mass Matrix
Now we initialize the mass and stiffness matrices. In general, the mass matrix at the elemental level is given
\begin{equation}
M_{ji}^e \ = \ w_j  \ \rho (\xi)  \ \frac{\mathrm{d}x}{\mathrm{d}\xi} \delta_{ij} \vert_ {\xi = \xi_j}
\end{equation}
Exercise 1
Implement the mass matrix using the integration weights at GLL locations $w$, the jacobian $J$, and density $\rho$. Then, perform the global assembly of the mass matrix, compute its inverse, and display the inverse mass matrix to visually inspect how it looks like.
Can you write Python code for it?


I am working on the file "12_1D_SH_Spectral_Elements/4_se_hetero_1d.ipynb".
The first task is:
1. Initialization of setup
Can you write Python code for it?

# Initialization of setup
# ---------------------------------------------------------------
nt    = 10000         # number of time steps
xmax  = 8000.         # Length of domain [m]
N     = 3             # Order of Lagrange polynomials
ne    = 250           # Number of elements
Tdom  = .2            # Dominant period of Ricker source wavelet
iplot = 20            # Plotting each iplot snapshot
vs    = 2500.         # S velocity [m/s]
rho   = 2000          # Density [kg/m^3]

# variables for elemental matrices
Me = np.zeros(N+1, dtype =  float)
Ke = np.zeros((N+1, N+1), dtype =  float)
# ----------------------------------------------------------------

# Initialization of GLL points integration weights
[xi, w] = gll(N)    # xi, N+1 coordinates [-1 1] of GLL points
                    # w Integration weights at GLL locations
# Space domain
le = xmax/ne        # Length of elements
# Vector with GLL points
k = 0
xg = np.zeros((N*ne)+1)
xg[k] = 0
for i in range(1,ne+1):
    for j in range(0,N):
        k = k+1
        xg[k] = (i-1)*le + .5*(xi[j+1]+1)*le

# ---------------------------------------------------------------
dxmin = min(np.diff(xg))
eps = 0.1           # Courant value
dt = eps*dxmin/vs   # Global time step

# Mapping - Jacobian
J = le/2
Ji = 1/J    # Inverse Jacobian

# 1st derivative of Lagrange polynomials
l1d = lagrange1st(N)   # Array with GLL as columns for each N+1 polynomial


I am working on the file "12_1D_SH_Spectral_Elements/4_se_hetero_1d.ipynb".
The first task is:
3. The Mass Matrix
Now we initialize the mass and stiffness matrices. In general, the mass matrix at the elemental level is given
\begin{equation}
M_{ji}^e \ = \ w_j  \ \rho (\xi)  \ \frac{\mathrm{d}x}{\mathrm{d}\xi} \delta_{ij} \vert_ {\xi = \xi_j}
\end{equation}
Exercise 2
Implements the mass matrix at each element using the integration weights at GLL locations $w$, the Jacobian $J$, and density $\rho$. Then, perform the global assembly of the mass matrix, compute its inverse, and display the inverse mass matrix to visually inspect how it looks like.
Can you write Python code for it?


I am working on the file "12_1D_SH_Spectral_Elements/4_se_hetero_1d.ipynb".
The first task is:
4. The Stiffness matrix
On the other hand, the general form of the stiffness matrix at the elemental level is
\begin{equation}
K_{ji}^e \ = \ \sum_{k = 1}^{N+1} w_k \mu (\xi) \partial_\xi \ell_j (\xi) \partial_\xi \ell_i (\xi) \left(\frac{\mathrm{d}\xi}{\mathrm{d}x} \right)^2 \frac{\mathrm{d}x}{\mathrm{d}\xi} \vert_{\xi = \xi_k}
\end{equation}
Exercise 3
Implements the stiffness matrix at each element using the integration weights at GLL locations $w$, the Jacobian $J$, and shear stress $\mu$. Then, perform the global assembly of the mass matrix and display the matrix to visually inspect how it looks like.
Can you write Python code for it?


I am working on the file "12_1D_SH_Spectral_Elements/4_se_hetero_1d.ipynb".
The first task is:
5. Finite element solution
Finally we implement the spectral element solution using the computed mass $M$ and stiffness $K$ matrices together with a finite differences extrapolation scheme
\begin{equation}
\mathbf{u}(t + dt) = dt^2 (\mathbf{M}^T)^{-1}[\mathbf{f} - \mathbf{K}^T\mathbf{u}] + 2\mathbf{u} - \mathbf{u}(t-dt).
\end{equation}
Can you write Python code for it?

# SE Solution, Time extrapolation
# ---------------------------------------------------------------

# initialize source time function and force vector f
src  = ricker(dt,Tdom)
isrc = int(np.floor(ng/2))   # Source location

# Initialization of solution vectors
u = np.zeros(ng)
uold = u
unew = u
f = u

# Initialize animated plot
# ---------------------------------------------------------------
plt.figure(figsize=(10,6))

# ploting low velocity zone
half = np.floor(xmax/2)
plt.axvspan(half - el_span/2*le,
            half + el_span/2*le,
            alpha=0.2, facecolor='b')
plt.title('SEM 1D Animation')
plt.xlabel(' x (m)')
plt.ylabel(' Amplitude ')
font = {'family': 'serif',
        'color':  'black',
        'weight': 'normal',
        'size': 16,
        }

lines = plt.plot(xg, u, lw=1.5)
plt.title('SEM 1D Animation   $v_{min} = %s v_{max}$' %percent, size=16)
plt.xlabel(' x (m)')
plt.ylabel(' Displacement ')

plt.ion() # set interective mode
plt.show()

# ---------------------------------------------------------------
# Time extrapolation
# ---------------------------------------------------------------
x_t = []
for it in range(nt):
    # Source initialization
    f = np.zeros(ng)
    if it < len(src):
        f[isrc-1] = src[it-1]

    # Time extrapolation
    unew = dt**2 * Minv @ (f - K @ u) + 2 * u - uold
    uold, u = u, unew

    # Solution in space-time
    x_t.append(u)

    # --------------------------------------
    # Animation plot. Display solution
    if not it % iplot:
        for l in lines:
            l.remove()
            del l
        # --------------------------------------
        # Display lines
        lines = plt.plot(xg, u, color="black", lw = 1.5)
        plt.xlim((0,xmax))
        plt.gcf().canvas.draw()


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Adversarial Noise
The pixels in the input image are float-values between 0.0 and 1.0. The adversarial noise is a number that is added or subtracted from the pixels in the input image.
The limit of the adversarial noise is set to 0.35 so the noise will be between ±0.35.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Adversarial noise for all target-classes
This is a helper-function for finding the adversarial noise for all target-classes. The function loops over all the class-numbers from 0 to 9 and runs the optimization above. The results are then stored in an array.
Can you write Python code for it?

def find_all_noise(num_iterations=1000):
    # Adversarial noise for all target-classes.
    all_noise = []

    # For each target-class.
    for i in range(num_classes):
        print("Finding adversarial noise for target-class:", i)

        # Reset the adversarial noise to zero.
        init_noise()

        # Optimize the adversarial noise.
        optimize(num_iterations=num_iterations,
                 adversary_target_cls=i)

        # Get the adversarial noise from inside the TensorFlow graph.
        noise = get_noise()

        # Append the noise to the array.
        all_noise.append(noise)

        # Print newline.
        print()

    return all_noise

all_noise = find_all_noise(num_iterations=300)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Below is the function for performing a number of optimization iterations so as to gradually improve the variables of the neural network. In each iteration, a new batch of data is selected from the training-set and then TensorFlow executes the optimizer using those training samples. The progress is printed every 100 iterations.
This function is similar to the previous tutorials, except that it now takes an argument for the adversarial target-class. When this target-class is set to an integer, it will be used instead of the true class-number for the training-data. The adversarial optimizer is also used instead of the normal optimizer, and after each step of the adversarial optimizer, the noise will be limited / clipped to the allowed range. This optimizes the adversarial noise and ignores the other variables of the neural network.
Can you write Python code for it?

def optimize(num_iterations, adversary_target_cls=None):
    # Start-time used for printing time-usage below.
    start_time = time.time()

    for i in range(num_iterations):

        # Get a batch of training examples.
        # x_batch now holds a batch of images and
        # y_true_batch are the true labels for those images.
        x_batch, y_true_batch = data.train.next_batch(train_batch_size)

        # If we are searching for the adversarial noise, then
        # use the adversarial target-class instead.
        if adversary_target_cls is not None:
            # The class-labels are One-Hot encoded.

            # Set all the class-labels to zero.
            y_true_batch = np.zeros_like(y_true_batch)

            # Set the element for the adversarial target-class to 1.
            y_true_batch[:, adversary_target_cls] = 1.0

        # Put the batch into a dict with the proper names
        # for placeholder variables in the TensorFlow graph.
        feed_dict_train = {x: x_batch,
                           y_true: y_true_batch}

        # If doing normal optimization of the neural network.
        if adversary_target_cls is None:
            # Run the optimizer using this batch of training data.
            # TensorFlow assigns the variables in feed_dict_train
            # to the placeholder variables and then runs the optimizer.
            session.run(optimizer, feed_dict=feed_dict_train)
        else:
            # Run the adversarial optimizer instead.
            # Note that we have 'faked' the class above to be
            # the adversarial target-class instead of the true class.
            session.run(optimizer_adversary, feed_dict=feed_dict_train)

            # Clip / limit the adversarial noise. This executes
            # another TensorFlow operation. It cannot be executed
            # in the same session.run() as the optimizer, because
            # it may run in parallel so the execution order is not
            # guaranteed. We need the clip to...


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Call the function to initialize the adversarial noise.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Combine the normal loss-function with the L2-loss for the adversarial noise.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Convolutional Neural Network
We will use PrettyTensor to construct the convolutional neural network. First we need to wrap the tensor for the noisy image in a PrettyTensor-object, which provides functions that construct the neural network.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Convolutional Neural Network
We will use the Layers API to construct the convolutional neural network, see Tutorial #03-B.
Can you write Python code for it?

# Start the network with the noisy input image.
net = x_noisy_image

# 1st convolutional layer.
net = tf.layers.conv2d(inputs=net, name='layer_conv1', padding='same',
                       filters=16, kernel_size=5, activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)

# 2nd convolutional layer.
net = tf.layers.conv2d(inputs=net, name='layer_conv2', padding='same',
                       filters=36, kernel_size=5, activation=tf.nn.relu)
net = tf.layers.max_pooling2d(inputs=net, pool_size=2, strides=2)

# Flatten layer.This should eventually be replaced by:
# net = tf.layers.flatten(net)
net = tf.contrib.layers.flatten(net)

# 1st fully-connected / dense layer.
net = tf.layers.dense(inputs=net, name='layer_fc1',
                      units=128, activation=tf.nn.relu)

# 2nd fully-connected / dense layer.
net = tf.layers.dense(inputs=net, name='layer_fc_out',
                      units=num_classes, activation=None)

# Unscaled output of the network.
logits = net

# Softmax output of the network.
y_pred = tf.nn.softmax(logits=logits)

# Loss measure to be optimized.
cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true,
                                                           logits=logits)
loss = tf.reduce_mean(cross_entropy)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Copy some of the data-dimensions for convenience.
Can you write Python code for it?

# The number of pixels in each dimension of an image.
img_size = data.img_size

# The images are stored in one-dimensional arrays of this length.
img_size_flat = data.img_size_flat

# Tuple with height and width of images used to reshape arrays.
img_shape = data.img_shape

# Number of classes, one class for each of 10 digits.
num_classes = data.num_classes

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = data.num_channels


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Data Dimensions
The data dimensions are used in several places in the source-code below. They are defined once so we can use these variables instead of numbers throughout the source-code below.
Can you write Python code for it?

# We know that MNIST images are 28 pixels in each dimension.
img_size = 28

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1

# Number of classes, one class for each of 10 digits.
num_classes = 10


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Find the adversarial noise
Before we start optimizing the adversarial noise, we first initialize it to zero. This was already done above but it is repeated here in case you want to re-run this code with another target-class.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Helper-function for showing the performance
Function for printing the classification accuracy on the test-set.
It takes a while to compute the classification for all the images in the test-set, that's why the results are re-used by calling the above functions directly from this function, so the classifications don't have to be recalculated by each function.
Note that this function can use a lot of computer memory, which is why the test-set is split into smaller batches. If you have little RAM in your computer and it crashes, then you can try and lower the batch-size.
Can you write Python code for it?

# Split the test-set into smaller batches of this size.
test_batch_size = 256

def print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    # Number of images in the test-set.
    num_test = data.num_test

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + test_batch_size, num_test)

        # Get the images from the test-set between index i and j.
        images = data.x_test[i:j, :]

        # Get the associated labels.
        labels = data.y_test[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = data.y_test_cls

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

   ...


# Split the test-set into smaller batches of this size.
test_batch_size = 256

def print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    # Number of images in the test-set.
    num_test = len(data.test.images)

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + test_batch_size, num_test)

        # Get the images from the test-set between index i and j.
        images = data.test.images[i:j, :]

        # Get the associated labels.
        labels = data.test.labels[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = data.test.cls

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum,...


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Helper-function to perform optimization iterations
There are 55,000 images in the training-set. It takes a long time to calculate the gradient of the model using all these images. We therefore only use a small batch of images in each iteration of the optimizer.
If your computer crashes or becomes very slow because you run out of RAM, then you may try and lower this number, but you may then need to perform more optimization iterations.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Helper-function to plot example errors
Function for plotting examples of images from the test-set that have been mis-classified.
Can you write Python code for it?

def plot_example_errors(cls_pred, correct):
    # This function is called from print_test_accuracy() below.

    # cls_pred is an array of the predicted class-number for
    # all images in the test-set.

    # correct is a boolean array whether the predicted class
    # is equal to the true class for each image in the test-set.

    # Negate the boolean array.
    incorrect = (correct == False)

    # Get the images from the test-set that have been
    # incorrectly classified.
    images = data.x_test[incorrect]

    # Get the predicted classes for those images.
    cls_pred = cls_pred[incorrect]

    # Get the true classes for those images.
    cls_true = data.y_test_cls[incorrect]

    # Get the adversarial noise from inside the TensorFlow graph.
    noise = get_noise()

    # Plot the first 9 images.
    plot_images(images=images[0:9],
                cls_true=cls_true[0:9],
                cls_pred=cls_pred[0:9],
                noise=noise)


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

%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
from datetime import timedelta
import math

# We also need PrettyTensor.
import prettytensor as pt


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Initialize variables
The variables for weights and biases must be initialized before we start optimizing them.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Interestingly, the neural network now has a higher classification accuracy on noisy images than we had on clean images before all these optimizations.
Can you write Python code for it?

print_test_accuracy(show_example_errors=True,
                    show_confusion_matrix=True)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Load Data
The MNIST data-set is about 12 MB and will be downloaded automatically if it is not located in the given path.
Can you write Python code for it?

from mnist import MNIST
data = MNIST(data_dir="data/MNIST/")


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Make immune to all target-classes (double runs)
Now try and use double-runs to make the neural network immune to adversarial noise for all target-classes. Unfortunately this does not seem to work so well either.
Making the neural network immune to one adversarial target-class appears to cancel the immunity towards the other target-classes.
Can you write Python code for it?

for i in range(10):
    make_immune(target_cls=i)

    # Print newline.
    print()

    make_immune(target_cls=i)

    # Print newline.
    print()


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Make immune to noise for all target-classes
Now try and make the neural network immune to adversarial noise for all target-classes. Unfortunately this does not seem to work so well.
Can you write Python code for it?

for i in range(10):
    make_immune(target_cls=i)

    # Print newline.
    print()


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Make immune to noise for target-class 3
First try and make the neural network immune to the adverserial noise for targer-class 3.
First we find the adversarial noise that causes the neural network to mis-classify most of the images in the test-set. Then we run the normal optimization which fine-tunes the variables of the neural network to ignore this noise and this brings the classification accuracy for the noisy images up to 95-97% again.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Next we have the placeholder variable for the true labels associated with the images that were input in the placeholder variable x. The shape of this placeholder variable is [None, num_classes] which means it may hold an arbitrary number of labels and each label is a vector of length num_classes which is 10 in this case.
Can you write Python code for it?

y_true = tf.placeholder(tf.float32, shape=[None, num_classes], name='y_true')


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Normal optimization of neural network
First we perform 1000 optimization iterations with the normal optimizer. This finds the variables that makes the neural network perform well on the training-set.
The adversarial noise is not effective yet because it has only been initialized to zero above and it is not being updated during this optimization.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Now perform optimization of the adversarial noise. This uses the adversarial optimizer instead of the normal optimizer, which means that it only optimizes the variable for the adversarial noise, while ignoring all the other variables of the neural network.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Now that we have wrapped the input image in a PrettyTensor object, we can add the convolutional and fully-connected layers in just a few lines of source-code.
Can you write Python code for it?

with pt.defaults_scope(activation_fn=tf.nn.relu):
    y_pred, loss = x_pretty.\
        conv2d(kernel=5, depth=16, name='layer_conv1').\
        max_pool(kernel=2, stride=2).\
        conv2d(kernel=5, depth=36, name='layer_conv2').\
        max_pool(kernel=2, stride=2).\
        flatten().\
        fully_connected(size=128, name='layer_fc1').\
        softmax_classifier(num_classes=num_classes, labels=y_true)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Now try and run it again. It is now more difficult to find adversarial noise for the target-class 3. The neural network seems to have become somewhat immune to adversarial noise.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Now we can create the new variable for the adversarial noise. It will be initialized to zero. It will not be trainable, so it will not be optimized along with the other variables of the neural network. This allows us to create two separate optimization procedures.
Can you write Python code for it?

x_noise = tf.Variable(tf.zeros([img_size, img_size, num_channels]),
                      name='x_noise', trainable=False,
                      collections=collections)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Optimization of these variables in the neural network is done with the Adam-optimizer using the loss-measure that was returned from PrettyTensor when we constructed the neural network above.
Note that optimization is not performed at this point. In fact, nothing is calculated at all, we just add the optimizer-object to the TensorFlow graph for later execution.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Optimizer for Adversarial Noise
Get the list of variables that must be optimized in the second procedure for the adversarial noise.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Optimizer for Normal Training
This is a list of the variables for the neural network that will be trained during the normal optimization procedure. Note that 'x_noise:0' is not in the list, so the adversarial noise is not being optimized in the normal procedure.
Can you write Python code for it?

[var.name for var in tf.trainable_variables()]


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Performance on clean images
Now let us see how the neural network performs on clean images so we reset the adversarial noise to zero.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Plot a few images to see if data is correct
Can you write Python code for it?

# Get the first images from the test-set.
images = data.x_test[0:9]

# Get the true classes for those images.
cls_true = data.y_test_cls[0:9]

# Plot the images and labels using our helper-function above.
plot_images(images=images, cls_true=cls_true)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Plot the adversarial noise
We have now performed many optimizations of both the neural network and the adversarial noise. Let us see how the adversarial noise looks now.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Plot the adversarial noise for all target-classes
This is a helper-function for plotting a grid with the adversarial noise for all target-classes 0 to 9.
Can you write Python code for it?

def plot_all_noise(all_noise):
    # Create figure with 10 sub-plots.
    fig, axes = plt.subplots(2, 5)
    fig.subplots_adjust(hspace=0.2, wspace=0.1)

    # For each sub-plot.
    for i, ax in enumerate(axes.flat):
        # Get the adversarial noise for the i'th target-class.
        noise = all_noise[i]

        # Plot the noise.
        ax.imshow(noise,
                  cmap='seismic', interpolation='nearest',
                  vmin=-1.0, vmax=1.0)

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(i)

        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])

    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()

plot_all_noise(all_noise)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
PrettyTensor version:
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Red pixels show positive noise values, and blue pixels show negative noise values.
In some of these noise-images you can see traces of the numbers. For example, the noise for target-class 0 shows a red circle surrounded by blue. This means that a little noise will be added to the input image in the shape of a circle, and it will dampen the other pixels. This is sufficient for most input images in the MNIST data-set to be mis-classified as a 0. Another example is the noise for 3 which also shows traces of the number 3 with red pixels. But the noise for the other classes is less obvious.
Immunity to adversarial noise
We will now try and make the neural network immune to adversarial noise. We do this by re-training the neural network to ignore the adversarial noise. This process can be repeated a number of times.
Helper-function to make a neural network immune to noise
This is the helper-function for making the neural network immune to adversarial noise. First it runs the optimization to find the adversarial noise. Then it runs the normal optimization to make the neural network immune to that noise.
Can you write Python code for it?

def make_immune(target_cls, num_iterations_adversary=500,
                num_iterations_immune=200):

    print("Target-class:", target_cls)
    print("Finding adversarial noise ...")

    # Find the adversarial noise.
    optimize(num_iterations=num_iterations_adversary,
             adversary_target_cls=target_cls)

    # Newline.
    print()

    # Print classification accuracy.
    print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False)

    # Newline.
    print()

    print("Making the neural network immune to the noise ...")

    # Try and make the neural network immune to this noise.
    # Note that the adversarial noise has not been reset to zero
    # so the x_noise variable still holds the noise.
    # So we are training the neural network to ignore the noise.
    optimize(num_iterations=num_iterations_immune)

    # Newline.
    print()

    # Print classification accuracy.
    print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Show the list of variable-names. There is only one, which is the adversarial noise variable that we created above.
Can you write Python code for it?

[var.name for var in adversary_variables]


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
TensorFlow Graph
The computational graph for the neural network will now be constructed using TensorFlow and PrettyTensor. As usual, we need to create placeholder variables for feeding images into the graph and then we add the adversarial noise to the images. The noisy images are then used as input to a convolutional neural network.
There are two separate optimization procedures for this network. A normal optimization procedure for the variables of the neural network itself, and another optimization procedure for the adversarial noise. Both optimization procedures are implemented directly in TensorFlow.
Placeholder variables
Placeholder variables provide the input to the computational graph in TensorFlow that we may change each time we execute the graph. We call this feeding the placeholder variables.
First we define the placeholder variable for the input images. This allows us to change the images that are input to the TensorFlow graph. This is a so-called tensor, which just means that it is a multi-dimensional array. The data-type is set to float32 and the shape is set to [None, img_size_flat], where None means that the tensor may hold an arbitrary number of images with each image being a vector of length img_size_flat.
Can you write Python code for it?

x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
TensorFlow Run
Create TensorFlow session
Once the TensorFlow graph has been created, we have to create a TensorFlow session which is used to execute the graph.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The adversarial noise has now been optimized and it can be shown in a plot. The red pixels show positive noise-values and the blue pixels show negative noise-values. This noise-pattern is added to every input image. The positive (red) noise-values makes the pixels darker and the negative (blue) noise-values makes the pixels brighter. Examples of this are shown below.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The adversarial noise will be limited / clipped to the given
± noise-limit that we set above. Note that this is actually not executed at this point in the computational graph, but will instead be executed after the optimization-step, see further below.
Can you write Python code for it?

x_noise_clip = tf.assign(x_noise, tf.clip_by_value(x_noise,
                                                   -noise_limit,
                                                   noise_limit))


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The classification accuracy is calculated by first type-casting the vector of booleans to floats, so that False becomes 0 and True becomes 1, and then taking the average of these numbers.
Can you write Python code for it?

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The convolutional layers expect x to be encoded as a 4-dim tensor so we have to reshape it so its shape is instead [num_images, img_height, img_width, num_channels]. Note that img_height == img_width == img_size and num_images can be inferred automatically by using -1 for the size of the first dimension. So the reshape operation is:
Can you write Python code for it?

x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The neural network now performs worse on clean images compared to noisy images.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The noisy image is just the sum of the input image and the adversarial noise.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
The optimizer for the adversarial noise will try and minimize two loss-measures: (1) The normal loss-measure for the neural network, so we will find the noise that gives the best classification accuracy for the adversarial target-class; and (2) the so-called L2-loss-measure which tries to keep the noise as low as possible.
The following weight determines how important the L2-loss is compared to the normal loss-measure. An L2-weight close to zero usually works best.
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
Then we create a list of the collections that we want the new noise-variable to belong to. If we add the noise-variable to the collection tf.GraphKeys.VARIABLES then it will also get initialized with all the other variables in the TensorFlow graph, but it will not get optimized. This is a bit confusing.
Can you write Python code for it?

collections = [tf.GraphKeys.VARIABLES, ADVERSARY_VARIABLES]


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
This function plots the adversarial noise and prints some statistics.
Can you write Python code for it?

def plot_noise():
    # Get the adversarial noise from inside the TensorFlow graph.
    noise = get_noise()

    # Print statistics.
    print("Noise:")
    print("- Min:", noise.min())
    print("- Max:", noise.max())
    print("- Std:", noise.std())

    # Plot the noise.
    plt.imshow(noise, interpolation='nearest', cmap='seismic',
               vmin=-1.0, vmax=1.0)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
This was developed using Python 3.5.2 (Anaconda) and TensorFlow version:
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
This was developed using Python 3.6 (Anaconda) and TensorFlow version:
Can you write Python code for it?


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
We can now create the optimizer for the adversarial noise. Because this optimizer is not supposed to update all the variables of the neural network, we must give it a list of the variables that we want updated, which is the variable for the adversarial noise. Also note the learning-rate is much greater than for the normal optimizer above.
Can you write Python code for it?

optimizer_adversary = tf.train.AdamOptimizer(learning_rate=1e-2).minimize(loss_adversary, var_list=adversary_variables)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
We could also have a placeholder variable for the class-number, but we will instead calculate it using argmax. Note that this is a TensorFlow operator so nothing is calculated at this point.
Can you write Python code for it?

y_true_cls = tf.argmax(y_true, dimension=1)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
We have now created two optimizers for the neural network, one for the variables of the neural network and another for the single variable with the adversarial noise.
Performance Measures
We need a few more operations in the TensorFlow graph which will make it easier for us to display the progress to the user during optimization.
First we calculate the predicted class number from the output of the Neural Network y_pred, which is a vector with 10 elements. The class number is the index of the largest element.
Can you write Python code for it?


y_pred_cls = tf.argmax(y_pred, dimension=1)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
We will combine the loss-function for the normal optimization with a so-called L2-loss for the noise-variable. This should result in the minimum values for the adversarial noise along with the best classification accuracy.
The L2-loss is scaled by a weight that is typically set close to zero.
Can you write Python code for it?

l2_loss_noise = noise_l2_weight * tf.nn.l2_loss(x_noise)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
When adding the noise to the input image, it may overflow the boundaries for a valid image, so we clip / limit the noisy image to ensure its pixel-values are between 0 and 1.
Can you write Python code for it?

x_noisy_image = tf.clip_by_value(x_noisy_image, 0.0, 1.0)


I am working on the file "12_Adversarial_Noise_MNIST.ipynb".
The first task is:
When this noise is added to all the images in the test-set, the result is typically a classification accuracy of 10-15% depending on the target-class that was chosen. We can also see from the confusion matrix that most images in the test-set are now classified as the desired target-class - although some of the target-classes require more adversarial noise than others.
So we have found adversarial noise that makes the neural network mis-classify almost all images in the test-set as our desired target-class.
We can also show some examples of mis-classified images with the adversarial noise. The noise is clearly visible but the digits are still easily identified by the human eye.
Can you write Python code for it?

print_test_accuracy(show_example_errors=True,
                    show_confusion_matrix=True)


I am working on the file "12_Eigenvalues_And_Singular_Values.ipynb".
The first task is:
12.1 power Iteration methods
Definition
Let A be an $m \times m$ matrix. A dominant eigenvalue of A is an eigenvalue $\lambda$ whose magnitude is greater than all other eigenvalues of A. If it exists, an eigenvector associated to $\lambda$ is called a dominant eigenvector
Eigenvalue equation $x\lambda = Ax$, where $x$ is an approximate eigenvector and $\lambda$ is unknown. <br/>
The least squares solution of $x^{T}x\lambda = Ax$, or
$$
    \lambda = \frac{x^{T}Ax}{x^{T}x}
$$
known  as the Rayleigh quotient
Can you write Python code for it?

def power_iteration(A, x, k=10):
    """
    Compute the dominant eigenvalue and eigenvector of a matrix A

    Arguments:
        - A : A matrix
        - x : initial and nonzero vector
        - k : number of steps (default:10)

    Returns:
        - eigval : dominant eigenvalue
        - eigvec : dominant eigenvector

    Exceptions:
        - ValueError:
            - x is a zero vector
    """
    if is_zero_vector(x):
        raise ValueError('x is a zero vector')
    for _ in range(k):
        eigvec = x / np.linalg.norm(x)
        x = np.matmul(A, eigvec)
        eigval = np.matmul(np.matmul(eigvec.T, A), eigvec)
    eigvec = x / np.linalg.norm(x)
    return eigval, eigvec


I am working on the file "12_Eigenvalues_And_Singular_Values.ipynb".
The first task is:
12.2 QR Algorithm
Normalized Simultaneous Iteration
Can you write Python code for it?

def normalized_simultaneous_iteration(A, k=10):
    """
    Compute the eigenvalue and eigenvector of a symmetric matrix A

    Arguments:
        - A : A matrix
        - k : number of steps (default:10)

    Returns:
        - eigval : dominant eigenvalue
        - eigvec : dominant eigenvector
    """
    m, n = A.shape
    Q = np.eye(m)
    for _ in range(k):
        Q, R = np.linalg.qr(np.matmul(A, Q))
    eigval = np.diag(np.matmul(np.matmul(Q.T, A), Q))
    eigvec = Q
    return eigval, eigvec


I am working on the file "12_Eigenvalues_And_Singular_Values.ipynb".
The first task is:
Example
$A =
\begin{bmatrix}
3 & 2 & 4 \
2 & 1 & 2 \
4 & 2 & 3 \
\end{bmatrix}
$
where
$
\left{\begin{matrix}\begin{align}
\lambda_{1} &= -1 \
v_{1} &= (-x_1, 0, x_1)
\end{align}\end{matrix}\right.
$

$
\left{\begin{matrix}\begin{align}
\lambda_{2} &= -\sqrt{17} + 4 \approx -0.1231056256176605 \
v_{2} &= (x_2, \frac{-\sqrt{17}-3}{2} \times x_2, x_2)
\end{align}\end{matrix}\right.
$

$
\left{\begin{matrix}\begin{align}
\lambda_{3} &= \sqrt{17} + 4 \approx 8.1231056256176605 \
v_{3} &= (x_3, \frac{\sqrt{17}-3}{2} \times x_3, x_3)
\end{align}\end{matrix}\right.
$
Can you write Python code for it?

A = np.array([
    [3,   2,  4],
    [2,   1,  2],
    [4,   2,  3]
])

eigval, eigvec = normalized_simultaneous_iteration(A)

print('eigenvalues : ')
print(eigval)
print()
print('eigenvectors : ')
print(eigvec)

print()

eigval, eigvec = np.linalg.eig(A)
print('eigenvalues (np.linalg.eig) : ')
print(eigval)
print()
print('eigenvectors (np.linalg.eig) : ')
print(eigvec)


A = np.array([
    [3,   2,  4],
    [2,   1,  2],
    [4,   2,  3]
])

x = np.random.rand(3)

print(inverse_power_iteration(A, x, -1.1))
print(inverse_power_iteration(A, x, 0))
print(inverse_power_iteration(A, x, 8))


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

A = np.array([
    [3,   2,  4],
    [2,   1,  2],
    [4,   2,  3]
])

x = np.random.rand(3)

print(rayleigh_quotient_iteration(A, x))


I am working on the file "12_Eigenvalues_And_Singular_Values.ipynb".
The first task is:
Rayleigh Quotient Iteration
Can you write Python code for it?

def rayleigh_quotient_iteration(A, x, k=10):
    """
    Compute the dominant eigenvalue and eigenvector of a matrix A

    Arguments:
        - A : A matrix
        - x : nonzero initial vector
        - k : number of steps (default:10)

    Returns:
        - eigval : dominant eigenvalue
        - eigvec : dominant eigenvector
    """
    for _ in range(k):
        eigvec = x / np.linalg.norm(x)
        eigval = np.matmul(np.matmul(eigvec.T, A), eigvec)
        x = np.linalg.solve(A - eigval * np.eye(A.shape[0]), eigvec)
    eigvec = x / np.linalg.norm(x)
    eigval = np.matmul(np.matmul(eigvec.T, A), eigvec)
    return eigval, eigvec


I am working on the file "12_Eigenvalues_And_Singular_Values.ipynb".
The first task is:
Unshifted QR Algorithm
Can you write Python code for it?

def unshifted_qr(A, k=10):
    """
    Compute the eigenvalue and eigenvector of a symmetric matrix A

    Arguments:
        - A : A matrix
        - k : number of steps (default:10)

    Returns:
        - eigval : dominant eigenvalue
        - eigvec : dominant eigenvector
    """
    m, n = A.shape
    Q = np.eye(m)
    Qbar = Q
    R = A
    for _ in range(k):
        Q, R = np.linalg.qr(np.matmul(R, Q))
        Qbar = np.matmul(Qbar, Q)
    eigval = np.diag(np.matmul(R, Q))
    eigvec = Qbar
    return eigval, eigvec


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q11. Get the indices where elements of y should be inserted to x to maintain order.
Can you write Python code for it?


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q3. Get the indices that would sort x along the second axis.
Can you write Python code for it?

x = np.array([[1,4],[3,1]])
out = np.argsort(x, axis=1)
print out


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q4. Create an array such that its fifth element would be the same as the element of sorted x, and it divide other elements by their value.
Can you write Python code for it?

x = np.random.permutation(10)
print "x =", x
print "\nCheck the fifth element of this new array is 5, the first four elements are all smaller than 5, and 6th through the end are bigger than 5\n",
out = np.partition(x, 5)
x.partition(5) # in-place equivalent
assert np.array_equal(x, out)
print out


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q7. Get the maximum and minimum values and their indices of x along the second axis, ignoring NaNs.
Can you write Python code for it?

x = np.array([[np.nan, 4], [3, 2]])
print "maximum values ignoring NaNs =", np.nanmax(x, 1)
print "max indices =", np.nanargmax(x, 1)
print "minimum values ignoring NaNs =", np.nanmin(x, 1)
print "min indices =", np.nanargmin(x, 1)


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q8. Get the values and indices of the elements that are bigger than 2 in x.
Can you write Python code for it?

x = np.array([[1, 2, 3], [1, 3, 5]])
print "Values bigger than 2 =", x[x>2]
print "Their indices are ", np.nonzero(x > 2)
assert np.array_equiv(x[x>2], x[np.nonzero(x > 2)])
assert np.array_equiv(x[x>2], np.extract(x > 2, x))


I am working on the file "12_Sorting_searching_and_counting_Solutions.ipynb".
The first task is:
Q9. Get the indices of the elements that are bigger than 2 in the flattend x.
Can you write Python code for it?

x = np.array([[1, 2, 3], [1, 3, 5]])
print np.flatnonzero(x>2)
assert np.array_equiv(np.flatnonzero(x), x.ravel().nonzero())


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Deformation gradient is usually entered as numpy array
Can you write Python code for it?


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Deformation tensors
Here we calculate deformation tensors for given deformation gradient and we will explore relation to polar decomposition
Can you write Python code for it?


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Left polar decomposition
Can you write Python code for it?

def_show(R)

def_show(V)

def_show(V @ R)

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


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Now we can calculate singular value decomposition
Can you write Python code for it?


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Relation between shear deformation and strain ellipse axial ratio
Here we make a small script to calcluate axial ratio as a function of shear deformation $\gamma$
Can you write Python code for it?

gammas = linspace(0, 5, 100)
ar = []
for gamma in gammas:
    F = array([[1, gamma], [0, 1]])
    u,s,v = svd(F)
    ar.append(s[0]/s[1])
plot(gammas, ar);


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
Right polar decomposition
Can you write Python code for it?

def_show(U)

def_show(R)

def_show(R @ U)


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
The axial ratio of the strain ellipse is defined as ratio of principal axes of ellipse, i.e. principal stretches, which are returned by svd in vector s:
Can you write Python code for it?


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
The right stretch tensor U and left stretch tensor V could be calculated as square roots of C and B respectively. To calculate square root of matrix we need to import function sqrtm from scipy:
Can you write Python code for it?

from scipy.linalg import sqrtm

V = sqrtm(B)
U = sqrtm(C)


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
The rotation matrix R could be calculated from left or right polar decomposition:
\begin{align}
\mathbf{R} \cdot \mathbf{U} &= \mathbf{F} \
\mathbf{R} &= \mathbf{F} \cdot \mathbf{U}^{-1}
\end{align}
or
\begin{align}
\mathbf{V} \cdot \mathbf{R} &= \mathbf{F} \
\mathbf{R} &= \mathbf{V}^{-1} \cdot \mathbf{F}
\end{align}
Can you write Python code for it?

R = inv(V) @ F
print('From right polar decomposition\n', R)
R = F @ inv(U)
print('From left polar decomposition\n',R)


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
To find orientation of the strain ellipse we can calculate angle from last rotation matrix. The angle from x axis is:
Can you write Python code for it?


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
We can also plot the angle of first principal axis to shear plane i.e. x direction:
Can you write Python code for it?

gammas = linspace(0, 3, 100)[1:]
ar = []
theta = []
for gamma in gammas:
    F = array([[1, gamma], [0, 1]])
    u,s,v = svd(F)
    ar.append(s[0]/s[1])
    theta.append(rad2deg(arctan2(u[1,0], u[0,0])))
f, ax1 = subplots()
ax2 = ax1.twinx()
ax1.plot(gammas, theta, 'r')
ax1.set_ylabel('Angle to shear plane', color='r')
ax1.tick_params('y', colors='r')
ax1.set_xlabel('Shear deformation')
ax2.plot(gammas, ar, 'g')
ax2.set_ylabel('Axial ratio', color='g')
ax2.tick_params('y', colors='g');


I am working on the file "12_Strain_calcualtions.ipynb".
The first task is:
or angle from y axis is
Can you write Python code for it?


I am working on the file "12_camera_explorer.ipynb".
The first task is:
Let's just loop through some interesting ones:
Can you write Python code for it?

camera.start_preview()
sleep(2)
cool_effects = ['none', 'watercolor', 'negative', 'emboss', 'washedout', 'solarize', 'oilpaint', 'sketch']
for effect in cool_effects:
    camera.image_effect = effect
    camera.annotate_text = "Effect: %s" % effect
    camera.capture('img/effect-%s.jpg' % effect)
    sleep(1)
camera.stop_preview()


I am working on the file "12_camera_explorer.ipynb".
The first task is:
Now we'll clean up our camera object...
Can you write Python code for it?


I am working on the file "12_camera_explorer.ipynb".
The first task is:
One can see all the available effects by listing the camera EFFECTS
Can you write Python code for it?


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Because cycle_length=3, the new dataset starts by pulling 3 elements from the previous dataset: that's [0,1,2,3,4,5,6], [7,8,9,0,1,2,3] and [4,5,6,7,8,9,0]. Then it calls the lambda function we gave it to create one dataset for each of the elements. Since we use Dataset.from_tensor_slices(), each dataset is going to return its elements one by one. Next, it pulls two items (since block_length=2) from each of these three datasets, and it iterates until all three datasets are out of items: 0,1 (from 1st), 7,8 (from 2nd), 4,5 (from 3rd), 2,3 (from 1st), 9,0 (from 2nd), and so on until 8,9 (from 3rd), 6 (from 1st), 3 (from 2nd), 0 (from 3rd). Next it tries to pull the next 3 elements from the original dataset, but there are just two left: [1,2,3,4,5,6,7] and [8,9]. Again, it creates datasets from these elements, and it pulls two items from each until both datasets are out of items: 1,2 (from 1st), 8,9 (from 2nd), 3,4 (from 1st), 5,6 (from 1st), 7 (from 1st). Notice that there's no interleaving at the end since the arrays do not have the same length.
Readers – the new way
Instead of using a source dataset based on from_tensor_slices() or from_tensor(), we can use a reader dataset. It handles most of the complexity for us (e.g., threads):
Can you write Python code for it?

tf.reset_default_graph()

filenames = ["my_test.csv"]

dataset = tf.data.TextLineDataset(filenames)


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

cluster_spec = tf.train.ClusterSpec({
    "ps": [
        "127.0.0.1:2221",  # /job:ps/task:0
        "127.0.0.1:2222",  # /job:ps/task:1
    ],
    "worker": [
        "127.0.0.1:2223",  # /job:worker/task:0
        "127.0.0.1:2224",  # /job:worker/task:1
        "127.0.0.1:2225",  # /job:worker/task:2
    ]})

task_ps0 = tf.train.Server(cluster_spec, job_name="ps", task_index=0)
task_ps1 = tf.train.Server(cluster_spec, job_name="ps", task_index=1)
task_worker0 = tf.train.Server(cluster_spec, job_name="worker", task_index=0)
task_worker1 = tf.train.Server(cluster_spec, job_name="worker", task_index=1)
task_worker2 = tf.train.Server(cluster_spec, job_name="worker", task_index=2)


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Data API
The Data API, introduced in TensorFlow 1.4, makes reading data efficiently much easier.
Can you write Python code for it?


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Finally, let's create a one-shot iterator:
Can you write Python code for it?

it = dataset.make_one_shot_iterator()
X, y = it.get_next()

with tf.Session() as sess:
    try:
        while True:
            X_val, y_val = sess.run([X, y])
            print(X_val, y_val)
    except tf.errors.OutOfRangeError as ex:
        print("Done")


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Great! It worked fine.
Note that, as always, a tensor is only evaluated once each time we run the graph (sess.run()): so even if we evaluate multiple tensors that all depend on next_element, it is only evaluated once. This is true as well if we ask for next_element to be evaluated twice in just one run:
Can you write Python code for it?

with tf.Session() as sess:
    try:
        while True:
            print(sess.run([next_element, next_element]))
    except tf.errors.OutOfRangeError:
        print("Done")


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Let's repeatedly evaluate next_element to go through the dataset. When there are not more elements, we get an OutOfRangeError:
Can you write Python code for it?

with tf.Session() as sess:
    try:
        while True:
            print(next_element.eval())
    except tf.errors.OutOfRangeError:
        print("Done")


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Local server
Can you write Python code for it?

import tensorflow as tf

c = tf.constant("Hello distributed TensorFlow!")
server = tf.train.Server.create_local_server()

with tf.Session(server.target) as sess:
    print(sess.run(c))


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Next, we can apply this decoding function to each element in the dataset using map():
Can you write Python code for it?


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Pinning operations across devices and servers
Can you write Python code for it?

reset_graph()

with tf.device("/job:ps"):
    a = tf.Variable(1.0, name="a")

with tf.device("/job:worker"):
    b = a + 2

with tf.device("/job:worker/task:1"):
    c = a + b

with tf.Session("grpc://127.0.0.1:2221") as sess:
    sess.run(a.initializer)
    print(c.eval())

reset_graph()

with tf.device(tf.train.replica_device_setter(
        ps_tasks=2,
        ps_device="/job:ps",
        worker_device="/job:worker")):
    v1 = tf.Variable(1.0, name="v1")  # pinned to /job:ps/task:0 (defaults to /cpu:0)
    v2 = tf.Variable(2.0, name="v2")  # pinned to /job:ps/task:1 (defaults to /cpu:0)
    v3 = tf.Variable(3.0, name="v3")  # pinned to /job:ps/task:0 (defaults to /cpu:0)
    s = v1 + v2            # pinned to /job:worker (defaults to task:0/cpu:0)
    with tf.device("/task:1"):
        p1 = 2 * s         # pinned to /job:worker/task:1 (defaults to /cpu:0)
        with tf.device("/cpu:0"):
            p2 = 3 * s     # pinned to /job:worker/task:1/cpu:0

config = tf.ConfigProto()
config.log_device_placement = True

with tf.Session("grpc://127.0.0.1:2221", config=config) as sess:
    v1.initializer.run()


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

reset_graph()

test_csv = open("my_test.csv", "w")
test_csv.write("x1, x2 , target\n")
test_csv.write("1.,    , 0\n")
test_csv.write("4., 5. , 1\n")
test_csv.write("7., 8. , 0\n")
test_csv.close()

filename_queue = tf.FIFOQueue(capacity=10, dtypes=[tf.string], shapes=[()])
filename = tf.placeholder(tf.string)
enqueue_filename = filename_queue.enqueue([filename])
close_filename_queue = filename_queue.close()

reader = tf.TextLineReader(skip_header_lines=1)
key, value = reader.read(filename_queue)

x1, x2, target = tf.decode_csv(value, record_defaults=[[-1.], [-1.], [-1]])
features = tf.stack([x1, x2])

instance_queue = tf.RandomShuffleQueue(
    capacity=10, min_after_dequeue=2,
    dtypes=[tf.float32, tf.int32], shapes=[[2],[]],
    name="instance_q", shared_name="shared_instance_q")
enqueue_instance = instance_queue.enqueue([features, target])
close_instance_queue = instance_queue.close()

minibatch_instances, minibatch_targets = instance_queue.dequeue_up_to(2)

with tf.Session() as sess:
    sess.run(enqueue_filename, feed_dict={filename: "my_test.csv"})
    sess.run(close_filename_queue)
    try:
        while True:
            sess.run(enqueue_instance)
    except tf.errors.OutOfRangeError as ex:
        print("No more files to read")
    sess.run(close_instance_queue)
    try:
        while True:
            print(sess.run([minibatch_instances, minibatch_targets]))
    except tf.errors.OutOfRangeError as ex:
        print("No more training instances")

#coord = tf.train.Coordinator()
#threads = tf.train.start_queue_runners(coord=coord)
#filename_queue = tf.train.string_input_producer(["test.csv"])
#coord.request_stop()
#coord.join(threads)


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
Setting a timeout
Can you write Python code for it?

reset_graph()

q = tf.FIFOQueue(capacity=10, dtypes=[tf.float32], shapes=[()])
v = tf.placeholder(tf.float32)
enqueue = q.enqueue([v])
dequeue = q.dequeue()
output = dequeue + 1

config = tf.ConfigProto()
config.operation_timeout_in_ms = 1000

with tf.Session(config=config) as sess:
    sess.run(enqueue, feed_dict={v: 1.0})
    sess.run(enqueue, feed_dict={v: 2.0})
    sess.run(enqueue, feed_dict={v: 3.0})
    print(sess.run(output))
    print(sess.run(output, feed_dict={dequeue: 5}))
    print(sess.run(output))
    print(sess.run(output))
    try:
        print(sess.run(output))
    except tf.errors.DeadlineExceededError as ex:
        print("Timed out while dequeuing")


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
The first line creates a dataset containing the integers 0 through 9. The second line creates a new dataset based on the first one, repeating its elements three times and creating batches of 7 elements. As you can see, we start with a source dataset, then we chain calls to various methods to apply transformations to the data.
Next, we create a one-shot-iterator to go through this dataset just once, and we call its get_next() method to get a tensor that represents the next element.
Can you write Python code for it?

iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
The interleave() method is powerful but a bit tricky to grasp at first. The easiest way to understand it is to look at an example:
Can you write Python code for it?

tf.reset_default_graph()

dataset = tf.data.Dataset.from_tensor_slices(np.arange(10))
dataset = dataset.repeat(3).batch(7)
dataset = dataset.interleave(
    lambda v: tf.data.Dataset.from_tensor_slices(v),
    cycle_length=3,
    block_length=2)
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()

with tf.Session() as sess:
    try:
        while True:
            print(next_element.eval(), end=",")
    except tf.errors.OutOfRangeError:
        print("Done")


I am working on the file "12_distributed_tensorflow.ipynb".
The first task is:
We still need to tell it how to decode each line:
Can you write Python code for it?

def decode_csv_line(line):
    x1, x2, y = tf.decode_csv(
        line, record_defaults=[[-1.], [-1.], [-1.]])
    X = tf.stack([x1, x2])
    return X, y


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Accordingly, the call of the get_discount_factors() method yields simulated zero-coupon bond prices for the time grid.
Can you write Python code for it?


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Finally, instantiate the geometric_brownian_motion object.
Can you write Python code for it?

gbm = geometric_brownian_motion('gbm', me)


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Second, the instantiation of the class.
Can you write Python code for it?


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Stochstic Drifts
Let us value use the stochastic short rate model to simulate a geometric Brownian motion with stochastic short rate. Define the market environment as follows:
Can you write Python code for it?

me.add_constant('initial_value', 36.)
me.add_constant('volatility', 0.2)
  # time horizon for the simulation
me.add_constant('currency', 'EUR')
me.add_constant('frequency', 'M')
  # monthly frequency; paramter accorind to pandas convention
me.add_constant('paths', 10)
  # number of paths for simulation


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
The call of the method get_forward_reates() yields the above time_list object and the simulated forward rates. In this case, 10 simulations.
Can you write Python code for it?


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Then add the stochastic_short_rate object as discount curve.
Can you write Python code for it?


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
Visualization of Simulated Stochastic Short Rate
Can you write Python code for it?

from pylab import plt
plt.style.use('seaborn')
%matplotlib inline

# short rate paths
plt.figure(figsize=(10, 6))
plt.plot(ssr.process.instrument_values[:, :10]);


import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
%matplotlib inline

# short rate paths
plt.figure(figsize=(10, 6))
plt.plot(ssr.process.instrument_values[:, :10]);


I am working on the file "12_dx_stochastic_short_rates.ipynb".
The first task is:
We get simulated instrument values as usual via the get_instrument_values() method.
Can you write Python code for it?


I am working on the file "12_ejemplos_graficos_parametricos/graficos_parametricos.ipynb".
The first task is:
1. Archivos
Los archivos de los laboratorios deben tener la siguiente estructura:
1. Archivo .ipynb de extensión ipython notebook, para python 3. Nombre varía de laboratorio a laboratorio.
1. Carpeta code/ que contiene los códigos del laboratorio. Varía de laboratorio a laboratorio, pero debe tener __init__.py y lab.py.
1. Carpeta data/ (opcional) que contiene los archivos de datos a utilizar en el laboratorio.
1. Carpeta images/ que contiene las imágenes del laboratorio. Varía de laboratorio a laboratorio, pero debería al menos contener utfsm.png.
1. Carpeta style/ que contiene el archivo lab.css con el estilo predefinido y común a los laboratorios.
2 Uso de ipython notebook
Los siguientes son ejemplos de acciones comunes en ipython notebook
Can you write Python code for it?

import numpy as np
from matplotlib import pyplot as plt

# Presionar tabulación con el cursor despues de np.arr
np.arr

# Presionar Ctr-Enter para obtener la documentacion de la funcion np.array usando "?"
np.array?

# Presionar Ctr-Enter
%whos

x = 10
%whos


I am working on the file "12_ejemplos_graficos_parametricos/graficos_parametricos.ipynb".
The first task is:
Tipos de letras
Se tienen los siguientes tipos de letras, gentileza del markdown y estilo lab.css:
1. emphasis
2. strong
3. strong and emphasis
4. code
5. code and emphasis
6. code and strong
7. code, strong and emphasis.
8. Código en python
Python
    for i in range(n):
        print(i)
9. Código en bash
Bash
echo "hola"
Desafío 1 (10%)
Después de pasar un contenido, es importante realizar una evaluación mediante algún problema o desafío. Indicar el porcentaje asociado a la evaluación.
Por ejemplo, aquí se requiere implementar la regla de cuadratura (integración) trapezoidal.
Can you write Python code for it?

def mi_funcion(x):
    f = 1 + x + x**3 + x**5 + np.sin(x)
    return f

N = 5
x = np.linspace(-1,1,N)
y = mi_funcion(x)
# FIX ME
I = 0 # FIX ME
# FIX ME
print("Area bajo la curva: %.3f" %I)

# Ilustración gráfica
x_aux = np.linspace(x.min(),x.max(),N**2)
fig = plt.figure(figsize=(12,8))
fig.gca().fill_between(x, 0, y, alpha=0.25)
plt.plot(x_aux, mi_funcion(x_aux), 'k')
plt.plot(x, y, 'r.-')
plt.show()


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Build RNN
You created a RNN Cell in the get_init_cell() function.  Time to use the cell to create a RNN.
- Build the RNN using the tf.nn.dynamic_rnn()
 - Apply the name "final_state" to the final state using tf.identity()
Return the outputs and final_state state in the following tuple (Outputs, FinalState)
Can you write Python code for it?

def build_rnn(cell, inputs):
    """
    Create a RNN using a RNN Cell
    :param cell: RNN Cell
    :param inputs: Input text data
    :return: Tuple (Outputs, Final State)
    """
    outputs, final_state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32)
    final_state = tf.identity(final_state, name='final_state')
    return outputs, final_state

tests.test_build_rnn(build_rnn)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Build RNN Cell and Initialize
Stack one or more BasicLSTMCells in a MultiRNNCell.
- The Rnn size should be set using rnn_size
- Initalize Cell State using the MultiRNNCell's zero_state() function
    - Apply the name "initial_state" to the initial state using tf.identity()
Return the cell and initial state in the following tuple (Cell, InitialState)
Can you write Python code for it?

def get_init_cell(batch_size, rnn_size):
    """
    Create an RNN Cell and initialize it.
    :param batch_size: Size of batches
    :param rnn_size: Size of RNNs
    :return: Tuple (cell, initialize state)
    """
    cell = tf.contrib.rnn.BasicLSTMCell(rnn_size)
    cell = tf.contrib.rnn.MultiRNNCell([cell] * 1)
    initial_state = cell.zero_state(batch_size, tf.float32)
    initial_state = tf.identity(initial_state, name='initial_state')
    return cell, initial_state

tests.test_get_init_cell(get_init_cell)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Build the Graph
Build the graph using the neural network you implemented.
Can you write Python code for it?

from tensorflow.contrib import seq2seq

train_graph = tf.Graph()
with train_graph.as_default():
    vocab_size = len(int_to_vocab)
    input_text, targets, lr = get_inputs()
    input_data_shape = tf.shape(input_text)
    cell, initial_state = get_init_cell(input_data_shape[0], rnn_size)
    logits, final_state = build_nn(cell, rnn_size, input_text, vocab_size, embed_dim)

    # Probabilities for generating words
    probs = tf.nn.softmax(logits, name='probs')

    # Loss function
    cost = seq2seq.sequence_loss(
        logits,
        targets,
        tf.ones([input_data_shape[0], input_data_shape[1]]))

    # 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 "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Build the Neural Network
You'll build the components necessary to build a RNN by implementing the following functions below:
- get_inputs
- get_init_cell
- get_embed
- build_rnn
- build_nn
- get_batches
Check the Version of TensorFlow and Access to GPU
Can you write Python code for it?

from distutils.version import LooseVersion
import warnings
import tensorflow as tf

# Check TensorFlow Version
assert LooseVersion(tf.__version__) >= LooseVersion('1.0'), 'Please use TensorFlow version 1.0 or newer'
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 "12_tv-script-generation/dlnd_tv_script_generation.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?

import helper
import numpy as np
import problem_unittests as tests

int_text, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Checkpoint
Can you write Python code for it?

import tensorflow as tf
import numpy as np
import helper
import problem_unittests as tests

_, vocab_to_int, int_to_vocab, token_dict = helper.load_preprocess()
seq_length, load_dir = helper.load_params()


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Choose Word
Implement the pick_word() function to select the next word using probabilities.
Can you write Python code for it?

def pick_word(probabilities, int_to_vocab):
    """
    Pick the next word in the generated text
    :param probabilities: Probabilites of the next word
    :param int_to_vocab: Dictionary of word ids as the keys and words as the values
    :return: String of the predicted word
    """
    #return int_to_vocab[np.argmax(probabilities)]
    return np.random.choice(list(int_to_vocab.values()), p=probabilities)

tests.test_pick_word(pick_word)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Generate TV Script
This will generate the TV script for you.  Set gen_length to the length of TV script you want to generate.
Can you write Python code for it?

gen_length = 200
# homer_simpson, moe_szyslak, or barney_gumble
prime_word = 'homer_simpson'

loaded_graph = tf.Graph()
with tf.Session(graph=loaded_graph) as sess:
    # Load saved model
    loader = tf.train.import_meta_graph(load_dir + '.meta')
    loader.restore(sess, load_dir)

    # Get Tensors from loaded model
    input_text, initial_state, final_state, probs = get_tensors(loaded_graph)

    # Sentences generation setup
    gen_sentences = [prime_word + ':']
    prev_state = sess.run(initial_state, {input_text: np.array([[1]])})

    # Generate sentences
    for n in range(gen_length):
        # Dynamic Input
        dyn_input = [[vocab_to_int[word] for word in gen_sentences[-seq_length:]]]
        dyn_seq_length = len(dyn_input[0])

        # Get Prediction
        probabilities, prev_state = sess.run(
            [probs, final_state],
            {input_text: dyn_input, initial_state: prev_state})

        pred_word = pick_word(probabilities[0, dyn_seq_length-1], int_to_vocab)

        gen_sentences.append(pred_word)

    # Remove tokens
    tv_script = ' '.join(gen_sentences)
    for key, token in token_dict.items():
        ending = ' ' if key in ['\n', '(', '"'] else ''
        tv_script = tv_script.replace(' ' + token.lower(), key)
    tv_script = tv_script.replace('\n ', '\n')
    tv_script = tv_script.replace('( ', '(')

    print(tv_script)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Input
Implement the get_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.
- Targets placeholder
- Learning Rate placeholder
Return the placeholders in the following tuple (Input, Targets, LearningRate)
Can you write Python code for it?

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

tests.test_get_inputs(get_inputs)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Neural Network Training
Hyperparameters
Tune the following parameters:

Set num_epochs to the number of epochs.
Set batch_size to the batch size.
Set rnn_size to the size of the RNNs.
Set embed_dim to the size of the embedding.
Set seq_length to the length of sequence.
Set learning_rate to the learning rate.
Set show_every_n_batches to the number of batches the neural network should print progress.
Can you write Python code for it?

# Number of Epochs
num_epochs = 100
# Batch Size
batch_size = 256
# RNN Size
rnn_size = 256
# Embedding Dimension Size
embed_dim = 128
# Sequence Length
seq_length = 20
# Learning Rate
learning_rate = 0.01
# Show stats for every n number of batches
show_every_n_batches = 32

save_dir = './save'


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Preprocess all the data and save it
Running the code cell below will preprocess all the data and save it to file.
Can you write Python code for it?

# Preprocess Training, Validation, and Testing Data
helper.preprocess_and_save_data(data_dir, token_lookup, create_lookup_tables)


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Save Parameters
Save seq_length and save_dir for generating a new TV script.
Can you write Python code for it?

# Save parameters for checkpoint
helper.save_params((seq_length, save_dir))


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Train
Train the neural network on the preprocessed data.  If you have a hard time getting a good loss, check the forums to see if anyone is having the same problem.
Can you write Python code for it?

batches = get_batches(int_text, batch_size, seq_length)

with tf.Session(graph=train_graph) as sess:
    sess.run(tf.global_variables_initializer())

    for epoch_i in range(num_epochs):
        state = sess.run(initial_state, {input_text: batches[0][0]})

        for batch_i, (x, y) in enumerate(batches):
            feed = {
                input_text: x,
                targets: y,
                initial_state: state,
                lr: learning_rate}
            train_loss, state, _ = sess.run([cost, final_state, train_op], feed)

            # Show every <show_every_n_batches> batches
            if (epoch_i * len(batches) + batch_i) % show_every_n_batches == 0:
                print('Epoch {:>3} Batch {:>4}/{}   train_loss = {:.3f}'.format(
                    epoch_i,
                    batch_i,
                    len(batches),
                    train_loss))

    # Save Model
    saver = tf.train.Saver()
    saver.save(sess, save_dir)
    print('Model Trained and Saved')


I am working on the file "12_tv-script-generation/dlnd_tv_script_generation.ipynb".
The first task is:
Word Embedding
Apply embedding to input_data using TensorFlow.  Return the embedded sequence.
Can you write Python code for it?

def get_embed(input_data, vocab_size, embed_dim):
    """
    Create embedding for <input_data>.
    :param input_data: TF placeholder for text input.
    :param vocab_size: Number of words in vocabulary.
    :param embed_dim: Number of embedding dimensions
    :return: Embedded input.
    """
    embedding = tf.Variable(tf.random_uniform([vocab_size, embed_dim], -1, 1))
    return tf.nn.embedding_lookup(embedding, input_data)

tests.test_get_embed(get_embed)


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Calculate the  signal with the flame in the field of view. In this case there is both path radiance and flame radiance in the field of view; the two must be added together.
The flame radiance is calculated by using Planck's law and multiplying with the spectral emissivity. The spectral calculation of the flame signal proceeds similarly to the calculation of the spectral background radiance. The flame signal is then determined by integrating over all wavelengths and then multiplying with the sensor parameters.
The signals from the background and flame are then added together, using the fill factor to determine the fraction contributed by the background.
Can you write Python code for it?

# get spectral radiance in  W/m^2.sr.cm-1
radianceFlame = ryplanck.planck(waven, flameTemperature,  type='en')\
    .reshape(-1, 1)/np.pi
inbandirradianceFlame = radianceFlame * detR * tauA * emis * sfilter *\
    fill * opticsFOV
totalirradianceFlame = np.trapz(inbandirradianceFlame.reshape(-1, 1),
    waven, axis=0)[0]
totalirradiancePathremainder = totalirradiancePath * (1-fill)
signalFlameOnly = totalirradianceFlame *transZ*responsivity *opticsArea
signalFlame = (totalirradianceFlame + totalirradiancePathremainder ) *\
    transZ*responsivity *opticsArea


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Calculate the path radiance signal (absence of the flame). First multiply out the spectral components  - this can be done because all variables are on a common spectral sampling. Then sum the integration by using the numpy.trapz function. Finally multiply the integral with the sensor parameters to determine the output signal.
Can you write Python code for it?

inbandirradiancePath =  lpathwn * detR * sfilter * opticsFOV
totalirradiancePath = np.trapz(inbandirradiancePath.reshape(-1, 1),waven, axis=0)[0]
signalPath = totalirradiancePath * transZ * responsivity * opticsArea


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Construct the filter spectral transmittance from parameters, using the pyradi.ryutils.sfilter function. Normally one would use measured filter data.
Can you write Python code for it?

#construct the sensor filter from parameters
sfilter = ryutils.sfilter(wavel,center=4.3, width=0.8, exponent=12,
           taupass=0.9, taustop=0.0001)


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Construct the flame emissivity from parameters, using the pyradi.ryutils.sfilter function. Normally one would use measured emissivity data.
Can you write Python code for it?

emis = ryutils.sfilter(wavel,center=4.33, width=0.45, exponent=6, taupass=0.8,
    taustop=0.1 )


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Convert path radiance spectral density from 1/cm$^{-1}$ to 1/$\mu$m, at the sample wavenumber points.
Use the function pyradi.ryutils.convertSpectralDensity for this purpose.
Can you write Python code for it?

(dum, lpathwl) = ryutils.convertSpectralDensity(waven, lpathwn, type='nl')


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Create the spectral range to be used for the calculation. Create the range in wavenumber domain and immediately create a corresponding wavelength domain. These points correspond value by value, so they can be used interchangeably in conversions and plotting.  Use pyradi.ryutils.convertSpectralDomain for this purpose.
Can you write Python code for it?

waven = np.arange(2000.0,  3300.0,  2.5).reshape(-1, 1)
wavel= ryutils.convertSpectralDomain(waven,  type='nl')


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
It is clear that the flame signal is several times larger than the path radiance
signal, even though the flame only fills 1% of the sensor FOV. The
severity by which the atmosphere attenuates the CO$_2$ exitance from the
flame is shown above. Note the path radiance is much smaller than the flame radiance.
Python and module versions, and dates
Can you write Python code for it?


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Load the detector file in wavelengths, and interpolate to  required spectral domain values.
Can you write Python code for it?

detR = ryfiles.loadColumnTextFile('./detectorflamesensor.txt',
    [1], abscissaOut=wavel, comment='%')


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
Plot the data read in and calculated above.  The visual inspection is a critical tool in validating the calculations.
In this particular instance observe how closely the path radiance matches the blackbody radiance at the same temperature. From this simple observation we can conclude that the atmospheric path emissivity is near unity.
Can you write Python code for it?

plotdata = detR
plotdata = np.hstack((plotdata,emis))
plotdata = np.hstack((plotdata,sfilter))
plotdata = np.hstack((plotdata,tauA))
label = ['Detector','Emissivity','Filter','Atmosphere transmittance']
plot1= ryplot.Plotter(1, 1, 2,'Flame sensor',figsize=(12,6))
plot1.plot(1, wavel, plotdata, "Spectral","Wavelength [$\mu$m]",
    "Relative magnitude",
    label=label, maxNX=10, maxNY=10)

LbbTropical = ryplanck.planck(wavel, 273+27, type='el').reshape(-1, 1)/np.pi

plotdata = LbbTropical
plotdata = np.hstack((plotdata,lpathwl))

label2=['300 K Planck Law','Tropical path radiance']
plot1.plot(2, wavel, plotdata, 'Comparing blackbody and path radiance',
           'Wavelength [$\mu$m]','Radiance [W/(m$^2$.sr.$\mu$m)]', label=label2)


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
The signal when the flame is present is given by
\begin{equation}
v_{\rm flame}=
\widehat{{ R}}\, Z_t \,A_{1} \left[
\left(\frac{A_{0} }{R_{01}^2}\right)\,
\int_{0}^{\infty}
 \epsilon_{0\lambda} L_{0\lambda}
\,\tau_{a\lambda} { S}\lambda
d\lambda
+
\left(\Omega -  \frac{A{0} }{R_{01}^2}  \right)\,
\int_{0}^{\infty}
 L_{{\rm path}\lambda}
 { S}{\lambda}
d\lambda
\right],
\end{equation}
where
$A{0}$ is the flame area because  $\cos\theta_0=1$, and
$R_{01}^2$ is the distance between the flame and the sensor.
Note that the flame only fills a small portion of the field of view and the rest is filled by sky path radiance.
Define sensor scalar parameters.
Can you write Python code for it?

opticsArea = 7.8e-3 # optical aperture area [m2]
opticsFOV = 1.0e-4 # sensor field of view [sr]
transZ = 1.0e4 # amplifier transimpedance gain [V/A]
responsivity= 2.5 # detector peak responsivity =A/W]


I am working on the file "12a-FlameSensorAnalysis.ipynb".
The first task is:
The system has the elements in the following picture:
Can you write Python code for it?


I am working on the file "12d-SpectralTemperatureEstimation.ipynb".
The first task is:
Analysing the graphs above, rough estimates of the temperatures can be formed.  The object temperatures can be estimated relatively easily from the bottom-left curve: approximately 500 K, 1000 K and 1500 K.  The sun temperature is not so easily estimated: 5500 K, 5900 K and 6000 K.  These estimates in themselves are inaccurate for final results, but can guide subsequent analysis.
Method 1: Differentiating Spectral Radiance
The first method is a refinement of the peak-detection method used above for the first order estimates.  It relies on the mathematical method that the location of peaks and minima can be determined by the zero crossings of the first derivative.  The derivative is calculated by using the Numpy diff method.  The problem with this technique is that the measurement is somewhat noisy, and the derivation operator amplifies the noise.  So we are making a noisy signal more noisy and then look for zero crossings - a method that hold tremendous promise to fail.  The noisy zero crossings make it very difficult to determine the true zero crossing accurately.   We will use the estimates determined above to limit the search range.
The data is noisy, so a filter was used to improve the signal to noise somewhat.  There is however the risk that the filtering process may interfere with the true nature of the data, so the degree of filtering is limited.  The Savitzky-Golay filter is used here.  This filter is commonly used in the spectroscopy community.
In the code below the first derivative is calculated using numpy.diff and then normalised (because the absolute scale is not important).  The differentiated signal is plotted for inspection. Note the noise in the signal - filtering seems to help quite a lot (but not sufficiently to count zero crossings naively).
In the analysis below uses privileged problem data, but only to determine the error between the answers and the true temperatures.
First detemine the approximate wavelength ranges where the estimated zero crossing occur, based on temperatures read of...
Can you write Python code for it?

#temperatures read of from above graphs
tLo = np.asarray([[400, 600],[900,1100],[1400,1600]])
tHi = np.asarray([[5000, 6500],[5000,6500],[5000,6500]])
#get peak wavelengths associated with these temperatures
wLo = 2897.77 / tLo
wHi = 2897.77 / tHi

print(tLo)
print(tHi)
print(wLo)
print(wHi)


I am working on the file "12d-SpectralTemperatureEstimation.ipynb".
The first task is:
Prepare the data file
This section is not part of the solution, it is used to prepare the data for the problem statement.
Calculate the spectral radiance curves from first principles.
Can you write Python code for it?

filename = 'twoSourceSpectrum.txt'

def bbfunc(wl,tsun,tobj,emis):
    """Given the two temperatures and emissivity, calculate the spectum.
    """
    lSun = 2.175e-5 * ryplanck.planck(wl,tsun, 'el') / np.pi # W/(m2.sr.um)
    lObj = ryplanck.planck(wl,tobj, 'el') / np.pi # W/(m2.sr.um)
    return  lSun * (1 - emis) + lObj * emis

def percent(val1, val2):
    return 100. * (val1  -val2) / val2


I am working on the file "12d-SpectralTemperatureEstimation.ipynb".
The first task is:
Solution
Recognise that the signature comprises two components: reflected sunlight and self emittance.
The total signature is given by
$L_{\rm tot} = \epsilon \,L_{\rm bb}(T_{\rm obj}) + (1\,-\,\epsilon)\, \psi L_{\rm bb}(T_{\rm sun})$,
where
$\psi=A_{\rm sun}/(\pi R_{\rm sun}^2) =2.1757\times10^{-5}$ [sr/sr] follows from the geometry between the earth and the sun,
$\epsilon$ is the object surface emissivity,
$T_{\rm obj}$ the object temperature, and
$T_{\rm sun}$ is the sun surface temperature.
The three variables to be solved are
$\epsilon$,
$T_{\rm obj}$, and
$T_{\rm sun}$.
The problem is coded in mathematical form as
def bbfunc(wl,tsun,tobj,emis):
    """Given the two temperatures and emissivity, calculate the spectum.
    """
    lSun = 2.175e-5 * ryplanck.planck(wl,tsun, 'el') / np.pi # W/(m2.sr.um)
    lObj = ryplanck.planck(wl,tobj, 'el') / np.pi # W/(m2.sr.um)
    return  lSun * (1 - emis) + lObj * emis

The first step in the analysis is to plot the supplied data.  Sometimes it helps to plot the data in different plotting scales.  In this case the sun and object are Planck radiators (given in problem statement) where the wavelength of peak radiance is related to the temperature by Wien's displacement law $T = 2898/\lambda_{p}$.  Exploiting this fact, the radiance curves are plotted on a Wien-law scale where wavelength is converted to temperature.  For an ideal Planck-law radiator the radiance peak can be used to read of the temperature on the temperature scale.
Can you write Python code for it?

radIn = np.loadtxt(filename, skiprows=1)
wl = radIn[:,0]
q = ryplot.Plotter(1,2,2,figsize=(12,8))

for i in range(0,len(emiss)):

    tPeak = 2898. / wl

    q.logLog(1,wl, radIn[:,i+1],
             'Input data', 'Wavelength $\mu$m', 'Radiance W/(m$^2$.sr.$\mu$m)',
             label=['Case {}'.format(i+1)], maxNX=4, pltaxis=[0.2, 10, 1e0, 1e4])
    q.semilogY(2,wl, radIn[:,i+1],
             'Input data', 'Wavelength $\mu$m', 'Radiance W/(m$^2$.sr.$\mu$m)',
             label=['Case {}'.format(i+1)], maxNX=4, pltaxis=[0.2, 10, 1e0, 1e4])
    q.semilogY(3,tPeak, radIn[:,i+1],
             'Input data', 'Temperature K', 'Radiance W/(m$^2$.sr.$\mu$m)',
             label=['Case {}'.format(i+1)], maxNX=4, pltaxis=[0., 2000., 1e0, 1e4])
    q.semilogY(4,tPeak, radIn[:,i+1],
             'Input data', 'Temperature K', 'Radiance W/(m$^2$.sr.$\mu$m)',
             label=['Case {}'.format(i+1)], maxNX=4, pltaxis=[4000., 8000., 4e1, 1e3])