问题描述
我正在尝试使用 Theano 执行线性回归,但我可能会遗漏或做错一些事情,因为我收到一条错误消息,这里有一个可重现的示例:
import theano.tensor as T
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
X_theano,y_theano = make_regression(n_samples = 1000,noise = 10.0)
XT_train,XT_test,yT_train,yT_test = train_test_split(X_theano,y_theano,test_size=0.33,random_state=42)
X = T.fmatrix("X")
Y = T.fvector("Y")
w_init = np.random.randn(X_theano.shape[1])*0.01
b_init = 0.0
w = theano.shared(w_init)
b = theano.shared(b_init)
# hypothesis function
hypo = T.dot(X,w) + b
# cost function
cost = T.mean((hypo - Y)**2) + 0.01 * T.sum(w**2) # Regularizacion L2
w_grad = T.grad(cost,w)
b_grad = T.grad(cost,b)
learning_rate = 0.01
train_op = theano.function([X,Y],cost,updates = [(w,w - learning_rate*w_grad),(b,b - learning_rate*b_grad)])
predict_op = theano.function(inputs = [X],outputs = hypo)
for i in range(1000):
train_op(XT_train,yT_train)
但是每次我尝试时都会收到一条错误消息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-105-c5e46364b2ff> in <module>
33
34 for i in range(1000):
---> 35 train_op(XT_train,yT_train)
36
37 ann_predict = predict_op(XT_test)
/opt/anaconda3/lib/python3.7/site-packages/theano/compile/function_module.py in __call__(self,*args,**kwargs)
811 s.storage[0] = s.type.filter(
812 arg,strict=s.strict,--> 813 allow_downcast=s.allow_downcast)
814
815 except Exception as e:
/opt/anaconda3/lib/python3.7/site-packages/theano/tensor/type.py in filter(self,data,strict,allow_downcast)
138 '"function". Value: "%s"'
139 % (self,data.dtype,self.dtype,repr(data)))
--> 140 raise TypeError(err_msg)
141 elif (allow_downcast is None and
142 type(data) is float and
TypeError: Bad input argument to theano function with name "<ipython-input-105-c5e46364b2ff>:30" at index 0 (0-based).
这是我第一次使用 Theano,我也在尝试熟悉关于人工神经网络的梯度下降,所以我认为我做错了什么,但我无法指出究竟是什么。
>解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)