Answerkey
Answerkey
Answerkey
PART - A
1. Binary [CO2] [K1]
Multiclass
2. Instead of depending upon one single model, we can use a group of models (ensemble) to make a [CO2] [K2]
prediction or classification decision. This form of learning is called ensemble learning.
Ex: Voting Classifier, Boosting, Bagging
3. Lasso regularization: In this case, N is L1 norm. It uses the modulus of weight as the penalty term [CO2] [K2]
N:
6. If yj is the output of our single neuron for the input vector X and is the output we desire for output [CO3] [K1]
neuron j, then the MSE error is mathematically expressed as
11. i) Logistic regression is an old classification technique. It provides the probability of an event (10) [CO2] [K2]
taking place, given an input value. The events are represented as categorical dependent
variables, and the probability of a particular dependent variable being 1 is given using the
logit function:
Example:
# Import the modules
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.model_selection import train_test_split
%matplotlib inline
filename = 'winequality-red.csv'
df = pd.read_csv(filename, sep=';')
2
X, Y = df[columns[0:-1]], df[columns[-1]]
scaler = MinMaxScaler()
X_new = scaler.fit_transform(X)
Y.loc[(Y<3)]=3
Y.loc[(Y<6.5) & (Y>=3 )] = 2
Y.loc[(Y>=6.5)] = 1
Y_new = pd.get_dummies(Y) # One hot encode
X_train, X_test, Y_train, y_test = \
train_test_split(X_new, Y_new, test_size=0.4, random_state=333)
class LogisticRegressor:
def __init__(self, d, n, lr=0.001 ):
# Place holders for input-output training data
self.X = tf.placeholder(tf.float32,\
shape=[None,d], name='input')
self.Y = tf.placeholder(tf.float32,\
name='output')
# Variables for weight and bias
self.b = tf.Variable(tf.zeros(n), dtype=tf.float32)
self.W = tf.Variable(tf.random_normal([d,n]),\
dtype=tf.float32)
# The Logistic Regression Model
h = tf.matmul(self.X, self.W) + self.b
self.Ypred = tf.nn.sigmoid(h)
# Loss function
self.loss = cost = tf.reduce_mean(-
tf.reduce_sum(self.Y*tf.log(self.Ypred),\
reduction_indices=1), name = 'cross-entropy-loss')
# Gradient Descent with learning
# rate of 0.05 to minimize loss
optimizer = tf.train.GradientDescentOptimizer(lr)
self.optimize = optimizer.minimize(self.loss)
# Initializing Variables
init_op = tf.global_variables_initializer()
self.sess = tf.Session()
self.sess.run(init_op)
def fit(self, X, Y,epochs=500):
total = []
for i in range(epochs):
_, l = self.sess.run([self.optimize,self.loss],\
feed_dict={self.X: X, self.Y: Y})
total.append(l)
if i%1000==0:
print('Epoch {0}/{1}: Loss {2}'.format(i,epochs,l))
return total
def predict(self, X):
return self.sess.run(self.Ypred, feed_dict={self.X:X})
def get_weights(self):
return self.sess.run([self.W, self.b])
12. i) class MLP: (10) [CO3] [K3]
def __init__(self,n_input=2,n_hidden=4, n_output=1,
act_func=[tf.nn.elu, tf.sigmoid], learning_rate= 0.001):
self.n_input = n_input # Number of inputs to the neuron
self.act_fn = act_func
seed = 123
self.X = tf.placeholder(tf.float32, name='X', shape=[None,n_input])
self.y = tf.placeholder(tf.float32, name='Y')
# Build the graph for a single neuron
# Hidden layer
self.W1 = tf.Variable(tf.random_normal([n_input,n_hidden],\
stddev=2, seed = seed), name = "weights")
3
self.b1 = tf.Variable(tf.random_normal([1, n_hidden], seed =
seed),\
name="bias")
tf.summary.histogram("Weights_Layer_1",self.W1)
tf.summary.histogram("Bias_Layer_1", self.b1)
# Output Layer
self.W2 = tf.Variable(tf.random_normal([n_hidden,n_output],\
stddev=2, seed = 0), name = "weights")
self.b2 = tf.Variable(tf.random_normal([1, n_output], seed =
seed),\
name="bias")
tf.summary.histogram("Weights_Layer_2",self.W2)
tf.summary.histogram("Bias_Layer_2", self.b2)
activity = tf.matmul(self.X, self.W1) + self.b1
h1 = self.act_fn[0](activity)
activity = tf.matmul(h1, self.W2) + self.b2
self.y_hat = self.act_fn[1](activity)
error = self.y - self.y_hat
self.loss = tf.reduce_mean(tf.square(error))\
+ 0.6*tf.nn.l2_loss(self.W1)
self.opt = tf.train.GradientDescentOptimizer(learning_rate\
=learning_rate).minimize(self.loss)
tf.summary.scalar("loss",self.loss)
init = tf.global_variables_initializer()
self.sess = tf.Session()
self.sess.run(init)
self.merge = tf.summary.merge_all()
self.writer = tf.summary.FileWriter("logs/",\
graph=tf.get_default_graph())
def train(self, X, Y, X_val, Y_val, epochs=100):
epoch = 0
X, Y = shuffle(X,Y)
loss = []
loss_val = []
while epoch &lt; epochs:
# Run the optimizer for the training set
merge, _, l = self.sess.run([self.merge,self.opt,self.loss],\
feed_dict={self.X: X, self.y: Y})
l_val = self.sess.run(self.loss, feed_dict=\
{self.X: X_val, self.y: Y_val})
loss.append(l)
loss_val.append(l_val)
self.writer.add_summary(merge, epoch)
if epoch % 10 == 0:
print("Epoch {}/{} training loss: {} Validation loss {}".\
format(epoch,epochs,l, l_val ))
epoch += 1
return loss, loss_val
def predict(self, X):
return self.sess.run(self.y_hat, feed_dict={self.X: X})
self.loss = tf.reduce_mean(tf.square(error)) + 0.6*tf.nn.l2_loss(self.W1)
_, d = X_train.shape
_, n = Y_train.shape
model = MLP(n_input=d, n_hidden=15, n_output=n)
loss, loss_val = model.train(X_train, Y_train, X_val, y_val, 6000)
4
The Forget Gate f(.) controls the amount of short-term memory, h, to be remembered for
further flow in the present time step.
The Input Gate i(.) controls the amount of input and working memory influencing the output
of the cell.
The Output Gate o(.) controls the amount of information that's used for updating the
shortterm memory.
GRU:
5
The loss function at the output neuron
The weight connecting hidden neuron k to the output neuron j would be given as follows:
The weight update connecting input neuron i to the hidden neuron k of hidden layer n can be
written as the following: