值错误:形状(9,)和(4,)未对齐

2024-06-16 14:09:14 发布

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

我正在使用强化学习训练神经网络玩2048。或者至少我认为我是,因为我对这个还不熟悉。在

这是什么神经网络.py看起来像:

import random
import numpy as np

def nonlin(x, deriv=False):
    if(deriv==True):
        return x * (1-x)
    return 1/(1+np.exp(-x))


np.random.seed(1)


class NeuralNetwork:

    next_ID = 0

    def __init__(self, HyperParams):
        self.synapses = []
        for synapse in range(len(HyperParams)-1):
            self.synapses.append(2*np.random.random((HyperParams[synapse], HyperParams[synapse+1]))-1)
        self.score = 0
        # self.name = words[random.randint(0, len(words))].strip()
        self.name = str(NeuralNetwork.next_ID)
        NeuralNetwork.next_ID += 1


    def train_batch(self, epoch, state, outcome):
        for i in range(epoch):
            self.layers = []
            self.layers.append(state)
            for j in range(len(self.synapses)):
                self.layers.append(nonlin(np.dot(self.layers[-1], self.synapses[j])))

            error = outcome - self.layers[-1]
            if (i % 1000) == 0: print(str(np.mean(np.abs(error))))

            for j in range(1,1+len(self.synapses)):
                delta = error * nonlin(self.layers[-j], True)
                error = delta.dot(self.synapses[-j].T)
                self.synapses[-j] += self.layers[-(j+1)].T.dot(delta)


    def train(self, state, outcome):
        self.layers = []
        self.layers.append(state)
        for j in range(len(self.synapses)):
            self.layers.append(nonlin(np.dot(self.layers[-1], self.synapses[j])))

        error = outcome - self.layers[-1]
        print("error: ", error.shape)
        for j in range(1,1+len(self.synapses)):
            delta = error * nonlin(self.layers[-j], True)
            print("delta: ", delta.shape)
            error = delta.dot(self.synapses[-j].T)
            print("layer: ", self.layers[-(j+1)].shape)
            print("layer.T: ", self.layers[-(j+1)].T.shape)

            # this is the issue
            print("dot: ", self.layers[-(j+1)].T.dot(delta).shape)
            self.synapses[-j] += self.layers[-(j+1)].T.dot(delta) 


    def next_gen(self):
        child = NeuralNetwork([1])
        for synapse in self.synapses:
            # add variation
            child.synapses.append(synapse + 0.1*np.random.random(synapse.shape)-0.05)
        # child.name += " son of " + self.name
        child.name += "<-" + self.name
        return child

    def feed(self, state):
        self.layers = []
        self.layers.append(state)
        for j in range(len(self.synapses)):
            self.layers.append(nonlin(np.dot(self.layers[-1], self.synapses[j])))
        return self.layers[-1]

2048.py如下所示:

^{pr2}$

有人告诉我,当numpy遇到两个1-D数组时,它会知道该怎么做,但这并没有发生。我应该让这些数组是二维的,它们的内部维数是1?你能帮忙吗?在

以下是完整的错误:

Traceback (most recent call last):
  File "2048.py", line 195, in <module>
    NN.train(state, move)
  File "/home/jeff/Programs/grad_descent/NeuralNetwork.py", line 71, in train
    print("dot: ", self.layers[-(j+1)].T.dot(delta).shape)
ValueError: shapes (9,) and (4,) not aligned: 9 (dim 0) != 4 (dim 0)

如你所见,它们都是1D向量,所以numpy应该在它们上点上。在


Tags: inselfforlenlayersnprangerandom
1条回答
网友
1楼 · 发布于 2024-06-16 14:09:14

如果使用np.newaxis给出一个显式的一维列表示,它就可以工作了。在

注意:如果您要寻找标量输出,那么这两个向量必须是equal length。OP中的错误消息显示您正在尝试获取长度-9和长度-4向量的点积。我假设您确实希望.dot()返回一个外部产品。如果不是这样,内积就不起作用了——在这种情况下,试着找出为什么你没有得到两个等长向量的地方。在

有:

a = np.array([1,2,3])
b = np.array([2,3,4,5])

a和{}的形状分别是(3,)和{}:

^{pr2}$

输出:

(3,)
(4,)
failed: shapes (3,) and (4,) not aligned: 3 (dim 0) != 4 (dim 0)

使用newaxis,形状变成(3,1)和{}:

aa = a[:, np.newaxis]
bb = b[:, np.newaxis]

try:
    print(aa.shape)
    print(bb.shape)
    print("aa.bb: \n{}".format(np.dot(aa,bb.T)))
except ValueError as e:
    print("failed: {}".format(e))

输出:

(3, 1)
(4, 1)  
aa.bb: 
[[ 2  3  4  5]
 [ 4  6  8 10]
 [ 6  9 12 15]]

相关问题 更多 >