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,)?