I am working on the file "783/HW1/task5_next_char.ipynb".
The first task is:
Debug the training
I have managed to get a loss below 10,000 and a validation error of about 1100 on the validation set (by playing around the parameters a little bit). However, this isn't very good.
One strategy for getting insight into what's wrong is to plot the loss function and the accuracies on the training and validation sets during optimization.
Can you write Python code for it?

# Plot the loss function and train / validation errors
plt.subplot(2, 1, 1)
plt.plot(stats['loss_history'])
plt.title('Loss history')
plt.xlabel('Iteration')
plt.ylabel('Loss')

plt.subplot(2, 1, 2)
train = plt.plot(stats['train_err_history'], label='train')
val = plt.plot(stats['val_err_history'], label='val')
plt.legend(loc='upper right', shadow=True)
plt.title('Classification error history')
plt.xlabel('Epoch')
plt.ylabel('Clasification error')
plt.show()

# Show some sample outputs:

print "Input - predicted char - true char"
for i in range(0,100):
    print int_list_to_string(X_val[i]) + " - "  \
        + int_list_to_string([int(x) for x in net.predict(X_val[i])]) \
        + " - " + int_list_to_string(y_val[i]), net.predict(X_val[i]), y_val[i]

test_err = np.sum(np.square(net.predict(X_test) - y_test), axis=1).mean()
print 'Test error: ', test_err

# Show some sample outputs:
print "Input - predicted char - true char"
for i in range(0,100):
    print int_list_to_string(X_test[i]) + " - "  \
        + int_list_to_string([int(x) for x in net.predict(X_test[i])]) \
        + " - " + int_list_to_string(y_test[i]), net.predict(X_test[i]), y_test[i]


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

loss, _ = net.loss(X, y, reg=0.1)
correct_loss = 66.3406756909

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


I am working on the file "783/HW1/task5_next_char.ipynb".
The first task is:
Load a dataset for training a simple regression network
Can you write Python code for it?

# Load the TEXT data
# If your memory turns out to be sufficient, try the following:
#def get_nextchar_data(training_ratio=0.6, val_ratio=0.1):
def get_nextchar_data(training_ratio=0.1, test_ratio=0.06, val_ratio=0.01):
  # Load the nextchar training data
  X, y = load_nextchar_dataset(nextchar_datafile)
  # Subsample the data
  length=len(y)
  num_training=int(length*training_ratio)
  num_val = int(length*val_ratio)
  num_test = min((length-num_training-num_val), int(length*test_ratio))
  mask = range(num_training-1)
  X_train = X[mask]
  y_train = y[mask]
  mask = range(num_training, num_training+num_test)
  X_test = X[mask]
  y_test = y[mask]
  mask = range(num_training+num_test, num_training+num_test+num_val)
  X_val = X[mask]
  y_val = y[mask]

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

nextchar_datafile = 'metu/dataset/nextchar_data.pkl'
input_size = 5 # Size of the input of the network
#plain_text_file_to_dataset("metu/dataset/ince_memed_1.txt", nextchar_datafile, input_size)
plain_text_file_to_dataset("metu/dataset/shakespeare.txt", nextchar_datafile, input_size)
X_train, y_train, X_val, y_val, X_test, y_test = get_nextchar_data()
print "Number of instances in the training set: ", len(X_train)
print "Number of instances in the validation set: ", len(X_val)
print "Number of instances in the testing set: ", len(X_test)

# We have loaded the dataset. That wasn't difficult, was it? :)
# Let's look at a few samples
#
from metu.data_utils import int_list_to_string, int_to_charstr

print "Input - Next char to be predicted"
for i in range(1,10):
    print int_list_to_string(X_train[i]) + " - " + int_list_to_string(y_train[i])


I am working on the file "783/HW1/task5_next_char.ipynb".
The first task is:
Now train our network on the nextchar dataset
Can you write Python code for it?

# Now, let's train a neural network

input_size = input_size
hidden_size = 5000
num_classes = 1
net = TwoLayerNet(input_size, hidden_size, num_classes)

# Train the network
stats = net.train(X_train, y_train, X_val, y_val,
            num_iters=4000, batch_size=32*8,
            learning_rate=6e-7, learning_rate_decay=0.95,
            reg=5, verbose=True)

# Predict on the validation set
val_err = np.sum(np.square(net.predict(X_val) - y_val), axis=1).mean()
print 'Validation error: ', val_err


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

from METU.gradient_check import eval_numerical_gradient

reg = 3e-3

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

net.init_weights()
noisy_X = GaussianNoise(0.5)(X)
loss, grads = net.loss(noisy_X, X, reg, activation_function='tanh')

# these should all be less than 1e-5 or so
f = lambda W: net.loss(noisy_X, X, reg, activation_function='tanh')[0]
W1_grad = eval_numerical_gradient(f, net.weights[1]['W'], verbose=False)
print '%s max relative error: %e' % ("W1", rel_error(W1_grad, grads[1]['W']))
W0_grad = eval_numerical_gradient(f, net.weights[0]['W'], verbose=False)
print '%s max relative error: %e' % ("W0", rel_error(W0_grad, grads[0]['W']))
b1_grad = eval_numerical_gradient(f, net.weights[1]['b'], verbose=False)
print '%s max relative error: %e' % ("b1", rel_error(b1_grad, grads[1]['b']))
b0_grad = eval_numerical_gradient(f, net.weights[0]['b'], verbose=False)
print '%s max relative error: %e' % ("b0", rel_error(b0_grad, grads[0]['b']))


I am working on the file "783/HW2/task1_denoising_autoencoder.ipynb".
The first task is:
Forward pass: compute loss
Open the file METU/denoising_autoencoder.py and look at the method DenoisingAutoencoder.loss. This function is very similar to the loss functions you have written in the first HW: It takes the data and weights and computes the class scores, the loss, and the gradients on the parameters.
Implement the first part of the forward pass which uses the weights and biases to compute the scores for the corrupted input. In the same function, implement the second part that computes the data and the regularization losses.
Can you write Python code for it?

loss,_ = net.loss(GaussianNoise(0.5)(X), X, reg=3e-3, activation_function='sigmoid')

correct_loss = 2.42210627243
print 'Your loss value:' + str(loss)

print 'Difference between your loss and correct loss:'
print np.sum(np.abs(loss - correct_loss))


I am working on the file "783/HW2/task1_denoising_autoencoder.ipynb".
The first task is:
Load the data
Now that you have implemented a DAE network that passes gradient checks and works on toy data, it's time to load up the MNIST dataset so we can use it to train DAE on a real dataset. Make sure that you have run "cs231n/datasets/get_datasets.sh" script before you continue with this step.
Can you write Python code for it?

from cs231n.data_utils import load_mnist

X_train, y_train, X_val, y_val, X_test, y_test = load_mnist()
X_train = X_train.reshape(X_train.shape[0], -1)
X_val = X_val.reshape(X_val.shape[0], -1)
X_test = X_test.reshape(X_test.shape[0], -1)

print 'Train data shape: ', X_train.shape
print 'Train labels shape: ', y_train.shape
print 'Test data shape: ', X_test.shape
print 'Test labels shape: ', y_test.shape

#Visualize some samples

x = np.reshape(X_train[100], (28,28))

plt.imshow(x)
plt.title(y_train[0])
plt.show()

plt.imshow(GaussianNoise(rate=0.5,sd=0.5)(x))
plt.show()
# Yes, DAE will learn to reconstruct from such corrupted data


I am working on the file "783/HW2/task1_denoising_autoencoder.ipynb".
The first task is:
We will use the class DenoisingAutoencoder in the file METU/denoising_autoencoder.py to represent instances of our network. The network parameters are stored in the instance variable self.params where keys are string parameter names and values are numpy arrays. Below, we initialize toy data and a toy model that we will use to develop your implementation.
Can you write Python code for it?

from METU.denoising_autoencoder import DenoisingAutoencoder
from METU.Noise import Noise, GaussianNoise

# Create a small net and some toy data to check your implementations.
# Note that we set the random seed for repeatable experiments.

input_size = 4
hidden_size = 2
num_inputs = 100
# Outputs are equal to the inputs
network_size = (input_size, hidden_size, input_size)

def init_toy_model(num_inputs, input_size):
  np.random.seed(0)
  net = DenoisingAutoencoder((input_size, hidden_size, input_size))
  net.init_weights()
  return net

def init_toy_data(num_inputs, input_size):
  np.random.seed(1)
  X = np.random.randn(num_inputs, input_size)
  return X

net = init_toy_model(num_inputs, input_size)
X = init_toy_data(num_inputs, input_size)
print "Ok, now we have a toy network"


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

# Test the affine_forward function

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

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

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

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

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


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

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

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

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

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


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


I am working on the file "783/HW2/task2_layers.ipynb".
The first task is:
Fast layers
Making convolution and pooling layers fast can be challenging. To spare you the pain, we've provided fast implementations of the forward and backward passes for convolution and pooling layers in the file cs231n/fast_layers.py.
The fast convolution implementation depends on a Cython extension; to compile it you need to run the following from the cs231n directory:
bash
python setup.py build_ext --inplace
The API for the fast versions of the convolution and pooling layers is exactly the same as the naive versions that you implemented above: the forward pass receives data, weights, and parameters and produces outputs and a cache object; the backward pass recieves upstream derivatives and the cache object and produces gradients with respect to the data and weights.
NOTE: The fast implementation for pooling will only perform optimally if the pooling regions are non-overlapping and tile the input. If these conditions are not met then the fast pooling implementation will not be much faster than the naive implementation.
You can compare the performance of the naive and fast versions of these layers by running the following:
Can you write Python code for it?

from cs231n.fast_layers import conv_forward_fast, conv_backward_fast
from time import time

x = np.random.randn(100, 3, 31, 31)
w = np.random.randn(25, 3, 3, 3)
b = np.random.randn(25,)
dout = np.random.randn(100, 25, 16, 16)
conv_param = {'stride': 2, 'pad': 1}

t0 = time()
out_naive, cache_naive = conv_forward_naive(x, w, b, conv_param)
t1 = time()
out_fast, cache_fast = conv_forward_fast(x, w, b, conv_param)
t2 = time()

print 'Testing conv_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'Difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive, dw_naive, db_naive = conv_backward_naive(dout, cache_naive)
t1 = time()
dx_fast, dw_fast, db_fast = conv_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting conv_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'Speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)
print 'dw difference: ', rel_error(dw_naive, dw_fast)
print 'db difference: ', rel_error(db_naive, db_fast)

from cs231n.fast_layers import max_pool_forward_fast, max_pool_backward_fast

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

t0 = time()
out_naive, cache_naive = max_pool_forward_naive(x, pool_param)
t1 = time()
out_fast, cache_fast = max_pool_forward_fast(x, pool_param)
t2 = time()

print 'Testing pool_forward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'fast: %fs' % (t2 - t1)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'difference: ', rel_error(out_naive, out_fast)

t0 = time()
dx_naive = max_pool_backward_naive(dout, cache_naive)
t1 = time()
dx_fast = max_pool_backward_fast(dout, cache_fast)
t2 = time()

print '\nTesting pool_backward_fast:'
print 'Naive: %fs' % (t1 - t0)
print 'Fast: %fs' % (t2 - t1)
print 'speedup: %fx' % ((t1 - t0) / (t2 - t1))
print 'dx difference: ', rel_error(dx_naive, dx_fast)


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

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

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

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

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

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


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

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

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

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

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


I am working on the file "783/HW2/task2_layers.ipynb".
The first task is:
Max pooling layer: forward naive
The last layer we need for a basic convolutional neural network is the max pooling layer. First implement the forward pass in the function max_pool_forward_naive in the file cs231n/layers.py.
Can you write Python code for it?


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

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

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

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

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


I am working on the file "783/HW2/task2_layers.ipynb".
The first task is:
ReLU layer: forward
Implement the relu_forward function and test your implementation by running the following:
Can you write Python code for it?

# Test the relu_forward function

x = np.linspace(-0.5, 0.5, num=12).reshape(3, 4)

out, _ = relu_forward(x)
correct_out = np.array([[ 0.,          0.,          0.,          0.,        ],
                        [ 0.,          0.,          0.04545455,  0.13636364,],
                        [ 0.22727273,  0.31818182,  0.40909091,  0.5,       ]])

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


I am working on the file "783/HW2/task2_layers.ipynb".
The first task is:
Sandwich layers
There are a couple common layer "sandwiches" that frequently appear in ConvNets. For example convolutional layers are frequently followed by ReLU and pooling, and affine layers are frequently followed by ReLU. To make it more convenient to use these common patterns, we have defined several convenience layers in the file cs231n/layer_utils.py. Lets grad-check them to make sure that they work correctly:
Can you write Python code for it?

from cs231n.layer_utils import conv_relu_pool_forward, conv_relu_pool_backward

x = np.random.randn(2, 3, 16, 16) #  N, C, H, W = X.shape
w = np.random.randn(3, 3, 3, 3)
b = np.random.randn(3,)
dout = np.random.randn(2, 3, 8, 8)
conv_param = {'stride': 1, 'pad': 1}
pool_param = {'pool_height': 2, 'pool_width': 2, 'stride': 2}

out, cache = conv_relu_pool_forward(x, w, b, conv_param, pool_param)
dx, dw, db = conv_relu_pool_backward(dout, cache)

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

print 'Testing conv_relu_pool_forward:'
print 'dx error: ', rel_error(dx_num, dx)
print 'dw error: ', rel_error(dw_num, dw)
print 'db error: ', rel_error(db_num, db)

from cs231n.layer_utils import conv_relu_forward, conv_relu_backward

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

out, cache = conv_relu_forward(x, w, b, conv_param)
dx, dw, db = conv_relu_backward(dout, cache)

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

print 'Testing conv_relu_forward:'
print 'dx error: ', rel_error(dx_num, dx)
print 'dw error: ', rel_error(dw_num, dw)
print 'db error: ', rel_error(db_num, db)

from cs231n.layer_utils import affine_relu_forward, affine_relu_backward

x = np.random.randn(2, 3, 4)
w = np.random.randn(12, 10)
b = np.random.randn(10)
dout = np.random.randn(2, 10)

out, cache = affine_relu_forward(x, w, b)
dx, dw, db = affine_relu_backward(dout,...


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
Experiment!
Experiment and try to get the best performance that you can on CIFAR-10 using a ConvNet. Here are some ideas to get you started:
Things you should try:

Filter size: Above we used 7x7; this makes pretty pictures but smaller filters may be more efficient
Number of filters: Above we used 32 filters. Do more or fewer do better?
Network depth: The network above has two layers of trainable parameters. Can you do better with a deeper network? You can implement alternative architectures in the file cs231n/classifiers/convnet.py. Some good architectures to try include:
[conv-relu-pool]xN - conv - relu - [affine]xM - [softmax or SVM]
[conv-relu-pool]XN - [affine]XM - [softmax or SVM]
[conv-relu-conv-relu-pool]xN - [affine]xM - [softmax or SVM]

Tips for training
For each network architecture that you try, you should tune the learning rate and regularization strength. When doing this there are a couple important things to keep in mind:

If the parameters are working well, you should see improvement within a few hundred iterations
Remember the course-to-fine approach for hyperparameter tuning: start by testing a large range of hyperparameters for just a few training iterations to find the combinations of parameters that are working at all.
Once you have found some sets of parameters that seem to work, search more finely around these parameters. You may need to train for more epochs.

Going above and beyond
If you are feeling adventurous there are many other features you can implement to try and improve your performance. You are not required to implement any of these; however they would be good things to try for extra credit.

Alternative update steps: For the assignment we implemented SGD+momentum and RMSprop; you could try alternatives like AdaGrad or AdaDelta.
Other forms of regularization such as L1 or Dropout
Alternative activation functions such as leaky ReLU or maxout
Model ensembles
Data augmentation

What we expect
At the very least, you should be able to train a ConvNet that gets at least 65%...
Can you write Python code for it?

model = init_my_convnet(filter_size=5)
trainer = ClassifierTrainer()
best_model, loss_history, train_acc_history, val_acc_history = trainer.train(
          X_train, y_train, X_val, y_val, model, my_convnet,
          reg=0.001, momentum=0.9, learning_rate=0.0001, batch_size=50, num_epochs=1,
          acc_frequency=50, verbose=True)


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
It seems to be working with those parameters let us train it more to see if we can get better accuracy on validation set.
Can you write Python code for it?

model = init_my_convnet(filter_size=5, Naff=1, Ncrp=2)
trainer = ClassifierTrainer()
best_model, loss_history, train_acc_history, val_acc_history = trainer.train(
          X_train, y_train, X_val, y_val, model, my_convnet,
          reg=0.001, momentum=0.9, learning_rate=0.0005, batch_size=50, num_epochs=5,
          acc_frequency=50, verbose=True)


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
Now let's try it with 2 conv_relu_pooling layers followed by 1 affine layer and softmax as loss.
Can you write Python code for it?

model = init_my_convnet(filter_size=5, Naff=1, Ncrp=2)
trainer = ClassifierTrainer()
best_model, loss_history, train_acc_history, val_acc_history = trainer.train(
          X_train, y_train, X_val, y_val, model, my_convnet,
          reg=0.001, momentum=0.9, learning_rate=0.0005, batch_size=50, num_epochs=1,
          acc_frequency=50, verbose=True)


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
Sanity check loss
After you build a new network, one of the first things you should do is sanity check the loss. When we use the softmax loss, we expect the loss for random weights (and no regularization) to be about log(C) for C classes. When we add regularization this should go up.
Can you write Python code for it?

model = init_two_layer_convnet()

X = np.random.randn(100, 3, 32, 32)
y = np.random.randint(10, size=100)

loss, _ = two_layer_convnet(X, model, y, reg=0)

# Sanity check: Loss should be about log(10) = 2.3026
print 'Sanity check loss (no regularization): ', loss

# Sanity check: Loss should go up when you add regularization
loss, _ = two_layer_convnet(X, model, y, reg=1)
print 'Sanity check loss (with regularization): ', loss


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
Train the net
Once the above works, training the net is the next thing to try. You can set the acc_frequency parameter to change the frequency at which the training and validation set accuracies are tested. If your parameters are set properly, you should see the training and validation accuracy start to improve within a hundred iterations, and you should be able to train a reasonable model with just one epoch.
Using the parameters below you should be able to get around 50% accuracy on the validation set.
Can you write Python code for it?

model = init_two_layer_convnet(filter_size=7)
trainer = ClassifierTrainer()
best_model, loss_history, train_acc_history, val_acc_history = trainer.train(
          X_train, y_train, X_val, y_val, model, two_layer_convnet,
          reg=0.001, momentum=0.9, learning_rate=0.0001, batch_size=50, num_epochs=1,
          acc_frequency=50, verbose=True)


I am working on the file "783/HW2/task3_convnet.ipynb".
The first task is:
Visualize weights
We can visualize the convolutional weights from the first layer. If everything worked properly, these will usually be edges and blobs of various colors and orientations.
Can you write Python code for it?

from cs231n.vis_utils import visualize_grid

grid = visualize_grid(best_model['W1'].transpose(0, 2, 3, 1))
plt.imshow(grid.astype('uint8'))


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Fooling Images
We can also use image gradients to generate "fooling images" as discussed in [2]. Given an image and a target class, we can perform gradient ascent over the image to maximize the target class, stopping when the network classifies the image as the target class. Implement the following function to generate fooling images.
[2] Szegedy et al, "Intriguing properties of neural networks", ICLR 2014
Can you write Python code for it?

def make_fooling_image(X, target_y, model):
  """
  Generate a fooling image that is close to X, but that the model classifies
  as target_y.

  Inputs:
  - X: Input image, of shape (1, 3, 64, 64)
  - target_y: An integer in the range [0, 100)
  - model: A PretrainedCNN

  Returns:
  - X_fooling: An image that is close to X, but that is classifed as target_y
    by the model.
  """
  X_fooling = X.copy()
  ##############################################################################
  # TODO: Generate a fooling image X_fooling that the model will classify as   #
  # the class target_y. Use gradient ascent on the target class score, using   #
  # the model.forward method to compute scores and the model.backward method   #
  # to compute image gradients.                                                #
  #                                                                            #
  # HINT: For most examples, you should be able to generate a fooling image    #
  # in fewer than 100 iterations of gradient ascent.                           #
  ##############################################################################
  from cs231n.layers import softmax_loss
  lr = 0.5
  it = 0
  loss = 1000.0
  while it<101:
    out, cache = model.forward(X_fooling)
    loss = model.loss(X_fooling)[0][target_y]
    dout = np.zeros_like(out)
    dout[0][target_y]=1000
    dx, _ = model.backward(dout, cache)
    X_fooling += lr*dx
    if it%100==0:
        print loss
        lr *= 0.9
    it = it+1
  ##############################################################################
  #                             END OF YOUR CODE                               #
  ##############################################################################
  return X_fooling


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Introducing TinyImageNet
The TinyImageNet dataset is a subset of the ILSVRC-2012 classification dataset. It consists of 200 object classes, and for each object class it provides 500 training images, 50 validation images, and 50 test images. All images have been downsampled to 64x64 pixels. We have provided the labels for all training and validation images, but have withheld the labels for the test images.
We have further split the full TinyImageNet dataset into two equal pieces, each with 100 object classes. We refer to these datasets as TinyImageNet-100-A and TinyImageNet-100-B; for this exercise you will work with TinyImageNet-100-A.
To download the data, go into the cs231n/datasets directory and run the script get_tiny_imagenet_a.sh. Then run the following code to load the TinyImageNet-100-A dataset into memory.
NOTE: The full TinyImageNet-100-A dataset will take up about 250MB of disk space, and loading the full TinyImageNet-100-A dataset into memory will use about 2.8GB of memory.
Can you write Python code for it?

data = load_tiny_imagenet('cs231n/datasets/tiny-imagenet-100-A', subtract_mean=True)


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Once you have completed the implementation in the cell above, run the following to visualize some class saliency maps on the validation set of TinyImageNet-100-A.
Can you write Python code for it?

def show_saliency_maps(mask):
  mask = np.asarray(mask)
  X = data['X_val'][mask]
  y = data['y_val'][mask]

  saliency = compute_saliency_maps(X, y, model)

  for i in xrange(mask.size):
    plt.subplot(2, mask.size, i + 1)
    plt.imshow(deprocess_image(X[i], data['mean_image']))
    plt.axis('off')
    plt.title(data['class_names'][y[i]][0])
    plt.subplot(2, mask.size, mask.size + i + 1)
    plt.title(mask[i])
    plt.imshow(saliency[i])
    plt.axis('off')
  plt.gcf().set_size_inches(10, 4)
  plt.show()

# Show some random images
mask = np.random.randint(data['X_val'].shape[0], size=5)
show_saliency_maps(mask)

# These are some cherry-picked images that should give good results
show_saliency_maps([128, 3225, 2417, 1640, 4619])


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Pretrained model
We have trained a deep CNN for you on the TinyImageNet-100-A dataset that we will use for image visualization. The model has 9 convolutional layers (with spatial batch normalization) and 1 fully-connected hidden layer (with batch normalization).
To get the model, run the script get_pretrained_model.sh from the cs231n/datasets directory. After doing so, run the following to load the model from disk.
Can you write Python code for it?


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Run the following to choose a random validation set image that is correctly classified by the network, and then make a fooling image.
Can you write Python code for it?

# Find a correctly classified validation image
while True:
  i = np.random.randint(data['X_val'].shape[0])
  X = data['X_val'][i:i+1]
  y = data['y_val'][i:i+1]
  y_pred = model.loss(X)[0].argmax()
  if y_pred == y: break

target_y = 67
X_fooling = make_fooling_image(X, target_y, model)

# Make sure that X_fooling is classified as y_target
scores = model.loss(X_fooling)
assert scores[0].argmax() == target_y, 'The network is not fooled!'

# Show original image, fooling image, and difference
plt.subplot(1, 3, 1)
plt.imshow(deprocess_image(X, data['mean_image']))
plt.axis('off')
plt.title(data['class_names'][y[0]][0])
plt.subplot(1, 3, 2)
plt.imshow(deprocess_image(X_fooling, data['mean_image'], renorm=True))
plt.title(data['class_names'][target_y][0])
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title('Difference')
plt.imshow(deprocess_image(X - X_fooling, data['mean_image']))
plt.axis('off')
plt.show()


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
TinyImageNet-100-A classes
Since ImageNet is based on the WordNet ontology, each class in ImageNet (and TinyImageNet) actually has several different names. For example "pop bottle" and "soda bottle" are both valid names for the same class. Run the following to see a list of all classes in TinyImageNet-100-A:
Can you write Python code for it?

for i, names in enumerate(data['class_names']):
  print i, ' '.join('"%s"' % name for name in names)


I am working on the file "783/HW2/task4_image_gradients.ipynb".
The first task is:
Visualize Examples
Run the following to visualize some example images from random classses in TinyImageNet-100-A. It selects classes and images randomly, so you can run it several times to see different images.
Can you write Python code for it?

# Visualize some examples of the training data
classes_to_show = 7
examples_per_class = 5

class_idxs = np.random.choice(len(data['class_names']), size=classes_to_show, replace=False)
for i, class_idx in enumerate(class_idxs):
  train_idxs, = np.nonzero(data['y_train'] == class_idx)
  train_idxs = np.random.choice(train_idxs, size=examples_per_class, replace=False)
  for j, train_idx in enumerate(train_idxs):
    img = deprocess_image(data['X_train'][train_idx], data['mean_image'])
    plt.subplot(examples_per_class, classes_to_show, 1 + i + classes_to_show * j)
    if j == 0:
      plt.title(data['class_names'][class_idx][0])
    plt.imshow(img)
    plt.gca().axis('off')

plt.show()


I am working on the file "783/HW3/GAN.ipynb".
The first task is:
As our input vector we will always use a uniform random distrubiton, but instead of using between 0-1 we will use 0-100 range to make life a little bit easier for our Generator network.
Can you write Python code for it?


I am working on the file "783/HW3/GAN.ipynb".
The first task is:
Generator Network
We will use a basic 2 layer FCN for our generator and discriminator, since our dataset is basically one dimensional it should be enough
Can you write Python code for it?

from cs231n.classifiers.neural_net import * #we will use the 2layer FCN from HW1
g_inpsize = 1   #we will just take 1 real number uniform distrubtion
g_hidsize = 50 #
g_outsize = 1   #we will output a real number from our data distribution

Generator = GenNet(g_inpsize, g_hidsize, g_outsize) #it is gonna perform cross entropy loss instead of softmax,
                                                         #since our labels are just real or fake,
                                                         #it fits our purposes

d_inpsize = 1 #first check without batch normaliziton
d_hidsize = 50
d_outsize = 1 #again we are just gonna output real or fake

Discriminator = DiscNet(d_inpsize, d_hidsize, d_outsize) #we will use slightly modified version of TwoLayerNet
                                                            #we just added sigmoid at the output layer since all of
                                                        #our outputs needs to be in range [0,1]


I am working on the file "783/HW3/GAN.ipynb".
The first task is:
Gradient Check
Can you write Python code for it?

from cs231n.gradient_check import eval_numerical_gradient, eval_numerical_gradient_array

D = 1
x = np.random.rand(200, D)
y = np.ones((200,1))
W1 = np.random.randn(D, d_hidsize)
W2 = np.random.randn(d_hidsize, d_hidsize)
W3 = np.random.randn(d_hidsize, d_outsize)
b1 = np.random.randn(d_hidsize)
b2 = np.random.randn(d_hidsize)
b3 = np.random.randn(d_outsize)

fx = lambda x: Discriminator.loss(x, y)[0]
fW1 = lambda W1: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]
fW2 = lambda W2: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]
fW3 = lambda W3: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]
fb1 = lambda b1: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]
fb2 = lambda b2: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]
fb3 = lambda b3: Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)[0]

