aespresso / chinese_sentiment

用tensorflow进行中文自然语言处理的情感分析
441 stars 161 forks source link

predict_sentiment(text) #2

Open bluesky632 opened 6 years ago

bluesky632 commented 6 years ago

您好,非常感谢您的分享,我在听完您的视频后,运行了程序, test_list = [ '酒店设施不是新的,服务态度很不好', '酒店卫生条件非常不好', '床铺非常舒适', '房间很凉,不给开暖气', '房间很凉爽,空调冷气很足', '酒店环境不好,住宿体验很不好', '房间隔音不到位' , '晚上回来发现没有打扫卫生', '因为过节所以要我临时加钱,比团购的价格贵' ] for text in test_list: predict_sentiment(text) 运行到上面的代码时报错了, 报错信息:InvalidArgumentError (see above for traceback): indices[0,231] = 83086 is not in [0, 50000) [[Node: embedding/Gather = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](embedding/embeddings/read, embedding/Cast)]] 不知道是不是tensorflow版本的问题,请问你用的是哪个版本的tensorflow?

davidqing2000 commented 5 years ago

可尝试改一下以下代码:

num_words = 50000 --替换为以下-》

num_words=len(cn_model.vocab)

bbbubblegum commented 5 years ago

你好,对于这个情感分析任务,我有几个疑问。 1.我们将一条短评,进行分词,然后将利用预训练的word2vec构建构建这个短评的embeddding,让这个embuding作为LSTM的输入项.而LSTM输入项应该是分词词语的向量才对吧,因为只有词语和词语之间才会有时间序列上的关联,而句子和句子间是没有的。 例如下图中的 x , x , x<t+1> ,应该是词语向量,而不是一个个embedding。 fb9ee87ffc5345babef134df0ab7259 2.如果LSTM输入的是词语向量,而LSTM中每个一LSTM单元的输出层都是二分类的话,是对一个一个词语去判断他的情感极性么?我觉得应该是整个LSTM循环之后再连接一个输出层二分类这样才是合理吧,然后如果是这样的话,我们对每一条短评都建立一个LSTM神经网络模型和二分类的输出层,最后一共m个短评构建出一个LSTM神经网络集群,从而去定义整个集群的损失函数sum(loss)/m么? 很抱歉,我是个初学者,在对LSTM和Word2vec上得理解得不够准确和透彻,希望得到您的解答,谢谢。

bluesky632 commented 5 years ago

我也是初学者,我的理解是:第一个问题,输入是词语的向量,也可以是单个字的向量,第二个问题我理解的也不是很透彻,展开图里的第二层是每个输入之间都是由联系的,并且LSTM的原理里,是对信息进行取舍判断的,我觉得是在一整句话的基础上,进行极性的判断,我也不是太清楚,如果有错误的话,请包涵。

bbbubblegum commented 5 years ago

谢谢你的解答 我的理解是 单个字,两个字,三个字,都可以是词语,只要是word2vec上可以将其转换成词向量的,都可以将这个词用词向量来表示。 一条短评中各个词语之间是有信息之间的关系,而一条短评和另一条短评之间是没有什么什么信息之间的联系的吧?我们embedding之后,把短评句子构建的矩阵作为输入项x,那么怎么应该下 x , x,x<t+1>都应该是该句子的词向量吧?而lstm神经网络中,我们不需要输出y,因为我们不需要知道每个词语是什么情感极性或者是其他的东西,只需要用他的激活项和词向量来控制门的开闭,最后整个lstm连接一个二分类的输出层,输出句子的情感极性。 我理解得到也可能不到位,望见谅。

Faith-Uchiha commented 5 years ago

num_words=len(cn_model.vocab)

这是把词典中所有的词都放进去了么?@davidqing2000

aespresso commented 5 years ago

请查看更新的debug之后的代码,解决了这个bug, 对于分类问题,可以看看我讲的机器学习中的数学: https://www.bilibili.com/video/av53400966

LiAI-tech commented 5 years ago

开始训练

model.fit(X_train, y_train, validation_split=0.1, epochs=20, batch_size=128, callbacks=callbacks)

