Kaggle competition: PadhAI: Text - Non Text Classification Level 4a
model_sol_6.optimizer.lerning_rate=0.01
gen = ImageDataGenerator()
batches = gen.flow(train_x_sol5, tf_utils.to_categorical(train_y_sol5, 10), batch_size=64)
 hist_sol_6 = model_sol_6.fit_generator(generator=batches, steps_per_epoch=train_x_sol5.shape[0] // 64,
epochs=50, verbose=2)
<save_to_csv>
class SigmoidNeuron:
def __init__(self):
self.w = None
self.b = None
def perceptron(self, x):
return np.dot(x, self.w.T)+ self.b
def sigmoid(self, x):
return 1.0/(1.0 + np.exp(-x))
def grad_w_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)* x
def grad_b_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)
 def grad_w_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred * x
elif y == 1:
return -1 *(1 - y_pred)* x
else:
raise ValueError("y should be 0 or 1")
 def grad_b_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred
elif y == 1:
return -1 *(1 - y_pred)
 else:
raise ValueError("y should be 0 or 1")
 def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False):
if initialise:
self.w = np.random.randn(1, X.shape[1])
 self.b = 0
if display_loss:
loss = {}
for i in tqdm_notebook(range(epochs), total=epochs, unit="epoch"):
dw = 0
db = 0
for x, y in zip(X, Y):
if loss_fn == "mse":
dw += self.grad_w_mse(x, y)
 db += self.grad_b_mse(x, y)
 elif loss_fn == "ce":
dw += self.grad_w_ce(x, y)
 db += self.grad_b_ce(x, y)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y, Y_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 if display_loss:
plt.plot(loss.values())
 plt.xlabel('Epochs')
 if loss_fn == "mse":
plt.ylabel('Mean Squared Error')
 elif loss_fn == "ce":
plt.ylabel('Log Loss')
 plt.show()
def predict(self, X):
Y_pred = []
for x in X:
y_pred = self.sigmoid(self.perceptron(x))
Y_pred.append(y_pred)
 return np.array(Y_pred )