num_grad = lambda x,y: eval_numerical_gradient(x,y, verbose=False, h=1e-6)

dx_num = num_grad(fx, x)
dW1_num = num_grad(fW1, W1)
dW2_num = num_grad(fW2, W2)
dW3_num = num_grad(fW3, W3)
db1_num = num_grad(fb1, b1)
db2_num = num_grad(fb2, b2)
db3_num = num_grad(fb3, b3)

loss, grads = Discriminator.loss(x, y, W1=W1, W2=W2, W3=W3, b1=b1, b2=b2, b3=b3)
dx, dW1, dW2, dW3, db1, db2, db3 = grads['X'], grads['W1'], grads['W2'], grads['W3'], grads['b1'], grads['b2'], grads['b3']
print 'dx error: ', rel_error(dx_num, dx)
print 'dW1 error: ', rel_error(dW1_num, dW1)
print 'dW2 error: ', rel_error(dW2_num, dW2)
print 'dW3 error: ', rel_error(dW3_num, dW3)
print 'db1 error: ', rel_error(db1_num, db1)
print 'db2 error: ', rel_error(db2_num, db2)
print 'db3 error: ', rel_error(db3_num, db3)

D = 1
x = np.random.randn(200, D)
W1 = np.random.randn(D, g_hidsize)
W2 = np.random.randn(g_hidsize, g_hidsize)
W3 = np.random.randn(g_hidsize, g_outsize)
b1 = np.random.randn(g_hidsize)
b2 = np.random.randn(g_hidsize)
b3 = np.random.randn(g_outsize)

