[Python] same accuracy with k-fold cross validation in python [closed]

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    I'm studying deep running and machine running these days. But I have problem with this code. it results same accuracy(0.5). all five accuracy also 0.5. I want to know why it results same accuracy. it runs in Colab.

    I definite NeuralNetwork class:

    def __init__(self, input_size):
    self.input_size = input_size
    # np.random.seed(20) # 무작위성 고정
    self.hidden_layer1 = np.random.rand(input_size, 5)
    self.hidden_layer2 = np.random.rand(5, 5)
    self.hidden_layer3 = np.random.rand(5, 1)
    self.output_weights = np.random.rand(1, 1)


    it is constructor of this class.

    # 5. 신경망의 활성화 함수 정의
    def sigmoid(self, z):
    return 1 / (1 + np.exp(-z))


    and I use sigmoid activate function.

    def forward_propagation(self, X):
    self.Z1 = np.dot(X, self.hidden_layer1)
    self.A1 = self.sigmoid(self.Z1)

    self.Z2 = np.dot(self.A1, self.hidden_layer2)
    self.A2 = self.sigmoid(self.Z2)

    self.Z3 = np.dot(self.A2, self.hidden_layer3)
    self.A3 = self.sigmoid(self.Z3)

    self.Z_output = np.dot(self.A3, self.output_weights)
    self.Y_hat = self.sigmoid(self.Z_output)
    return self.Y_hat


    this is forward-propagation method.

    def compute_cost(self, Y, Y_hat):
    m = Y.shape[0]
    cost = -1/m * np.sum(Y * np.log(Y_hat + 1e-15) + (1 - Y) * np.log(1 - Y_hat + 1e-15)) # small constant added for numerical stability
    return cost


    this method calculates weights.

    def back_propagation(self, X, Y, learning_rate=0.01):
    m = X.shape[0]
    dZ_output = self.Y_hat - Y.reshape(-1, 1)
    dW_output = (1/m) * np.dot(self.A3.T, dZ_output)
    dZ3 = np.dot(dZ_output, self.output_weights.T) * (self.A3 * (1 - self.A3))
    dW3 = (1/m) * np.dot(self.A2.T, dZ3)

    dZ2 = np.dot(dZ3, self.hidden_layer3.T) * (self.A2 * (1 - self.A2))
    dW2 = (1/m) * np.dot(self.A1.T, dZ2)

    dZ1 = np.dot(dZ2, self.hidden_layer2.T) * (self.A1 * (1 - self.A1))
    dW1 = (1/m) * np.dot(X.T, dZ1)

    self.output_weights -= learning_rate * dW_output
    self.hidden_layer3 -= learning_rate * dW3
    self.hidden_layer2 -= learning_rate * dW2
    self.hidden_layer1 -= learning_rate * dW1


    this is back-propagation method and updates weights.

    def train(self, X, Y, epochs=1000, learning_rate=0.01):
    for epoch in range(epochs):
    Y_hat = self.forward_propagation(X)
    cost = self.compute_cost(Y, Y_hat)
    self.back_propagation(X, Y, learning_rate)


    this method can train model and I can choose epoch and learning_rate (hyper parameter)

    def evaluate_model(X, Y):
    skf = StratifiedKFold(n_splits=5)
    for train_index, test_index in skf.split(X, Y):
    X_train, X_test = X[train_index], X[test_index]
    Y_train, Y_test = Y[train_index], Y[test_index]

    model = NeuralNetwork(input_size=X_train.shape[1])
    model.train(X_train, Y_train, epochs=1000, learning_rate=0.01)

    Y_pred = model.forward_propagation(X_test)
    Y_pred = (Y_pred > 0.5).astype(int) # threshold 적용

    # 11. 모델 평가
    print(confusion_matrix(Y_test, Y_pred))
    print(classification_report(Y_test, Y_pred))

    # 모델 평가 실행
    evaluate_model(X, Y)


    Lastly, It use K-fold cross validation (K=5) and prints accuracy of my deep-running model.

    Thanks for your help!

    Continue reading...

Compartilhe esta Página