Tensorflow 'NoneType' object has no attribute 'dtype' when trying to use StringLookup with input from custom Lambda function

I’m trying to use a Lambda function to convert given values and then encode them using StringLookup. I convert the values using find_cluster function below. It gives the following exception though:

~/Dev/anaconda3/envs/Python36/lib/python3.6/site-packages/keras/layers/preprocessing/index_lookup.py in call(self, inputs)
    628 
    629     # TODO(b/190445202): remove output rank restriction.
--> 630     if lookups.shape.rank > 2:
    631       raise ValueError(
    632           "Received input shape {}, which would result in output rank {}. "

TypeError: '>' not supported between instances of 'NoneType' and 'int'

The code:

def find_cluster(arr):
    arr = arr.astype('str')
    
    result = []
    for el in arr:
        all_mappings = node_cluster_mapping_v3_with_count[el] if el in node_cluster_mapping_v3_with_count else []
        filtered_mappings = [e[0] for e in all_mappings[:min(len(all_mappings),3)]]
        result.extend(filtered_mappings)
        
    print(result)
    
    tensor = tf.convert_to_tensor(result)
    
    return tensor
    
FindCluster = keras.layers.core.Lambda(lambda x: tf.numpy_function(find_cluster,[x],'string'))
# FindCluster = keras.layers.core.Lambda(lambda x: print(x.shape))

cluster_names = unique_labels + ['None']

graph_input = Input(shape=(), dtype='string', name='graph_input')                           
x = FindCluster(graph_input)
x = tf.keras.layers.StringLookup(vocabulary=cluster_names, output_mode='multi_hot')(x)
m = Model(inputs=graph_input, outputs=x)

res = m.predict(['Atom','Florence'])

The result when I commented out StringLookup layer is:

array([b'Science-Chemistry', b'Art-Fashion', b'Art-Painting',
       b'Art-Sculpting-Artist'], dtype=object)

When printed shape of this in find_cluster function it’s (4,1). When I printed shape of Tensor x in FindCluster Lambda layer it’s (None,). May it be related to this? How can I create a tensor from a 1d numpy array with shape (None,)?

Hi @cuneyttyler,

Sorry for the delay in response.
It seems the issue is due to mismatch of shape between lambda layer and function find_cluster output. To resolve this I suggest to flattened the function output into a one-dimensional tensor by using this tf.convert_to_tensor(result, dtype=tf.string) or reshape input tensor inside the Lambda function to ensure it has the correct shape in StringLookup by explicitly reshaping the result from find_cluster to (batch_size, 1).

Thank You.