我有一个类(“LogicalUnit”),我在其中创建多个函数和一个类(“TestModule”),我在其中使用所述函数。但是,当我在TestModule中使用LogicalUnit中的函数时,我从两个参数调用numpys dot函数得到一个类型错误(“不支持的操作数类型为*:'int'和'NoneType'”)。我不知道为什么我会出现类型错误,因为我还没有声明参数的类型。
import numpy as np
class LogicalUnit():
def __init__(self, table):
self.X = table[:,0:3]
self.Y = table[:,3:]
self.w = np.array([0.0,0.0,0.0]).reshape(3,1)
self.w = self.computeWeights(self.w,self.X,self.Y)
def computeGradient(self,w, X, Y):
pred = np.dot(self.X,self.w)
pred = self.sigma(pred)
gradient = np.dot(self.X.T, (pred-self.Y))
return gradient/len(Y)
def computeWeights(self,w,X,Y):
for i in range(iterations):
temp = lr * self.computeGradient(self.w,self.X,self.Y)
w-=temp
import LogicalUnit as lu
import numpy as np
orData = np.array([...])
orUnit = lu.LogicalUnit(orData)
print("orUnit ", orUnit.getCurrentWeights())
错误:runfile('C:/Python/TestModule.py',wdir ='C:/ Python')Traceback(最近一次调用最后一次):
文件“”,第1行,在runfile中('C:/Python/TestModule.py',wdir ='C:/ Python')
文件“C:\ Anaconda \ envs \ Neural \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,第704行,在runfile execfile(filename,namespace)中
文件“C:\ Anaconda \ envs \ Neural \ lib \ site-packages \ spyder_kernels \ customize \ spydercustomize.py”,第108行,execfile exec(compile(f.read(),filename,'exec'),namespace)
文件“C:/Python/TestModule.py”,第10行,打印(“orUnit”或orUnit.getCurrentWeights())
文件“C:\ Python \ LogicalUnit.py”,第22行,在getCurrentWeights中返回self.computeWeights(self.w,self.X,self.Y)
文件“C:\ Python \ LogicalUnit.py”,第19行,在computeWeights中temp = lr * self.computeGradient(self.w,self.X,self.Y)
文件“C:\ Python \ LogicalUnit.py”,第13行,在computeGradient pred = np.dot(self.X,self.w)
TypeError:*:'int'和'NoneType'不支持的操作数类型
发布于 2018-12-05 11:10:03
更改
self.w = self.computeWeights(self.w,self.X,self.Y)
至
self.computeWeights(self.X,self.Y)
和
def computeWeights(self,w,X,Y):
for i in range(iterations):
temp = lr * self.computeGradient(self.w,self.X,self.Y)
w-=temp
至
def computeWeights(self,X,Y):
for i in range(iterations):
temp = lr * self.computeGradient(self.w,self.X,self.Y)
self.w-=temp
也许可以?
如果你只是将它存储在对象中,你实际上不需要传入w作为参数
https://stackoverflow.com/questions/-100006216
复制相似问题