ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

我是 crate 和 programm 中的新手 A.I,我知道如何解决这个问题,我在 YouTube 的帮助下编程,但他没有这个问题,因为我是新手,所以我没有知道如何解决它,更不用说它是什么了 - 请帮助 D:

问题:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-58-2d579b4cf5ca> in <module>()
     16 
     17 
---> 18 train(neural_net, X, Y, l2_cost, 0.5)

<ipython-input-58-2d579b4cf5ca> in train(neural_net, X, Y, l2_cost, lr)
     11   #Forward pass - Pase adelantado
     12   for l, layer in enumerate(neural_net):
---> 13     z = out[-1][1] @ neural_net[l].W + neural_net[l].b
     14     a = neural_net[l].act_f[0](z)
     15     out.append((z, a))

ValueError: matmul: Input operand 0 does not have enough dimensions (has 0, gufunc core with signature (n?,k),(k,m?)->(n?,m?) requires 1)

我正在 Google Colab 编程 - 我需要帮助 我的代码: '''

import numpy as np
import scipy as cs
import matplotlib.pyplot as plt

from sklearn.datasets import make_circles

# Crera Data set
n = 500 #cantidad de cosas a analizar
p = 2 #cantidad de caracterizticas de las cosas a analizar

X, Y = make_circles(n_samples = n, factor = 0.5, noise=0.05)

plt.scatter(X[Y == 0,0], X[Y == 0,1])
plt.scatter(X[Y == 1,0], X[Y == 1,1], c = 'salmon')
plt.show()

#Clase de la capa de la RED
class neural_layer():

  def __init__(self, n_conn, n_neur, act_f):

    self.act_f = act_f
    self.b = np.random.rand(1, n_neur) * 2 - 1
    self.W = np.random.rand(n_conn, n_neur) * 2 - 1

#Funciones de Activacion
sigm = (lambda x: 1 / (1 + np.e ** (-x)),
       lambda x: x * (1 - x))

relu = lambda x: np.maximum(0, x)

_x = np.linspace(-5, 5, 100)
plt.plot(_x, relu(_x))

l0 = neural_layer(p, 4, sigm)
l1 = neural_layer(4, 8, sigm)
#hasta el infinito....

def create_nn(topology, act_f):

  nn = [] #contenedor de cada una de las capas de la red 'Neural Network'

  for l, layer in enumerate(topology[:-1]):
    nn.append(neural_layer(topology[1], topology[l+1], act_f))
  return nn

topology = [p, 4, 8, 16, 8, 4, 1] #topologia de la red

neural_net = create_nn(topology, sigm)

l2_cost = (lambda Yp, Yr: np.mean((Yp - Yr) ** 2), #Error cuadratico medio
           lambda Yp, Yr: (Yp - Yr))

def train(neural_net, X, Y, l2_cost, lr=0.5):
  out = [(None, X)]
  #Forward pass - Pase adelantado
  for l, layer in enumerate(neural_net):
    z = out[-1][1] @ neural_net[l].W + neural_net[l].b
    a = neural_net[l].act_f[0](z)
    out.append((z, a))
train(neural_net, X, Y, l2_cost, 0.5)

'''

您正在尝试将 2 个不兼容的矩阵相乘。

W 形状 = (4,4)

out[-1][1] shape = (500,2)

这条线

  z = out[-1][1] @ neural_net[l].W + neural_net[l].b

这是你如何修复它

您必须更改下面的行,而不是“1”,它应该是“l”

变化:

 nn.append(neural_layer(topology[1], topology[l+1], act_f))

 nn.append(neural_layer(topology[l], topology[l+1], act_f))