2
这里是我的FUN类,但结果显示AttributeError: FUN instance has no attribute '__trunc__'
。请告诉我编码错误和修改内容在哪里。AttributeError:FUN实例没有属性'__trunc__'
import math
import random
import string
import numpy as np
import pickle
from itertools import chain
random.seed(0)
class FUN:
def __init__(self):
print "fun"
# set a random numbers between a & b
def rand(a, b):
self.rand = (b-a)*random.random() + a
return self.rand
# sigmoid function, tanh ~ 1/(1+e^-x)
def sigmoid(x):
self.sig = math.tanh(x)
return self.sig
def sigmoid1(x):
self.sig1 = 1/(1+math.exp(-x))
return self.sig1
# derivative of sigmoid function, in terms of the output (y)
def dsigmoid(y):
self.dsig = 1.0 - y**2
return self.dsig
# getting 2d array
#def matrix(I, J, fill=0.0):
#return [[val for col in range(I)] for row in range(J)]
#obtain a matrix
def matrix(I, J, fill=0.0):
m = []
for i in range(I):
m.append([fill]*J)
return m
f = FUN()
print f.matrix(2,3)
上面的代码是给下列类型的错误:
fun
Traceback (most recent call last):
File "functions.py", line 42, in <module>
print f.matrix(2,3)
File "functions.py", line 38, in matrix
for i in range(I):
AttributeError: FUN instance has no attribute '__trunc__'
请帮我解决这个错误。
感谢您的建议,它在发表自己的论点后正在努力。但是对于随机浮动函数,它给出的错误为:Traceback(最近调用最后一个): 文件“functions.py”,第43行,在 print f.rand(2.0,3.1) 文件“functions.py “,line 18,in rand self.rand = float((ba)* random.random()+ a) TypeError:不支持的操作数类型为 - :'type'和'type' –
lkkkk
@Latik:You因为'def rand(self,a,b):',then'self.rand =(ba)* random.random()+ a',然后'返回self.rand'工作正常。 –
好吧....它正在工作.. thnx – lkkkk