自我构造的深度学习模型

时间:2019-03-11 07:03:49

标签: python neural-network deep-learning

我正在建立一个模型,通过以下两个步骤在第一隐藏层神经元中进行更改:假设我们在输入层中有4个输入,其中i = 1到4。

    Step 1(a) : Hidden layer1
              Hidden layer neurons are say H1,H2, H3, then
    H1= summation of  Wi1
    H2= summation of Wi2
    H3 = summation of Wi3

    Step1(b) : To calculate the Hidden node with high rank :
    sum = H1 + H2 + H3
    max(sum/H1 , sum/H2, sum/H3)

    Step 2(a) :
    To calculate the importance of features at that hidden layer neuron with higher rank. Suppose we got H2 as a higher rank neuron at Hidden layer neuron 1

     F1= summation (Wi2) / W12
     F2= summation (Wi2)/W22
     .
     .
    Fn = summation(Wi2)/Wn2
Now sort F1,F2...Fn in descending order and take say 80% of input features .
Now for the further hidden layers, we will calculate the neurons as (no of neurons in the current layer + no of neurons in output layer)/2. 

我已经成功地编码了以上步骤,并且在这里包括了我的代码。 enter image description here

对于上述2个sts,代码工作正常,但我不知道如何进一步进行。如何构建分类器并对其进行训练?

现在还不了解如何建立模型以及如何训练模型。如何进行正向传播和反向传播?即使我编写了前向传播函数,那么如何将迄今为止完成的工作包括在内?请帮助我。

import numpy as np
import keras as ks
import tensorflow as tf
import pandas as pd
                                    # Fix Random Seed for Reproducibility
np.random.seed(1)
from tensorflow import set_random_seed
set_random_seed(2)
                                     # Loading the Dataset
dataset = pd.read_csv('creditcard.csv')
print(dataset.shape)
print(dataset.head(3))
X= dataset.drop('Class',axis=1)
Y= dataset['Class']

                                    # Scaling the Time and Amount column
from sklearn.preprocessing import StandardScaler,RobustScaler
st_scaler = StandardScaler()
rb_scaler= RobustScaler()
dataset['scaled_amount'] = rb_scaler.fit_transform(dataset['Amount'].values.reshape(-1,1))
dataset['scaled_time'] = rb_scaler.fit_transform(dataset['Time'].values.reshape(-1,1))
print('ScaledAmount')
print('scaledTime')
print(dataset['scaled_amount'], dataset['scaled_time'])
dataset.drop(['Time', 'Amount'], axis=1, inplace=True)
scaled_amount= dataset['scaled_amount']
scaled_time=dataset['scaled_time']
dataset.drop(['scaled_amount','scaled_time'],axis=1,inplace=True)
dataset.insert(0,'scaled_amount',scaled_amount)
dataset.insert(1,'scaled_time',scaled_time)
print(dataset.head(3))

                                  # Splitting the Dataset
from sklearn.model_selection import train_test_split
X_train, X_test, Y_train,Y_test = train_test_split(X,Y,test_size=0.2,shuffle=True,random_state=42)
print("Shape of Training set:", X_train.shape, Y_train.shape)
print("Shape of Test set:", X_test.shape, Y_test.shape)
Y_train=Y_train.values
print("Class 1 in Training set {}:",format(sum(Y_train==1)))
print("Class 0 in Training set{}",format(sum(Y_train==0)))
print("Class 1 in Test set {}", format(sum(Y_test==1)))
print("Class 0 in Test set {}", format(sum(Y_test==0)))

inputSize= 30
hl1_neurons=16            #Hidden Layer1 Neurons
outputSize =1

W1= np.random.randn(inputSize,hl1_neurons) # Initializing weight matrix between input and hiddenlayer1 randomly
#print("Length of W1", len(W1))
print("shape of W1", W1.shape)
print("W1", W1)
W2= np.random.randn(hl1_neurons,outputSize) #Initializing weight matrix between hiddenlayer1 and output layer randomly
print("W2",W2)
print("Length of W2",len(W2))

def sigma(x):
    return 1/(1+np.exp(-x))

                                      # Step 1(a)
column_sums=[]
column_sums= [sum([row[i] for row in W1]) for i in range(0,len(W1[0]))]
print("column_sums",column_sums)
Sig_result=[]        # Stores the result obtained after applying sigmoid activation functon to the value of each Hidden layer neuron
for j in range(0,len(column_sums)):
       sig_res = sigma(column_sums[j])
       Sig_result.append(sig_res)
print("Result of Sigmoid Activation function for each Hidden Layer neuron:",Sig_result)
total = sum(Sig_result)      # Summation of values of all the hiddenlayer neurons
print("Total is", total)
                                     # Step 1(b)
list1=[]
for i in range(0,len(Sig_result)):
       result = total/Sig_result[i]
       list1.append(result)
       print(" Result for Hiddenlayer neurons",i, result)
print("List1:",list1)      # Stores the result of 1 (b)
Maximum = max(list1) # Maximum value held by one of the hidden layer neuron
print("Maximum",Maximum)
print("The Hidden layer neuron",str([i for i,j in enumerate(list1) if j==Maximum]),"is of highest rank with maximum value",Maximum)

                                  # Step 2(b)
                                  # Same thing as calculating column sums
sum=0
for i in range(16):
    for j in range(30):
        sum+=W1[j][i]
    print("Sum of the column",i,"is",sum)
    sum=0

sum=0
for j in range(30):
    sum +=W1[j][3]
print("Sum of the weights coming to neuron with highest rank is",sum)

Feature_list=[]
for i in range(30):
    res = (sum/W1[i][3])
    Feature_list.append(res)
    res=0
print("Feature List:",Feature_list)

0 个答案:

没有答案