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