Theano Cost函数,TypeError:未知参数类型:< class'numpy.ndarray'> [英] Theano Cost Function, TypeError: Unknown parameter type: <class 'numpy.ndarray'>

查看:144
本文介绍了Theano Cost函数,TypeError:未知参数类型:< class'numpy.ndarray'>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Theano的新手,只是学习而已.我在Theano中实现了python中的ANN作为学习过程.我正在使用Spyder.

I'm new to Theano, just learning it. I have a ANN in python that I'm implementing in Theano as learning process. I'm using Spyder.

Theano抛出错误:TypeError:未知参数类型:class'numpy.ndarray'

And Theano throws out an error: TypeError: Unknown parameter type: class 'numpy.ndarray'

我不确定错误在哪里.是在成本函数中还是在梯度下降中?造成这种情况的典型原因是什么?

I'm not sure where the error is. Is it in the cost function or in the gradient descent? And what is the typical reason for it?

这是我的代码:

X = T.dmatrix()
y = T.dmatrix()

X_input = np.genfromtxt('X.csv',delimiter=',') #5000x195
y_input = np.genfromtxt('y.csv',delimiter=',')  #5000x75

input_layer_size, hidden_layer_size_1, hidden_layer_size_2, y_size = 195, 15, 15, 75

theta1 = theano.shared(np.array(np.random.rand(hidden_layer_size_1, (input_layer_size+1)), dtype=theano.config.floatX))
theta2 = theano.shared(np.array(np.random.rand(hidden_layer_size_2, (hidden_layer_size_1+1)), dtype=theano.config.floatX))
theta3 = theano.shared(np.array(np.random.rand(y_size, hidden_layer_size_2+1), dtype=theano.config.floatX))

def computeCost(X, y, w1, w2, w3):
    m = X.shape[0]
    b = T.ones((m,1))
    a_1 = T.concatenate([b, X], axis=1)
    z_2 = T.dot(a_1, T.transpose(w1))
    a_2 = T.nnet.nnet.sigmoid(z_2)
    a_2 = T.concatenate([b, a_2], axis=1)
    z_3 = T.dot(a_2, T.transpose(w2))
    a_3 = T.nnet.nnet.sigmoid(z_3)
    a_3 = T.concatenate([b, a_3], axis=1)
    z_4 = T.dot(a_3, T.transpose(w3))
    h   = T.nnet.nnet.sigmoid(z_4)
    cost = T.sum(-y * T.log(h) - (1-y) * T.log(1-h))/m
    return cost

fc = computeCost(X, y, theta1, theta2, theta3)

def grad_desc(cost, theta):
    alpha = 0.1 #learning rate
    return theta - (alpha * T.grad(cost, wrt=theta))

cost = theano.function(inputs=[X_input, y_input], outputs=fc, updates=[
        (theta1, grad_desc(fc, theta1)),
        (theta2, grad_desc(fc, theta2)),
        (theta3, grad_desc(fc, theta3))])

我的最后一个代码生成了此错误:

My last code generated this error:

Traceback (most recent call last):

  File "ipython-input-88-099323f95e73", line 1, in <module>
cost = theano.function(inputs=[X_input, y_input], outputs=fc, updates=[(theta1, grad_desc(fc, theta1)), (theta2, grad_desc(fc, theta2)), (theta3, grad_desc(fc, theta3))])


  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\function.py", line 320, in function
output_keys=output_keys)

  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\pfunc.py", line 390, in pfunc
for p in params]

  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\pfunc.py", line 390, in <listcomp>
for p in params]

  File "C:\Program Files\Anaconda3\lib\site-packages\theano\compile\pfunc.py", line 489, in _pfunc_param_to_in
raise TypeError('Unknown parameter type: %s' % type(param))

TypeError: Unknown parameter type: class 'numpy.ndarray'

推荐答案

在您的theano.function中,您的输入是numpy数组(X_input和y_input).您希望输入是符号变量,例如:

In your theano.function your inputs are numpy arrays (X_input and y_input). You want the inputs to be symbolic variables, such as:

cost = theano.function(inputs=[X, y], outputs=fc, updates=[

这将创建一个可以用numpy数组调用的函数,以执行实际的计算,如下所示:

This will create a function which can be called with numpy arrays to perform the actual computation, as in:

actual_cost = cost(X_input, y_input)

这篇关于Theano Cost函数,TypeError:未知参数类型:&lt; class'numpy.ndarray'&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