clab / clab/dynet

one_hot for sequences doesn't work as expected

Open
#1,530 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
3.4k
Forks
701
PR merge metrics
No merged PRs in 30d

Description

This may be my incorrect interpretation of the docstring (specifically of the term *batch*), but I expected the sequence-wise invocation of `dy.one_hot` to return a *sequence* of one-hot encodings...instead it appears to concatenate them all into a single vector.

```python
$ ipython --no-banner

In [1]: import dynet as dy

In [2]: dy.one_hot?
Docstring:
Inputs a one hot vector into the graph.
A one hot vecotr is a vector where one coordinate is 1 and everything else is 0
If ``idx`` is a list, returns a batch of one hot vectors where batch element ``b`` is one hot in ``idx[b]``

Args:
d (int): dimension of the vector(s)
idx (int,list): One hot index
device(string): Optional, device on which to create the expression.

Returns:
Expression: One hot vector(s) expression
Type: builtin_function_or_method

In [3]: dy.one_hot(10, 5).value()
Out[3]: [0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0]

In [4]: dy.one_hot(10, [5,3]).value()
Out[4]:
[0.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0]
```
whereas I would have expected something more like the output of:
```
In [5]: [dy.one_hot(10, x).value() for x in [5,3]]
Out[5]:
[[0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at the dy.one_hot API entry point and compare the scalar-index and list-index behavior shown in the issue. Check whether a list is intended to represent a batch or a sequence, then verify the result shape and documentation against that interpretation. Done means the behavior or documentation matches a clearly defined expectation for list inputs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.