상세 컨텐츠

본문 제목

NotImplementedError: Cannot convert a symbolic tf.Tensor (strided_slice_1:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported.

프로그래밍/오류와 해결

by 아싸호랑나비 2022. 12. 20. 10:19

본문

케라스를 이용한 딥러닝을 하다가 해당 에러가 발생했습니다

넘파이 버전이 문제라는 말을 들어서 다운그레이드를해봤지만 딱히 달라지는것없었습니다

 

 

해결방법))

 

케라스에서는 분류 문제 일때 타겟을 one-hot인코딩해야된다는것을 발견했습니다

케라스와 파이토치를 둘다 미숙하게 알다보니 이런일이 발생한것같습니다

 

케라스로 진행하는 분류에 대한 예제는 이곳에 잘 설명되어있습니다

https://pinkwink.kr/1128

 

[Keras] 붓꽃 Iris 데이터 분류해보기 Iris classification using Keras

최근 저는 케라스를 이용해서 선형 회귀도 살짝 공부할 겸, 다변수 입력에 대한 선형회귀 문제로 혈중 지방함량이라는 데이터를 어디서 구해서 케라스를 이용한 다변수 함수에 대한 선형회귀를

pinkwink.kr

 

인줄알았는데 아니었다 metric이문제였다 

 

사용자설정 metric을 사용했었는데 accuracy로 바꿔주니 정상적으로 작동했다

 

macro f1이 문제였는데 원래 사용한 코드는 다음과 같고 pytorch에서는 문제없이 돌아갔던 코드라 

이것이 문제일것이라고 처음엔 생각하지 못했던것같다

def competition_metric(true, pred):
    return f1_score(true, pred, average="macro")

구글링후 새로운 코드로 바꿔주었고 이후에는 문제없이 동작했다

from keras import backend as K
def f1(y_true, y_pred):
    def recall(y_true, y_pred):
        """Recall metric.

        Only computes a batch-wise average of recall.

        Computes the recall, a metric for multi-label classification of
        how many relevant items are selected.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
        recall = true_positives / (possible_positives + K.epsilon())
        return recall

    def precision(y_true, y_pred):
        """Precision metric.

        Only computes a batch-wise average of precision.

        Computes the precision, a metric for multi-label classification of
        how many selected items are relevant.
        """
        true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
        predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
        precision = true_positives / (predicted_positives + K.epsilon())
        return precision
    precision = precision(y_true, y_pred)
    recall = recall(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

이걸로 바꿔주니까동작했다

'프로그래밍 > 오류와 해결' 카테고리의 다른 글

cv2 resize error 해결  (0) 2022.09.03

관련글 더보기