Skip to content

Commit

Permalink
tf learn
Browse files Browse the repository at this point in the history
  • Loading branch information
anty-zhang committed Dec 24, 2019
1 parent 06b344e commit 5aec102
Show file tree
Hide file tree
Showing 18 changed files with 476 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ neat-checkpoint*
*.svg
*.pyc
__pycache__

build
*.c
41 changes: 41 additions & 0 deletions mycython/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

[TOC]

# 介绍
cython 是 python的C/C++扩展库,python自动生成C++代码然后再由C++生成C++的扩展模块库,然后python就可以跑C++代码了。


## example
### first example

```bash
# hello.pyx -> 用pyx 转为c++代码
#
def day_hello():
print("Hello World.")

# setup.py -> 通过cythonize来把它变成c++的代码
from distutils.core import setup
from Cython.Build import cythonize
setup(name = 'Hello world app', ext_modules = cythonize("hello.pyx"))

# 编译
python setup.py build

# 加载到python的site-packages库中
python setup.py install
```

## 通过静态类提高性能 --> static_compute.pyx

- cdef:

- cpdef:

## 调用C函数提供性能 --> static_c_compute.pyx

## 总结

- 浮点数计算,cython调用C/C++可以极大提高性能

- I/O 密集型调用C/C++函数不能提高性能,而多线程可以极大提高性能
2 changes: 2 additions & 0 deletions mycython/first_example/hello.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def day_hello():
print("Hello World.")
7 changes: 7 additions & 0 deletions mycython/first_example/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env python
# encoding: utf-8

from distutils.core import setup
from Cython.Build import cythonize
setup(name = 'Hello world app', ext_modules = cythonize("hello.pyx"))

54 changes: 54 additions & 0 deletions mycython/static_c_example/performance_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env python
# coding=utf-8
import raw_python_compute
import static_compute
import static_c_compute

import time

lon1, lat1, lon2, lat2 = -72.345, 34.323, -61.823, 54.826

start_time = time.clock()
raw_python_compute.f_compute(3.2, 6.9, 1000000)
end_time = time.clock()
print("runing1 time: %f s" % (end_time - start_time))
start_time = time.clock()
for i in range(1000000):
raw_python_compute.spherical_distance(lon1, lat1, lon2, lat2)
end_time = time.clock()
print("runing2 time: %f s" % (end_time - start_time))

print("------------------------------------------------------------")

start_time = time.clock()
static_compute.f_compute(3.2, 6.9, 1000000)
end_time = time.clock()
print("runing1 time: %f s" % (end_time - start_time))
start_time = time.clock()
for i in range(1000000):
static_compute.spherical_distance(lon1, lat1, lon2, lat2)
end_time = time.clock()
print("runing2 time: %f s" % (end_time - start_time))

print("------------------------------------------------------------")

start_time = time.clock()
static_c_compute.f_compute(3.2, 6.9, 1000000)
end_time = time.clock()
print("runing1 time: %f s" % (end_time - start_time))
start_time = time.clock()
for i in range(1000000):
static_c_compute.spherical_distance(lon1, lat1, lon2, lat2)
end_time = time.clock()
print("runing2 time: %f s" % (end_time - start_time))

"""
runing1 time: 0.303566 s
runing2 time: 1.365852 s
------------------------------------------------------------
runing1 time: 0.001498 s
runing2 time: 0.857026 s
------------------------------------------------------------
runing1 time: 0.001402 s
runing2 time: 0.284110 s
"""
20 changes: 20 additions & 0 deletions mycython/static_c_example/raw_python_compute.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding=utf-8
# 第一个方法可以计算地球表面任意两个经纬度之间的距离,这个方法使用了很多三角函数,这些三角函数由python的math模块提供
# 第二个方法就是纯数字的计算

import math
def spherical_distance(lon1, lat1, lon2, lat2):
radius = 3956
x = math.pi/180.0
a = (90.0 - lat1)*x
b = (90.0 - lat2)*x
theta = (lon2 - lon1)*x
distance = math.acos(math.cos(a)*math.cos(b)) + (math.sin(a) * math.sin(b) * math.cos(theta))
return radius * distance

def f_compute(a, x, N):
s = 0
dx = (x - a)/N
for i in range(N):
s += ((a + i * dx) ** 2 - (a + i * dx))
return s * dx
7 changes: 7 additions & 0 deletions mycython/static_c_example/raw_python_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from distutils.core import setup
from Cython.Build import cythonize

setup(
name="raw python compute",
ext_modules=cythonize("raw_python_compute.pyx")
)
25 changes: 25 additions & 0 deletions mycython/static_c_example/static_c_compute.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# coding=utf-8
#import math
cdef extern from "math.h":
float cosf(float theta)
float sinf(float theta)
float acosf(float theta)

def spherical_distance(float lon1, float lat1, float lon2, float lat2):
cdef float radius = 3956
cdef float pi = 3.14159265
cdef float x = pi/180.0
cdef float a,b,theta,distance
a = (90.0 - lat1)*x
b = (90.0 - lat2)*x
theta = (lon2 - lon1)*x
distance = acosf(cosf(a)*cosf(b)) + (sinf(a) * sinf(b) * cosf(theta))
return radius * distance

def f_compute(double a, double x, int N):
cdef int i
cdef double s = 0
cdef double dx = (x - a)/N
for i in range(N):
s += ((a + i * dx) ** 2 - (a + i * dx))
return s * dx
7 changes: 7 additions & 0 deletions mycython/static_c_example/static_c_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from distutils.core import setup
from Cython.Build import cythonize

