# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

import numpy as np
from sklearn.datasets import load_iris
from sklearn.utils import shuffle
import pandas as pd
import matplotlib.pyplot as plt
# %matplotlib inline

# +
data=load_iris()
x=data.data
y=data.target.reshape(1,x.shape[0])
shuff=list(np.random.permutation(x.shape[0]))
x=x[shuff,:].T
y=y[:,shuff]

#normalizing
x=x-(np.sum(x,axis=0)/x.shape[0])
sigma2=np.sum(x**2,axis=0)/x.shape[0]
x=x/sigma2
x.shape
y.shape
# -

temp=np.append(x,y).reshape(150,5)
df=pd.DataFrame(temp)
vis=df.corr()
vis.style.background_gradient(cmap="coolwarm")

x.shape

# +
df=pd.DataFrame(np.corrcoef(x,y))
print(df)

plt.scatter(x[0,:],y)
plt.show()
# -

x_train=x[:,:125].reshape(4,125)
y_train=y[:125].reshape(1,125)
x_test=x[:,125:].reshape(4,25)
y_test=y[125:].reshape(1,25)

# +
y_t1=np.zeros((x_train.shape[1],3))
y_t1[np.arange(x_train.shape[1]),y_train]=1

y_t2=np.zeros((x_test.shape[1],3))
y_t2[np.arange(x_test.shape[1]),y_test]=1

# -

"""def relu_derivative(z):
    temp=np.zeros(z.shape)
    temp[z>0]=1
    return temp"""

# +
learning_rate=0.0355
lambd=0.075
iter=5000
cost_cache=[]

w1=np.random.rand(50,4) * np.sqrt(2/x_train.shape[0])
b1=np.zeros((50,1))
w2=np.random.rand(70,50)*np.sqrt(2/w1.shape[1])
b2=np.zeros((70,1))
w3=np.random.rand(3,70) * np.sqrt(1/w2.shape[1])
b3=np.zeros((3,1))
m=x_train.shape[1]


for i in range(iter):
    #for prop
    z1=np.dot(w1,x_train)+b1
    a1=np.maximum(0,z1)
    z2=np.dot(w2,a1)+b2
    a2=np.maximum(0,z2)
    z3=np.dot(w3,a2)+b3
    a3=np.exp(z3)/np.sum(np.exp(z3),axis=0)

    logprobs = np.multiply(-np.log(a3),y_t1.T)
    cost = 1./m * np.nansum(logprobs)+(lambd/(2*m))*(np.sum(w1**2)+np.sum(w2**2)+np.sum(w3**2))
    cost_cache.append(cost)
    if i%100==0:
        print("cost after "+str(i)+" iteration is : "+str(cost))

    #back prop
    dz3=(a3-y_t1.T)
    dw3=(np.dot(dz3,a2.T)+(lambd)*w3)/m
    db3=np.sum(dz3,axis=1,keepdims=True)/m
    w3=w3-learning_rate*dw3
    b3=b3-learning_rate*db3

    dz2=np.dot(dw3.T,dz3)*np.int64(z2>0)
    dw2=(np.dot(dz2,a1.T)+(lambd)*w2)/m
    db2=np.sum(dz2,axis=1,keepdims=True)/m
    w2=w2-learning_rate*dw2
    b2=b2-learning_rate*db2

    dz1=np.dot(dw2.T,dz2)*np.int64(z1>0)
    dw1=(np.dot(dz1,x_train.T)+(lambd)*w1)/m
    db1=np.sum(dz1,axis=1,keepdims=True)/m

    w1=w1-learning_rate*dw1
    b1=b1-learning_rate*db1
    
parameters={"w1":w1,
            "b1":b1,
            "w2":w2,
            "b2":b2,
            "w3":w3,
            "b3":b3
}    

"""print("w1 :"+str(w1))
print("b1 :"+str(b1))
print("w2 :"+str(w2))
print("b2 :"+str(b2))
print("w3 :"+str(w3))
print("b3 :"+str(b3))"""


# +
def predict(x,parameters):
    w1=parameters["w1"]
    b1=parameters["b1"]
    w2=parameters["w2"]
    b2=parameters["b2"]
    w3=parameters["w3"]
    b3=parameters["b3"]
    
    z1=np.dot(w1,x)+b1
    a1=np.maximum(0,z1)
    z2=np.dot(w2,a1)+b2
    a2=np.maximum(0,z2)
    z3=np.dot(w3,a2)+b3
    a3=np.exp(z3)/np.sum(np.exp(z3),axis=0)

    l=np.argmax(a3,axis=0)
    return l;

print("Train Accuracy : "+str(np.mean(predict(x_train,parameters)==y_train)*100)+" %")
print("Test Accuracy : "+str(np.mean(predict(x_test,parameters)==y_test)*100)+" %")
# -

import matplotlib.pyplot as plt
i=np.arange(iter)
#c=cost_cache[2000:2100:2]

plt.plot(i,cost_cache)
plt.xlabel("#iteration")
plt.ylabel("cost")
plt.title("cost showing")

import scipy
from scipy import ndimage

num_px=4
fname="H:\image\images.jpg"
image = np.array(ndimage.imread(fname, flatten=True))
#my_image = scipy.misc.imresize(image, size=(num_px,num_px)).reshape((1, num_px*num_px*3)).T
pre=predict(my_image,parameters)




StarcoderdataJupyterScriptsDedupFiltered