首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从另一个类中的一个类调用函数时的TypeError

从另一个类中的一个类调用函数时的TypeError
EN

Stack Overflow用户
提问于 2018-12-05 01:20:53
回答 1查看 0关注 0票数 0

我有一个类(“LogicalUnit”),我在其中创建多个函数和一个类(“TestModule”),我在其中使用所述函数。但是,当我在TestModule中使用LogicalUnit中的函数时,我从两个参数调用numpys dot函数得到一个类型错误(“不支持的操作数类型为*:'int'和'NoneType'”)。我不知道为什么我会出现类型错误,因为我还没有声明参数的类型。

代码语言:javascript
复制
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
代码语言:javascript
复制
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,'e​​xec'),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'不支持的操作数类型

EN

Stack Overflow用户

发布于 2018-12-05 11:10:03

更改

代码语言:javascript
复制
    self.w = self.computeWeights(self.w,self.X,self.Y)

代码语言:javascript
复制
    self.computeWeights(self.X,self.Y)

代码语言:javascript
复制
def computeWeights(self,w,X,Y):
    for i in range(iterations):
        temp = lr * self.computeGradient(self.w,self.X,self.Y)
        w-=temp

代码语言:javascript
复制
def computeWeights(self,X,Y):
    for i in range(iterations):
        temp = lr * self.computeGradient(self.w,self.X,self.Y)
        self.w-=temp

也许可以?

如果你只是将它存储在对象中,你实际上不需要传入w作为参数

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100006216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档