handson-ml2 icon indicating copy to clipboard operation
handson-ml2 copied to clipboard

chapter 16 Sentiment Analysis

Open massamuel opened this issue 6 years ago • 2 comments

When I run this line of code I get this error

vocabulary = Counter()
for X_batch, y_batch in datasets["train"].batch(32).map(preprocess):
    for review in X_batch:
        vocabulary.update(list(review.numpy()))

 AttributeError: 'SparseTensor' object has no attribute 'to_tensor'

massamuel avatar Jan 25 '20 19:01 massamuel

Hi @massamuel ,

I just ran the code, and I cannot reproduce this error. Did you manage to find the cause? If not, could you please make sure you have the latest version of the notebook, and the latest version of all libraries, and run all the cells in order?

Thanks!

ageron avatar Apr 01 '20 00:04 ageron

Hi @massamuel ,

The issue is in your implementation of preprocess function. In the book-implementation last statement converts RaggedTensor to Tensor (this is where you are getting error). Root cause is that you applying .to_tensor()-attribute to SparseTensor instead of RaggedTensor. Probably you are using numpy/Python - native function to split a sentence to words. Use tf.strings.split instead.

IvanKuchin avatar Jul 03 '20 21:07 IvanKuchin