Search code examples
python-3.xneo4jneo4j-python-driver

Add Word Embedding in Neo4j


Trying to add word embedding to my nodes using Unwind.

The code:

result = conn.query("MATCH (m:Word) WHERE NOT exists(m.embedding) RETURN m")
nodes = [x['m'] for x in result]
        for dic in nodes:
            dic['embedding'] = list(np.round(model_st.encode(dic['name'], show_progress_bar=False), 3))
query_update = f""" UNWIND $nodes as res_dict
                    MATCH (n:Word {{name: res_dict.name}})
                    SET n.embedding = res_dict.embedding
                """
self.conn.query(query=query_update, parameters={'nodes': nodes})

But when I try to run it, I get the following error:

Parameters of type float32 are not supported

How can I achieve it in Python? Is there a better way to set an embedding to a node?


Solution

  • I was able to add embedding using the following code

    embedding = list(np.round(model_st.encode(name, show_progress_bar=False).astype(np.float64),3))
    

    I still think it's weird neo4j don't have a native way to create embedding (using apoc/gds)