clab / clab/dynet

Initializing Lookup Matrix for Tensors

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

Description

Hi,

When I tried initializing a 4-d tensor with an appropriately shaped numpy array using init_from_array, I get "TypeError: 'only size-1 arrays can be converted to Python scalars"

This was the code snippet that created the bug:
```
def loadEmbedding_rand(path, word_indices):
n = len(word_indices)
m = 300 # Word Embedding dimensions
emb = np.empty((n, m), dtype=np.float32)
emb[:, :] = np.random.normal(size=(n, m))
# Explicitly assign embedding of to be zeros.
emb[0:2, :] = np.zeros((1, m), dtype="float32")
with open(path, 'r') as f:
for i, line in enumerate(f):
s = line.split()
if s[0] in word_indices:
try:
emb[word_indices[s[0]], :] = np.asarray(s[1:])
except Exception as e:
print(e)
continue
return np.reshape(emb, (n,1,1,m))

word_embeds = loadEmbedding_rand("../wiki.en.vec", word_indices)
wembed = model.add_lookup_parameters((len(word_indices), 1, 1, EMB_SIZE))
wembed.init_from_array(word_embeds)
```

Initialization using
`wembed = model.add_lookup_parameters((len(word_indices), 1, 1, EMB_SIZE), init = dy.NumpyInitializer(word_embeds))`, works however.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by reproducing the reported 4-D array case through init_from_array and compare it with the working dy.NumpyInitializer path. Trace the tensor initialization entry points involved and add coverage so a correctly shaped NumPy array initializes without the reported TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, numpy, python
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.