【问题标题】:Scipy minimize producing random broadcast errorsScipy 最小化产生随机广播错误
【发布时间】:2019-04-10 13:03:51
【问题描述】:

我正在尝试制作一个非常简单的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_train 函数下),但我得到一个错误提示

ValueError:操作数无法与形状一起广播 (4,16) (8,4)

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

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

【问题讨论】:

  • 你没有给我们足够的信息来帮助你。
  • 请问您认为哪些信息会有所帮助?我已经发布了我用来生成游戏的所有内容以及我用来构建神经网络的代码。不太确定我错过了什么,但我对所有这些都很陌生
  • 我能提供帮助的唯一方法是复制您的代码,运行它,然后查看发生错误的确切位置。然后开始添加诊断打印以检查所涉及变量的形状。并继续这样做,直到我明白为什么。但我无法在我的脑海或手机上运行你的代码。

标签: python numpy scipy neural-network array-broadcasting


【解决方案1】:

您应该查看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 不按照直觉行事。

【讨论】:

  • 错误仍然存​​在。我不认为这是导致错误的部分原因导致 np.matmul 的两个输入都是二维矩阵。无论如何,我尝试将其更改为您建议的内容,但错误仍然存​​在。我认为该错误可能与 scipy.opt.minimize 的工作原理有关,但我真的不知道如何测试它,因为在我优化输入之前,其他一切似乎都正常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
相关资源
最近更新 更多