2

How do I convert a list of strings into a string/character tensor in Pytorch?

Related example with numpy:

import numpy as np

mylist = ["this","is","my","list"]

np.array([mylist])

Returns:

array([['this', 'is', 'my', 'list']], dtype='<U4')

However, in pytorch:

torch.tensor(mylist)

Returns:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-156-36722d81da09> in <module>
----> 1 torch.tensor(mylist)

ValueError: too many dimensions 'str'

A tensor is a multi-dimensional array, so I'm assuming this is possible pytorch.

Note: this post does not answer my question

1
  • 1
    According to the docs there's no string type tensor. Commented Dec 15, 2019 at 8:20

1 Answer 1

6

There is no string tensor so you cannot directly convert to pytorch tensor of strings.

Alternative, you can convert the string to ASCII char values and save that as a Tensor.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.