Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] AttributeError: 'tuple' object has no attribute 'layer' #152

Closed
mahdisnapp opened this issue Jul 15, 2019 · 24 comments
Closed

[BUG] AttributeError: 'tuple' object has no attribute 'layer' #152

mahdisnapp opened this issue Jul 15, 2019 · 24 comments
Assignees
Labels
bug Something isn't working

Comments

@mahdisnapp
Copy link

first of all thanks for your impressive work and effort. I faced a problem when using your package in google colab. while I want to use bert embedding with this command:
bert_embedding = BERTEmbedding(bert_model_path,
task=kashgari.CLASSIFICATION,
sequence_length=128)
I got this error:
AttributeError: 'tuple' object has no attribute 'layer'
thanks in advance for your help.

@mahdisnapp mahdisnapp added the bug Something isn't working label Jul 15, 2019
@BrikerMan
Copy link
Owner

Are you working on a multi-label classification task? I might there is a small issue in the documents.

@BrikerMan
Copy link
Owner

BrikerMan commented Jul 15, 2019

Long story short, if you import keras_bert from setting the environment variable, it will cause this issue. You can fix it by importing Kashgari at the top or add the two lines at the top. Basically, remember to import kashgari first.

import os
os.environ['TF_KERAS'] = '1'

@BrikerMan BrikerMan changed the title [BUG] [BUG] AttributeError: 'tuple' object has no attribute 'layer' Jul 15, 2019
@mahdisnapp
Copy link
Author

thanks for response, to answer your question, I am working on multi-class classification not multi-label classification,
base on your suggestion I will import kashgari first then import keras_bert and let you know if it is fixed.
Important note: I want to use tokenizer from keras_bert.
again thanks .

@BrikerMan
Copy link
Owner

I am working on to make it easier to access the keras_bert tokenizer. Will release 5.0.1 today. Check out this commit 22ad76f

@mahdisnapp
Copy link
Author

thanks for your help, problem has been solved. I noticed a note in your tutorial for BERT embeddings, you tokenized sentences with BERT tokenizer but did not pass them into your neural net model, you just feed the raw text as input data.
tokenizer = Tokenizer(token_dict) sentences_tokenized = [] for sentence in sentences: sentence_tokenized = tokenizer.tokenize(sentence) sentences_tokenized.append(sentence_tokenized)

but then
train_x, train_y = sentences[:2], labels[:2] validate_x, validate_y = sentences[2:], labels[2:]

@BrikerMan
Copy link
Owner

BrikerMan commented Jul 15, 2019

Fixed the typo and released the 0.5.1 version, it is much easier to use BERT tokenizer. Check it out, document

@mahdisnapp
Copy link
Author

dear BrikerMan
now we have other main error, after update, we got this error
tokenizer = bert_embed.tokenizer
'BERTEmbedding' object has no attribute 'tokenizer'

@BrikerMan
Copy link
Owner

BrikerMan commented Jul 15, 2019 via email

@mahdisnapp
Copy link
Author

yes I updated my kashgari just while you informed me.

@BrikerMan
Copy link
Owner

I just tried and it works. Please make sure you have imported the latest version if you started a kernel like jupyter before updating, it will require a kernel restart to import latest version. You can confirm by

print(kashgari.__version__)

Here is my demo: colab link

@mahdisnapp
Copy link
Author

errors have been solved, thank you again.

@mahdisnapp
Copy link
Author

dear BrikerMan,
your packages worked well and I have developed my models with success until the error occurred today,
when I tried to fit model I got this error:
mdel.fit(x_train, y_train, x_validate, y_validate, epochs = 5, batch_size = 32)

Keras: operands could not be broadcast together with shapes

@BrikerMan
Copy link
Owner

dear BrikerMan,
your packages worked well and I have developed my models with success until the error occurred today,
when I tried to fit model I got this error:
mdel.fit(x_train, y_train, x_validate, y_validate, epochs = 5, batch_size = 32)

Keras: operands could not be broadcast together with shapes

I need a little bit more details with this bug. Please provide the model config and data samples.

@mahdisnapp
Copy link
Author

I used BiLSTM model from kashgari.tasks.classification, my input data (x_train and x_validate) are tokenized texts and outpus (y_train, y_validate) are labels,
note : i used BERTEmbedding and it's tokenization method
my training size is 450 and validate size is 50

@BrikerMan
Copy link
Owner

Maybe you could provide the code, how you build the model, and sample data. Or just give me a colab notebook which could reproduce the issue.

@BrikerMan
Copy link
Owner

It seems the issue with data, please run this code.

print(x_train[0])
print(x_validate[0])
print(y_train[0])
print(y_validate[0])

@mahdisnapp
Copy link
Author

there were no problem with shape of training and validation sets, I provide link to my colab
colab

@BrikerMan
Copy link
Owner

there were no problem with shape of training and validation sets, I provide link to my colab
colab

I might know the issue, try to convert data to list rather than np.array.

mdel.fit(list(x_train), list(y_train), list(x_validate),list(y_validate), epochs = 5, batch_size = 32)

@mahdisnapp
Copy link
Author

yes your solution worked, may I ask what is the reason the model did not support numpy array as an input?
again thanks for your patience and kindness to help us.

@BrikerMan
Copy link
Owner

BrikerMan commented Jul 16, 2019

I need to add all your corpus together to build token dict and label dict. So here is one important line

self.embedding.analyze_corpus(x_train + x_validate, y_train + y_validate)

But when you add two numpy array, they must be able to broadcast

I have tried mainly on my build in corpus and haven't tested on numpy array input. So, this is a bug and will fix in the next version by converting x_train and x_validate to list before adding them.

@BrikerMan
Copy link
Owner

@mahdisnapp Maybe you guys could help me write some blog about how you use Kashgari~ That means a lot. I hope more people know about this project.

@mahdisnapp
Copy link
Author

Dear BrikerMan
your work is very impressive and I want to help you to improve kashgari performance and add new features to it. at the moment I could help with bugs or add other models. if you need help just let me know. and if you want I could mail you for further talks

@BrikerMan
Copy link
Owner

Dear BrikerMan
your work is very impressive and I want to help you to improve kashgari performance and add new features to it. at the moment I could help with bugs or add other models. if you need help just let me know. and if you want I could mail you for further talks

Let's start with fixing bugs and documents, also feel free to add new features and create pull request. here is the contributing guide.

@alexwwang
Copy link
Collaborator

alexwwang commented Jul 16, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants