mikesj-public / convolutional_autoencoder

Code for a convolutional autoencoder written on python, theano, lasagne, nolearn
168 stars 54 forks source link

TypeError: cost must be a scalar. #4

Open noelcodella opened 9 years ago

noelcodella commented 9 years ago

Hi,

I'm getting an error: "TypeError: cost must be a scalar." in line 432 of gradient.py in theano.

I'm using Anaconda with Python 2.7, Lasagne 0.1.dev0, scikit-learn 0.15.2, Theano 0.7, "nolearn-0.6a0.dev0".

Can you specify the versions of Theano, Lasagne, and nolearn that work with your code?

Full error results are below:

python mnist_conv_autoencode.py Using gpu device 0: Tesla K40m /usr/local/anaconda/lib/python2.7/site-packages/Lasagne-0.1.dev0-py2.7.egg/lasagne/init.py:86: UserWarning: The uniform initializer no longer uses Glorot e.'s approach to determine the bounds, but defaults to the range (-0.01, 0.01) instead. Please use the new GlorotUniform initializer to get the old behaviororotUniform is now the default for all layers. warnings.warn("The uniform initializer no longer uses Glorot et al.'s " /usr/local/anaconda/lib/python2.7/site-packages/Lasagne-0.1.dev0-py2.7.egg/lasagne/layers/helper.py:69: UserWarning: get_all_layers() has been changed to rn layers in topological order. The former implementation is still available as get_all_layers_old(), but will be removed before the first release of Lasagno ignore this warning, use warnings.filterwarnings('ignore', '.*topo.*'). warnings.warn("get_all_layers() has been changed to return layers in " /usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py:376: UserWarning: layer.get_output_shape() is deprecated and will be removed for the firelease of Lasagne. Please use layer.output_shape instead. output_shape = layer.get_output_shape() ReshapeLayer (None, 784) produces 784 outputs Conv2DLayer (None, 1, 28, 28) produces 784 outputs Unpool2DLayer (None, 32, 34, 34) produces 36992 outputs ReshapeLayer (None, 32, 17, 17) produces 9248 outputs DenseLayer (None, 9248) produces 9248 outputs DenseLayer (None, 40) produces 40 outputs ReshapeLayer (None, 3872) produces 3872 outputs MaxPool2DLayer (None, 32, 11, 11) produces 3872 outputs Conv2DLayer (None, 32, 22, 22) produces 15488 outputs InputLayer (None, 1, 28, 28) produces 784 outputs /usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py:283: UserWarning: layer.get_output(...) is deprecated and will be removed for the first ase of Lasagne. Please use lasagne.layers.get_output(layer, ...) instead. output_layer.get_output(X_batch), y_batch) /usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py:285: UserWarning: layer.get_output(...) is deprecated and will be removed for the first ase of Lasagne. Please use lasagne.layers.get_output(layer, ...) instead. output_layer.get_output(X_batch, deterministic=True), y_batch) /usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py:286: UserWarning: layer.get_output(...) is deprecated and will be removed for the first ase of Lasagne. Please use lasagne.layers.get_output(layer, ...) instead. predict_proba = output_layer.get_output(X_batch, deterministic=True) Traceback (most recent call last): File "mnist_conv_autoencode.py", line 158, in ae.fit(X_train, X_out) File "/usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 145, in fit self.y_tensor_type, File "/usr/local/anaconda/lib/python2.7/site-packages/nolearn/lasagne.py", line 295, in _create_iter_funcs updates = update(loss_train, all_params, **update_params) File "/usr/local/anaconda/lib/python2.7/site-packages/Lasagne-0.1.dev0-py2.7.egg/lasagne/updates.py", line 324, in nesterov_momentum updates = sgd(loss_or_grads, params, learning_rate) File "/usr/local/anaconda/lib/python2.7/site-packages/Lasagne-0.1.dev0-py2.7.egg/lasagne/updates.py", line 134, in sgd grads = get_or_compute_grads(loss_or_grads, params) File "/usr/local/anaconda/lib/python2.7/site-packages/Lasagne-0.1.dev0-py2.7.egg/lasagne/updates.py", line 110, in get_or_compute_grads return theano.grad(loss_or_grads, params) File "/usr/local/anaconda/lib/python2.7/site-packages/theano/gradient.py", line 432, in grad raise TypeError("cost must be a scalar.") TypeError: cost must be a scalar.

noelcodella commented 9 years ago

Here's a solution: https://github.com/dnouri/kfkd-tutorial/issues/16

Though it was tricky for me: since I was using conda, pip was not installing the packages to the correct site-packages folder. So the issue was not being resolved for me even though I was following the above procedure. I had to manually remove the incorrect packages and copy over the compatible ones.

mikesj-public commented 9 years ago

Yeah, in retrospect, I don't think using lasagne and nolearn together was ideal, there seem to so many incompatibilities.

Which lasagne / nolearn combo worked for you?

mikesj-public commented 9 years ago

Also, are you running in a virtualenv? If not I would strongly recommend you do, with a lot of these libraries it's a nightmare to switch between versions.

noelcodella commented 9 years ago

I guess that link installed: Lasagne-0.1.dev0 nolearn-0.6a0.dev0

From: sudo pip install -r https://raw.githubusercontent.com/dnouri/kfkd-tutorial/master/requirements.txt

The problem was that it installed to: /usr/lib/python2.7/site-packages/

instead of: /usr/local/anaconda/lib/python2.7/site-packages

which I needed since I was using anaconda. Simply moving the packages over and deleting the old ones solved the issue for me.