title: Set Theano Flags for Keras with GPU
body: <p>I am trying to train a model in Keras with the Theano backend and use my GPU. If I run with 100 images or 20K I get an error from Theano(look below) saying I do not have enough memory but I have 6GB on my GPU. I have used the THEANO_FLAG from <a href="http://keras.io/getting-started/faq/" rel="nofollow noreferrer">Keras docs</a> "THEANO_FLAGS=device=gpu,floatX=float32 python my_keras_script.py" the problem I used the <a href="https://stackoverflow.com/questions/21608025/how-to-set-up-theano-config">this one</a> from stackoverflow (echo -e "\n[global]\nfloatX=float32\ndevice=gpu0\n[lib]\ncnmem=0\n" >> ~/.theanorc
  ) to set the cnmem variable so if you use the flags from Keras it will use what cnmem you set with the stack overflow one. </p>
  
  <p>I have set cnmem to 0.83 (the highest it goes without erroring out right away) and to 0 and nothing has the 822 MB's that it requires but I have 6GB of video memory. I am sure I am doing something simple wrong but there isn't any information I can find on it.</p>
  
  <p>I am on Ubuntu 14.04 and I have CUDA installed. And I just did the <a href="https://github.com/fchollet/keras/blob/master/examples/mnist_transfer_cnn.py" rel="nofollow noreferrer">Keras MNIST</a> example using "THEANO_FLAGS=device=gpu,floatX=float32 python mnist_transfer_cnn.py
  "</p>
  
  <pre><code>MemoryError: Error allocating 822083584 bytes of device memory (out of  memory).
  Apply node that caused the error: GpuElemwise{add,no_inplace} (GpuDnnConv{algo='small', inplace=True}.0, GpuReshape{4}.0)
  Toposort index: 375
  Inputs types: [CudaNdarrayType(float32, 4D), CudaNdarrayType(float32, (True, False, True, True))]
  Inputs shapes: [(64, 64, 224, 224), (1, 64, 1, 1)]
  Inputs strides: [(3211264, 50176, 224, 1), (0, 1, 0, 0)]
  Inputs values: ['not shown', 'not shown']
  Outputs clients: [[GpuElemwise{Composite{(i0 * (i1 + Abs(i1)))},no_inplace}(CudaNdarrayConstant{[[[[ 0.5]]]]}, GpuElemwise{add,no_inplace}.0), GpuElemwise{Composite{((i0 * i1) + (i0 * i1 * sgn(i2)))}}[(0, 1)](CudaNdarrayConstant{[[[[ 0.5]]]]}, GpuDnnPoolGrad{mode='max'}.0, GpuElemwise{add,no_inplace}.0)]]
  
  HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
  HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.
  </code></pre>