Importing Keras raises TypeError - python

I'm doing a school project, and decided to work on an idea of using a django website to display a chatbot's response. The website should also allow users to add an intent for the chatbot to learn from. After working on the website, I tried importing Keras to begin writing the training file.
import random
import json
import pickle
import numpy as np
import nltk
from nltk.stem import WordNetLemmatizer
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
# Rest of the code isn't necessary as error is raised above
However, when I ran the file, it keeps giving me the following error:
Traceback (most recent call last):
File "D:\User\SILPythonChatbot\SILProject\BotApp\training.py", line 9, in <module>
from keras.models import Sequential
File "D:\User\SILPythonChatbot\venv\lib\site-packages\keras\__init__.py", line 21, in <module>
from keras import models
File "D:\User\SILPythonChatbot\venv\lib\site-packages\keras\models\__init__.py", line 18, in <module>
from keras.engine.functional import Functional
File "D:\User\SILPythonChatbot\venv\lib\site-packages\keras\engine\functional.py", line 24, in <module>
import tensorflow.compat.v2 as tf
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
from tensorflow.python.tools import module_util as _module_util
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\__init__.py", line 42, in <module>
from tensorflow.python import data
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\__init__.py", line 21, in <module>
from tensorflow.python.data import experimental
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 96, in <module>
from tensorflow.python.data.experimental import service
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 419, in <module>
from tensorflow.python.data.experimental.ops.data_service_ops import distribute
File "D:\UserSILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 22, in <module>
from tensorflow.python.data.experimental.ops import compression_ops
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module>
from tensorflow.python.data.util import structure
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\util\structure.py", line 22, in <module>
from tensorflow.python.data.util import nest
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\data\util\nest.py", line 34, in <module>
from tensorflow.python.framework import sparse_tensor as _sparse_tensor
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module>
from tensorflow.python.framework import constant_op
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module>
from tensorflow.python.eager import execute
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\eager\execute.py", line 22, in <module>
from tensorflow.python.framework import ops
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 49, in <module>
from tensorflow.python.framework import cpp_shape_inference_pb2
File "D:\User\SILPythonChatbot\venv\lib\site-packages\tensorflow\python\framework\cpp_shape_inference_pb2.py", line 203, in <module>
'HandleShapeAndType' : _reflection.GeneratedProtocolMessageType('HandleShapeAndType', (_message.Message,), {
TypeError: 'DTypeMeta' object is not iterable
I tried reinstalling. However, that failed as well.
Does anybody know why this happens, or how to fix it? Thanks.

Related

Tensorflow.Keras not recognised on PyCharm M1 Mac

I am learning machine learning and trying to create a one output from 3 input single layer from Kaggle, I am trying to import tensor flow and keras with the following code:
from tensorflow import keras
from tensorflow.keras import layers
# Create a networkth 1 linear unit
model = keras.Sequential([
layers.Dense(unit =1, input_shape=[3])
])
but I get the following error:
"/Users/ahmedhamadto/PycharmProjects/Deep Learning/venv/bin/python" "/Users/ahmedhamadto/PycharmProjects/Deep Learning/main.py"
Traceback (most recent call last):
File "/Users/ahmedhamadto/PycharmProjects/Deep Learning/main.py", line 1, in <module>
from tensorflow import keras
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/__init__.py", line 37, in <module>
from tensorflow.python.tools import module_util as _module_util
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/__init__.py", line 37, in <module>
from tensorflow.python.eager import context
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/python/eager/context.py", line 29, in <module>
from tensorflow.core.framework import function_pb2
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tensorflow/core/framework/function_pb2.py", line 7, in <module>
from google.protobuf import descriptor as _descriptor
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/google/protobuf/descriptor.py", line 47, in <module>
from google.protobuf.pyext import _message
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/google/protobuf/pyext/_message.cpython-310-darwin.so, 0x0002): symbol not found in flat namespace (__ZNK6google8protobuf10TextFormat21FastFieldValuePrinter19PrintMessageContentERKNS0_7MessageEiibPNS1_17BaseTextGeneratorE)
Process finished with exit code 1
I am using a M1 Pro MacBook Pro incase the info is relevant.

protocol buffer error while creating annotations to my object detection project

How to solve this problem
I have never faced this kind of error before and cant find a way out
Anyone please help me out
Traceback (most recent call last):
File "D:\DK\Dev\Number-plate-detection\Tensorflow\scripts\generate_tfrecord.py", line 27, in <module>
import tensorflow.compat.v1 as tf
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
from tensorflow.python.tools import module_util as _module_util
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\python\__init__.py", line 40, in <module>
from tensorflow.python.eager import context
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\python\eager\context.py", line 32, in <module>
from tensorflow.core.framework import function_pb2
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 16, in <module>
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\core\framework\attr_value_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\core\framework\tensor_pb2.py", line 16, in <module>
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\core\framework\resource_handle_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\tensorflow\core\framework\tensor_shape_pb2.py", line 36, in <module>
_descriptor.FieldDescriptor(
File "D:\DK\Dev\Number-plate-detection\npd\lib\site-packages\google\protobuf\descriptor.py", line 560, in __new__
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).```

AttributeError: module 'tensorflow.python.ops.linalg.linear_operator_util' has no attribute 'matmul_with_broadcast'

I am trying to create my own PyEnvironment for TF-Agents.
However, this error keeps showing up:
AttributeError: module 'tensorflow.python.ops.linalg.linear_operator_util' has no attribute 'matmul_with_broadcast'
I have found out that this seems to be an issue with tensorflow-probability, but I have installed the version tensorflow-probability=0.7.0 recommended at
https://github.com/tensorflow/agents/issues/91
I have tried reinstalling and updating
tensorflow-gpu=2.0.0-beta1
tf-agents-nightly
tensorflow-probability=0.7.0
Here is a minimal code example:
from tf_agents.environments import py_environment
class myEnv(py_environment.PyEnvironment):
def __init__(self):
pass
def _reset(self):
pass
def _step(self, action):
pass
This is the full error message when running this minimum example:
Traceback (most recent call last): File ".\env_mwe.py", line 1, in <module>
from tf_agents.environments import py_environment File "C:\Python37\lib\site-packages\tf_agents\environments\__init__.py", line 18, in <module>
from tf_agents.environments import batched_py_environment File "C:\Python37\lib\site-packages\tf_agents\environments\batched_py_environment.py", line 33, in <module>
from tf_agents.environments import py_environment File "C:\Python37\lib\site-packages\tf_agents\environments\py_environment.py", line 29, in <module>
from tf_agents.trajectories import time_step as ts File "C:\Python37\lib\site-packages\tf_agents\trajectories\__init__.py", line 19, in <module>
from tf_agents.trajectories import time_step File "C:\Python37\lib\site-packages\tf_agents\trajectories\time_step.py", line 28, in <module>
from tf_agents.specs import array_spec File "C:\Python37\lib\site-packages\tf_agents\specs\__init__.py", line 20, in <module>
from tf_agents.specs.distribution_spec import DistributionSpec File "C:\Python37\lib\site-packages\tf_agents\specs\distribution_spec.py", line 22, in <module>
import tensorflow_probability as tfp File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\tensorflow_probability\__init__.py", line 78, in <module>
from tensorflow_probability.python import * # pylint: disable=wildcard-import File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\tensorflow_probability\python\__init__.py", line 22, in <module>
from tensorflow_probability.python import distributions File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\tensorflow_probability\python\distributions\__init__.py", line 64, in <module>
from tensorflow_probability.python.distributions.linear_gaussian_ssm import LinearGaussianStateSpaceModel File "E:\Users\tmp\AppData\Roaming\Python\Python37\site-packages\tensorflow_probability\python\distributions\linear_gaussian_ssm.py", line 41, in <module>
_matmul = linear_operator_util.matmul_with_broadcast AttributeError: module 'tensorflow.python.ops.linalg.linear_operator_util' has no attribute 'matmul_with_broadcast'
Found it!
In order to use tensorflow-gpu with tensorflow-probability, one needs to install tfp-nightly
So pip3 install --upgrade tfp-nightly did the trick
To use 2.0.0beta1 requires tfp-nightly.

python3.5 import tensorflow.contrib error: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them

I am using python 3.5 and installed tensorflow-gpu 1.12.0 on Linux. When I run this in python3.5:
import tensorflow as tf
It works fine with no errors.
However, when I run:
import tensorflow.contrib
There is an error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/__init__.py", line 49, in <module>
from tensorflow.contrib import distributions
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/distributions/__init__.py", line 38, in <module>
from tensorflow.contrib.distributions.python.ops.estimator import *
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/distributions/python/ops/estimator.py", line 21, in <module>
from tensorflow.contrib.learn.python.learn.estimators.head import _compute_weighted_loss
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/__init__.py", line 96, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/__init__.py", line 28, in <module>
from tensorflow.contrib.learn.python.learn import *
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/__init__.py", line 30, in <module>
from tensorflow.contrib.learn.python.learn import estimators
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/__init__.py", line 302, in <module>
from tensorflow.contrib.learn.python.learn.estimators.dnn import DNNClassifier
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn.py", line 35, in <module>
from tensorflow.contrib.learn.python.learn.estimators import dnn_linear_combined
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/dnn_linear_combined.py", line 36, in <module>
from tensorflow.contrib.learn.python.learn.estimators import estimator
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 52, in <module>
from tensorflow.contrib.learn.python.learn.learn_io import data_feeder
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/learn_io/__init__.py", line 26, in <module>
from tensorflow.contrib.learn.python.learn.learn_io.dask_io import extract_dask_data
File "/home/myname/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/learn/python/learn/learn_io/dask_io.py", line 33, in <module>
import dask.dataframe as dd
File "/home/myname/anaconda3/lib/python3.5/site-packages/dask/dataframe/__init__.py", line 1, in <module>
from .core import (DataFrame, Series, Index, _Frame, map_partitions,
File "/home/myname/anaconda3/lib/python3.5/site-packages/dask/dataframe/core.py", line 23, in <module>
from .. import array as da
File "/home/myname/anaconda3/lib/python3.5/site-packages/dask/array/__init__.py", line 4, in <module>
from .core import (Array, stack, concatenate, take, tensordot, transpose,
File "/home/myname/anaconda3/lib/python3.5/site-packages/dask/array/core.py", line 13, in <module>
from toolz.curried import (pipe, partition, concat, unique, pluck, join, first,
File "/home/myname/anaconda3/lib/python3.5/site-packages/toolz/curried/__init__.py", line 53, in <module>
_curry_namespace(vars(toolz)),
File "/home/myname/anaconda3/lib/python3.5/site-packages/toolz/curried/__init__.py", line 48, in _curry_namespace
for name, f in ns.items() if '__' not in name
File "/home/myname/anaconda3/lib/python3.5/site-packages/toolz/curried/__init__.py", line 48, in <genexpr>
for name, f in ns.items() if '__' not in name
File "/home/myname/anaconda3/lib/python3.5/site-packages/toolz/curried/__init__.py", line 42, in _should_curry
return (callable(f) and _nargs(f) > 1 or f in do_curry)
File "/home/myname/anaconda3/lib/python3.5/site-packages/toolz/curried/__init__.py", line 35, in _nargs
return len(inspect.getargspec(f).args)
File "/home/myname/anaconda3/lib/python3.5/inspect.py", line 1050, in getargspec
raise ValueError("Function has keyword-only arguments or annotations"
ValueError: Function has keyword-only arguments or annotations, use getfullargspec() API which can support them
I searched for getfullargspec() API but got even more confused. As far as I know, the getfullargspec() API from inspect can only do with something like
getfullargsspec(function_name)
How can I do with it? I could appreciate it a lot if you could explain the cause and also possibly a simple solution.
Plus, I am running the code on a remote server without root permission. Command lines such as
sudo xxxxx
do not work for me.
Thank you for your help!
I also face this problem. I found this way to solve this problem.
conda install toolz
Good Luck

Theano tensor import error

I have been trying to import theano.tensor in my code.
I have used theano.tensor before. All of my previous code importing theano.tensor works perfectly in my machine. Now I am trying to write another script importing the tensor from theano and I am getting the following exception.
Traceback (most recent call last):
File "code.py", line 2, in <module>
import theano.tensor as T
File "/anaconda3/lib/python3.5/site-packages/theano/__init__.py", line 52, in <module>
from theano.gof import (
File "/anaconda3/lib/python3.5/site-packages/theano/gof/__init__.py", line 56, in <module>
from theano.gof.opt import (
File "/anaconda3/lib/python3.5/site-packages/theano/gof/opt.py", line 11, in <module>
import pdb
File "/anaconda3/lib/python3.5/pdb.py", line 75, in <module>
import code
File "/localtmp/saikat/CovInfo/Closure/code.py", line 2, in <module>
import theano.tensor as T
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/__init__.py", line 6, in <module>
from theano.tensor.basic import *
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/basic.py", line 17, in <module>
from theano.tensor import elemwise
File "/anaconda3/lib/python3.5/site-packages/theano/tensor/elemwise.py", line 13, in <module>
from theano import scalar
File "/anaconda3/lib/python3.5/site-packages/theano/scalar/__init__.py", line 2, in <module>
from .basic import *
File "/anaconda3/lib/python3.5/site-packages/theano/scalar/basic.py", line 25, in <module>
from theano import gof, printing
File "/anaconda3/lib/python3.5/site-packages/theano/printing.py", line 22, in <module>
from theano.compile import Function, debugmode, SharedVariable
File "/anaconda3/lib/python3.5/site-packages/theano/compile/__init__.py", line 9, in <module>
from theano.compile.function_module import *
File "/anaconda3/lib/python3.5/site-packages/theano/compile/function_module.py", line 22, in <module>
import theano.compile.mode
File "/anaconda3/lib/python3.5/site-packages/theano/compile/mode.py", line 77, in <module>
OPT_NONE = gof.Query(include=[], exclude=exclude)
AttributeError: module 'theano.gof' has no attribute 'Query'
I cannot find any plausible reason for this exception.
I guess i got your problem. See in the error log:
File "/anaconda3/lib/python3.5/pdb.py", line 75, in <module>
import code
I believe there is another script called code.py in Theano which gets called from pdb.py when python interpreter executes your script which is also named as code.py. I am guessing python interpreter is mixing up these two scripts and executing the wrong one! You can change the filename and check whether the error disappear or not.

Categories

Resources