属性错误:'list'对象没有属性类型'astype'

问题描述 投票:0回答:2

我正在尝试使用 python 从头开始创建神经网络。但我遇到了这种错误。谁能帮助我吗?

代码:

import numpy as py
import nnfs as nf ## import nnfs this is 
from nnfs.datasets import spiral_data

nf.init() # reset to the same number as before to check whethere it works.
X = [[1, 2, 3, 2.5],
     [2.0, 5.0, 1.0, 2.0],
     [1.5, 2.7, 3.3,0.8]]
##############
    ##weights =[[0.2, 0.8, 0.4, 0.5],[0.2, 0.8, 0.4, 0.5],[0.2, 0.8, 0.4, 0.5]]
    ##biases = [2,3,0.5]
    ##weights2 =[[0.1,-0.14,0.5],[-.5, 0.12, -.33],[-0.44, 0.73,-.13]]
    ##biases2 = [-1,2,-.5]
    ##layer1_output = py.dot(weights,py.transpose(input)) + biases # Dot product give shape of the each neuron. 
    ##layer2_output = py.dot(layer1_output,py.array(weights2).T) + biases2
    ##print(layer2_output)
#########################

##GENERATE DATASET###
x,y = spiral_data(100,3)

class Layer_Dense:
    def __init__(self, n_inputs,n_neurons): ##
      self.weights = 0.10 * py.random.randn(n_inputs,n_neurons) # size of input, shape of weight (shape of neuron = neuron X transposed input. )
      self.biases = py.zeros((1,n_neurons))
    def forward(self,inputs):
        self.output = py.dot(inputs, self.weights) + self.biases


class Activation_ReLU:
   def forward(self, inputs):
      self.output = py.maximum(0,inputs)

layer1 = Layer_Dense(2,5) # number of inputs , number of neurons
activation1 = Activation_ReLU()
layer1.forward(X)
print(layer1.output)
activation1.forward(layer1.ouput)
print(activation1.output)


File "/Users/eungbaekkim9andy/coding/python/NeuralNetwork/NeuralNetwork.py", line 36, in <module>
    layer1.forward(X)
  File "/Users/eungbaekkim9andy/coding/python/NeuralNetwork/NeuralNetwork.py", line 27, in forward
    self.output = py.dot(inputs, self.weights) + self.biases
  File "/Users/eungbaekkim9andy/miniconda3/lib/python3.9/site-packages/nnfs/core.py", line 22, in dot
    return orig_dot(*[a.astype('float64') for a in args], **kwargs).astype('float32')
  File "/Users/eungbaekkim9andy/miniconda3/lib/python3.9/site-packages/nnfs/core.py", line 22, in <listcomp>
    return orig_dot(*[a.astype('float64') for a in args], **kwargs).astype('float32')
AttributeError: 'list' object has no attribute 'astype'

我尝试将 n_inputs 更改为 np.array(n_inputs ) 但我认为它不起作用。

python attributeerror
2个回答
0
投票

您输入错误:

    activation1.forward(layer1.ouput)

另外,不确定这个输出是否是您正在寻找的,但我在这里更改为小写(x):

    layer1.forward(x)
    print(layer1.output)
    activation1.forward(layer1.output)
    print(activation1.output)

output


0
投票

我自己遵循课程,似乎点函数需要一个数组

将 X 列表转换为 numpy 数组

X = np.array(X)

应该可以工作

© www.soinside.com 2019 - 2024. All rights reserved.