Giter Site home page Giter Site logo

Comments (11)

nelson-liu avatar nelson-liu commented on May 31, 2024 7

Thanks for the issue.

Torchtext needs to convert the string number to an int or float somewhere down the line and it currently doesn't do this. A quick fix would be to manually add a pipeline to the postprocessing argument that converts everything in the TARGETS field to int. With a slightly modified version of your code:

Edit: just noticed that your example uses doubles. changed my code accordingly

(tab separated file)

$ cat test.txt
1.1   test string
1.2   test string2
1.3   test string3

The following works on my machine in the meantime while we patch this:

In [1]: import torch

In [2]: from torchtext import data

In [3]: TEXT = data.Field(batch_first=True)

In [4]: TARGETS = data.Field(sequential=False, tensor_type=torch.DoubleTensor, batch_first=True, use_vocab=False, postprocessing=data.Pipeline(lambda x: float(x)))

In [5]: fields = [('targets', TARGETS), ('text', TEXT)]

In [6]: dataset = data.TabularDataset(path="test.txt", format="tsv", fields=fields)

In [7]: TEXT.build_vocab(dataset)

In [8]: train_iter = data.Iterator(dataset, batch_size=1, sort_key=lambda x: len(x.text), shuffle=True)

In [9]: batch = next(iter(train_iter))

In [10]: batch.targets
Out[10]: 
Variable containing:
 1.3000
[torch.cuda.DoubleTensor of size 1 (GPU 0)]

Hope that helps.

from text.

greed2411 avatar greed2411 commented on May 31, 2024 3

for me the above one, didn't work.
if anyone is still wondering,
change postprocessing=data.Pipeline(lambda x: float(x)) to preprocessing= lambda x: float(x)
that made it work for me (pytorch 0.4 and torchtext 0.2.3)

from text.

giovannipcarvalho avatar giovannipcarvalho commented on May 31, 2024 2

@greed2411 you don't even need the lambda. Field(use_vocab=False, preprocessing=float) is enough.

edit: It seems to work for RawField but not Field. 😕
edit2: ah, forgot to set sequential=False.

from text.

finiteautomata avatar finiteautomata commented on May 31, 2024 2

data.LabelField(dtype = torch.float, use_vocab=False, preprocessing=float) does the trick as data.LabelField already sets use_sequential=False (and also removes <unk> token)

from text.

ashudeep avatar ashudeep commented on May 31, 2024

Found use_vocab argument 😞

from text.

ashudeep avatar ashudeep commented on May 31, 2024

Even after setting use_vocab=False. I get

RuntimeError: already counted a million dimensions in a given sequence. Most likely your items are also sequences and there's no way to infer how many dimension should the tensor have

It is the same error that one gets when you try to do torch.DoubleTensor('1.2'). Is there something I am doing wrong?

from text.

ashudeep avatar ashudeep commented on May 31, 2024

Thanks for the solution @nelson-liu

from text.

nelson-liu avatar nelson-liu commented on May 31, 2024

could you leave this open for now --- there is a bug behind this that would be nice to track (the fact that we do not actually convert values with use_vocab=False to numbers). Thanks!

from text.

ashudeep avatar ashudeep commented on May 31, 2024

Sure, I agree.

from text.

jekbradbury avatar jekbradbury commented on May 31, 2024

Yeah, I was originally imagining that values would be provided as Python numerical types -- but that isn't really consistent with the nature of the library as loading mostly text values. Certainly if it sees strings it should convert them!

from text.

ImtiazKhanDS avatar ImtiazKhanDS commented on May 31, 2024

If both my fields like target and source are sequences then also we get the same error , any idea on how to resolve this?

from text.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.