[Python] ERROR with numpy " with shapes (100,153) (100,) "

Discussão em 'Python' iniciado por Stack, Julho 27, 2025.

  1. Stack

    Stack Membro Participativo

    print(f"w2i = {len(word2ind)}")
    # inp = np.zeros(len(word2ind))
    # weigh1_2 =
    # for in1 in range(2):
    # for i in
    wrd
    hidden_layer , alfa , itr , top = 100 , 0.1 , 2 , 10
    layer0 = np.zeros(len(wrd))
    weigh0_1 = np.random.rand(len(wrd) ,hidden_layer )
    weigh1_2 = np.random.rand(hidden_layer , len(wrd))

    for i1 in range(itr):
    for i2 in range(len(sentence)):
    layer0 = np.zeros(len(wrd))
    inp = sentence[i2]

    for i in range(3):
    if inp in wrd :
    layer0[wrd.index(inp)] = 1
    print(f"layer0 {str(layer0)}")
    layer1 = layer0.dot(weigh0_1)

    print(f"layer 1 = {layer1}")

    layer2 = layer1.dot(weigh1_2)

    layer3 = softmax(layer2)

    if True :
    print(f"TOP {top}: ")
    arr = np.array(layer3)
    print(type(layer3), layer3)

    top_of_val = np.argsort(arr)[-top:][::-1]
    ind = 1
    for i in top_of_val:
    print(f"\t{ind} . {wrd} == {arr}")

    #########################################

    tr = inp[3]
    tr_val = layer3[wrd.index(tr)]

    diff_l3 = (tr_val - 1 )
    diff_l3 = np.array( tr_val * (1 - tr_val))
    diff_l2 = diff_l3.dot(weigh1_2)
    diff_l1 = diff_l2.dot(weigh0_1)

    weigh1_2 = weigh1_2 - ((diff_l2 * layer1) * alfa )
    weigh0_1= weigh0_1 - (diff_l1 * alfa * layer0 )


    I have this code and this error :

    Traceback (most recent call last):
    File "/home/idea/code V2/py_code_27jun/main.py", line 93, in <module>
    weigh1_2 = weigh1_2 - ((diff_l2 * layer1) * alfa )
    ~~~~~~~~^~~~~~~~
    ValueError: operands could not be broadcast together with shapes (100,153) (100,)
    sh-5.2$


    if i edit (100,) to (100,1) it will be work ? and how it do

    the AI say i need do reshape ? but i not understand how

    UDP : if i add .T to layer1 - error if i addd .T to diff_l2 and to weigh1_2 i will have problem with "weigh0_1"

    Continue reading...

Compartilhe esta Página