fx = lambda x: Generator.loss(x, Discriminator, W1=W1, W2=W2, W3=W3, b1=b1,...


I am working on the file "783/HW3/GAN.ipynb".
The first task is:
Let us start with a standart distribution as our dataset
Can you write Python code for it?

N = 1000000
mean = 0.0
std = 1.0

data = np.random.normal(mean, std, (N,1))

_ = plt.hist(data, 100, normed=1)
_ = plt.hist(inputVector[np.abs(inputVector)<4], 100, normed=1)

g_inpsize = 1   #we will just take 1 real number uniform distrubtion
g_hidsize = 50 #
g_outsize = 1   #we will output a real number from our data distribution

Generator = GenNet(g_inpsize, g_hidsize, g_outsize) #it is gonna perform cross entropy loss instead of softmax,
                                                         #since our labels are just real or fake,
                                                         #it fits our purposes

d_inpsize = 1 #first check without batch normaliziton
d_hidsize = 50
d_outsize = 1 #again we are just gonna output real or fake

Discriminator = DiscNet(d_inpsize, d_hidsize, d_outsize) #we will use slightly modified version of TwoLayerNet
                                                            #we just added sigmoid at the output layer since all of
                                                        #our outputs needs to be in range [0,1]

Trainer = GANTrainer(Generator, Discriminator, data, update_rule='adam',
           num_epochs=5,
           batch_size=100,
           optim_config={
             'learning_rate': 2e-5,
           },
           lr_decay=0.995,
           verbose=True, print_every=10000)

Trainer.train()

plt.plot([x[0] for x in Trainer.loss_history])
plt.plot([x[1] for x in Trainer.loss_history])
plt.plot([x[2] for x in Trainer.loss_history])


I am working on the file "783/HW3/Next Character Prediction with RNN.ipynb".
The first task is:
I simply modified the code for CapitoningRNN to get rid of initial hidden state that has been feed from CNN, instead I give all zeros, and also get rid of word embedding layer since we are going to use characters we could use just their ascii representative.
Also in the loss and sample function there are a few modifications to reflect those changes.
Can you write Python code for it?

small_rnn_model = NextCharRNN(
          cell_type='rnn',
          input_dim=input_size,
          hidden_dim=512,
          charvec_dim=1,
        )

small_rnn_solver = NextCharSolver(small_rnn_model, X_train,
           update_rule='adam',
           num_epochs=50,
           batch_size=100,
           optim_config={
             'learning_rate': 1e-2,
           },
           lr_decay=0.95,
           verbose=True, print_every=100,
         )
small_rnn_solver.train()

# Plot the training losses
plt.plot(small_rnn_solver.loss_history)
plt.xlabel('Iteration')
plt.ylabel('Loss')
plt.title('Training loss history')
plt.show()

mbs=10
idx = np.random.choice(len(X_train), mbs)
minibatch = X_train[idx]
next_chars = small_rnn_model.sample(minibatch, 6)
print 'Training Data:'
for i in xrange(mbs):
    print 'Predicted string:', int_list_to_string(next_chars[i,:])
    print 'Real string:', int_list_to_string(minibatch[i,:])
    print

idx = np.random.choice(len(X_val), mbs)
minibatch = X_val[idx]
next_chars = small_rnn_model.sample(minibatch, 6)
print 'Validation Data:'
for i in xrange(mbs):
    print 'Predicted string:', int_list_to_string(next_chars[i,:])
    print 'Real string:', int_list_to_string(minibatch[i,:])
    print


I am working on the file "7_BestPractices_Testing_Performance.ipynb".
The first task is:
Make sure your code does what it is supposed to do by testing it with simple examples where you know what to expect

Save those tests (e.g. in if __name__ == __main__ of each module) to keep your code correct while changing parts

⚠ Automate testing

```python
import unittest
def square(x):
    return x*x
class TestSquare(unittest.TestCase):
def test_square(self):
    self.assertEqual(square(2), 4)

if name == 'main':
    unittest.main()
```

⚠ Generate fuzzy tests using hypothesis:

```python
from hypothesis import given
from hypothesis.strategies import text, floats
@given(text(min_size=10), text(min_size=10), text(min_size=10))
def test_triangle_inequality(self, a, b, c):
    self.assertTrue(zipstance(a, c) <= zipstance(a, b) + zipstance(b, c))
```

⚠ Turn documentation into tests using doctest:

```python
def square(x):
    """Return the square of x.
>>> square(2)
0
"""
return x*x

if name == 'main':
    import doctest
    doctest.testmod()
```
Will give
```bash
$ python doctestest.py

File "doctestest.py", line 4, in main.square
Failed example:
    square(2)
Expected:
    0
Got:
    4

1 items had failures:
   1 of   1 in main.square
Test Failed 1 failures.
```

⚠ Use mutation tests if you need to be sure that your code is correct. They replace pieces of code, e.g. < with > to find out whether your tests cover everything.

Optimizing Performance
Quick Tests
Can you write Python code for it?

%timeit [i**2 for i in range(100000)]

%timeit np.arange(100000)**2


I am working on the file "7_BestPractices_Testing_Performance.ipynb".
The first task is:
Structuring Code

Don't repeat yourself (and others) to keep your programs small

Structure code into functions and files. You can import functions and data from files (so-called modules):

```python
File module/example.py
x = 'Example data'
def example_function():
    pass
if name == 'main':
    whatever()
File in .
from module import example
example.x
```
Documentation

Document why and not what (what states the code)

Commit early and often to keep track of why you change things

False (usually out-dated) documentation is worse than none

Turn questions (yours or others) into documentation

Write self-documenting code, e.g. (from real-life C++) change

C++
if ((j != k) and (d_type[d_lattice->d_cell_id[j]] == mesenchyme)
        and (d_type[d_lattice->d_cell_id[k]] == mesenchyme)
        and (dist < r_link)
        and (fabs(r.w/(d_X[d_lattice->d_cell_id[j]].w +
             d_X[d_lattice->d_cell_id[k]].w)) > 0.2)) {
    d_link[i].a = d_lattice->d_cell_id[j];
    d_link[i].b = d_lattice->d_cell_id[k];
}
to
C++
auto both_mesenchyme = (d_type[d_lattice->d_cell_id[j]] == mesenchyme)
    and (d_type[d_lattice->d_cell_id[k]] == mesenchyme);
auto along_w = fabs(r.w/(d_X[d_lattice->d_cell_id[j]].w +
                    d_X[d_lattice->d_cell_id[k]].w)) > 0.2;
if (both_mesenchyme and (dist < r_link) and along_w) {
    d_link[i].a = d_lattice->d_cell_id[j];
    d_link[i].b = d_lattice->d_cell_id[k];
}

Use docstrings:

python
def documented_function():
    """This is a docstring"""
    pass

⚠ Turn documentation into code using docopt:

```python
"""A Docopt Example.
Usage:
  docoptest.py [flag] [--parameter <x>]
  docoptest.py (-h | --help)
Options:
  -h --help        Show this screen.
  --parameter <x>  Pass parameter.
"""
if name == 'main':
    from docopt import docopt
args = docopt(__doc__)
print(args)

```
Results in
```bash
$ python docoptest.py wrong usage
Usage:
  docoptest.py [flag] [--parameter <x>]
  docoptest.py (-h | --help)
$ python...
Can you write Python code for it?

mass = -1
assert mass > 0, 'Mass cannot be negative!'


I am working on the file "7_BestPractices_Testing_Performance.ipynb".
The first task is:
Vectorize, 7x
While the initial function was already using numpy and vectors it still involves a for-loop that can be vectorized ... well, actually tensorized:
python
def forces(t, X, N):
    r = X[N] - np.tile(X, (k, 1, 1)).transpose(1, 0, 2)
    norm_r = np.minimum(np.linalg.norm(r, axis=2), r_max)
    norm_F = 2*(r_min - norm_r)*(norm_r - r_max) - (norm_r - r_max)**2
    F = np.sum(r*(norm_F/norm_r)[:, None].transpose(0, 2, 1), axis=1)
    return F.ravel()
Re-use resources, 1.5x
Broadcasting gives just a "view" instead of a copy returned by np.tile:
python
def forces(t, X, N):
    r = X[N] - X[:, None, :]
    norm_r = np.minimum(np.linalg.norm(r, axis=2), r_max)
    norm_F = 2*(r_min - norm_r)*(norm_r - r_max) - (norm_r - r_max)**2
    F = np.sum(r*(norm_F/norm_r)[:, None].transpose(0, 2, 1), axis=1)
    return F.ravel()
The standard inplace operators +=, -=, *=, and /= can speed up large numpy calculations a little, Pandas operations often have an inplace flag, e.g. df.reset_index(inplace=True). Sometimes allocating memory with np.empty or pd.DataFrame pays off. Make sure to use the fastest data type for each column of a pd.DataFrame.
⚠ Compile the calculation, 2x (3.5x on GTX 1060 for 10k cells)
Finally code can be compiled in various ways. We choose Theano because it requires little rewritting (and can run code on the GPU as well):
```python
from theano import Tensor as T
from theano import function
X = T.matrix('X', dtype='floatX')
N = T.imatrix('N')
r = X[N] - X[:, None, :]
norm_r = T.minimum(r.norm(2, axis=2), 1)
norm_F = 2(0.5 - norm_r)(norm_r - 1) - (norm_r - 1)*2
F = T.sum(r(norm_F/norm_r).dimshuffle(0, 1, 'x'), axis=1)
f = function([X, N], F.ravel(), allow_input_downcast=True)
def forces(t, X, N):
    return f(X.reshape(-1, 3), self.N)
```
Aftermath
Can you write Python code for it?

p = pstats.Stats('theanostats')
p.strip_dirs().sort_stats('cumulative').print_stats(10);


I am working on the file "7_Boundary_Value_Problems.ipynb".
The first task is:
7.3 Collocation and the Finite Element Method
Example
Apply the Finite Element Method to the BVP
$$
\left{\begin{matrix}\begin{align}
y''  &= 4y \
y(0) &= 1  \
y(1) &= 3
\end{align}\end{matrix}\right.
$$
Can you write Python code for it?

def finite_element_method_sp( a, b, ya, yb, n):
    h = (b - a) / (n + 1)
    alpha = (8 / 3) * h + 2 / h
    beta = (2 / 3) * h - 1 / h
    A = np.zeros(n * n).reshape(n, n)
    np.fill_diagonal(A, alpha)
    dia_range = np.arange(n - 1)
    A[dia_range, dia_range + 1] = beta
    A[dia_range + 1, dia_range] = beta
    b = np.zeros(n)
    b[0] = -ya * beta
    b[-1] = -yb * beta
    x = np.linalg.solve(A, b)
    return x

finite_element_method_sp(0, 1, 1, 3, 3)


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
Lambda Functions
Say you want to create a new column indicating whether a given order was profitable or not (1/0).
You need to apply a function which returns 1 if Profit > 0, else 0. This can be easily done using the apply() method on a column of the dataframe.
Can you write Python code for it?

# Create a function to be applied
def is_positive(x):
    return x > 0

# Create a new column
master_df['is_profitable'] = master_df['Profit'].apply(is_positive)
master_df.head()


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
Now you can use the new column to compare the percentage of profitable orders across groups.
Can you write Python code for it?

# Comparing percentage of profitable orders across customer segments
by_segment = master_df.groupby('Customer_Segment')
by_segment.is_profitable.mean()

# Comparing percentage of profitable orders across product categories
by_category = master_df.groupby('Product_Category')
by_category.is_profitable.mean()


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
Pivot Tables
You may want to use pandas pivot tables as an alternative to groupby(). They provide Excel-like functionalities to create aggregate tables.
Can you write Python code for it?

# Read documentation
help(pd.DataFrame.pivot_table)


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
The general syntax is pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', ...).
* data is a dataframe
* values contains the column to aggregate
* index is the row in the pivot table
* columns contains the columns you want in the pivot table
* aggfunc is the aggregate function
Let's see some examples.
Can you write Python code for it?

# E.g. Compare average Sales across customer segments
master_df.pivot_table(values = 'Sales', index = 'Customer_Segment', aggfunc = 'mean')

# E.g. compare total number of profitable orders across regions
# Note that since is_profitable is 1/0, we can directly compute the sum
master_df.pivot_table(values = 'is_profitable', index = 'Region', aggfunc = 'sum')

# Grouping by both rows and columns
# Compare the total profit across product categories and customer segments
# Since there are two categorical variables, we use both rows (index) and columns
master_df.pivot_table(values = 'Profit',
                      index = 'Product_Category',
                      columns = 'Customer_Segment',
                      aggfunc = 'sum')


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
The same can be done in just one line of code using lambda functions.
Can you write Python code for it?

# Create a new column using a lambda function
master_df['is_profitable'] = master_df['Profit'].apply(lambda x: x > 0)
master_df.head()


I am working on the file "7_Lambda_Functions_Pivot_Tables.ipynb".
The first task is:
You sometimes need to create new columns using existing columns, for instance, say you want a column Profit / Order_Quantity.
Can you write Python code for it?

# Creating a column Profit / Order_Quantity
master_df['profit_per_qty'] = master_df['Profit'] / master_df['Order_Quantity']
master_df.head()


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.5.1: Schematic diagram of an antenna radiation pattern (Image taken from https://commons.wikimedia.org/wiki/File:Sidelobes_en.svg).
Example: Primary beam of the JVLA (Jansky Very Large Array)
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.5.2: Primary beam of the JVLA at 1.41 GHz.
For a JVLA antenna, the primary beam gain varies with direction at a given frequency (see Figures 7.5.1 and 7.5.2), and with frequency towards a given direction (see Figures 7.5.3 and 7.5.4; the beam pattern scales with frequency, becoming more compact at higher frequencies).
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.5.6: Relative rotation of the primary beam pattern wrt the sky during the course of an observation. The blue and green sources experience different beam gains at different times, while the beam gain for the red source at the phase centre remains unchanged.
The rotation of the primary beam causes the beam gain in a given direction to vary with time.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.5.7: Rotation of the beam pattern in Figure 7.5.2 during the course of an observation.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.7.2: Relative rotation of the beam and the sky during the course of an observation, for an antenna with an alt-azimuth mount.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Figure 7.7.3: Relative rotation of the beam and the sky during the course of an observation, for an antenna with an equatorial mount.
7.7.2 Parallactic angle
The parallactic angle is the spherical angle between two great circles on the celestial sphere - one passing through the source and the zenith, and the other passing through the source and the north celestial pole.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?

from IPython.display import Image

HTML('../style/code_toggle.html')


I am working on the file "7_Observing_Systems/7_5_primary_beam.ipynb".
The first task is:
TODO:

why is it important to understand the primary beam
a direction-dependent effect
present the E jones
what is intrinsic and apparent flux
example: sky field with and without beam attenuation
why is it a plane wave? source is in the far field, define far-field distance, define far/near field
consider a single dish
optics intution of a primary beam: reflecting a plane wave to a focus using a simple parabolic dish
simple aperture of a parabolic dish is a disc -> bessel beam pattern ; fourier relation
voltage beam, power beam ; coordinate system
primary lobe, sidelobes, FWHM/resolution, directivity, gain
adding complexity: aperture blockage, secondary reflector
aperture efficiency: surface accuracy, blockage, taper, spillover
typical types of dishes: primary focus, cassegrain, offset gregorian (with examples)
antenna mounts
parallactic angle
example: primary beam of kat-7 (beam pattern, freq dependence, spatial slice, freq slice)
example: primary beam of meerkat (beam pattern, freq dependence, spatial slice, freq slice)
extra: pointing accuracy, jitter, deformation
the relation of a primary beam of a single dish to the PSF of an interferometric array

how is the primary beam used in calibration and imaging?

https://www.cv.nrao.edu/course/astr534/ReflectorAntennas.html

https://www.cv.nrao.edu/course/astr534/RadioTelescopes.html
white book chapter 3
malloux antenna theory

7.5 The Primary Beam (E- and P-Jones)
The primary beam of an antenna (also known as the radiation pattern) is the directional dependence of the gain of the antenna.
The primary beam of the antenna is the most important direction-dependent propagation effect.
It has a multiplicative effect in the image plane, and a convolutional effect in the visibility plane, due to the Fourier Transform relationship between the image and visibility planes.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Figure 7.7.2: Different imaging regimes in the presence of scattering, shown as function of the integration time and Fresnel time ($t_{\rm F} = r_{\rm F}/v$) on the vertical axis and the strength of the scattering shown on the horizontal axis. These regimes can be explored using the ScatterBrane code below in combination with typical values from Table 7.7.1. (Credit: Narayan 1992)
The following code is taken from the SLIMSCAT package courtesy of Michael Johnson and Katherine Rosenfeld. Changing the input parameter values and input image will enable a more intuitive understanding of scattering effects in the different imaging regimes shown in Figure 7.7.2.
Can you write Python code for it?

if doExample:

    # construct gaussian source
    nx,ny = 220,256
    yy,xx = np.meshgrid(np.arange(ny)-ny/2,np.arange(nx)-nx/2,indexing='ij')
    isrc = np.exp(-0.5*(xx**2 + yy**2) / (0.07*np.min([nx,ny]))**2)

    slimscat.generate_screen(screenfile='gaussian_screen.bin')

    # scatter source
    iss = slimscat.run_slimscat(isrc,1.,screenfile='gaussian_screen.bin')

if doExample:

    plt.figure(figsize=(15,6))

    plt.subplot(121)
    plt.imshow(isrc,vmin=isrc.min(),vmax=isrc.max())
    plt.title('source')
    plt.colorbar()

    plt.subplot(122)
    plt.imshow(iss,vmin=isrc.min(),vmax=isrc.max())
    plt.title('scattered')
    plt.colorbar()


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Figure 7.7.4: Fun colour example of a scattered RGB image of the black hole sillhouette of Gargantua (from the movie Interstellar).
If you increase the wavelength of the observation, or decrease the frequency (see input to slimscat.generate_screen), you'll see how interstellar scattering can be a major barrier to successfully measuring the shadow of a black hole with the Event Horizon Telescope! See http://www.eventhorizontelescope.org/ for more details on this project.
7.7.2 Ionosphere
The ionosphere is a layer of ionized plasma with an altitude range of 60-1000 km above sea level. This is driven mostly by ultraviolet photons from the Sun that ionize the upper layers of the atmosphere. As a result, there is a natural diurnal cycle, with the layers nearest the Sun being the mostly highly ionized. This level of ionization is measured with the unit Total Electron Content (TEC), where 1 TEC unit (TECU) = $10^{16}$ m$^{-2}$. Figure~xx shows a global map with the TEC values at a given time, showing that this is most concentrated near the equator.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Figure 7.7.5: Total Electron Content above the Earth at given time, showing the enhancement towards the equatorial regions nearest the Sun. (Image credit: http://www.astrosurf.com/luxorion/qsl-review-propagation-software-research.htm)
The ionosphere is a subject of intensive and complex study itself. Here we just describe the primary (adverse) effects it has on radio observations. Like any plasma, the ionosphere has a oscillation or plamsa frequency, given by $$\nu_p = \frac{e}{2\pi}\sqrt{\frac{n_e}{\epsilon_0 m}} \ \simeq 9\sqrt{n_e} \ \ {\rm (Hz)},$$ where $e, n_e, \epsilon_0$ are the electron charge, density and permitivity of free space respectively. A typical value for the ionosphere's plasma frequency is less than $\nu_p \sim 15$ MHz. Note than the ionosphere is opaque to any radiation below this frequency. While this disadvantage in low frequency observations of the Universe from Earth, it has also be used to aid long-distance radio communication by reflecting radio waves off the lower surface of the ionosphere.
The free electrons in the ionosphere cause a propogation delay of an incident electro-magnetic wave, which can be represented as an "excess path length", $$L_0 \simeq -\frac{1}{2} \int^\infty_0 \left[ \frac{\nu_p(h)}{\nu}\right]^2 {\rm d}h, $$
where $h$ is the height above sea level. Note the $\nu^2$ dependance, which is why the ionophere is primarily a problem for low radio frequency observations, such as attempts to detect the epoch of reionization. Within the Jones matrix formalism, this delay can be represented as follows,
$$
\mathbf{Z} = Z = \left[ \begin{array}{cc}\mathrm{e}^{-2\pi \imath \tau / \lambda} & 0 \ 0 & \mathrm{e}^{-2\pi \imath \tau / \lambda} \end{array} \right] = \mathrm{e}^{-2\pi \imath \tau / \lambda},
$$
which is of course the same as any other phase only effect, of which there are many described in this chapter. Although the $\mathbf{Z}$ Jones matrix has no amplitude effect, it is important to note that the variable or differential delay imposed by the ionsphere result...
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Figure 7.7.6: Four different radio array regimes with regard to ionospheric calibration (see text). Credit: Huib Intema (2013 Lorentz Center presentation).
7.7.3 Trophosphere
This is the lowest layer of the neutral atmosphere and has typical scale height of 2 km. The refractivity of water vapour in this layer at radio wavelengths is of order 20x greater than in the optical/near-infrared (recall that typical "seeing" in the optical window is ~1 arcsec). Since water vapour is poorely mixed in this layer, there are phase fluctuations introduced due to the refractive index inhomogenities that result. The delay due to a non-unity refractive index ($n$) is $$\Delta t = \frac{1}{c} \int (n - 1) dy, $$ where c is the speed of light the refractive path length is the straight line path. The refractivity of moist air is described by the empirical formula $$N = 77.6\frac{p_D}{T} + 64.8\frac{p_V}{T} + 3.776 \times 10^5 \frac{p_V}{T^2}, $$ where the refractivity $N \equiv 10^6(n-1)$, $T$ is the temperature in Kelvins, $p_D$ and $p_V$ are the partial pressures of dry air and water vapor respectively. Since the atmosphere obeys the equation of hydrostatic equilibrium (to a good approximation), it is clear that to zero-th order, the trosphere's refractive index will have some dependence on time-of-day, time-of-season, site altitude and latitude. However, water vapour is not well mixed in the atmosphere and is therefore not well correlated with ground-based meteorological parameters.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Figure 7.7.7: Map of the Earth showing water vapour content during August 2010. (NASA image by Robert Simmon)
The troposphere has several effects on high frequency ($>> 10$~GHz) radio observations, the most important (or detrimental) are:

absorption
refraction
turbulence-induced scattering

The absorption (also known as the opacity) is due to pressure-broadened molecular resonances of water vapour, oxygen and ozone. Between 100 GHz to 1 THz there are a series of strong water vapour lines, necessitating the contruction of millimetre observatories at the high altitudes ($>2$ km), where several atmospheric windows are made accesible (see Figure 7.8.2).
The mean refraction is an effect that must be corrected in the telescope pointing, increasingly so at low elevation. However, small-scale tropospheric turbulence results in rapid phase fluctuations, significantly limiting telescope sensitivity. At $>$100 GHz, coherence times are limited to $\sim$10 seconds for even some of the best sites in the world.
The power spectrum of these phase fluctuations are usually described as a Kologmov turbulent screen, with a phase structure function defined as:
$$D_\phi(d) = \ <[\Phi(x) - \Phi(x - d)]^2>, $$
where $\Phi(x)$ is the phase at point $x$, $\Phi(x-d)$ is the phase at point $x-d$, and the angle brackets represent the ensemble average. This implies that the closer two points are to one another, the more similar their phase delay will be. The theory on this subject in particular is vast and not covered in any further detail here, but see Thompson et al. 2001, and reference therein.
In terms of the Jones matrix formalism, the troposhere is equiavalent to the ionosphere, however, with the additional amplitude loss due to non-zero atmospheric opacity.
Can you write Python code for it?


I am working on the file "7_Observing_Systems/7_7_propagation_effects.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?

import scipy.signal
from IPython.display import Image
import matplotlib.image as mpimg

# This section uses the optional slimscat package (https://krosenfeld.github.io/slimscat/current/)
try:
    import slimscat
    doExample = True
except ImportError:
    print 'Warning: could not import slimscat, will not run example'
    doExample = False


I am working on the file "7_Observing_Systems/7_8_rfi.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?


I am working on the file "7_VisualizationTableauData.ipynb".
The first task is:
Each of the datasheets downloaded from ELSI had download metadata on the top of them and total and key information on the bottom of them that were not data rows.  This metadata, total, and key information was manually deleted before import.  Some of the files had ="0" instead of 0 in the cells.  This was found/replaced before import using the sed -i '' 's/="0"/0/g' *.csv command from the terminal.
Can you write Python code for it?

#CITATION: This is the data from National Center for Education Statistics on Schools
#Schools for all 50 states and Washington, D.C.
#http://nces.ed.gov/ccd/elsi/
#ELSI Root Data Source: U.S. Department of Education National Center for Education Statistics Common Core of Data (CCD) "Public Elementary/Secondary School Universe Survey" 2012-13 v.1a  2013-14 v.1a.
#KEY:
#† indicates that the data are not applicable.
#– indicates that the data are missing.
#‡ indicates that the data do not meet NCES data quality standards.

schoolinformation = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL Information Tab.csv", dtype=np.str)
schoolcharacteristicsa = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL CharacteristicsA Tab.csv", dtype=np.str)
schoolcharacteristicsb = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL CharacteristicsB Tab.csv", dtype=np.str)
schoolenrollment = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL Enrollments Tab.csv", dtype=np.str)
schoolenrollmentdetails = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL Enrollment Details Tab.csv", dtype=np.str)
#schoolenrollmentK3 = pd.read_csv("tempdata/2013-2014 SCHOOL Enrollment by Grade, Race-Ethnicity, and Gender Tab PreK-3.csv", dtype=np.str)
#schoolenrollment48 = pd.read_csv("tempdata/2013-2014 SCHOOL Enrollment by Grade, Race-Ethnicity and Gender Tab 4-8.csv", dtype=np.str)
#schoolenrollment912 = pd.read_csv("tempdata/2013-2014 SCHOOL Enrollment by Grade, Race-Ethnicity and Gender Tab 9-Ungraded.csv", dtype=np.str)
schoolteacherstaff = pd.read_csv("data/rawdata/schools/2009-2010 SCHOOL Teacher and Staff Tab.csv", dtype=np.str)


I am working on the file "7_VisualizationTableauData.ipynb".
The first task is:
Join all of the school datasets.
Can you write Python code for it?

joinedschool = schoolteacherstaff.join([schoolcharacteristicsa, schoolcharacteristicsb, schoolenrollment, schoolenrollmentdetails])

joinedschool = schoolinformation.merge(joinedschool, 'left', 'School ID - NCES Assigned [Public School] Latest available year', suffixes=('', '_DEL'))

#Need to get rid of Excel syntax ="" from some of the columns
for i, col in enumerate(joinedschool.columns):
    joinedschool[col] = joinedschool[col].map(lambda x: str(x).lstrip('="').rstrip('"'))

#If by chance any rows have NaN, replace with the ELSI standard for missing data '–'
joinedschool = joinedschool.fillna('–')
joinedschool = joinedschool.replace('nan', '–')

# Replacing Missing Data / NA / Bad Quality data with blank, later to be turned into NaN for float columns
# CITATIOIN : http://pandas.pydata.org/pandas-docs/version/0.15.2/missing_data.html

joinedschool = joinedschool.replace('\xe2\x80\x93', '') # Replace "-" (Missing Data) with blank
joinedschool = joinedschool.replace('\xe2\x80\xa0', '') # Replace "†" (Not Applicable) with blank
joinedschool = joinedschool.replace('\xe2\x80\xa1', '') # Replace "‡" (Bad Quality) with blank

joinedschool['i_agency_type_regional_education_services'] = np.where(joinedschool['Agency Type [District] 2009-10']=='4-Regional education services agency', 1, 0)
joinedschool['i_agency_type_local_school_district'] = np.where(joinedschool['Agency Type [District] 2009-10']=='1-Local school district', 1, 0)
joinedschool['i_lgo_PK'] = np.where(joinedschool['Lowest Grade Offered [Public School] 2009-10']=='Prekindergarten', 1, 0)
joinedschool['i_lgo_K'] = np.where(joinedschool['Lowest Grade Offered [Public School] 2009-10']=='Kindergarten', 1, 0)
joinedschool['Black Students [Public School] 2009-10'] = joinedschool['Black Students [Public School] 2009-10'].replace('', np.nan)
joinedschool['Grades 9-12 Students [Public School] 2009-10'] = joinedschool['Grades 9-12 Students [Public School] 2009-10'].replace('', np.nan)
joinedschool['Total Students [Public School] 2009-10'] = joinedschool['Total...


I am working on the file "7_VisualizationTableauData.ipynb".
The first task is:
This is the full list of all column names in the schools dataframe.
Can you write Python code for it?

for col in joinedschool.columns:
    print col


I am working on the file "7ex/math.ipynb".
The first task is:
Examlpe:
   x + 4 y ==  2
-2 x +   y == 14

from sympy import Matrix, solve_linear_system
from sympy.abc import x, y
system = Matrix(( (1, 4, 2), (-2, 1, 14)))
solve_linear_system(system, x, y)
Can you write Python code for it?


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Convert CSV's dataframes to Apache Parquet files
Can you write Python code for it?

# Save all the five files to parquet format

socialDF
retweetDF
replyDF
mentionDF
activityDF


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Importing and creating SparkSession
Can you write Python code for it?

from pyspark import SparkFiles
from pyspark.sql import SparkSession
from pyspark.sql.window import Window
from pyspark.sql.types import *
from pyspark.sql.functions import *

# Create a SparkSession.
spark = SparkSession.builder\
                .master("local[*]")\
                .appName("ETL")\
                .config("spark.executor.logs.rolling.time.interval", "daily")\
                .getOrCreate()


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Load the parquet files into new dataframes
Can you write Python code for it?

# Read all the five files from parquet format

socialDFpq = spark.read
retweetDFpq = spark.read
replyDFpq = spark.read
mentionDFpq = spark.read
activityDFpq = spark.read


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Spark SQL using DataFrames API
Can you write Python code for it?

# Users who have most followers

socialDFpq

# Users who have most mentions
mentionDFpq

# Of the top 5 followed users, how many mentions has each one?

# top_f contains "top 5 users who have most followers"
top_f =

top_f.


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Spark SQL using SQL language
Create temporary views of all tables so we can use SQL statements
Can you write Python code for it?

# Create temporary views so we can use SQL statements
socialDFpq.
retweetDFpq.
replyDFpq.
mentionDFpq.
activityDFpq.

# List all the tables in spark memory

spark.

# Users who have most followers using SQL
spark.

# Users who have most mentions using SQL
spark.

# Of the top 5 followed users, how many mentions has each one? (using SQL)


I am working on the file "8-ETL-pyspark-Handson.ipynb".
The first task is:
Working with dataframes
Can you write Python code for it?

# Display the schema of the dataframes

socialDFpq
socialDFpq

# Show the top 5 rows of each dataframe

socialDFpq
retweetDFpq
replyDFpq
mentionDFpq
activityDFpq


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Cuando llamamos a interact, hacemos pasar fixed(20) para mantener q fijado al valor de 20.
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
De  nuevo, el  tamaño del intervalo se puede determinar haciendo uso de un tercer elemento en la tupla:
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Este ejemplo aclara como interactuar con con procesos mediante sus argumentos clave:

Si el argumento clave es Widget con un atributo tipo value, se utiliza el widget tipo slider. Cualquier widget con un atributo que resulte en un valor numérico se puede utilizar, incluso los personalizados.
De lo contrario, el valor se trata como una widget abbreviation que se convierte en un widget antes de su uso.

La siguiente tabla ofrece una visión general de los diferentes widget abbreviation:

Keyword argument	Widget

`True` or `False`	CheckboxWidget

`'Hola'`	TextareaWidget

`value` or `(min,max)` or `(min,max,step)` if integers are passed	IntSliderWidget

`value` or `(min,max)` or `(min,max,step)` if floats are passed	FloatSliderWidget

`('naranja','manzana')` or `{'uno':1,'dos':2}`	Dropdown

Hemos visto arriba cómo funcionan los widgets checkbox y de área de texto. Veamos, más detalles sobre las diferentes abreviaturas para deslizadores y menús desplegables.
Si se pasa un tupla con dos enteros (min, max) un deslizador con valores también enteros se crea utilizando los mismos como máximo y mínimo. En este caso, se utiliza el tamaño de paso predeterminado de 1.
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Fijando argumentos usando fixed
Hay momentos en los que es posible que vayamos a explorar una función utilizando interact, pero queramos fijar una o más de sus argumentos a valores específicos. Esto puede lograrse haciendo uso la función fixed.
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Haciendo un gráfico interactivo
En el siguiente ejemplo, una función que representa gráficamente la suma de dos ondas sinusoidales la combinamos con interact.
Can you write Python code for it?

def dibuja_ondas(frequencia1, frequencia2):
    x = np.linspace(0, 3*np.pi, 500)
    y = np.sin(x*frequencia1) + np.sin(x*frequencia2)
    plt.plot(x,y)

    plt.title(u'¡Mira mamá, gráficos interactivos!')

interact( dibuja_ondas, frequencia1=20., frequencia2=21.)


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Referencias

Ejemplos de IPython
Can you write Python code for it?

# Esta celda da el estilo al notebook
from IPython.core.display import HTML
css_file = './css/aeropython.css'
HTML(open(css_file, "r").read())


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Si hacemos pasar una cadena de texto (string), interact generará un campo de texto:
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Si se pasa un tupla con tres enteros (min,max,step), lo que hacemos es determinar también el tamaño de paso:
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
También podemos crear menús desplegables haciendo pasar una tupla de cadenas de texto (strings). En este caso, las cadenas de texto son utilizados como los nombres de la interfaz de usuario del menú desplegable y pasan a la función Python subyacente.
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Uso básico de interact
At the most basic level, interact autogenerates UI controls for function arguments, and then calls the function with those arguments when you manipulate the controls interactively. To use interact, you need to define a function that you want to explore. Here is a function that prints its only argument x.
A nivel más básico, interact genera controles automáticamente mediante una interfaz gráfica de usuario para los argumentos de una función que debe ser definida previamente. Así, interact llama a dicha función con los argumentos que se definieron para manipularlos con controles de forma interactiva.
Por tanto, para utilizar interact, es necesario definir la función en Python que desea explorar. Vamos a crear es una función que imprime su único argumento x:
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
When you pass this function as the first argument to interact along with an integer keyword argument (x=10), a slider is generated and bound to the function.
Ahora utilizaremos esta función como primer argumento para interact junto con un argumento predifinido a un entero (x = 10), se autogenera un control deslizante (slider) y con que nos permite interactuar con esta función.
Can you write Python code for it?


I am working on the file "8-widgets interactivos.ipynb".
The first task is:
Widgets  interactivos y Matplotlib
Como ya sabes, IPython de maravilla con Matplotlib. Veamos cómo podemos combinar los widgets interactivos con esta bibliote gráfica.
Repaso: creando un gráfico
A modo de repaso, veamos cómo creabamos un gráfico simple. Primero debemos de cargar las bibliotecas:
Can you write Python code for it?

from __future__ import print_function
from IPython.html.widgets import interact, interactive, fixed

import matplotlib.pyplot as plt
import numpy as np

%matplotlib inline

# Vamos a cambiar el estilo por defecto (opcional)
plt.style.use('ggplot')

# Creamos un array con valores de x
x = np.linspace(0, 3*np.pi, 500)
y = np.sin(x**2)

plt.plot(x, y)
plt.title(u'Un gráfico simple');


I am working on the file "8. Classes/Python_Classes.ipynb".
The first task is:
Ahora que ya tenemos la función, podemos agregarla a nuestra clase para que se convierta en un método. Esto es bastante sencillo ya que solo tenemos que poner nuestro método dentro de la indentación de nuestra clase y para llamar al método ocuparemos la misma notación de punto. Ejemplo
casa.precio
Ejercicio 4
copia el método "precio que escribiste anteriormente en la clase MiCasa y ve en cuánto está valuada la casa que construíste
Can you write Python code for it?

class MiCasa(object):
        """Clase que va a crear un objeto casa con los atributos
        cuartos, puertas, ventanas y tamaño"""
        color = 'Blanco'
        def __init__(self, cuartos, puertas, ventanas, tamaño):
            self.cuartos = cuartos
            self.puertas = puertas
            self.ventanas = ventanas
            self.tamaño = tamaño
        def precio(self, impuesto):
            PrecioCuartos = 50000*self.cuartos
            if self.tamaño <= 150:
                PrecioTamaño = 800000
            else:
                PrecioTamaño = 1000000
            Precio = PrecioCuartos + PrecioTamaño
            Total = Precio + ((Precio*impuesto)/100)
            return Total, Precio

otra_casa_blanca = MiCasa(5,10,20,100)

otra_casa_blanca.color

casa_blanca.color


I am working on the file "8. Classes/Python_Classes.ipynb".
The first task is:
Ejercicio 2
Después de que creaste una (o varias) instancias de esta clase, prueba acceder a sus atributos con la notación de punto. Ejemplo
casa.ventanas
Can you write Python code for it?


I am working on the file "8. Classes/Python_Classes.ipynb".
The first task is:
Felicidades!!!
Hemos finalizado con el concepto básico y la sintaxis de las clases, ahora vamos a profundizar un poco en las particularidades.
Pero antes de continuar, alguien tiene preguntas?
Yo si tengo algunas, la primera es qué pasa si queremos usar un método que está fuera de la clase, podemos llamarlo? o el caso contrario, qué pasa si queremos usar un método que hay en una clase para que funcione en otro ejemplo independiente de la instancia?
Para responder esto hay que experimentar!!!
Ejercicio 5
Haz una función que se llame expandir que dependa de del tamaño de la casa. Esta función debería de agrandar el tamaño en un 25%. Ahora prueba usar esta función con una instancia de MiCasa
Can you write Python code for it?

def expandir(tamaño):
    return tamaño + ((tamaño*25)/100)

expandir(100)

expandir()


I am working on the file "8. Extra/8.1 Custom Layer.ipynb".
The first task is:
AntiRectifier Layer
Can you write Python code for it?

class Antirectifier(Layer):
    '''This is the combination of a sample-wise
    L2 normalization with the concatenation of the
    positive part of the input with the negative part
    of the input. The result is a tensor of samples that are
    twice as large as the input samples.

    It can be used in place of a ReLU.

    # Input shape
        2D tensor of shape (samples, n)

    # Output shape
        2D tensor of shape (samples, 2*n)

    # Theoretical justification
        When applying ReLU, assuming that the distribution
        of the previous output is approximately centered around 0.,
        you are discarding half of your input. This is inefficient.

        Antirectifier allows to return all-positive outputs like ReLU,
        without discarding any data.

        Tests on MNIST show that Antirectifier allows to train networks
        with twice less parameters yet with comparable
        classification accuracy as an equivalent ReLU-based network.
    '''

    def compute_output_shape(self, input_shape):
        shape = list(input_shape)
        assert len(shape) == 2  # only valid for 2D tensors
        shape[-1] *= 2
        return tuple(shape)

    def call(self, inputs):
        inputs -= K.mean(inputs, axis=1, keepdims=True)
        inputs = K.l2_normalize(inputs, axis=1)
        pos = K.relu(inputs)
        neg = K.relu(-inputs)
        return K.concatenate([pos, neg], axis=1)


I am working on the file "8. Extra/8.1 Custom Layer.ipynb".
The first task is:
Data Preparation
Can you write Python code for it?

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()

X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)


I am working on the file "8. Extra/8.1 Custom Layer.ipynb".
The first task is:
Model with Custom Layer
Can you write Python code for it?

# build the model
model = Sequential()
model.add(Dense(256, input_shape=(784,)))
model.add(Antirectifier())
model.add(Dropout(0.1))
model.add(Dense(256))
model.add(Antirectifier())
model.add(Dropout(0.1))
model.add(Dense(10))
model.add(Activation('softmax'))

# compile the model
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

# train the model
model.fit(X_train, Y_train,
          batch_size=batch_size, epochs=nb_epoch,
          verbose=1, validation_data=(X_test, Y_test))


I am working on the file "8. Extra/8.1 Custom Layer.ipynb".
The first task is:
Parametrs and Settings
Can you write Python code for it?

# global parameters
batch_size = 128
nb_classes = 10
nb_epoch = 10


I am working on the file "8. Extra/8.2 Multi-Modal Networks.ipynb".
The first task is:
At this point, we feed into the model our auxiliary input data by concatenating it with the LSTM output:
Can you write Python code for it?

from keras.layers import concatenate

auxiliary_input = Input(shape=(5,), name='aux_input')
x = concatenate([lstm_out, auxiliary_input])

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)


I am working on the file "8. Extra/8.2 Multi-Modal Networks.ipynb".
The first task is:
Here we insert the auxiliary loss, allowing the LSTM and Embedding layer to be trained smoothly even though the main loss will be much higher in the model.
Can you write Python code for it?

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)


I am working on the file "8. Extra/8.2 Multi-Modal Networks.ipynb".
The first task is:
Keras supports different Merge strategies:

add: element-wise sum
concatenate: tensor concatenation. You can specify the concatenation axis via the argument concat_axis.
multiply: element-wise multiplication
average: tensor average
maximum: element-wise maximum of the inputs.
dot: dot product. You can specify which axes to reduce along via the argument dot_axes. You can also specify applying any normalisation. In that case, the output of the dot product is the cosine proximity between the two samples.

You can also pass a function as the mode argument, allowing for arbitrary transformations:
python
merged = Merge([left_branch, right_branch], mode=lambda x: x[0] - x[1])

Even more interesting
Here's a good use case for the functional API: models with multiple inputs and outputs.
The functional API makes it easy to manipulate a large number of intertwined datastreams.
Let's consider the following model (from: https://keras.io/getting-started/functional-api-guide/ )
Problem and Data
We seek to predict how many retweets and likes a news headline will receive on Twitter.
The main input to the model will be the headline itself, as a sequence of words, but to spice things up, our model will also have an auxiliary input, receiving extra data such as the time of day when the headline was posted, etc.
The model will also be supervised via two loss functions.
Using the main loss function earlier in a model is a good regularization mechanism for deep models.
<img src="https://s3.amazonaws.com/keras.io/img/multi-input-multi-output-graph.png" width="40%" />
Can you write Python code for it?

from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

# Headline input: meant to receive sequences of 100 integers, between 1 and 10000.
# Note that we can name any layer by passing it a "name" argument.
main_input = Input(shape=(100,), dtype='int32', name='main_input')

# This embedding layer will encode the input sequence
# into a sequence of dense 512-dimensional vectors.
x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input)

# A LSTM will transform the vector sequence into a single vector,
# containing information about the entire sequence
lstm_out = LSTM(32)(x)


I am working on the file "8. Extra/8.2 Multi-Modal Networks.ipynb".
The first task is:
We compile the model and assign a weight of 0.2 to the auxiliary loss.
To specify different loss_weights or loss for each different output, you can use a list or a dictionary. Here we pass a single loss as the loss argument, so the same loss will be used on all outputs.
Note:
Since our inputs and outputs are named (we passed them a "name" argument),
We can compile&fit the model via:
Can you write Python code for it?

model.compile(optimizer='rmsprop',
              loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
              loss_weights={'main_output': 1., 'aux_output': 0.2})


I am working on the file "8.SVM_Kernel.ipynb".
The first task is:
Kernel Transformations
The kernel function allows us to do this without needing to do these transformation ourselves can be any of the following:
- linear: $\langle x, x'\rangle.$
- polynomial: $(\gamma \langle x, x'\rangle + r)^d$. d is specified by keyword degree, r by coef0.
- rbf: $\exp(-\gamma |x-x'|^2)$. $\gamma$ is specified by keyword gamma, must be greater than 0.
- sigmoid $(\tanh(\gamma \langle x,x'\rangle + r))$, where r is specified by coef0.
Can you write Python code for it?

from sklearn import svm

def plot_kernels(p, k):
    theta = np.random.uniform(0, 2*np.pi*2, p)
    raidus = np.array(np.random.randn(p)*2 + 25)
    circle = [[np.cos(t), np.sin(t)] for t in theta]
    x0 = raidus.reshape(p,1) * circle
    x1 = np.random.randn(p, 2)*4
    X = np.r_[x0,x1]
    y = [0] * p + [1] * p

    print(k)
    lin_svc = svm.SVC(kernel='linear', C=1)
    rbf_svc = svm.SVC(kernel='rbf', gamma=0.7, C=1)
    poly_svc = svm.SVC(kernel='poly', degree=2, C=1)

    if k == "linear":
        clf = lin_svc
    elif k == "rbf":
        clf = rbf_svc
    else:
        clf = poly_svc

    clf.fit(X,y)
    clf.predict(X)

    # plot the boundaries
    x_min, x_max = -30, 30
    y_min, y_max = -30, 30
    step = 0.1

    xx, yy = np.meshgrid(np.arange(x_min, x_max, step), np.arange(y_min, y_max, step))
    xxyy = np.c_[xx.ravel(), yy.ravel()]
    Z = clf.predict(xxyy)
    Z = Z.reshape(xx.shape)
    cs = plt.contourf(xx, yy, Z, cmap=plt.cm.viridis, alpha = 0.5)

    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, s=40)
    plt.xlim(-30,30)
    plt.ylim(-30,30)
    plt.show()

plot_kernels(30,"linear")

plot_kernels(30,"poly")

plot_kernels(30,"rbf")


I am working on the file "8.SVM_Kernel.ipynb".
The first task is:
Non Linear Separable Points
Lets see an example of this in a two-dimensional space
Can you write Python code for it?

np.random.seed(1234)

def plot_points(p):
    theta = np.random.uniform(0, 2*np.pi*2, p)
    raidus = np.array(np.random.randn(p)*2 + 25)
    circle = [[np.cos(t), np.sin(t)] for t in theta]
    x0 = raidus.reshape(p,1) * circle
    x1 = np.random.randn(p, 2)*4
    X = np.r_[x0,x1]
    y = [0] * p + [1] * p
    plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, s=40)
    #plt.xlim(-20,20)
    #plt.ylim(-20,20)
    plt.show()

plot_points(20)


I am working on the file "8.SVM_Kernel.ipynb".
The first task is:
Non-Linear Transformations
If the points are not linearly separable, then we can use some transformation to make them linear seperable. This is called Non-Linear Transformation.
Here we can use the square transformation to see the output
$$ z = x_1^2 + x_2^2 $$
Can you write Python code for it?

def plot_points_transforms(p):
    theta = np.random.uniform(0, 2*np.pi*2, p)
    raidus = np.array(np.random.randn(p)*2 + 25)
    circle = [[np.cos(t), np.sin(t)] for t in theta]
    x0 = raidus.reshape(p,1) * circle
    x1 = np.random.randn(p, 2)*4
    X = np.r_[x0,x1]
    y = [0] * p + [1] * p
    z = np.square(X).sum(axis =1)

    #plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.RdBu, s=40)
    plt.scatter(X[:, 0], z, c=y, cmap=plt.cm.RdBu, s=40)
    #plt.xlim(-20,20)
    #plt.ylim(-20,20)
    plt.show()

plot_points_transforms(30)


I am working on the file "8_Calibration/8_1_Calibration_Least_Squares_Problem.ipynb".
The first task is:
8.1 Calibration as a Least Squares Problem
In this section we discuss the procedure that is generally used in practice to perform calibration. We will use the unpolarized RIME in this section instead of the full-polarized RIME (see $\S$ 7 ➞). It provides us with a much simpler framework with which we can grasp the basics of calibration. Moreover, we assume for the sake of simplicity that the observed data are only corrupted by the instrument's antenna gains. This assumption results in a idealised treatment as there are many other factors that do in fact corrupt radio interferometric data (see $\S$ 7 ➞).
The unpolarized RIME is given by the following:

   Unpolarized RIME

\begin{equation}
d_{pq}(t) = g_p(t) g_q^*(t) \tilde{d}_{pq}(t) + \epsilon_{pq}(t),
\end{equation}

where $d_{pq}(t)$ and $\tilde{d}{pq}(t)$ denote the corrupted observed and uncorrupted visibility at time $t$ associated with baseline $pq$. Moreover, the factors $g_p$ and $g_q$
denote the complex gain of antenna $p$ and $q$. The term $\epsilon{pq}$ is a zero mean (Gaussian)
noise term, representing thermal noise.
We begin this section by generating the $uv$-tracks of a fictitious instrument in $\S$ 8.1.1 ⤵. In $\S$ 8.1.2 ⤵ we phrase the calibration problem (for the antenna gains) as a least squares minimization problem. Then in $\S$ 8.1.3 ⤵ we simulate "realistic" visibility data for the $uv$-tracks by including gain errors and adding noise to the resulting visibilities (similar to adding noise to a simple sinusoid as seen in $\S$ 2.11 ➞). We then vectorize the problem in $\S$ 8.1.4 ⤵, enabling us to use the built in scipy Levenberg-Marquardt algorithm to calibrate the data produced in $\S$ 8.1.3 ⤵. We implement the aforementioned steps via a wrapper...
Can you write Python code for it?

lam = 3e8/1.4e9 #observational wavelenth
print "lam = ",lam
b = np.array([100,200,300])/lam
print "b [wavelengths] = ",b
plt.plot(np.array([0,100,200]),np.array([0,0,0]),'ro')
plt.xlim([-250,250])
plt.ylim([-100,100])
plt.xlabel("East-West [m]", fontsize=18)
plt.ylabel("North-South [m]", fontsize=18)
plt.title("ENU-coordinates of three element interferometer.", fontsize=18)
plt.show()


I am working on the file "8_Calibration/8_1_Calibration_Least_Squares_Problem.ipynb".
The first task is:
8.1.2. Unpolarized Calibration
As explained in $\S$ 7.2 ➞ the RIME assumes that our observed signal is polarized. For the sake
of simplicity, however, we will now introduce the calibration problem with the underlying assumption that the observed signal is unpolarized. Unpolarized calibration is achieved by solving the following minimization problem:

   Unpolarized Calibration

\begin{equation}
\min_{\boldsymbol{\mathcal{G}}} \left \| \boldsymbol{\mathcal{D}} - \boldsymbol{\mathcal{G}}\boldsymbol{\mathcal{M}}\boldsymbol{\mathcal{G}}^H \right \|,
\end{equation}

where
* $\boldsymbol{\mathcal{D}}$ is the observed visibility matrix. Each entry, which we denote by $d_{pq}$, represents the visibility measured by the baseline formed by antennas $p$ and $q$.
* $\boldsymbol{\mathcal{M}}$ is the model visibility matrix. The entry $m_{pq}$ of $\boldsymbol{\mathcal{M}}$ denotes a true or model visibility which was created with the calibration sky model and a $uv$-point on the $uv$-track associated with baseline $pq$.
* $\boldsymbol{\mathcal{G}} = \textrm{diag}(\mathbf{g})$ is the antenna gain matrix, where $\mathbf{g}=[g_1,g_2,\cdots,g_N]^T$ denotes the antenna gain vector. The operator $\textrm{diag}(\cdot)$ forms a diagonal matrix from a vector by putting the elements of the vector on the main diagonal. The vector $\mathbf{g}$ represents the instrumental response of the antennas, i.e. the complex antenna gains. These antenna gains are chosen in such a way that they minimize the difference between the observed and model visibilities.
* $\boldsymbol{\mathcal{G}}\boldsymbol{\mathcal{M}}\boldsymbol{\mathcal{G}}^H$ is the predicted visibility matrix. This matrix contains the model visibilities after the antenna gains have been applied to them.
The superscript $(\cdot)^H$ denotes the Hermitian or conjugate transpose and $\left \| \cdot \right \|$ denotes the norm used. Most calibration algorithms...
Can you write Python code for it?

'''Creates the observed visibilities
point_sources - skymodel of point sources - (amplitude, l, m)
u_m - the u coordinates of observation (packed in a 2D structure)
v_m - the v coordinates of observation (packed in a 2D structure)
g - the antenna gain error vector
sig - the noise
'''
def create_vis_mat(point_sources,u_m,v_m,g,sig):
    D = np.zeros(u.shape)
    G = np.diag(g)
    #Step 1: Create Model Visibility Matrix
    for k in xrange(len(point_sources)): #for each point source
        l_0 = point_sources[k,1]
        m_0 = point_sources[k,2]
        D = D + point_sources[k,0]*np.exp(-2*np.pi*1j*(u_m*l_0+v_m*m_0))

    for t in xrange(D.shape[2]): #for each time-step

        #Step 2: Corrupting the Visibilities
        D[:,:,t] = np.dot(G,D[:,:,t])
        D[:,:,t] = np.dot(D[:,:,t],G.conj())

        #Step 3: Adding Noise
        D[:,:,t] = D[:,:,t] + sig*np.random.randn(u_m.shape[0],u_m.shape[1]) \
            + sig*np.random.randn(u_m.shape[0],u_m.shape[1])*1j

    return D


I am working on the file "8_Calibration/8_1_Calibration_Least_Squares_Problem.ipynb".
The first task is:
Import section specific modules:
Can you write Python code for it?

from scipy import optimize
%pylab inline
pylab.rcParams['figure.figsize'] = (15, 10)

HTML('../style/code_toggle.html')


I am working on the file "8_Calibration/8_1_Calibration_Least_Squares_Problem.ipynb".
The first task is:
Our hour angle range is from -6h to 6h, and our declination is set to $60^{\circ}$.
As the earth rotates the antennas trace out $uv$-tracks (ellipses) as shown in the code fragment below, where the red tracks are due to baseline $pq$ and blue tracks are due to baseline $qp$. We can construct these $uv$-tracks by using Eq. 8.1 ⤵<!--\ref{cal:eq:uv_cov}-->.
Can you write Python code for it?

u = np.zeros((len(b),len(H)))
v = np.zeros((len(b),len(H)))
for k in xrange(len(b)):
    u[k,:] = b[k]*np.cos(H)
    v[k,:] = b[k]*np.sin(H)*np.sin(delta)
    plt.plot(u[k,:],v[k,:],"r")
    plt.plot(-u[k,:],-v[k,:],"b")
plt.xlabel("$u$ [rad$^{-1}$]", fontsize=18)
plt.ylabel("$v$ [rad$^{-1}$]", fontsize=18)
plt.title("$uv$-Coverage of three element interferometer", fontsize=18)
plt.show()


I am working on the file "8_Calibration/8_1_Calibration_Least_Squares_Problem.ipynb".
The first task is:
The above function works by vectorizing the real and imaginary part of $\boldsymbol{\mathcal{D}}$ and
storing the result in $\mathbf{d}$. The vector $\mathbf{m}$ is generated in a similar manner.
The error vector $\mathbf{r}$ is calculated by err_func. We initialize $\breve{\mathbf{g}}$ with
$\breve{\mathbf{g}}_0=[\mathbf{1},\mathbf{0}]$. We can then call
optimize.leastsq(self.err_func, g_0, args=(d, m)).
We can now calculate $\mathbf{g} = \breve{\mathbf{g}}_U+\imath\breve{\mathbf{g}}_L$ and
$\boldsymbol{\mathscr{G}}=\mathbf{g}\mathbf{g}^H$. This is repeated for each observational time-slot.
8.1.5 Corrected Visibilites
Before imaging, we have to correct our observed visibilities by removing the effect that the antenna gains had on the observed visibilities. This can be accomplished by using

   Correcting Visibilities

\begin{equation}
 \boldsymbol{\mathcal{D}}^\mathrm{(c)} = \boldsymbol{\mathcal{G}}^{-1}\boldsymbol{\mathcal{D}}\boldsymbol{\mathcal{G}}^{-H} = \boldsymbol{\boldsymbol{\mathscr{G}}}^{\odot-1}\odot\boldsymbol{\mathcal{D}},
\end{equation}

where

$\boldsymbol{\mathcal{D}}^\mathrm{(c)}$ is the corrected visibility matrix.
$\boldsymbol{\mathscr{G}}^{\odot-1}$ denotes the visibility calibration matrix, which is computed by taking the Hadamard (element-wise) inverse of $\boldsymbol{\mathscr{G}}$.

The superscript $(\cdot)^{-1}$ denotes matrix inversion, while $(\cdot)^{-H}$ denotes the inverse of the Hermitian transpose. The operator $\odot$ denotes the Hadamard product.
We calculate the corrected visibilities below.

Advice: The matrix and vector operations (like $\odot$) and operators used in this section are discussed in more detail in [$\S$ 2.10 ➞](../2_Mathematical_Groundwork/2_10_linear_algebra.ipynb)

Can you write Python code for it?