类型错误:无法将“4”解释为数据类型。

14

我正在尝试学习神经网络。以下是代码。我遇到了错误"TypeError: Cannot interpret '4' as a data type"。请问有谁能帮我找出这个错误?

import numpy as np

inputs = [[1, 2 , 3, 2.5],
      [2, 5, 9, 10],
      [5, 1, 2, 7],
      [3, 2, 1, 4],
      [1,1.5, 7, 8]]

class layer_dense:
      def __init__ (self, n_inputs, m_neurons):
        self.weights= np.random.rand(n_inputs, m_neurons)
        self.biases= np.zeros(1, m_neurons)
     def forward (self, inputs):
        self.output= np.dot(inputs, self.weights)+self.biases
    
layer1 = layer_dense(4, 4)
layer2 = layer_dense(5,2)

layer1.forward(inputs)
layer2.forward(layer1.output)
print(layer2.output)
4个回答

16

函数描述

numpy.zeros(shape, dtype=float, order='C')

第二个参数应该是数据类型而不是数字。


3
在其他答案中,他们已经提到了Numpy处理它的默认方法。但是,我认为你想创建一个4x4的数组。
因此,如果有人想要创建一个更大的数组,他们应该以元组的形式提供数字。格式如下:
print(np.zeros((4,4)))

其他选项,如dtype和order,是特定于高级编程的,其中程序员更喜欢“C样式”或“Fortan样式”,有时提及数据类型可能是优势或必要的,这取决于情况。


2
零的标记如下:
numpy.zeros(shape, dtype=float, order='C')

形状参数应该提供为整数或多个整数的元组。您收到的错误是由于将4解释为dtype。


0
这行代码 "self.biases= np.zeros(1, m_neurons)" 你漏掉了一些括号,正确的代码应该是 "self.biases= np.zeros((1, m_neurons))"。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接