You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have a custom TextVectorization layer, it doesn't use any nn. layers, it's just a dictionary of words that is used to fill in a torch.zeros vector. I want it to be baked in so I put it as the first layer in my model.
Unfortunately, it doesn't work with torchinfo.summary(mode, input_shape(["test"] * batch_size).
Which is bothersome.
The model.forward(["this is a test"]) works just fine so I am somewhat confident that it's an issue with torchinfo not being able to handle my custom layer. It worked fine without it (with random int tokens as input data).
Code and Setup TextVectorization
classTextVectorization(nn.Module):
def__init__(self, max_vocabulary, max_tokens):
super(TextVectorization, self).__init__()
self.max_tokens=max_tokensself.max_vocabulary=max_vocabularyself.word_dictionary=dict()
self.dictionary_size=0defadapt(self, dataset):
word_frequencies=defaultdict(int)
fortextindataset:
forwordintext[0].split():
word_frequencies[word] +=1# Sort the dictionary by word frequencies in descending ordersorted_word_frequencies=dict(sorted(word_frequencies.items(),
key=lambdaitem: item[1],
reverse=True)
)
# Take the top N most frequent wordsmost_frequent=list(sorted_word_frequencies.items())[:self.max_vocabulary]
self.dictionary_size=len(most_frequent)
# Note starting at 2 since 0 (padding) and 1 (missing) are reservedforword_value, (word, frequency) inenumerate(most_frequent, 2):
self.word_dictionary[word] =word_valuedefvocabulary_size(self):
returnself.dictionary_sizedefdictionary(self):
returnself.word_dictionarydefforward(self, batch_x):
batch_text_vectors=torch.zeros((len(batch_x), self.max_tokens), dtype=torch.int32)
fori, textinenumerate(batch_x):
# Split the text and tokenize itwords=text.split()[:self.max_tokens]
forpos, wordinenumerate(words):
batch_text_vectors[i, pos] =self.word_dictionary.get(word, 1)
returnbatch_text_vectors
This is related to #254 and probably also #280. The code expects "tensor-like" input, not strings. Even if this isn't fixed, the error should definitely be caught earlier and stated more clearly. As it stands, process_input doesn't know what to do with this kind of input, and there are related issues coming from traverse_input_data.
I would love to work on this. Does anyone have opinions on what should be done: new functionality to handle text input, or a better error message?
Either solution sounds good to me. The better error message sounds like a good place to start, and then handling text input would be a good followup. PRs are definitely welcome!
Describe the bug
I have a custom TextVectorization layer, it doesn't use any
nn.
layers, it's just a dictionary of words that is used to fill in atorch.zeros
vector. I want it to be baked in so I put it as the first layer in my model.Unfortunately, it doesn't work with
torchinfo.summary(mode, input_shape(["test"] * batch_size)
.Which is bothersome.
The
model.forward(["this is a test"])
works just fine so I am somewhat confident that it's an issue with torchinfo not being able to handle my custom layer. It worked fine without it (with random int tokens as input data).Code and Setup
TextVectorization
Model
Summary
Runtime Error
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: