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)' #5

Open monajalal opened 6 years ago

monajalal commented 6 years ago

When I run your code I get this error:

[jalal@goku twitter-sentiment-analysis]$  echo "This is a sample tweet to predict on" | python predict.py
Using TensorFlow backend.
2018-03-03 22:15:52.298930: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-03 22:15:52.298964: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-03 22:15:52.298974: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-03-03 22:15:52.298981: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-03 22:15:52.298987: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2018-03-03 22:15:52.521523: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 0 with properties: 
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.6705
pciBusID 0000:05:00.0
Total memory: 10.92GiB
Free memory: 9.92GiB
2018-03-03 22:15:52.742797: W tensorflow/stream_executor/cuda/cuda_driver.cc:523] A non-primary context 0x5626add8a580 exists before initializing the StreamExecutor. We haven't verified StreamExecutor works with that.
2018-03-03 22:15:52.743474: I tensorflow/core/common_runtime/gpu/gpu_device.cc:955] Found device 1 with properties: 
name: GeForce GTX 1080 Ti
major: 6 minor: 1 memoryClockRate (GHz) 1.6705
pciBusID 0000:06:00.0
Total memory: 10.92GiB
Free memory: 10.76GiB
2018-03-03 22:15:52.744161: I tensorflow/core/common_runtime/gpu/gpu_device.cc:976] DMA: 0 1 
2018-03-03 22:15:52.744178: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 0:   Y Y 
2018-03-03 22:15:52.744185: I tensorflow/core/common_runtime/gpu/gpu_device.cc:986] 1:   Y Y 
2018-03-03 22:15:52.744197: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:05:00.0)
2018-03-03 22:15:52.744206: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1045] Creating TensorFlow device (/gpu:1) -> (device: 1, name: GeForce GTX 1080 Ti, pci bus id: 0000:06:00.0)
/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/preprocessing/text.py:145: UserWarning: The `nb_words` argument in `Tokenizer` has been renamed `num_words`.
  warnings.warn('The `nb_words` argument in `Tokenizer` '
/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/engine/topology.py:1253: 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)
/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/engine/topology.py:1253: UserWarning: Update your `Embedding` call to the Keras 2 API: `Embedding(trainable=False, name="embedding_1", activity_regularizer=None, input_dtype="int32", mask_zero=False, input_dim=20000, batch_input_shape=[None, 100..., output_dim=200, input_length=1000, embeddings_initializer="uniform", embeddings_regularizer=None, embeddings_constraint=None)`
  return cls(**config)
Traceback (most recent call last):
  File "predict.py", line 41, in <module>
    main()
  File "predict.py", line 20, in main
    model = load_model('model.h5')
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/models.py", line 239, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/models.py", line 313, in model_from_config
    return layer_module.deserialize(config, custom_objects=custom_objects)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 139, in deserialize_keras_object
    list(custom_objects.items())))
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/models.py", line 1249, in from_config
    model.add(layer)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/models.py", line 442, in add
    layer(x)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/engine/topology.py", line 576, in __call__
    self.build(input_shapes[0])
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/layers/embeddings.py", line 101, in build
    dtype=self.dtype)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/engine/topology.py", line 400, in add_weight
    constraint=constraint)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 376, in variable
    v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 199, in __init__
    expected_shape=expected_shape)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 289, in _init_from_args
    initial_value, name="initial_value", dtype=dtype)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 611, in convert_to_tensor
    as_ref=False)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 676, in internal_convert_to_tensor
    ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
  File "/scratch/sjn/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 549, 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)'
[jalal@goku twitter-sentiment-analysis]$ 

How should I fix it?

arthurbarros commented 6 years ago

same here +1

MrcRjs commented 6 years ago

Similar error

/usr/local/lib/python3.6/site-packages/h5py/init.py:36: FutureWarning: Conversion of the second argument of issubdtype from float to np.floating is deprecated. In future, it will be treated as np.float64 == np.dtype(float).type. from ._conv import register_converters as _register_converters Using TensorFlow backend. /usr/local/lib/python3.6/site-packages/keras/preprocessing/text.py:172: UserWarning: The nb_words argument in Tokenizer has been renamed num_words. warnings.warn('The nb_words argument in Tokenizer ' /usr/local/lib/python3.6/site-packages/keras/engine/topology.py:1271: 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/local/lib/python3.6/site-packages/keras/engine/topology.py:1271: UserWarning: Update your Embedding call to the Keras 2 API: Embedding(trainable=False, name="embedding_1", activity_regularizer=None, input_dtype="int32", mask_zero=False, input_dim=20000, batch_input_shape=[None, 100..., output_dim=200, input_length=1000, embeddings_initializer="uniform", embeddings_regularizer=None, embeddings_constraint=None) 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/local/lib/python3.6/site-packages/keras/models.py", line 270, in load_model model = model_from_config(model_config, custom_objects=custom_objects) File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 347, in model_from_config return layer_module.deserialize(config, custom_objects=custom_objects) File "/usr/local/lib/python3.6/site-packages/keras/layers/init.py", line 55, in deserialize printable_module_name='layer') File "/usr/local/lib/python3.6/site-packages/keras/utils/generic_utils.py", line 144, in deserialize_keras_object list(custom_objects.items()))) File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 1412, in from_config model.add(layer) File "/usr/local/lib/python3.6/site-packages/keras/models.py", line 497, in add layer(x) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 592, in call self.build(input_shapes[0]) File "/usr/local/lib/python3.6/site-packages/keras/layers/embeddings.py", line 105, in build dtype=self.dtype) File "/usr/local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/usr/local/lib/python3.6/site-packages/keras/engine/topology.py", line 416, in add_weight constraint=constraint) File "/usr/local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 396, in variable v = tf.Variable(value, dtype=tf.as_dtype(dtype), name=name) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 235, in init constraint=constraint) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/variables.py", line 355, in _init_from_args initial_value, name="initial_value", dtype=dtype) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1014, in convert_to_tensor as_ref=False) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1104, in internal_convert_to_tensor ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 947, 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)'

VikramKharvi commented 5 years ago

We should have same version of keras installed in your system and the trained model.