简体   繁体   English

无法广播numpy数组,但是.shape表示它们具有相同的形状

[英]Unable to broadcast numpy array, but .shape says they have the same shape

Here's the first part of my function that wants to generate text from a trained LSTM and a word embedding of dim 50. The problem comes when I try to set row i of X equal to the embedding vector y_embed. 这是我的函数的第一部分,它希望从受过训练的LSTM和暗淡50的单词嵌入生成文本。当我尝试将X的行i设置为等于嵌入向量y_embed时,问题就来了。 However, that problem only comes up on the third iteration of the for loop. 但是,该问题仅在for循环的第三次迭代中出现。 That's strange to me, because I'd expect every row of X to have the same shape. 这对我来说很奇怪,因为我希望X的每一行都具有相同的形状。

def generate_text(my_model, length):
    ix = np.random.randint(VOCAB_SIZE) #start generating by some 
         random index
    y_word = [reverse_dictionary[ix]] #get the word with that index
    y_embed = w2vec[ix] #get the embedding vector
    print(y_embed.shape)

    X = np.zeros((1, length, EMBED_DIM)) #make our numpy array
    print(X[0,2].shape)
    for i in range(length): #however many words we want
        print("i is "+str(i))
        X[0, i] = y_embed #current row of X is current word embedding
        y_embed = my_model.predict(X[:, :i+1, :])[0] 
        #input what we've generated so far, model.predict gives us a list, take the first one
        #we'll add it to our input on the next loop iteration

        y_word.append(vec2w(y_embed)) #lookup the word by its embedding

The for loop works for its first two iterations, and then throws this error when i=2: for循环可用于前两个迭代,然后在i = 2时抛出此错误:

 X[0, i] = y_embed #current row of X is current word embedding  
 ValueError: could not broadcast input array from shape (2,50) into shape (50)

So that's why I have it print the shape of y_embed and X[0,2] beforehand, and the console prints: 因此,这就是为什么我要事先打印y_embed和X [0,2]的形状,并在控制台上打印的原因:

(50,)

(50,)

So as far as I can tell, they DO have the same shape. 据我所知,它们确实具有相同的形状。 I'm still pretty new to numpy, so maybe it's something obvious, but I can't figure this one out. 我对numpy还是很陌生,所以也许这很明显,但是我无法弄清楚。 I should add that I'm using Keras, and model.predict expects a 3D tensor, which is why X is defined the way it is. 我应该补充一点,我正在使用Keras,并且model.predict需要一个3D张量,这就是为什么以这种方式定义X的原因。 I also tried setting X[0,i,:] = y_embed but that produced the same error at the same time. 我还尝试设置X [0,i ,:] = y_embed,但同时会产生相同的错误。

X = np.zeros((1, length, EMBED_DIM))

X is 3d. X是3d。

X[0, i]

selects on first 2 dir, so it is (EMBED_DIM,), which according to the error is (50,). 在前2个目录中进行选择,因此它是(EMBED_DIM,),根据错误它是(50,)。

The error thinks y_embed is (2,50), 2 columns of 50. Apparently it was created by the last iteration. 错误认为y_embed是(2,50),两列50。显然,它是由上一次迭代创建的。

my_model.predict(X[:, :i+1, :])[0] 

With i==1 , it is giving predict X[:,:2,:] , a (2,50) array. i==1 ,它给出了predict X[:,:2,:] (2,50)数组。 I don't know what predict does, but I don't think it's a coincidence that the output has the same shape as the input. 我不知道predict会做什么,但是我不认为输出具有与输入相同的形状是一个巧合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM