[Keras] Layer model is not connected, no input to return.

    텐서플로우(Tensorflow) + 케라스(Keras) 모델에서 Seq2Seq를 테스트 하는 중에 케라스에서 배포하는 소스에서 에러가 발생하였다.

     

    7808/8000 [============================>.] - ETA: 0s - loss: 0.4221 - accuracy: 0.8742
    7872/8000 [============================>.] - ETA: 0s - loss: 0.4222 - accuracy: 0.8742
    7936/8000 [============================>.] - ETA: 0s - loss: 0.4222 - accuracy: 0.8742
    8000/8000 [==============================] - 33s 4ms/sample - loss: 0.4221 - accuracy: 0.8742 - val_loss: 0.5077 - val_accuracy: 0.8502
    2020-05-20 13:08:30.745000: W tensorflow/python/util/util.cc:299] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
    WARNING:tensorflow:From C:\Anaconda3\envs\python\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1781: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
    Instructions for updating:
    If using Keras pass *_constraint arguments to layers.
    Traceback (most recent call last):
      File "E:/Project/universe/source-python/friday/seq2seq.py", line 118, in <module>
        encoder_inputs = model.input[0]  # input_1
      File "C:\Anaconda3\envs\python\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py", line 1557, in input
        ' is not connected, no input to return.')
    AttributeError: Layer model is not connected, no input to return.
    
    Process finished with exit code  

     

    보다시피 학습은 잘되었는데 AttributeError가 발생하면서 만들어진 모델에 연결이 안된다고 하였다. 구글링해보니 다음과 같은 서양애들의 의견이 있었다.

     

    Any update on this?save your model in keras .h5 format instead of tensorflow checkpoint format seems to work.

     

    모델 포맷을 .h5로 하라는 말이 보여서, 소스를 수정하였다.

     

    # Save model
    #model.save("s2s")
    model.save('s2s2.h5')
    
    # Define sampling models
    # Restore the model and construct the encoder and decoder.
    model = keras.models.load_model("s2s2.h5")

     

     

    AS-IS

    s2s 폴더에 모델이 생성된 모습

     

    TO-BE

    s2s2.h5 모델이 생긴 것을 볼 수 있다.

     

    트레이닝 데이터

    안녕하세요. 그래 안녕
    날씨를 알려주세요. 오늘 날씨는 맑은 편이다.
    어벤져스. 어쎔블!
    네 이름이 뭐니? My name is Neo.

     

    결과

    3/3 [==============================] - 0s 17ms/sample - loss: 0.2456 - accuracy: 0.9804 - val_loss: 10.1672 - val_accuracy: 0.2353
    -
    Input sentence: 안녕하세요.
    Decoded sentence: 그래 안녕
    
    -
    Input sentence: 날씨를 알려주세요.
    Decoded sentence: 오늘 날씨는 맑은편이다.
    
    -
    Input sentence: 어벤져스.
    Decoded sentence: 어쎔블!
    
    -
    Input sentence: 네 이름이 뭐니?
    Decoded sentence: 그래 안녕       

     

    마지막 문장을 유추 못한 것이 흠이긴 하지만, 결과가 정상적으로 잘 나오고 있다.

    반응형

    댓글

    Designed by JB FACTORY