반응형
ㅁ Weight update
o 인공신경망에서 가중치 업데이트를 어떻게 하는가?
- 현재 역전파되는 오차까지 계산했다.
- 오차는 가중치를 조절해 가는데 중요한 지표다.
- 신경망의 오차는 단순한 선형 분류자가 아니다.
- 각 노드에 입력과 가중치를 곱하고 합한 후 활성함수를 통과한다.
- 각각의 노드를 연결하는 가중치의 업데이트는 정교한 계산이 필요하다.
ㅁ 단계별 신경망 만들기
o 구조
- 초기화(입력,은닉,출력노드수 결정)
- 학습(학습을 통해 가중치 업데이트)
- 질의(연산 후 출력 노드에 결과 전달)
- 입력 데이터를 받을때 0 부터 1사이의 작은 값을 쓰는데, 범위 안네 들어올 수 있또록 데이터를 바꾸고 학습해야한다.
- 데이터셋이 있으면 첫번째 데이터셋을 받아서 가중치를 받고 sigmoid하고 출력하고 target value와 비교하고
- 오차가 나오면 가중치를 업데이트를 하는 것을 반복한다.
- 그럼 모델의 결과가 나오는데 그 모델에 테스트 값을 넣어서 결과를 비교한다.
- 내가 만든 모델의 결과가 타겟벨류가 가트면 맞고, 틀리는 것을 성능 테스트한다고 보면된다.
- 테스트할때는 순정파만 해서 결과 보고 타겟벨류와 같은지만 확인하면 된다.
classNeuralnetwork: def __init__(): def train(): def query():
import numpy # scipy.special for the sigmoid function expit() import scipy.special # library for plotting arrays import matplotlib.pyplot # ensure the plots are inside this notebook, not an external window %matplotlib inline # neural network class definition class NeuralNetwork: ############### # initialise the neural network def __init__(self, inputnodes, hiddennodes, outputnodes, learningrate): # set number of nodes in each input, hidden, output layer self.inodes = inputnodes self.hnodes = hiddennodes self.onodes = outputnodes ############### 입력계층의 노드, 은닉계층의 노드, 출력 계층의 노드, 학습률 결정 ############### # link weight matrices, wih and who # weights inside the arrays are w_i_j, where link is from node i to node j in the next layer # w11 w21 # w12 w22 etc self.wih = numpy.random.normal(0.0, pow(self.inodes, -0.5), (self.hnodes, self.inodes)) self.who = numpy.random.normal(0.0, pow(self.hnodes, -0.5), (self.onodes, self.hnodes)) ############### 표준정규분포 0.0으로 하고 POW는 노드의 수를 루트노드스분의로 처리 ############### # learning rate self.lr = learningrate ############### 학습률 결정 ############### # activation function is the sigmoid function self.activation_function = lambda x: scipy.special.expit(x) ############### 활성함수 적용 (Sigmoid Function) lambda 메서드 이름, explit = 시그모이드함수 이름 # train the neural network def train(self, inputs_list, targets_list): # convert inputs list to 2d array inputs = numpy.array(inputs_list, ndmin=2).T #.T로 역전파 수행 targets = numpy.array(targets_list, ndmin=2).T # calculate signals into hidden layer hidden_inputs = numpy.dot(self.wih, inputs) # 곱의 합 시그모이드의 전 값 # calculate the signals emerging from hidden layer hidden_outputs = self.activation_function(hidden_inputs) # 시그모이드 통과된 결과값고 HR에서 출력된 값 # calculate signals into final output layer final_inputs = numpy.dot(self.who, hidden_outputs) # Or에서 다시 반복한다 Ok값 # calculate the signals emerging from final output layer final_outputs = self.activation_function(final_inputs) # output layer error is the (target - actual) output_errors = targets - final_outputs # hidden layer error is the output_errors, split by weights, recombined at hidden nodes hidden_errors = numpy.dot(self.who.T, output_errors) # update the weights for the links between the hidden and output layers self.who += self.lr * numpy.dot((output_errors * final_outputs * (1.0 - final_outputs)), numpy.transpose(hidden_outputs)) # update the weights for the links between the input and hidden layers self.wih += self.lr * numpy.dot((hidden_errors * hidden_outputs * (1.0 - hidden_outputs)), numpy.transpose(inputs)) # query the neural network def query(self, inputs_list): # convert inputs list to 2d array inputs = numpy.array(inputs_list, ndmin=2).T # calculate signals into hidden layer hidden_inputs = numpy.dot(self.wih, inputs) # calculate the signals emerging from hidden layer hidden_outputs = self.activation_function(hidden_inputs) # calculate signals into final output layer final_inputs = numpy.dot(self.who, hidden_outputs) # calculate the signals emerging from final output layer ############### final_outputs = self.activation_function(final_inputs) ############### 내 모델의 결과값 Target Value와 비교하고 성능테스트 실시 return final_outputs
반응형
'Develop > AI' 카테고리의 다른 글
인공지능(신경망, 딥러닝) 교육 정리(Artificial Neural Network 4) (0) | 2023.01.15 |
---|---|
인공지능(신경망, 딥러닝) 교육 정리 (Preparing Data) (0) | 2023.01.08 |
인공지능(신경망, 딥러닝) 교육 정리(Artificial Neural Network 2) (0) | 2023.01.08 |
인공지능(신경망, 딥러닝) 교육 정리 (Backpropagation) (0) | 2023.01.08 |
인공지능(신경망, 딥러닝) 교육 정리(Artificial Neural Network 1) (0) | 2023.01.08 |
최근댓글