👩‍💻LEARN : ML&Data/Lecture

[Advanced Learning Algorithms] #5. Multiclass Classification

쟈니유 2023. 3. 27. 21:06
728x90

클래스가 3개 이상일 경우


#5. Multiclass Classification 

Softmax

Softmax 개요 

Softmax Cost 

- aj가 커질수록 loss가 줄어들음 

 

y는 분류의 문제이므로 정확한 분류(N)를 맞춘 경우에 loss함수의 평균을 구해 cost 값을 확인할 수 있다. 

 

 

 

 

 

Neural Network with Softmax output

  • Output layer에서 나오는 결과는 직전 layer의 a vector와 해당 layer의 파라미터를 곱한 z 들이 나옴
  • Softmax 함수를 적용하기 때문에, 최종 activation은 이를 softmax 함수에 적용한 10개의 결과가 나옴 (probabiliy)
  • 다른 activation function과의 차이점은, softmax에서는 하나의 도출값에도 z1~z10까지 모두 사용된다는 것 

 

Improved implementation of softmax 

Numerical roundoff errors 

- 같은 결과가 나와야하지만 컴퓨터는 메모리 등의 이슈로 완벽하게 같지 못한 결과가 나옴 

Logistic regression 적용법

- 마지막 layer에 activation = 'linear' 적용

- model.compile(loss=BinaryCrossEntropy(from_logits=True)) 세팅 (logits = ext(-z)의 z) 

Softmax 에서 roundoff errors를 줄이는 법 

- 동일하게 activation = linear로 주고 model.compile에서 logits = True 주기 

Classification with multiple outputs 

Multi-label Classification 

- 이미지에서 '버스','택시','사람'을 인식할 때 따로따로 별도의 뉴럴 네트워크를 구축하는 것이 아니라 그냥 하나의 뉴럴네트워크에서 multi-label class 세개를 아웃풋으로 두면 됨 

 

#소프트맥스 함수 만들기

def my_softmax(z):
    ez = np.exp(z)              #element-wise exponenial
    sm = ez/np.sum(ez)          #다른 ez들의 합으로 특정 ez 나누기 
    return(sm)
    
    
#앤드류응쨩이 알려준 보다 정확한 모델 만들기 

preferred_model = Sequential(
    [ 
        Dense(25, activation = 'relu'),
        Dense(15, activation = 'relu'),
        Dense(4, activation = 'linear')   #<-- Note
    ]
)
preferred_model.compile(
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),  #<-- Note
    optimizer=tf.keras.optimizers.Adam(0.001),
)

preferred_model.fit(
    X_train,y_train,
    epochs=10
)
        
        
#그런데 이렇게 하면 확률로 나오지 않아서 확률로 만드는 작업 해줘야함 

p_preferred = preferred_model.predict(X_train)
sm_preferred = tf.nn.softmax(p_preferred).numpy()

#카테고리 만들어주기 

for i in range(5):
    print( f"{p_preferred[i]}, category: {np.argmax(p_preferred[i])}")

 

※ model.compile(loss='SparseCategorialCrossentropy or CategoricalCrossEntropy')

  • SparseCategorialCrossentropy: expects the target to be an integer corresponding to the index. For example, if there are 10 potential target values, y would be between 0 and 9.
  • CategoricalCrossEntropy: Expects the target value of an example to be one-hot encoded where the value at the target index is 1 while the other N-1 entries are zero. An example with 10 potential target values, where the target is 2 would be [0,0,1,0,0,0,0,0,0,0].