laviavigdor / twitter-sentiment-analysis

Sentiment Analysis on twitter using Keras / TensorFlow / GloVe
29 stars 10 forks source link

ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(20000, 200), dtype=float32)' #2

Closed durika closed 7 years ago

durika commented 7 years ago

I follow the instructions and get this error, can you please help?

[root@localhost sentiment]# echo "This is a sample tweet to predict on" | python predict.py Using TensorFlow backend. /usr/lib/python2.7/site-packages/keras/preprocessing/text.py:139: UserWarning: The nb_words argument in Tokenizer has been renamed num_words. warnings.warn('The nb_words argument in Tokenizer ' /usr/lib/python2.7/site-packages/keras/engine/topology.py:1242: UserWarning: The dropout argument is no longer support in Embedding. You can apply a keras.layers.SpatialDropout1D layer right after the Embedding layer to get the same behavior. return cls(config) /usr/lib/python2.7/site-packages/keras/engine/topology.py:1242: UserWarning: Update your Embedding call to the Keras 2 API: Embedding(embeddings_initializer="uniform", trainable=False, name="embedding_1", output_dim=200, activity_regularizer=None, embeddings_regularizer=None, input_dtype="int32", embeddings_constraint=None, mask_zero=False, input_dim=20000, batch_input_shape=[None, 100..., input_length=1000) return cls(config) Traceback (most recent call last): File "predict.py", line 41, in main() File "predict.py", line 20, in main model = load_model('model.h5') File "/usr/lib/python2.7/site-packages/keras/models.py", line 233, in load_model model = model_from_config(model_config, custom_objects=custom_objects) File "/usr/lib/python2.7/site-packages/keras/models.py", line 307, in model_from_config return layer_module.deserialize(config, custom_objects=custom_objects) File "/usr/lib/python2.7/site-packages/keras/layers/init.py", line 54, in deserialize printable_module_name='layer') File "/usr/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object list(custom_objects.items()))) File "/usr/lib/python2.7/site-packages/keras/models.py", line 1210, in from_config model.add(layer) File "/usr/lib/python2.7/site-packages/keras/models.py", line 436, in add layer(x) File "/usr/lib/python2.7/site-packages/keras/engine/topology.py", line 569, in call self.build(input_shapes[0]) File "/usr/lib/python2.7/site-packages/keras/layers/embeddings.py", line 101, in build dtype=self.dtype) File "/usr/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 87, in wrapper return func(*args, **kwargs) File "/usr/lib/python2.7/site-packages/keras/engine/topology.py", line 391, in add_weight weight = K.variable(initializer(shape), dtype=dtype, name=name) File "/usr/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 321, in variable v = tf.Variable(value, dtype=_convert_string_dtype(dtype), name=name) File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 200, in init expected_shape=expected_shape) File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/variables.py", line 289, in _init_from_args initial_value, name="initial_value", dtype=dtype) File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 676, in convert_to_tensor as_ref=False) File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 741, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 614, in _TensorTensorConversionFunction % (dtype.name, t.dtype.name, str(t))) ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("embedding_1/random_uniform:0", shape=(20000, 200), dtype=float32)'

I have these packages installed: [root@localhost sentiment]# pip list -l backports.ssl-match-hostname (3.4.0.2) backports.weakref (1.0rc1) bleach (1.5.0) blivet (0.61.15.59) Brlapi (0.6.0) cffi (1.6.0) chardet (2.2.1) configobj (4.7.2) configshell-fb (1.1.18) coverage (3.6b3) cryptography (1.3.1) cupshelpers (1.0) custodia (0.1.0) decorator (3.4.0) di (0.3) dnspython (1.12.0) enum34 (1.0.4) ethtool (0.8) firstboot (19.5) fros (1.0) funcsigs (1.0.2) gssapi (1.2.0) h5py (2.7.0) html5lib (0.9999999) idna (2.0) iniparse (0.4) initial-setup (0.3.9.36) ipaclient (4.4.0) ipaddress (1.0.16) ipalib (4.4.0) ipaplatform (4.4.0) ipapython (4.4.0) IPy (0.75) javapackages (1.0.0) jwcrypto (0.2.1) Keras (2.0.6) kitchen (1.1.1) kmod (0.1) langtable (0.0.31) lxml (3.2.1) Markdown (2.6.8) mock (2.0.0) netaddr (0.7.5) netifaces (0.10.4) ntplib (0.3.2) numpy (1.13.1) pbr (3.1.1) perf (0.1) pip (8.1.2) ply (3.4) policycoreutils-default-encoding (0.1) protobuf (3.3.0) pyasn1 (0.1.9) pycparser (2.14) pycups (1.9.63) pycurl (7.19.0) pygobject (3.14.0) pygpgme (0.3) pyinotify (0.9.4) pykickstart (1.99.66.10) pyliblzma (0.5.3) pyOpenSSL (0.13.1) pyparsing (1.5.6) pyparted (3.9) pysmbc (1.0.13) python-augeas (0.5.0) python-dateutil (1.5) python-dmidecode (3.10.13) python-ldap (2.4.15) python-meh (0.25.2) python-nss (0.16.0) python-yubico (1.2.3) pytz (2016.6.1) pyudev (0.15) pyusb (1.0.0b1) pyxattr (0.5.1) PyYAML (3.12) qrcode (5.0.1) requests (2.6.0) rtslib-fb (2.1.57) scikit-learn (0.18.2) scipy (0.19.1) seobject (0.1) sepolicy (1.1) setroubleshoot (1.1) setuptools (0.9.8) six (1.10.0) sklearn (0.0) slip (0.4.0) slip.dbus (0.4.0) SSSDConfig (1.14.0) targetcli-fb (2.1.fb41) tensorflow (1.2.1) Theano (0.9.0) urlgrabber (3.10) urllib3 (1.10.2) urwid (1.1.1) Werkzeug (0.12.2) wheel (0.29.0) yum-langpacks (0.4.2) yum-metadata-parser (1.1.4) You are using pip version 8.1.2, however version 9.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

durika commented 7 years ago

It works with ubuntu 16.04 and default python 2.7

shivdhar commented 6 years ago

It seems to be a Keras 2 vs 1 incompatibility issue.

pip install "keras<2"

installs the highest Keras 1 version. I only needed to change the Keras version; TensorFlow 1.4.1 (and everything else - scipy, numpy, etc.) works with the code.