Theano Cost Function, TypeError: Unknown parameter type: class numpy.ndarray

2024/10/15 19:25:53

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.

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?

Here is my code:

X = T.dmatrix()
y = T.dmatrix()X_input = np.genfromtxt('X.csv',delimiter=',') #5000x195
y_input = np.genfromtxt('y.csv',delimiter=',')  #5000x75input_layer_size, hidden_layer_size_1, hidden_layer_size_2, y_size = 195, 15, 15, 75theta1 = 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))/mreturn costfc = computeCost(X, y, theta1, theta2, theta3)def grad_desc(cost, theta):alpha = 0.1 #learning ratereturn 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'
Answer

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=[

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)
https://en.xdnf.cn/q/117799.html

Related Q&A

Bar plotting grouped Pandas

I have a question regarding plotting grouped DataFrame data.The data looks like:data =index taste food0 good cheese 1 bad tomato 2 worse tomato 3 worse …

Nginx+bottle+uwsgi Server returning 404 on every request

I have setup an Nginx server with following configuration:server {listen 8080;server_name localhost;location / {include uwsgi_params;uwsgi_pass unix:/tmp/uwsgi.notesapi.socket;uwsgi_param UWSGI_PYHOME …

Checking multiple for items in a for loop python

Ive written a code to tell the user that the their brackets are not balanced.I can exactly tell where my code is going wrong.Once it comes across the first situation of brackets, it does not continue t…

Can I export pandas DataFrame to Excel stripping tzinfo?

I have a timezone aware TimeSeries in pandas 0.10.1. I want to export to Excel, but the timezone prevents the date from being recognized as a date in Excel.In [40]: resultado Out[40]: fecha_hora 2013-…

Converting kwargs into variables?

How do I convert kwargs objects into local variables? I am a math teacher and I want to write a helper function where I can use JS-style template strings to write problems.I want to do something like …

Maya Python: Unbound Method due to Reload()

I have two files that import the same object tracking method from a third file. It works something like thisfile TrackingMethodclass Tracker(object):def __init__(self,nodeName=None,dag=None,obj=None):#…

How to append to a table in BigQuery using Python BigQuery API

Ive been able to append/create a table from a Pandas dataframe using the pandas-gbq package. In particular using the to_gbq method. However, When I want to check the table using the BigQuery web UI I s…

Splitting values out of a CSV Reader Python

Here is my current code a_reader = None a_reader = open(data.csv, rU) a_csv_reader = csv.reader(a_reader)for row in a_csv_reader:print row a_reader.close()count = 0 sum = 0.0 a_reader = open(…

python - list variable not storing proper result in recursion

I am trying to store all possible parenthesisation of a list through recursion.Example: printRange(0,3) Answer will be [[0],[1], [2]], [[0], [1, 2]], [[0, 1], [2]], [[0, 1, 2]]I could get a right answe…

Embedding matplotlib canvas into tkinter GUI - plot is not showing up, but no error is thrown

Running the python python script below does not show the embedded matplotlib plot. However it also throws no error message. Upon running the script, it is supposed to display a GUI displaying 4 buttons…