haven-jeon / PyKoSpacing

Automatic Korean word spacing with Python
GNU General Public License v3.0
397 stars 118 forks source link

Colab 에서 pykospacing 사용 문의 #66

Closed HJ-Yoo closed 2 months ago

HJ-Yoo commented 2 months ago

안녕하세요, colab에서 패키지 설치하여 사용하고자 하는데 에러를 만나 문의드립니다. 버전의 문제일까요? 감사합니다.

스크린샷 2024-07-02 오후 8 18 55 스크린샷 2024-07-02 오후 8 19 25

HyeongJaeKwon commented 2 months ago

image image

저도 같은 이슈로 문의드립니다.

pykospacing의 requirements.txt에 tensorflow <= 2.16.1 h5py >= 3.10.0 argparse >= 1.4.0 라고 나와있는데

python >=3.2 버전 부터 argparse가 standard library에 속하게 되었고, 그로인해 기본 argparse 패키지 버전이 1.1로 픽스가 되어있고, 현재 저는 python 3.10 (구글 코렙)를 사용하고 있는 입장에서, argparse를 1.4 로 업데이트해도 계속해서 1.1로 사용되고있는 상황입니다. 이것은 로컬환경에서 다른 파이썬버전 python 3.9~3.11로 해도 똑같이 argparse는 계속 1.1로 픽스 되었습니다.

에러코드는 InvalidArgumentError: Exception encountered when calling TFSMLayer.call() 인 상황인데,

얼핏보기에 tensorflow 오류로 보이나, tensorflow 버전이 현재 requirement에 맞는 버전이라, 저는 argparse로 인해 일어난 에러라고 추정했습니다.

혹시 파이썬 버전을 3.1로 하고 개별argparse 패키지 1.4를 다운받으면 문제가 해결되는 것 일까요? 파이썬 >=3.2를 사용하면서 pykospacing을 사용할 수 있는 방법이 있을까요?

Jinwoo1126 commented 2 months ago

(24/07/02 기준)

저도 같은 오류가 발생하였는데,

Google Colab에서 아래 코드를 다시 수행하여 Version을 2.16.2로 올려주면 해결이 되었습니다.

!pip install -U tensorflow

다만, 저는 버전 업데이트 후 다른 이슈가 있었는데

InvalidArgumentError: Exception encountered when calling TFSMLayer.call().

cannot compute __inference_signature_wrapper___call___755 as input #0(zero-based) was expected to be a float tensor but is a int32 tensor [Op:__inference_signature_wrapper___call___755]

Arguments received by TFSMLayer.call():
  • inputs=tf.Tensor(shape=(1, 200), dtype=int32)
  • training=False
  • kwargs=<class 'inspect._empty'>

위의 메시지와 같이 입력이 float32로 들어와야 하는데 int32로 들어온다고 하여 pykospacing/embedding_maker.py에서 encoding_and_padding의 return을 float로 casitng 해주어 해결하였습니다.

def encoding_and_padding(word2idx_dic, sequences, **params):
    """
    1. making item to idx
    2. padding

    :word2idx_dic
    :sequences: list of lists where each element is a sequence
    :maxlen: int, maximum length
    :dtype: type to cast the resulting sequence.
    :padding: 'pre' or 'post', pad either before or after each sequence.
    :truncating: 'pre' or 'post', remove values from sequences larger than
        maxlen either in the beginning or in the end of the sequence
    :value: float, value to pad the sequences to the desired value.
    """
    seq_idx = [[word2idx_dic.get(a, word2idx_dic['__ETC__']) for a in i] for i in sequences]
    params['value'] = word2idx_dic['__PAD__']
    return (sequence.pad_sequences(seq_idx, **params).astype('float32')) ## float32로 casting
HyeongJaeKwon commented 2 months ago

알려주신 방법으로 고쳐졌습니다, 감사합니다

HJ-Yoo commented 2 months ago

@Jinwoo1126 도움 주셔서 감사합니다! 저도 해결했습니다.

이슈 닫도록 하겠습니다.