Apress / building-chatbots-with-python

Source Code for 'Building Chatbots with Python' by Sumit Raj
Other
101 stars 75 forks source link

ImportError: cannot import name 'Layer' #3

Open GavinMoreYoung opened 5 years ago

GavinMoreYoung commented 5 years ago

i run chapter IV code see the issue , i do not know how to solve it, i hope you can help me. i am look forward to your reply. python version: Python 3.6.8 pip list display: Package Version


absl-py 0.7.0 APScheduler 3.5.3 asn1crypto 0.24.0 astor 0.7.1 attrs 18.2.0 Automat 0.7.0 boto3 1.9.100 botocore 1.12.100 certifi 2018.11.29 cffi 1.12.1 chardet 3.0.4 Click 7.0 cloudpickle 0.6.1 colorama 0.4.1 colorclass 2.2.0 coloredlogs 10.0 colorhash 1.0.2 ConfigArgParse 0.13.0 constantly 15.1.0 cryptography 2.5 cycler 0.10.0 decorator 4.3.2 docopt 0.6.2 docutils 0.14 fakeredis 0.10.3 fbmessenger 5.4.0 Flask 1.0.2 Flask-Cors 3.0.7 Flask-JWT-Simple 0.0.3 future 0.17.1 gast 0.2.2 gevent 1.4.0 greenlet 0.4.15 grpcio 1.18.0 h5py 2.9.0 humanfriendly 4.18 hyperlink 18.0.0 idna 2.8 incremental 17.5.0 itsdangerous 1.1.0 Jinja2 2.10 jmespath 0.9.3 jsonpickle 1.1 jsonschema 2.6.0 Keras-Applications 1.0.6 Keras-Preprocessing 1.0.5 kiwisolver 1.0.1 klein 17.10.0 Markdown 3.0.1 MarkupSafe 1.1.0 matplotlib 2.2.3 mattermostwrapper 2.1 networkx 2.2 numpy 1.16.1 packaging 18.0 pika 0.12.0 pip 19.0.2 pipenv 2018.11.26 prompt-toolkit 2.0.9 protobuf 3.6.1 pycparser 2.19 pydot 1.4.1 PyHamcrest 1.9.0 PyJWT 1.7.1 pykwalify 1.7.0 pymongo 3.7.2 pyparsing 2.3.1 pyreadline 2.1 PySocks 1.6.8 python-dateutil 2.8.0 python-engineio 3.4.3 python-socketio 3.1.2 python-telegram-bot 11.1.0 pytz 2018.9 PyYAML 3.13 questionary 1.0.2 rasa-core 0.13.2 rasa-core-sdk 0.12.2 rasa-nlu 0.14.3 redis 2.10.6 requests 2.21.0 requests-toolbelt 0.9.1 rocketchat-API 0.6.26 ruamel.yaml 0.15.88 s3transfer 0.2.0 scikit-learn 0.20.2 scipy 1.2.1 setuptools 40.6.2 simplejson 3.16.0 six 1.12.0 slackclient 1.3.0 tensorboard 1.12.2 tensorflow 1.12.0 termcolor 1.1.0 terminaltables 3.1.0 tqdm 4.31.1 twilio 6.24.1 Twisted 18.9.0 typing 3.6.6 tzlocal 1.5.1 urllib3 1.24.1 virtualenv 16.4.0 virtualenv-clone 0.5.1 wcwidth 0.1.7 webexteamssdk 1.1.1 websocket-client 0.54.0 Werkzeug 0.14.1 wheel 0.33.1 zope.interface 4.6.0 run this file train_initialize.py: from future import absolute_import from future import division from future import print_function from future import unicode_literals

from rasa_core import utils from rasa_core.agent import Agent from rasa_core.policies.keras_policy import KerasPolicy from rasa_core.policies.memoization import MemoizationPolicy

if name == 'main': utils.configure_colored_logging(loglevel="DEBUG")

training_data_file = './data/stories.md'
model_path = './models/dialogue'

agent = Agent("horoscope_domain.yml",
              policies=[MemoizationPolicy(max_history = 2), KerasPolicy()])

training_data = agent.load_data(training_data_file)

agent.train(
        training_data,
        augmentation_factor=50,
        # epochs=500,
        # batch_size=10,
        validation_split=0.2
)

agent.persist(model_path)

