Scipy最小化随机广播错误的产生

2024-04-24 01:25:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图制作一个非常简单的neural network来播放2048,但是在运行scipy优化器时,我总是会出错。在使用功能运行网络时

NN_game

使用与我在优化器中使用的形状相同的权重,我没有任何问题。你知道吗

import numpy as np
import scipy.optimize as opt
import NxN_2048 as game
import pickle, os, time

def gamestart(n):
    return game.new_elements([[0] * n] * n)

def activation(weight, input_layer):
    matmul = np.matmul(weight, input_layer)
    pos = 1 * (matmul > 0)
    neg = 0.1 * (matmul <= 0)
    return (pos + neg) * matmul + 1

def NN_move(board,w1,w2,w3):
    flat_board = np.array(board).flatten().astype(np.float32) #flat_board has size (n**2, 1)
    h1 = activation(w1, flat_board) #h1 has shape (n, 1)
    #print(h1.shape)
    h2 = activation(w2, h1) #h2 has shape (2*n, 1)
    #print(h2.shape)
    h3 = activation(w3, h2) #h3 has shape (4, 1)
    #print(h3.shape)
    move_func = [game.shiftup,game.shiftdown,game.shiftleft,game.shiftright]
    while True:
        if move_func[np.argmax(h3)](board) != board:
            l = ['up','down','left','right']
            #print(l[np.argmax(h3)])
            return move_func[np.argmax(h3)](board)
        else:
            h3[np.argmax(h3)] = np.NINF

def NN_game(w1,w2,w3):
    board = gamestart(4)
    while game.shiftup(board)!=board or game.shiftdown(board)!=board or game.shiftleft(board)!=board or game.shiftright(board)!=board:
        board = NN_move(board,w1,w2,w3)
        board = game.new_elements(board)
    score = sum(sum(np.array(board)))
    return score

def NN_game_packed_args(args):
    return NN_game(args[0], args[1], args[2])

def NN_train():
    n = 4
    w1 = np.random.randn(n, n**2)
    w2 = np.random.randn(2*n, n)
    w3 = np.random.randn(4, 2*n)
    lambda_ = 10

    #game_score = NN_game_packed_args([w1,w2,w3])
    #print(game_score)

    def cross_entropy(args):
        game_score = NN_game_packed_args(args)
        return game_score + lambda_ * (sum(sum(args[0])) + sum(sum(args[1])) + sum(sum(args[2])))

    for i in range(10000):
        results = opt.minimize(cross_entropy, (w1,w2,w3) , method='BFGS')
        print(results.x[0].shape, results.x[1].shape, results.x[2].shape)
        w1,w2,w3 = results.x[0],results.x[1],results.x[2]
        if i % 1 == 0:
            print('Iterations:', str(i),'       |       Current scoring:', str(NN_game_packed_args((w1,w2,w3))))

    return w1,w2,w3

NN_train()

我试图使用scipy.optimize.minimize来最小化交叉熵(在NN\u列函数下),但是我得到一个错误

ValueError: operands could not be broadcast together with shapes (4,16) (8,4)

尽管代码有时可以工作,但是在1次迭代之后,这个错误就会出现。我已经检查了我使用的激活层的形状是否正确,所以我不太确定发生了什么。你知道吗

我上传了另一个用于生成boardhere的文件


Tags: boardgamereturndefnpargsnnresults
1条回答
网友
1楼 · 发布于 2024-04-24 01:25:29

您应该查看numpy.matmul的文档,如果这真的是您想要的。也许把你的激活改成

def activation(weight, input_layer):
    matmul = weight.dot(input_layer)
    pos = 1 * (matmul > 0)
    neg = 0.1 * (matmul <= 0)
    return (pos + neg) * matmul + 1

将删除这些广播错误吗?这是因为numpy.matmul公司不按直觉行事。你知道吗

相关问题 更多 >