报错如下: ValueError Traceback (most recent call last) E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 526 as_ref=input_arg.is_ref, --> 527 preferred_dtype=default_dtype) 528 except TypeError as err:

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors) 1295 if ret is None: -> 1296 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 1297

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, asref) 285 = as_ref --> 286 return constant(v, dtype=dtype, name=name) 287

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in constant(value, dtype, shape, name) 226 return _constant_impl(value, dtype, shape, name, verify_shape=False, --> 227 allow_broadcast=True) 228

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast) 264 value, dtype=dtype, shape=shape, verify_shape=verify_shape, --> 265 allow_broadcast=allow_broadcast)) 266 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast) 436 if values is None: --> 437 raise ValueError("None values not supported.") 438 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last) E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 540 observed = ops.internal_convert_to_tensor( --> 541 values, as_ref=input_arg.is_ref).dtype.name 542 except ValueError as err:

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx, accept_composite_tensors) 1295 if ret is None: -> 1296 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref) 1297

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in _constant_tensor_conversion_function(v, dtype, name, asref) 285 = as_ref --> 286 return constant(v, dtype=dtype, name=name) 287

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in constant(value, dtype, shape, name) 226 return _constant_impl(value, dtype, shape, name, verify_shape=False, --> 227 allow_broadcast=True) 228

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\constant_op.py in _constant_impl(value, dtype, shape, name, verify_shape, allow_broadcast) 264 value, dtype=dtype, shape=shape, verify_shape=verify_shape, --> 265 allow_broadcast=allow_broadcast)) 266 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape, allow_broadcast) 436 if values is None: --> 437 raise ValueError("None values not supported.") 438 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)

in 4 epochs=20, 5 batch_size=128, ----> 6 callbacks=callbacks) E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 726 max_queue_size=max_queue_size, 727 workers=workers, --> 728 use_multiprocessing=use_multiprocessing) 729 730 def evaluate(self, E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs) 672 validation_steps=validation_steps, 673 validation_freq=validation_freq, --> 674 steps_name='steps_per_epoch') 675 676 def evaluate(self, E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs) 187 # function we recompile the metrics based on the updated 188 # sample_weight_mode value. --> 189 f = _make_execution_function(model, mode) 190 191 # Prepare validation data. Hold references to the iterator and the input list E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training_arrays.py in _make_execution_function(model, mode) 563 if model._distribution_strategy: 564 return distributed_training_utils._make_execution_function(model, mode) --> 565 return model._make_execution_function(mode) 566 567 E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _make_execution_function(self, mode) 2182 def _make_execution_function(self, mode): 2183 if mode == ModeKeys.TRAIN: -> 2184 self._make_train_function() 2185 return self.train_function 2186 if mode == ModeKeys.TEST: E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\engine\training.py in _make_train_function(self) 2114 # Training updates 2115 updates = self.optimizer.get_updates( -> 2116 params=self._collected_trainable_weights, loss=self.total_loss) 2117 # Unconditional updates 2118 updates += self.get_updates_for(None) E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\optimizers.py in get_updates(self, loss, params) 474 475 def get_updates(self, loss, params): --> 476 grads = self.get_gradients(loss, params) 477 self.updates = [] 478 E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\keras\optimizers.py in get_gradients(self, loss, params) 90 """ 91 grads = K.gradients(loss, params) ---> 92 if None in grads: 93 raise ValueError('An operation has `None` for gradient. ' 94 'Please make sure that all of your ops have a ' E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\math_ops.py in tensor_equals(self, other) 1334 return gen_math_ops.equal(self, other, incompatible_shape_error=False) 1335 else: -> 1336 return gen_math_ops.equal(self, other) 1337 else: 1338 # In legacy graph mode, tensor equality is object equality E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\ops\gen_math_ops.py in equal(x, y, incompatible_shape_error, name) 3624 _, _, _op = _op_def_lib._apply_op_helper( 3625 "Equal", x=x, y=y, incompatible_shape_error=incompatible_shape_error, -> 3626 name=name) 3627 _result = _op.outputs[:] 3628 _inputs_flat = _op.inputs E:\Anaconda3\envs\tensorflow\lib\site-packages\tensorflow_core\python\framework\op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords) 543 raise ValueError( 544 "Tried to convert '%s' to a tensor and failed. Error: %s" % --> 545 (input_name, err)) 546 prefix = ("Input '%s' of '%s' Op has type %s that does not match" % 547 (input_name, op_type_name, observed)) ValueError: Tried to convert 'y' to a tensor and failed. Error: None values not supported. 是因为TensorFlow版本不同导致的吗?
LiAI-tech commented 5 years ago

bug已解决,是TensorFlow框架问题,代码没有问题

可参考https://github.com/tensorflow/tensorflow/pull/33097/files#diff-bfba746b141112f6e8f852c873e01b84R92