error information: 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Generated trackers will be deduplicated based on their unique last 5 states. 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Number of augmentation rounds is 3 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Starting data generation round 0 ... (with 1 trackers) Processed Story Blocks: 100%|██████████| 5/5 [00:00<00:00, 99.20it/s, # trackers=1] 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Finished phase (5 training samples found). 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Data generation rounds finished. 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Found 0 unused checkpoints 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Starting augmentation round 0 ... (with 5 trackers) Processed Story Blocks: 100%|██████████| 5/5 [00:00<00:00, 557.03it/s, # trackers=4] 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Finished phase (26 training samples found). 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Starting augmentation round 1 ... (with 20 trackers) Processed Story Blocks: 100%|██████████| 5/5 [00:00<00:00, 175.77it/s, # trackers=11] 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Finished phase (79 training samples found). 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Starting augmentation round 2 ... (with 20 trackers) Processed Story Blocks: 100%|██████████| 5/5 [00:00<00:00, 156.55it/s, # trackers=9] 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Finished phase (118 training samples found). 2019-02-22 10:58:22 DEBUG rasa_core.training.generator - Found 118 training trackers. 2019-02-22 10:58:22 DEBUG rasa_core.agent - Agent trainer got kwargs: {'augmentation_factor': 50, 'validation_split': 0.2} 2019-02-22 10:58:22 DEBUG rasa_core.featurizers - Creating states and action examples from collected trackers (by MaxHistoryTrackerFeaturizer(SingleStateFeaturizer))... Processed trackers: 100%|██████████| 118/118 [00:00<00:00, 687.36it/s, # actions=42] 2019-02-22 10:58:22 DEBUG rasa_core.featurizers - Created 42 action examples. Processed actions: 42it [00:00, 9352.34it/s, # examples=42] 2019-02-22 10:58:22 DEBUG rasa_core.policies.memoization - Memorized 42 unique examples. 2019-02-22 10:58:22 DEBUG rasa_core.featurizers - Creating states and action examples from collected trackers (by MaxHistoryTrackerFeaturizer(BinarySingleStateFeaturizer))... Processed trackers: 100%|██████████| 118/118 [00:00<00:00, 770.20it/s, # actions=93] 2019-02-22 10:58:23 DEBUG rasa_core.featurizers - Created 93 action examples. Traceback (most recent call last): File "C:/Users/Admin/Desktop/building-chatbots-with-python-master/Chapter IV/Chapter IV Code horoscope_bot/horoscope_bot/train_initialize.py", line 28, in validation_split=0.2 File "D:\Python\lib\site-packages\rasa_core\agent.py", line 520, in train kwargs) File "D:\Python\lib\site-packages\rasa_core\policies\ensemble.py", line 63, in train policy.train(training_trackers, domain, kwargs) File "D:\Python\lib\site-packages\rasa_core\policies\keras_policy.py", line 162, in train shuffled_y.shape[1:]) File "D:\Python\lib\site-packages\rasa_core\policies\keras_policy.py", line 93, in model_architecture from tensorflow.keras.models import Sequential File "D:\Python\lib\site-packages\tensorflow\keras__init.py", line 20, in from . import layers File "D:\Python\lib\site-packages\tensorflow\keras\layers\init__.py", line 8, in from tensorflow.python.estimator.keras import Layer ImportError: cannot import name 'Layer'

I look forward your reply . thanks

sumit12dec commented 5 years ago

Hi @GavinMoreYoung I looked into your issue and tried to replicate and see what can be potential fix.

Here is what I did after I could replicate your issue.

1) Created a new req.txt file with below version in the file. rasa-core==0.11.1 rasa-core-sdk==0.11.0 rasa-nlu==0.13.2 gunicorn==19.9.0 requests==2.18.4 spacy==2.0.11 sklearn-crfsuite==0.3.6

2) pip3 install -r req.txt 3) Then went to python3 interpreter 4) Then tried importing the line which was giving error and it's working for me now.

from tensorflow.python.estimator.keras import Layer

Here is a screenshot of my successful import. https://imgur.com/a/ih71nq3

Let me know if it works for you

Regards, Sumit Raj

sumit12dec commented 5 years ago

Hi @GavinMoreYoung

Hope you are doing well and your issue was resolved. Feel free to comment here or come join our discussion forum https://buildingchatbotswithpython.sumitraj.in/

Request you to please leave a review of the book on Amazon.

https://www.amazon.com/Building-Chatbots-Python-Language-Processing/dp/1484240952

Regards, Sumit