解决TypeError: in method ‘IndexFlat_add‘, argument 3 of type ‘float const *‘
在使用faiss时报错如题`TypeError: in method ‘IndexFlat_add’, argument 3 of type ‘float const*’`。```pythonbeat_emb = normalize(beat_emb, norm = "l2")hidden_size = 128gpu_index = faiss.IndexFlatIP(hidden_size)gp
·
一、问题描述
在使用faiss时报错如题TypeError: in method ‘IndexFlat_add’, argument 3 of type ‘float const*’
。
beat_emb = normalize(beat_emb, norm = "l2")
hidden_size = 128
gpu_index = faiss.IndexFlatIP(hidden_size)
gpu_index.add(beat_emb)
top_N = 50
predicts = dict()
# D: distance距离矩阵, I: index索引矩阵 I.shape: (user_num, emb_dim)
D, I = gpu_index.search(user_emb, top_N)
beat_data = beat_data.reset_index(drop = True)
# 报错
TypeError: in method ‘IndexFlat_add’, argument 3 of type ‘float const*’
二、解决方法
在gpu_index.add(beat_emb)
时,即建立index后添加数据进index时,ndarrays数组的类型必须为float32
,所以在add
之前加一句beat_emb = beat_emb.astype('float32')
类型转换即可。
更多推荐
已为社区贡献25条内容
所有评论(0)