Open bluesky632 opened 6 years ago
可尝试改一下以下代码:
num_words=len(cn_model.vocab)
你好,对于这个情感分析任务,我有几个疑问。
1.我们将一条短评,进行分词,然后将利用预训练的word2vec构建构建这个短评的embeddding,让这个embuding作为LSTM的输入项.而LSTM输入项应该是分词词语的向量才对吧,因为只有词语和词语之间才会有时间序列上的关联,而句子和句子间是没有的。
例如下图中的 x
我也是初学者,我的理解是:第一个问题,输入是词语的向量,也可以是单个字的向量,第二个问题我理解的也不是很透彻,展开图里的第二层是每个输入之间都是由联系的,并且LSTM的原理里,是对信息进行取舍判断的,我觉得是在一整句话的基础上,进行极性的判断,我也不是太清楚,如果有错误的话,请包涵。
谢谢你的解答
我的理解是 单个字,两个字,三个字,都可以是词语,只要是word2vec上可以将其转换成词向量的,都可以将这个词用词向量来表示。
一条短评中各个词语之间是有信息之间的关系,而一条短评和另一条短评之间是没有什么什么信息之间的联系的吧?我们embedding之后,把短评句子构建的矩阵作为输入项x,那么怎么应该下 x
num_words=len(cn_model.vocab)
这是把词典中所有的词都放进去了么?@davidqing2000
请查看更新的debug之后的代码,解决了这个bug, 对于分类问题,可以看看我讲的机器学习中的数学: https://www.bilibili.com/video/av53400966
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)
bug已解决,是TensorFlow框架问题,代码没有问题
可参考https://github.com/tensorflow/tensorflow/pull/33097/files#diff-bfba746b141112f6e8f852c873e01b84R92
您好,非常感谢您的分享,我在听完您的视频后,运行了程序, 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?