Posts Tagged ‘Python’

Few Python base Deep Learning Libs

June 23, 2015 Leave a comment

Lasagne: light weighted Theano extension, Theano can be used explicitly

Keras: is a minimalist, highly modular neural network library in the spirit of Torch, written in Python, that uses Theano under the hood for fast tensor manipulation on GPU and CPU. It was developed with a focus on enabling fast experimentation.

Pylean2: wrapper for Theano, yaml, experimental oriented.

Caffe: CNN oriented deep learning framework using c++, with python wrapper, easy model definitions using prototxt.

Theano: general gpu math

nolearn: a probably even simpler one

you can find more here.

For Lasagne and nolearn, they are still in the rapid develop stage, so they changes a lot. Be careful with the versions installed, they need to match each other. If you are having problems such as “cost must be a scalar”, you can refer link here to solve it by uninstall and reinstall them.

pip uninstall Lasagne
pip uninstall nolearn
pip install -r

Remote access ipython notebooks

February 18, 2015 1 comment

Original post:

remote$ipython notebook --no-browser --port=8889

local$ssh -N -f -L localhost:8888:localhost:8889 remote_user@remote_host

To close the SSH tunnel on the local machine, look for the process and kill it manually:

local_user@local_host$ ps aux | grep localhost:8889
local_user 18418  0.0  0.0  41488   684 ?        Ss   17:27   0:00 ssh -N -f -L localhost:8888:localhost:8889 remote_user@remote_host
local_user 18424  0.0  0.0  11572   932 pts/6    S+   17:27   0:00 grep localhost:8889

local_user@local_host$ kill -15 18418

Alternatively, you can start the tunnel without the -f option. The process will then remain in the foreground and can be killed with ctrl-c.

On the remote machine, kill the IPython server with ctrl-c ctrl-c.

Note: If you are running GPU & Theano on your remote machine, you can launch the notebook by:

THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 ipython notebook –no-browser –port=8889

Another simple way is to do the following (adding ip=*):

# In the remote server

$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 ipython notebook –no-browser –ip=* –port=7777

then you can reach the notebook from http:// the-ip-address-of-your-remote-server:7777/

Categories: Uncategorized Tags:

Setup Django

April 25, 2012 Leave a comment

OK, here’s the simplest way/setting for Django. I am using mac, but it should be similar for other linux.

First. Installing an official release.

  1. Download the latest release from download page.
  2. Untar the downloaded file (e.g. tar xzvf Django-X.Y.tar.gz, where X.Y is the version number of the latest release).
  3. Change into the directory created in step 2 (e.g. cd Django-X.Y).
  4. If you’re using Linux, Mac OS X or some other flavor of Unix, enter the command sudo python install at the shell prompt.

After install it, run python in the command line, and check:

import django
print django.get_version()

Second,  is to Configure your database and create your first app.

From the command line, cd into a directory where you’d like to store your code, then run the following command: startproject mysite
Since you install it through command line, ‘’ should be in your system path. If you can not find it, try to find it in site-packages/django/bin, where site-packages is a directory within your Python installation. Consider symlinking to from some place on your path, such as /usr/local/bin.
The above command will create your first site like this:

Now run the server by doing:

python runserver.

You will see somthing like this in your terminal:

Validating models...
0 errors found.Django version 1.4, using settings 'mysite.settings'
Development server is running at
Quit the server with CONTROL-C.
 goto,  cool, it works.  Please also note that MAC actually comes with a embedded Apache Server, you may like to check in details here.
Now you needs to configure you database. Edit mysite/ To make it as simple as possible, use sqlite3 ( it goes with python, so you don’t have to install additional packages) Change the ENGINE from ‘django.db.backends.‘ to ‘django.db.backends.sqlite3‘.
OK, you are now ready for creating your own models 🙂
Categories: Python, Web Tags: ,

Fix Matplotlib MPLCONFIGDIR error

April 17, 2012 Leave a comment

When using Apache + python-cgi, I encounter this error:

<type ‘exceptions.RuntimeError’>: Failed to create /$dirstring$/common/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data 

An official explanation of MPLCONFIGDIR is here.

MPLCONFIGDIR This is the directory used to store user customizations to matplotlib, as well as some caches to improve performance. If MPLCONFIGDIR is not defined, HOME/.matplotlib is used by default.

It turns out that on my windows machine HOME/.matplotlib is now writable. Here’s a way to simply bypass this problem.

Find out the place where you first import matplotlib, right before it, set the $HOME dir to a temporary dir where it’s writable:

For example, I was importing matplotlib in an indirect way through “from scipy.cluster.vq import kmeans2“, so right before it, do

os.environ[‘HOME’] = r’C:\……\Apache2.2\htdocs\tmp’

from scipy.cluster.vq import kmeans2

Setup Apache Server on MAC

April 13, 2012 1 comment

To Set Up a Server on my mac.

  • Goto Preference: Sharing, Check the Sharing View
  • Now the apache is running, to do a test: put:  localhost in your browser, you will see “it works!”
  • When you check the box for Sharing View, you will be notified that there are two different websites are now available on your computer
    • 2. Computer Website: http://your-computer-name/
    •       The Site Folder in  <Library/WebServer/>
    •       There are 3 subfolders under this folder: a) CGI-Executable; b) Documents; c) Share
    •        You should put your executables in this folder, such as the below python code:

print “Content-type: text/html”
print “<html><head>”
print “”
print “</head><body>”
print “Test Page”
print “</body></html>”

Name it as ‘’, and in the terminal change it to

                   $ sudo chmod 755

Now you can run it by : http://localhost/cgi-bin/

Here’s a useful link:

Also here’s how to share a folder:

In Terminal, first cd to the folder you’d like to make available via the Web server. For example, cd ~/Pictures to switch to your user’s Pictures folder. Next, you activate Python’s built-in Web server with this command: python -m SimpleHTTPServer (and yes, capitalization counts). Press Return, and you’ll see a message stating Serving HTTP on port 8000 .... The important bit there is the port number (8000). (You can use a different port number by simply including it at the end of the command, like so: python -m SimpleHTTPServer 8080.) To access the server from the machine you’re running the Python server on, switch to a Web browser, and enter http://localhost:8000 (replace 8000 with the port you used, if you didn’t use the defaut) in the URL bar. When you press Return, you’ll see a listing of all files in the directory. Depending on what types of files are there, and which browser you’re using, you can then click on files to view them in the browser. In my testing, Firefox works better than Safari, as Safari wants to download file types it doesn’t know how to handle while Firefox will often let you open them with a helper application. To access the server from another machine on your network, you need to know the server computer’s name. You can find this in the Sharing System Preferences panel, in the aptly-named Computer Name section of the panel. Just below the computer’s actual name, you’ll see a line that begins Computers on your local network can access…, followed by the name they’ll need to use to do so. Typically, this is simply the Computer Name followed by .local—so if the machine’s name is OctoMac, for instance, you can then access the server by entering http://OctoMac.local:8000 in your browser’s URL bar.

Install OpenCv with Python Binding on MacOS

April 4, 2012 1 comment

I had tried several versions of install opencv on my mac that can be used in python.

Here are lessons — use the python provided by MacOS, you may have the following problem if you are using other versions.


– Use python by MacOS, which you should be able to finda and locate, e.g. the link /usr/bin/python    (my version is Python 2.7)

I found that I can not use the python I installed somewhere else, such as </Library/Frameworks/Python.framework/Versions/2.7/>

– I used the brew to install opencv:

   $sudo brew install opencv  

It enventually install it to  </usr/local/Cellar/opencv/2.3.1a>

– Next step, you need to add the path </usr/local/Cellar/opencv/2.3.1a/lib/python2.7/site-packages> to your PYTHONPATH

If you check the sit-packages folder, it should include two files: and

So, change your ~/.profile to include the following line:

export PYTHONPATH=”/usr/local/Cellar/opencv/2.3.1a/lib/python2.7/site-packages:${PYTHONPATH}”

Otherwise, whenever you would like to import the cv, you will have to first include the path in your python code, something like this:


Now, time to do the test:

I couldn’t find the example folder of python by the package installed by brew.

So I went ahead and use the previous download one, which is a full package opencv:

$ls ~/Documents/OpenCV-2.3.1/samples/python             (where you will be able to find the file, then go ahead to run it)

$ python ~/Documents/OpenCV-2.3.1/samples/python/

Nice , now it’s running, finally 🙂

Configure Apache web server for Python on Windows

March 26, 2012 Leave a comment

Configure the Apache web server to run Python programs on Windows machines.

For detailed information, please check the below link:

1. Install Python and Apache

2. Configure Apache to run Python CGI

The next step is to edit ‘httpd.conf’ apache configuration file located in the apache install directory in the conf directory. Search the httpd.conf file for the line

Options Indexes FollowSymLinks

Add ExecCGI to the end of the line. The line should now look similar to the following (NOTE: there may be more options listed):

Options Indexes FollowSymLinks ExecCGI

Next, search for the following:

#AddHandler cgi-script .cgi

Uncomment this line by removing the # in front of the line, and add a .py to the end of the line. The new line should look like this:

AddHandler cgi-script .cgi .py

3. Restart Apache

Now, the apache web server needs to be restarted. You can do this either via the Apache service located in the services control panel or via the Start -> All Programs -> Apache . . . -> Control Apache Server menu option.

4. Run a test Python page

You can use the EditRocket hello world Python sample web page template for a test Python page. This is located in File -> New From Template -> python -> Sample_Web_Page. The following is an example of the template:

print “Content-type: text/html”
print “<html><head>”
print “”
print “</head><body>”
print “Hello.”
print “</body></html>”

Note the line #!/usr/bin/python. This line needs changed to match the location of your Python installation. For example,


Here is an example assuming Python is installed in the C:\Python27 location

print “Content-type: text/html”
print “<html><head>”
print “”
print “</head><body>”
print “Hello.”
print “</body></html>”

Save this file as to your htdocs folder under your apache installation directory. Open your web browser and type in your apache host (and :port if the port is something other than 80) followed by, for example