0

I am confused about one code line in Github, in line 295

text = text[..., :5]

I was wondering if it take the text list from index=0 to 5.

However, it didn't work, I tried to use text = ["an oil painting of a corgi"] as input, it shows error, string indices must be integers

I am still confused about it, what the meaning of it?

tripleee
  • 175,061
  • 34
  • 275
  • 318
4daJKong
  • 1,825
  • 9
  • 21
  • That works for Numpy arrays. See https://numpy.org/doc/stable/user/basics.indexing.html – AKX May 04 '23 at 08:55
  • btw. you can click on the line in github to select it and have it referenced under the link: https://github.com/lucidrains/DALLE2-pytorch/blob/main/dalle2_pytorch/dalle2_pytorch.py#L295 – kosciej16 May 04 '23 at 09:01
  • @AKX Hi, I tried to use `text = np.array(["an oil painting of a corgi"])` as input, it still shows error, `'bool' object has no attribute 'cumsum' ` – 4daJKong May 04 '23 at 09:03
  • @kosciej16 thanks, but I still have no idea about the format of input, what should I input if the code could work? – 4daJKong May 04 '23 at 09:06
  • @4daJKong _What_ shows that error? – AKX May 04 '23 at 09:08
  • @AKX `text = np.array(["an oil painting of a corgi"]) ` `text = text[..., :5]` `print(text)` – 4daJKong May 04 '23 at 09:10
  • This is not valid Python. I'm adding the [tag:numpy] tag, where this is almost certainly a duplicate. – tripleee May 04 '23 at 09:31
  • @4daJKong those 3 lines definitely do _not_ raise `'bool' object has no attribute 'cumsum'` on my machine. – AKX May 04 '23 at 10:19

1 Answers1

1

How to make it work? As syntax you wrote takes first 5 rows from every column, you could fill column with rows, where every row is a letter:

res = np.array(list("an oil painting of a corgi"))
print(res[..., :5])

Is there any python magic behind? Not really, everything can be easily achieved by us:

class MyClass:
    def __getitem__(self, key):
        # ofc key could be just a single value
        a, b = key
        if a is Ellipsis:
            if isinstance(b, slice):
                print("HA!")


res = MyClass()
res[..., :5]
kosciej16
  • 6,294
  • 1
  • 18
  • 29