1
import tensorflow as tf
import tensorflow.keras.layers as nn
import numpy as np

class Base(tf.keras.Model):
    def __init__(self):
        super(Base, self).__init__()
        
        self.user_emb = nn.Embedding(20000, 128, input_length=1)
        self.item_emb = nn.Embedding(10000, 128, input_length=1)

        self.test_dense = nn.Dense(80, activation=None)
        self.final_dense = nn.Dense(1)

    def call(self, inputs, **kwargs):
        user, item = inputs
        user_emb = self.user_emb(user)
        item_emb = self.item_emb(item)

        join_emb = tf.concat([user_emb, item_emb], -1)

        logit = self.test_dense(join_emb)
        logit = tf.squeeze(self.final_dense(logit))
        output = tf.nn.sigmoid(logit)

        return output

# Main
if __name__ == '__main__':

    model = Base()
    model.compile(loss='binary_crossentropy', optimizer='adam',
                  metrics=[])

    a = np.random.randint(1,20000,size=(10000))
    b = np.random.randint(1, 10000, size=(10000))
    y = np.random.randint(0, 2, size=(10000))
    X = [a, b]
    model.fit(X, y, epochs=1, batch_size=32)

When I run the above code, I get

TypeError: object of type 'NoneType' has no len().

I use Tensorflow 2.0.0, python 3.6

error

2 Answers 2

1

I downgraded my Tensorflow version to 2.0.0 and ran the your code which produced the same error.

When I upgraded to version 2.4.1 it works perfectly fine.

You can upgrade Tensorflow like this:

pip install tensorflow==2.4.1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I found the solution. When I remove tf.squeeze() of last layer, it works fine.
1

The solution is to remove tf.squeeze() of last layer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.