predictions = model_sol_6.predict(test_x_sol5)
 predictions = [ np.argmax(x)for x in predictions ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission6.csv', index=False )<drop_column>
languages = ['ta', 'hi', 'en']
images_train = read_all(".. /input/level_4a_train/"+LEVEL+"/"+"background", key_prefix='bgr_')
 for language in languages:
images_train.update(read_all(".. /input/level_4a_train/"+LEVEL+"/"+language, key_prefix=language+"_"))
print(len(images_train))
images_test = read_all(".. /input/level_4a_test/kaggle_"+LEVEL, key_prefix='')
 print(len(images_test))
os.remove('submission1.csv')
 os.remove('submission2.csv')
 os.remove('submission3.csv')
 os.remove('submission4.csv')
 os.remove('submission5.csv')
 os.remove('submission6.csv' )<choose_model_class>
scaler = StandardScaler()
X_scaled_train = scaler.fit_transform(X_train)
 X_scaled_test = scaler.transform(X_test )
final_train_x = train_x[..., tf.newaxis]
final_ohe_train_y = tf_utils.to_categorical(train_y, 10)
 final_train_batches = image_augmentator.flow(final_train_x, final_ohe_train_y, batch_size=64)
 final_model = Sequential()
final_model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu', input_shape=extended_splitted_train_X.shape[1:]))
final_model.add(BatchNormalization())
 final_model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
final_model.add(MaxPooling2D(pool_size=2))
final_model.add(Dropout(0.1))
final_model.add(BatchNormalization())
 final_model.add(Conv2D(filters=32, kernel_size=3, padding='same', activation='relu'))
final_model.add(MaxPooling2D(pool_size=2))
final_model.add(Flatten())
 final_model.add(BatchNormalization())
 final_model.add(Dense(64))
final_model.add(Activation('relu'))
final_model.add(Dropout(0.2))
final_model.add(BatchNormalization())
 final_model.add(Dense(10, activation='softmax'))
final_model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
 final_model.fit_generator(generator=final_train_batches, steps_per_epoch=final_train_batches.n,
epochs=1, verbose=1 )<save_to_csv>
class SigmoidNeuronMy:
def __init__(self):
self.w = None
self.b = None
def perceptron(self, x):
return np.dot(x, self.w.T)+ self.b
def sigmoid(self, x):
return 1.0/(1.0 + np.exp(-x))
def grad_w_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)* x
def grad_b_mse(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
return(y_pred - y)* y_pred *(1 - y_pred)
 def grad_w_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred * x
elif y == 1:
return -1 *(1 - y_pred)* x
else:
raise ValueError("y should be 0 or 1")
 def grad_b_ce(self, x, y):
y_pred = self.sigmoid(self.perceptron(x))
if y == 0:
return y_pred
elif y == 1:
return -1 *(1 - y_pred)
 else:
raise ValueError("y should be 0 or 1")
 def fit(self, X, Y, epochs=1, learning_rate=1, initialise=True, loss_fn="mse", display_loss=False, setting = False):
if initialise:
self.w = np.random.randn(1, X.shape[1])
 self.b = 0
loss = {}
if display_loss:
loss = {}
for i in tqdm_notebook(range(epochs), total=epochs, unit="epoch"):
dw = 0
db = 0
for x, y in zip(X, Y):
if loss_fn == "mse":
dw += self.grad_w_mse(x, y)
 db += self.grad_b_mse(x, y)
 elif loss_fn == "ce":
dw += self.grad_w_ce(x, y)
 db += self.grad_b_ce(x, y)
 self.w -= learning_rate * dw
self.b -= learning_rate * db
if display_loss:
Y_pred = self.sigmoid(self.perceptron(X))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y, Y_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 if setting:
Y_pred = self.sigmoid(self.perceptron(X))
if loss_fn == "mse":
loss[i] = mean_squared_error(Y, Y_pred)
 elif loss_fn == "ce":
loss[i] = log_loss(Y, Y_pred)
 if setting:
return list(loss.values())
 if display_loss:
plt.plot(loss.values())
 plt.xlabel('Epochs')
 if loss_fn == "mse":
plt.ylabel('Mean Squared Error')
 elif loss_fn == "ce":
plt.ylabel('Log Loss')
 plt.show()
def optimumValue(self , X , Y, loss_fn="mse"):
lr = np.linspace(0, 0.05, 6)
 epochs = np.linspace(0, 100, 11)
 acc = {}
min_loss = 1000
lr_fin = 0
epoch_fin = 0
std = 0
fin = []
for j in tqdm_notebook(range(len(lr)) , total=len(lr), unit="lr"):
for i in tqdm_notebook(range(len(epochs)) , total=len(epochs), unit="epoch"):
for k in range(4):
X1_train, X1_test, y1_train, y1_test = train_test_split(X, Y, test_size=0.2, random_state=k**2)
 self.fit(X1_train, y1_train, epochs=epochs[i].astype("int")+1, learning_rate=lr[j], loss_fn="ce", display_loss=False)
 Y_pred_train = self.predict(X1_test)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
acc[k] = accuracy_score(Y_pred_binarised_train, y1_test)
 std=statistics.stdev(list(acc.values()))
accuracy = max(list(acc.values()))
fin.append([accuracy,std,epochs[i].astype("int")+1,lr[j]])
 print(fin)
 def predict(self, X):
Y_pred = []
for x in X:
y_pred = self.sigmoid(self.perceptron(x))
Y_pred.append(y_pred)
 return np.array(Y_pred)
predictions = final_model.predict(test_x[..., tf.newaxis])
 predictions = [ np.argmax(x)for x in predictions ]
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 submission.drop('Label', axis=1, inplace=True)
 submission['Label'] = predictions
submission.to_csv('submission.csv', index=False )<load_from_csv>
train_data=pd.read_csv('/kaggle/input/titanic/train.csv')
 train_data<load_from_csv>
sn_ce = SigmoidNeuronMy()
sn_ce.fit(X_scaled_train, Y_train, epochs=800, learning_rate=0.00005, loss_fn="ce", display_loss=True )
test_data=pd.read_csv('/kaggle/input/titanic/test.csv')
 test_data<drop_column>
def print_accuracy(sn):
Y_pred_train = sn.predict(X_scaled_train)
 Y_pred_binarised_train =(Y_pred_train >= 0.5 ).astype("int" ).ravel()
accuracy_train = accuracy_score(Y_pred_binarised_train, Y_train)
 print("Train Accuracy : ", accuracy_train)
 print("-"*50 )
train_results = train_data["Survived"].copy()
train_data.drop("Survived", axis=1, inplace=True, errors="ignore")
 titanic = pd.concat([train_data, test_data])
 traindex = train_data.index
testdex = test_data.index<filter>
print_accuracy(sn_ce )
<define_variables><EOS> 
Y_pred_test = sn_ce.predict(X_scaled_test)
 Y_pred_binarised_test =(Y_pred_test >= 0.5 ).astype("int" ).ravel()
submission = {}
submission['ImageId'] = ID_test
submission['Class'] = Y_pred_binarised_test
submission = pd.DataFrame(submission)
 submission = submission[['ImageId', 'Class']]
submission = submission.sort_values(['ImageId'])
 submission.to_csv("submisision.csv", index=False )