centos6.5 install python2.7.11 and paddle

1. The first is the preparation before installation:
since gcc is used in the installation process, install the Development tools
command:
yum groupinstall "Development tools"
Install the python dependency package in advance
Command:
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel

2. Install python
and download python. If you want to install the latest one, you can go to
 https://www.python.org/ftp/python/ to find
wget https://www.python.org/ftp/python/2.7.11 /Python-2.7.11.tgz
tar vxf Python-2.7.11.tgz
cd Python-2.7.11.tgz ./configure
--enable-shared --prefix=/usr/local
make && make install
If --enable-shared is added, the shared library will be compiled. If the program is not added, it will prompt that the shared library cannot be found.
If you forget to add the --enable-shared parameter, it will prompt that the lib library cannot be found when running.
python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory
(1) Edit vi /etc/ld.so.conf 
and log in with a non-root account, use sudo vi / The etc/ld.so.conf command
adds the lib library address of python2.7, such as my /usr/local/python27/lib, and saves the file
(2) Execute the /sbin/ldconfig -v command. If you are not logged in as an account with root privileges, use sudo /sbin/ldconfig -v. In this way, ldd can find this library, and there will be no error when executing python2.7
/etc/ld.so.conf:
This file records the path of the dynamic link library used during compilation.
By default, the compiler will only use the library files in the /lib and /usr/lib directories
as I specified, /usr/local, without specifying --prefix=/usr so that the lib library is installed in / Under usr/local, without adding /usr/local/lib in /etc/ld.so.conf, an error will be reported,
3. Install the pip tool
Official website: https://pypi.python.org/pypi/pip#downloads :

下载解压:
python setup.py install
安装工具
通过pip list 能输出版本号说明安装成功。
我在安装过程中提示缺少setuptools
在安装pip之前错误解决:
 ImportError: No module named setuptools 解决方案
shell中输入:
wget http://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz
tar zxvf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
python setup.py build
python setup.py install
以上工作完成后重新安装pip,进入pip目录执行命令python setup.py install
安成后输入pip list 有信息输出标识安装成功
但是安装完成之后下载包很慢,所以需要将连接修改为国内镜像地址,提高下载速度
几个主要的镜像源:
清华:https://pypi.tuna.tsinghua.edu.cn/simple
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
华中理工大学:http://pypi.hustunique.com/
山东理工大学:http://pypi.sdutlinux.org/ 
豆瓣:http://pypi.douban.com/simple/
Linux系统下,在用修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加“.”,表示是隐藏文件夹)
内容如下:
[global]index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]trusted-host=mirrors.aliyun.com
windows下,直接在user目录中创建一个pip目录,如:C:\Users\XXX\pip,新建文件pip.ini。内容和上边一样。
4.安装paddle
首先说一下paddlepaddle是什么?paddlepaddle是百度旗下深度学习开源平台。
pip install paddlepaddle
安装需要几分钟,需要下载所依赖的包。
上边的三个步骤需要没有任何错误的情况下,第四步安装成功的可能性会大一点。
表示paddle安装成功
运行一个房价预测训练模型:
import os
import paddle.v2 as paddle
import paddle.v2.dataset.uci_housing as uci_housing

with_gpu = os.getenv('WITH_GPU', '0') != '0'


def main():
    # init
    paddle.init(use_gpu=with_gpu, trainer_count=1)

    # network config
    x = paddle.layer.data(name='x', type=paddle.data_type.dense_vector(13))
    y_predict = paddle.layer.fc(input=x, size=1, act=paddle.activation.Linear())
    y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1))
    cost = paddle.layer.square_error_cost(input=y_predict, label=y)

    # Save the inference topology to protobuf.
    inference_topology = paddle.topology.Topology(layers=y_predict)
    with open("inference_topology.pkl", 'wb') as f:
        inference_topology.serialize_for_inference(f)

    # create parameters
    parameters = paddle.parameters.create(cost)

    # create optimizer
    optimizer = paddle.optimizer.Momentum(momentum=0)

    trainer = paddle.trainer.SGD(
        cost=cost, parameters=parameters, update_equation=optimizer)

    feeding = {'x': 0, 'y': 1}

    # event_handler to print training and testing info
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 100 == 0:
                print "Pass %d, Batch %d, Cost %f" % (
                    event.pass_id, event.batch_id, event.cost)

        if isinstance(event, paddle.event.EndPass):
            if event.pass_id % 10 == 0:
                with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
                    trainer.save_parameter_to_tar(f)
            result = trainer.test(
                reader=paddle.batch(uci_housing.test(), batch_size=2),
                feeding=feeding)
            print "Test %d, Cost %f" % (event.pass_id, result.cost)

    # training
    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(uci_housing.train(), buf_size=500),
            batch_size=2),
        feeding=feeding,
        event_handler=event_handler,
        num_passes=30)

    # inference
    test_data_creator = paddle.dataset.uci_housing.test()
    test_data = []
    test_label = []

    for item in test_data_creator():
        test_data.append((item[0], ))
        test_label.append(item[1])
        if len(test_data) == 5:
            break

    # load parameters from tar file.
    # users can remove the comments and change the model name
    # with open('params_pass_20.tar', 'r') as f:
    #     parameters = paddle.parameters.Parameters.from_tar(f)

    probs = paddle.infer(
        output_layer=y_predict, parameters=parameters, input=test_data)

    for i in xrange(len(probs)):
        print "label=" + str(test_label[i][0]) + ", predict=" + str(probs[i][0])


if __name__ == '__main__':
    main()
结果:



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325858647&siteId=291194637