Please help me understand where I might be going wrong in the following code because while calculating accuracy for the training data, the above IndexError: Tuple index out of range is constantly occurring.
- y has been converted to dummies so that there is a column for each class.
- SGD algorithm defined
My code:
from sklearn.preprocessing import StandardScaler
y = df["label"]
X = df.drop("label", axis=1)
y_dummies = pd.get_dummies(y)
X_train, X_test,y_train,y_test=train_test_split(X,y,test_size=0.25,random_state=25)
X_scaled = StandardScaler().fit_transform(X_train)
import numpy as np
def softmax(scores):
scaled_scores = scores-scores.max(axis=1, keepdims=True)
exp_scores = np.exp(scaled_scores)
softmax_scores = exp_scores/(np.sum(exp_scores,axis=1,keepdims=True))
return softmax_scores
def get_log_loss(X, w, y):
scores = np.dot(X,w)
predictions = softmax(scores)
log_likelihood = np.sum(y*np.log(predictions))/len(y)
log_loss = -log_likelihood
return log_loss
def get_gradient(X, w, y):
scores = np.dot(X,w)
predictions = softmax(scores)
error = y - predictions
gradient = -(np.dot(X.T, error))/len(y)
return gradient
def SGD_softmax(X, y, lr, batch_size, max_epochs):
w = np.zeros([X.shape[1], y.shape[1]])
old_loss = 1000
losses = []
target = .01
count = 0
while (count<max_epochs):
shuffled_index = np.random.permutation(X.shape[0])
batch_starts = range(0, X.shape[0], batch_size)
for start_index in batch_starts:
batch = shuffled_index[start_index:start_index + batch_size]
x_batch = X[batch]
y_batch = y[batch]
gradient = get_gradient(x_batch, w, y_batch)
w = w-lr*gradient
current_loss = get_log_loss(X, w, y)
gain = (old_loss - current_loss)/np.abs(old_loss)
losses.append(current_loss)
old_loss = current_loss
if (gain<target):
lr = lr/2
count = count+1
return (w, losses)
const = np.ones((X_scaled.shape[0],1))
X_train_biased = np.concatenate([const,X_scaled],1)
max_epochs=15
batch_size = 32
lr=.01
w, losses = SGD_softmax(X_train_biased, y_train.values, lr, batch_size, max_epochs)
The error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-39-1be95351e661> in <module>()
10 lr=.01
11
---> 12 w, losses = SGD_softmax(X_train_biased, y_train.values, lr, batch_size, max_epochs)
13 predictions = np.argmax(np.dot(X_train_biased, w), axis=1)
14 actual = np.argmax(y_train.values, axis=1)
<ipython-input-36-f0dfc421945b> in SGD_softmax(X, y, lr, batch_size, max_epochs)
29 def SGD_softmax(X, y, lr, batch_size, max_epochs):
---> 30 w = np.zeros([X.shape[1], y.shape[1]])
31 old_loss = 1000
32 losses = []
IndexError: tuple index out of range