setup(
name="static c compute",
ext_modules=cythonize("static_c_compute.pyx")
)
20 changes: 20 additions & 0 deletions mycython/static_c_example/static_compute.pyx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# coding=utf-8
import math
cpdef float spherical_distance(float lon1, float lat1, float lon2, float lat2):
cdef float radius = 3956
cdef float pi = 3.14159265
cdef float x = pi/180.0
cdef float a,b,theta,distance
a = (90.0 - lat1)*x
b = (90.0 - lat2)*x
theta = (lon2 - lon1)*x
distance = math.acos(math.cos(a)*math.cos(b)) + (math.sin(a) * math.sin(b) * math.cos(theta))
return radius * distance

def f_compute(double a, double x, int N):
cdef int i
cdef double s = 0
cdef double dx = (x - a)/N
for i in range(N):
s += ((a + i * dx) ** 2 - (a + i * dx))
return s * dx
7 changes: 7 additions & 0 deletions mycython/static_c_example/static_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from distutils.core import setup
from Cython.Build import cythonize

setup(
name="static func and var compute",
ext_modules=cythonize("static_compute.pyx")
)
13 changes: 13 additions & 0 deletions shell/commands/curl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

[TOC]

# Get 请求

## Get请求转中文

```bash
# 直接请求会出现异常
curl -i http://localhost:8444/api/test?msg=ddd我
# get 请求参数msg带中文,需要将其转码后才可以正常请求
curl -G --data-urlencode "msg=ddd我" http://localhost:8444/api/test
```
150 changes: 150 additions & 0 deletions shell/commands/docker-basic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@


[TOC]

# 安装

## mac
```bash
brew cask install docker

# 配置国内源
https://registry.docker-cn.com
http://141e5461.m.daocloud.io

```

# 镜像

## 基本操作

```bash
docker pull --help
docker pull [选项] [Docker Registry地址]<仓库名>:<标签>

Docker Registry地址:地址的格式一般是 <域名/IP>[:端口号] 。默认地址是 Docker Hub。
仓库名:如之前所说,这里的仓库名是两段式名称,既 。 对于 Docker Hub,如果不给出用户名,则默认为library,也就是官方镜像。

docker run -it --rm ubuntu:14.04 bash
docker images

# 悬空镜像
docker images -f dangling=true
# 删除悬空镜像
### docker rmi $(docker images -q -f dangling=true)

# 中间层镜像
docker images -a

# 查看mongo:3.2之后的镜像
docker images -f since=mongo:3.2
# 查看mongo:3.2之前的镜像
docker images -f before=mongo:3.2
# 镜像format输出
docker images --format "{{.ID}}: {{.Repository}}"
docker images --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"

## docker commit---将容器的存储层保存下来成为镜像
docker run --rm --name webserver -d -p 80:80 nginx
docker exec -it webserver bash
echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
exit
curl http://localhost

docker diff webserver
docker commit --author "[email protected]" -m 'echo hello' webserver nginx:v2

# 查看镜像历史
docker history nginx:v2
docker history nginx:latest
docker run --rm --name web2 -d -p 81:80 nginx:v2

```

## 使用dockerFile定制镜像

```bash
# dockerfile---修改,配置,安装,构建,操作命令脚本

```

## Dockerfile命令

```bash
每个指令都会建立一层

# FROM
scratch是虚拟镜像。静态编译类型可以使镜像体积变小

# RUN
shell 格式:RUN <命令>
exec 格式:RUN <"可执行文件", "参数1", "参数2">

# COPY

# ADD
尽量用COPY

# CMD 只可以出现一次,如果写了多个, 只有最后一个生效。

# ENTRYPOINT 只可以出现一次,如果写了多个, 只有最后一个生效。
# ENV
# ARG
# VOLUME
# EXPOSE
# WORKDIR
# USER
# HEALTHCHECK 只可以出现一次,如果写了多个, 只有最后一个生效
# ONBUILD
#

docker inspect --format '{{json .State.Health}}' web | python -m json.tool

docker build -t nginx:v3 .
"" 是指定上下文路径
构建方式:
1)普通方式
2)git方式
3)tar方式
4)从标准输入中读取Dockerfile文件进行构建: docker build - < Docerfile 或者 cat Dockerfile build -
docker build - < xx.tar.gz

```

# 容器

## 基本操作

```bash
docker start docker stop docker restart docker attach docker nsenter

# 启动服务参数
# --mount: 表示要进行挂载
# source: 指定要运行部署的模型地址
# target: 这个是要挂载的目标位置
# -t: 指定的是挂载到哪个容器
# -d: 后台运行
# -p: 指定主机到docker容器的端口映射
# -e: 环境变量
# -v: docker数据卷
# --name: 指定容器name,后续使用比用container_id更方便

# 启动容器
docker run -dit ubuntu

# 进入容器
docker attach containerid # 如果从这个 stdin 中 exit,会导致容器的停止
docker exec -it 69d1 bash

# 查看运行容器
docker container ls
docker ps

# 查看全部容器
docker ps -a
docker save <repo_id:tag> > ubuntu.tar
docker save -o ubuntu.tar <repo_id:tag>
docker load < ubuntu.tar
docker load -i ubuntu.tar

```
Loading

0 comments on commit 5aec102

Please sign in to comment.