ValueError:操作数无法与形状一起广播(11,384)(96,)(11,384)

问题描述 投票:-1回答:1
nlp = spacy.load('en_core_web_sm')

vecs1 = [] <br>
for qu1 in tqdm(list(train_df['question1'])):<br>
    doc1 = nlp(qu1) <br>
    mean_vec1 = np.zeros([len(doc1), 384])<br>
    for word1 in doc1:<br>
        vec1 = word1.vector<br>
        try: <br>
            idf = word2tfidf[str(word1)]<br>
        except:<br>
            idf = 0<br>
        # compute final vec<br>
        mean_vec1 += (vec1 * idf)<br>
    mean_vec1 = mean_vec1.mean(axis=0)<br>
    vecs1.append(mean_vec1)<br>
train_df['q1_feats_m'] = list(vecs1)

上面的代码我得到了这个错误


()最近的ValueError Traceback(最近一次调用) 18 idf = 0 19#计算最终的vec ---> 20 mean_vec1 + =(vec1 * idf) 21 mean_vec1 = mean_vec1.mean(axis = 0) 22 vecs1.append(mean_vec1)

ValueError:操作数无法与形状一起广播(11,384)(96,)(11,384)

python numpy spacy
1个回答
0
投票

我在这里发布这个以允许粘贴整个代码。

在不知道您的数据的情况下,很难说。添加以下打印命令行以验证阵列的形状。从错误看来你的vec1(96,)和idf(11,384)不能成倍增加!

nlp = spacy.load('en_core_web_sm')

vecs1 = []
for qu1 in tqdm(list(train_df['question1'])):
    doc1 = nlp(qu1)
    mean_vec1 = np.zeros([len(doc1), 384])
    for word1 in doc1:
        vec1 = word1.vector
        try: 
            idf = word2tfidf[str(word1)]
        except:
            idf = 0

        # Debug Prints
        print("idf Shape: %s" %idf.shape)
        print("vec1 Shape: %s" %vec1.shape)
        print("mean_vec1 Shape: %s" %mean_vec1.shape)


        # compute final vec
        mean_vec1 += (vec1 * idf)
    mean_vec1 = mean_vec1.mean(axis=0)
    vecs1.append(mean_vec1)
train_df['q1_feats_m'] = list(vecs1)