自 17.05 版本开始,Docker 支持多步骤镜像创建( Multi-stage build)特性,可以精简最终生成的镜像大小 。

对于需要编译的应用(如 CGoJava语言等)来说,通常情况下至少需要准备两个 环境的 Docker镜像:

  • 编译环境镜像 : 包括完整的编译引擎、依赖库等,往往比较庞大。作用是编译应用为二进制文件;
  • 运行环境镜像: 利用编译好的二进制文件,运行应用,由于不需要编译环境,体积比较小。

使用多步骤创建,可以在保证最终生成的运行环境镜像保持精筒的情况下,使用单一的Dockerfile,降低维护复杂度 。

Go语言应用为例。创建干净目录,进入到目录中,创建main.go文件,内容为:

// main.go will output "Hello, Docker"
package main

import ("fmt")

func main() {
    fmt.println("Hello, Docker")
}

创建 Dockerfile,使用 golang:1.9镜像编译应用二进制文件为 app,使用精简的镜像 alpine:latest 作为运行环境 。Dockerfile 完整内容为:

FROM golang:1.9 as builder # define stage name as builder
RUN mkdir -p /go/src/test
WORKDIR /go/src/test
COPY main.go
RUN CGO_ENABLED=O GOOS=linux go build -o app

FROM alpine:latest
RUN apk –no-cache add ca-certificates
WORKDIR /root/
COPY –from=builder /go/src/test/app . # copy file from the builder stage
CMD ["./app"]

执行如下命令创建镜像,并运行应用:

$ docker build -t yeasy/test multistage:latest .
Sending build context to Docker daemon 3.072kB
Step 1/10 : FROM golang:1.9
Successfully built 5fd0cb93dda0
Successfully tagged yeasy/test-multistage:latest 
$ docker run --rm yeasy/test-m ultistage:latest
Hello, Docker

查看生成的最终镜像,大小只有 6.55 MB:

$docker images | grep test-multistage
yeasy/test-multistage latest 5fd0cb93dda0 1 minutes ago 6.55MB

文章来源:

《Docker技术入门与实践》第三版 杨保华、戴王剑、曹亚仑 编著

文章目录

1. dd命令

dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。

注意:指定数字的地方若以下列字符结尾,则乘以相应的数字:b=512;c=1; k=1024;w=2

参数注释:

  1. if=文件名:输入文件名,缺省为标准输入。即指定源文件。< if=input file >
  2. of=文件名:输出文件名,缺省为标准输出。即指定目的文件。< of=output file >
  3. ibs=bytes:一次读入bytes个字节,即指定一个块大小为bytes个字节。
    obs=bytes:一次输出bytes个字节,即指定一个块大小为bytes个字节。
    bs=bytes:同时设置读入/输出的块大小为bytes个字节。
  4. cbs=bytes:一次转换bytes个字节,即指定转换缓冲区大小。
  5. skip=blocks:从输入文件开头跳过blocks个块后再开始复制。
  6. seek=blocks:从输出文件开头跳过blocks个块后再开始复制。
    注意:通常只用当输出文件是磁盘或磁带时才有效,即备份到磁盘或磁带时才有效。
  7. count=blocks:仅拷贝blocks个块,块大小等于ibs指定的字节数。
  8. conv=conversion:用指定的参数转换文件。
    • ascii:转换ebcdic为ascii
    • ebcdic:转换ascii为ebcdic
    • ibm:转换ascii为alternate ebcdic
    • block:把每一行转换为长度为cbs,不足部分用空格填充
    • unblock:使每一行的长度都为cbs,不足部分用空格填充
    • lcase:把大写字符转换为小写字符
    • ucase:把小写字符转换为大写字符
    • swab:交换输入的每对字节
    • noerror:出错时不停止
    • notrunc:不截短输出文件
    • sync:将每个输入块填充到ibs个字节,不足部分用空(NUL)字符补齐。

2. dd应用实例

1. 将本地的/dev/hdb整盘备份到/dev/hdd

$ dd if=/dev/hdb of=/dev/hdd

2. 将/dev/hdb全盘数据备份到指定路径的image文件

$ dd if=/dev/hdb of=/root/image

3. 将备份文件恢复到指定盘

$ dd if=/root/image of=/dev/hdb

4. 备份/dev/hdb全盘数据,并利用gzip工具进行压缩,保存到指定路径

$ dd if=/dev/hdb | gzip > /root/image.gz

5. 将压缩的备份文件恢复到指定盘

$ gzip -dc /root/image.gz | dd of=/dev/hdb

6. 备份与恢复MBR

备份磁盘开始的512个字节大小的MBR信息到指定文件:

$ dd if=/dev/hda of=/root/image count=1 bs=512

count=1指仅拷贝一个块;bs=512指块大小为512个字节。

恢复:

$ dd if=/root/image of=/dev/had

将备份的MBR信息写到磁盘开始部分

7. 备份软盘

$ dd if=/dev/fd0 of=disk.img count=1 bs=1440k (即块大小为1.44M)

8. 拷贝内存内容到硬盘

$ dd if=/dev/mem of=/root/mem.bin bs=1024 (指定块大小为1k)

9. 拷贝光盘内容到指定文件夹,并保存为cd.iso文件

$ dd if=/dev/cdrom(hdc) of=/root/cd.iso

10. 增加swap分区文件大小

第一步:创建一个大小为256M的文件:

$ dd if=/dev/zero of=/swapfile bs=1024 count=262144

第二步:把这个文件变成swap文件:

$ mkswap /swapfile

第三步:启用这个swap文件:

$ swapon /swapfile

第四步:编辑/etc/fstab文件,使在每次开机时自动加载swap文件:

$ /swapfile swap swap default 0 0

11. 销毁磁盘数据

$ dd if=/dev/urandom of=/dev/hda1

注意:利用随机的数据填充硬盘,在某些必要的场合可以用来销毁数据。

12. 测试硬盘的写速度

$ dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file

通过以上命令输出的命令执行时间,可以计算出硬盘的写速度。

13. 测试硬盘的读速度

$ dd if=/root/1Gb.file bs=64k | dd of=/dev/null

通过以上命令输出的命令执行时间,可以计算出硬盘的读速度。

14. 确定硬盘的最佳块大小

$ dd if=/dev/zero bs=1024 count=1000000 of=/root/1Gb.file
$ dd if=/dev/zero bs=2048 count=500000 of=/root/1Gb.file
$ dd if=/dev/zero bs=4096 count=250000 of=/root/1Gb.file
$ dd if=/dev/zero bs=8192 count=125000 of=/root/1Gb.file

通过比较以上命令输出中所显示的命令执行时间,即可确定系统最佳的块大小。

15. 修复硬盘

$ dd if=/dev/sda of=/dev/sda

或者

$ dd if=/dev/hda of=/dev/hda

当硬盘较长时间(一年以上)放置不使用后,磁盘上会产生magnetic flux point,当磁头读到这些区域时会遇到困难,并可能导致I/O错误。当这种情况影响到硬盘的第一个扇区时,可能导致硬盘报废。上边的命令有可能使这些数 据起死回生。并且这个过程是安全、高效的。

16. 利用netcat远程备份

$ dd if=/dev/hda bs=16065b | netcat < targethost-IP > 1234

在源主机上执行此命令备份/dev/hda

netcat -l -p 1234 | dd of=/dev/hdc bs=16065b

在目的主机上执行此命令来接收数据并写入/dev/hdc

$ netcat -l -p 1234 | bzip2 > partition.img
$ netcat -l -p 1234 | gzip > partition.img

以上两条指令是目的主机指令的变化分别采用bzip2、gzip对数据进行压缩,并将备份文件保存在当前目录。

17. 将一个很大的视频文件中的第i个字节的值改成0x41(也就是大写字母A的ASCII值)

$ echo A | dd of=bigfile seek=$i bs=1 count=1 conv=notrunc

3. /dev/null和/dev/zero的区别

  • /dev/null,外号叫无底洞,你可以向它输出任何数据,它通吃,并且不会撑着!它是空设备,也称为位桶(bit bucket)。任何写入它的输出都会被抛弃。如果不想让消息以标准输出显示或写入文件,那么可以将消息重定向到位桶。
  • /dev/zero,是一个输入设备,你可你用它来初始化文件。该设备无穷尽地提供0,可以使用任何你需要的数目——设备提供的要多的多。他可以用于向设备或文件写入字符串0。

Link: http://www.linuxde.net/2013/03/12928.html

文章目录

Introduction

A virtual private network, or VPN, allows you to securely encrypt traffic as it travels through untrusted networks, such as those at the coffee shop, a conference, or an airport.

IKEv2, or Internet Key Exchange v2, is a protocol that allows for direct IPSec tunneling between the server and client. In IKEv2 VPN implementations, IPSec provides encryption for the network traffic. IKEv2 is natively supported on some platforms (OS X 10.11+, iOS 9.1+, and Windows 10) with no additional applications necessary, and it handles client hiccups quite smoothly.

In this tutorial, you’ll set up an IKEv2 VPN server using StrongSwan on an Ubuntu 18.04 server and connect to it from Windows, macOS, Ubuntu, iOS, and Android clients.

Prerequisites

To complete this tutorial, you will need:

One Ubuntu 18.04 server configured by following the Ubuntu 18.04 initial server setup guide, including a sudo non-root user and a firewall.

Step 1 Installing StrongSwan

First, we’ll install StrongSwan, an open-source IPSec daemon which we’ll configure as our VPN server. We’ll also install the public key infrastructure component so that we can create a certificate authority to provide credentials for our infrastructure.

Update the local package cache and install the software by typing:

$ sudo apt update
$ sudo apt install strongswan strongswan-pki ufw

Now that everything’s installed, let’s move on to creating our certificates.

Step 2 Creating a Certificate Authority

An IKEv2 server requires a certificate to identify itself to clients. To help us create the certificate required, the strongswan-pki package comes with a utility to generate a certificate authority and server certificates. To begin, let’s create a few directories to store all the assets we’ll be working on. The directory structure matches some of the directories in /etc/ipsec.d, where we will eventually move all of the items we create. We’ll lock down the permissions so that our private files can’t be seen by other users:

$ mkdir -p ~/pki/{cacerts,certs,private}
$ chmod 700 ~/pki

Now that we have a directory structure to store everything, we can generate a root key. This will be a 4096-bit RSA key that will be used to sign our root certificate authority.

Execute these commands to generate the key:

ipsec pki --gen --type rsa --size 4096 --outform pem \
> ~/pki/private/ca-key.pem

Now that we have a key, we can move on to creating our root certificate authority, using the key to sign the root certificate:

$ ipsec pki --self --ca --lifetime 3650 --in ~/pki/private/ca-key.pem \
    --type rsa --dn "CN=VPN root CA" --outform pem > ~/pki/cacerts/ca-cert.pem

You can change the distinguished name (DN) values to something else to if you would like. The common name here is just the indicator, so it doesn’t have to match anything in your infrastructure.

Now that we’ve got our root certificate authority up and running, we can create a certificate that the VPN server will use.

Step 3 Generating a Certificate for the VPN Server

We’ll now create a certificate and key for the VPN server. This certificate will allow the client to verify the server’s authenticity using the CA certificate we just generated.

First, create a private key for the VPN server with the following command:

$ ipsec pki --gen --type rsa --size 4096 --outform pem > ~/pki/private/server-key.pem

Now, create and sign the VPN server certificate with the certificate authority’s key you created in the previous step. Execute the following command, but change the Common Name (CN) and the Subject Alternate Name (SAN) field to your VPN server’s DNS name or IP address:

$ ipsec pki --pub --in ~/pki/private/server-key.pem --type rsa \
    | ipsec pki --issue --lifetime 1825 \
        --cacert ~/pki/cacerts/ca-cert.pem \
        --cakey ~/pki/private/ca-key.pem \
        --dn "CN=server_domain_or_IP" --san "server_domain_or_IP" \
        --flag serverAuth --flag ikeIntermediate --outform pem \
    >  ~/pki/certs/server-cert.pem

Now that we’ve generated all of the TLS/SSL files StrongSwan needs, we can move the files into place in the /etc/ipsec.d directory by typing:

$ sudo cp -r ~/pki/* /etc/ipsec.d/

In this step, we’ve created a certificate pair that would be used to secure communications between the client and the server. We’ve also signed the certificates with the CA key, so the client will be able to verify the authenticity of the VPN server using the CA certificate. Now that have all of the certificates ready, we’ll move on to configuring the software.

Step 4 Configuring StrongSwan

StrongSwan has a default configuration file with some examples, but we will have to do most of the configuration ourselves. Let’s back up the file for reference before starting from scratch:

$ sudo mv /etc/ipsec.conf{,.original}

Create and open a new blank configuration file by typing:

sudo vim /etc/ipsec.conf

First, we’ll tell StrongSwan to log daemon statuses for debugging and allow duplicate connections. Add these lines to the file:

config setup
    charondebug="ike 1, knl 1, cfg 0"
    uniqueids=no

conn ikev2-vpn
    auto=add
    compress=no
    type=tunnel
    keyexchange=ikev2
    fragmentation=yes
    forceencaps=yes
    dpdaction=clear
    dpddelay=300s
    rekey=no
    left=%any
    leftid=@server_domain_or_IP
    leftcert=server-cert.pem
    leftsendcert=always
    leftsubnet=0.0.0.0/0
    right=%any
    rightid=%any
    rightauth=eap-mschapv2
    rightsourceip=10.10.10.0/24
    rightdns=8.8.8.8,8.8.4.4
    rightsendcert=never
    eap_identity=%identity

Save and close the file once you’ve verified that you’ve configured things as shown.

Now that we’ve configured the VPN parameters, let’s move on to creating an account so our users can connect to the server.

Step 5 Configuring VPN Authentication

Our VPN server is now configured to accept client connections, but we don’t have any credentials configured yet. We’ll need to configure a couple things in a special configuration file called ipsec.secrets:

  • We need to tell StrongSwan where to find the private key for our server certificate, so the server will be able to authenticate to clients.
  • We also need to set up a list of users that will be allowed to connect to the VPN.

Let’s open the secrets file for editing:

$ sudo vim /etc/ipsec.secrets

First, we’ll tell StrongSwan where to find our private key,Then, we’ll define the user credentials. You can make up any username or password combination that you like:

: RSA "server-key.pem"
your_username : EAP "your_password"

Save and close the file. Now that we’ve finished working with the VPN parameters, we’ll restart the VPN service so that our configuration is applied:

$ sudo systemctl restart strongswan

Now that the VPN server has been fully configured with both server options and user credentials, it’s time to move on to configuring the most important part: the firewall.

Step 6 Configuring the Firewall & Kernel IP Forwarding

With the StrongSwan configuration complete, we need to configure the firewall to forward and allow VPN traffic through.

If you followed the prerequisite tutorial, you should have a very basic UFW firewall enabled. If you don’t yet have UFW configured, you can create a baseline configuration and enable it by typing:

$ sudo ufw allow OpenSSH
$ sudo ufw enable

Now, add a rule to allow UDP traffic to the standard IPSec ports, 500 and 4500:

$ sudo ufw allow 500,4500/udp

Next, we will open up one of UFW’s configuration files to add a few low-level policies for routing and forwarding IPSec packets. Before we do, we need to find which network interface on our server is used for internet access. We can find that by querying for the interface associated with the default route:

$ ip route | grep default

Your public interface should follow the word “dev”. For example, this result shows the interface named eth0, which is highlighted below:

Output
default via 203.0.113.7 dev eth0 proto static

When you have your public network interface, open the /etc/ufw/before.rules file in your text editor:

$ sudo nano /etc/ufw/before.rules
Near the top of the file (before the *filter line), add the following configuration block:
*nat
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -m policy --pol ipsec --dir out -j ACCEPT
-A POSTROUTING -s 10.10.10.0/24 -o eth0 -j MASQUERADE
COMMIT

*mangle
-A FORWARD --match policy --pol ipsec --dir in -s 10.10.10.0/24 -o eth0 -p tcp -m tcp --tcp-flags SYN,RST SYN -m tcpmss --mss 1361:1536 -j TCPMSS --set-mss 1360
COMMIT

*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]
. . .

Change each instance of eth0 in the above configuration to match the interface name you found with ip route. The *nat lines create rules so that the firewall can correctly route and manipulate traffic between the VPN clients and the internet. The *mangle line adjusts the maximum packet segment size to prevent potential issues with certain VPN clients.

Next, after the *filter and chain definition lines, add one more block of configuration:

. . .
*filter
:ufw-before-input - [0:0]
:ufw-before-output - [0:0]
:ufw-before-forward - [0:0]
:ufw-not-local - [0:0]

-A ufw-before-forward --match policy --pol ipsec --dir in --proto esp -s 10.10.10.0/24 -j ACCEPT
-A ufw-before-forward --match policy --pol ipsec --dir out --proto esp -d 10.10.10.0/24 -j ACCEPT

These lines tell the firewall to forward ESP (Encapsulating Security Payload) traffic so the VPN clients will be able to connect. ESP provides additional security for our VPN packets as they’re traversing untrusted networks.

When you’re finished, save and close the file.

Before we restart the firewall, we’ll change some network kernel parameters to allow routing from one interface to another. Open UFW’s kernel parameters configuration file:

$ sudo nano /etc/ufw/sysctl.conf

We’ll need to configure a few things here:

  • First, we’ll enable IPv4 packet forwarding.
  • We’ll disable Path MTU discovery to prevent packet fragmentation problems.
  • We also won’t accept ICMP redirects nor send ICMP redirects to prevent man-in-the-middle attacks.

The changes you need to make to the file are highlighted in the following code:

. . .

# Enable forwarding
# Uncomment the following line
net/ipv4/ip_forward=1

. . .

# Do not accept ICMP redirects (prevent MITM attacks)
# Ensure the following line is set
net/ipv4/conf/all/accept_redirects=0

# Do not send ICMP redirects (we are not a router)
# Add the following lines
net/ipv4/conf/all/send_redirects=0
net/ipv4/ip_no_pmtu_disc=1

Save the file when you are finished. UFW will apply these changes the next time it starts.

Now, we can enable all of our changes by disabling and re-enabling the firewall:

$ sudo ufw disable
$ sudo ufw enable

You’ll be prompted to confirm the process. Type Y to enable UFW again with the new settings.

Reboot server to make it active.

Step 7 Testing the VPN Connection on iOS, and macOS

Now that you have everything set up, it’s time to try it out. First, you’ll need to copy the CA certificate you created and install it on your client device(s) that will connect to the VPN. The easiest way to do this is to log into your server and output the contents of the certificate file:

$ cat /etc/ipsec.d/cacerts/ca-cert.pem

You’ll see output similar to this:

-----BEGIN CERTIFICATE-----
MIIFQjCCAyqgAwIBAgIIFkQGvkH4ej0wDQYJKoZIhvcNAQEMBQAwPzELMAkGA1UE

. . .

EwbVLOXcNduWK2TPbk/+82GRMtjftran6hKbpKGghBVDPVFGFT6Z0OfubpkQ9RsQ
BayqOb/Q
-----END CERTIFICATE-----

Copy this output to your computer, including the —–BEGIN CERTIFICATE—– and —–END CERTIFICATE—– lines, and save it to a file with a recognizable name, such as ca-cert.pem. Ensure the file you create has the .pem extension.

Alternatively, use SFTP to transfer the file to your computer.

Once you have the ca-cert.pem file downloaded to your computer, you can set up the connection to the VPN.

Connecting from iOS

To configure the VPN connection on an iOS device, follow these steps:

  1. Send yourself an email with the root certificate attached.
  2. Open the email on your iOS device and tap on the attached certificate file, then tap Install and enter your passcode. Once it installs, tap Done.
  3. Go to SettingsGeneralVPN and tap Add VPN Configuration. This will bring up the VPN connection configuration screen.
  4. Tap on Type and select IKEv2.
  5. In the Description field, enter a short name for the VPN connection. This could be anything you like.
  6. In the Server and Remote ID field, enter the server’s domain name or IP address. The Local ID field can be left blank.
  7. Enter your username and password in the Authentication section, then tap Done.
  8. Select the VPN connection that you just created, tap the switch on the top of the page, and you’ll be connected.

Connecting from macOS

Follow these steps to import the certificate:

  1. Double-click the certificate file. Keychain Access will pop up with a dialog that says “Keychain Access is trying to modify the system keychain. Enter your password to allow this.”
  2. Enter your password, then click on Modify Keychain
  3. Double-click the newly imported VPN certificate. This brings up a small properties window where you can specify the trust levels. Set IP Security (IPSec) to Always Trust and you’ll be prompted for your password again. This setting saves automatically after entering the password.

Now that the certificate is important and trusted, configure the VPN connection with these steps:

  1. Go to System Preferences and choose Network.
  2. Click on the small “plus” button on the lower-left of the list of networks.
  3. In the popup that appears, Set Interface to VPN, set the VPN Type to IKEv2, and give the connection a name.
  4. In the Server and Remote ID field, enter the server’s domain name or IP address. Leave the Local IDblank.
  5. Click on Authentication Settings, select Username, and enter your username and password you configured for your VPN user. Then click OK.

Finally, click on Connect to connect to the VPN. You should now be connected to the VPN.

Link: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-ikev2-vpn-server-with-strongswan-on-ubuntu-18-04-2

文章目录

本次“超级账本Fabric的部署”采用的操作系统为Ubuntu 18.0.4。

1. 安装Docker

安装步骤见Ubuntu 18.0.4 安装Docker

2. 安装Docker管理服务

docker-compose是一个基于Python的docker实例管理软件包
安装方法如下:

$ sudo apt-get install -y python-pip
$ sudo apt-get install -y docker-compose

3. 从Dockerhub获取镜像并安装

超级账本相关的docker镜像列表可通过
https://hub.docker.com/u/hyperledger/查询及获取

在进行拉取镜像前请确保docker服务处于启动状态,启动命令如下:

$ service docker start

3.1 拉取fabric-peer

$ docker pull hyperledger/fabric-peer

3.2 拉取fabric-orderer

$ docker pull hyperledger/fabric-orderer

3.3 拉取fabric-ca

$ docker pull hyperledger/fabric-ca

3.4 拉取fabric-tools

$ docker pull hyperledger/fabric-tools

4. 参考文献

  1. 《区块链原理、设计与应用》 杨保华、陈昌 编著

文章目录

1. 测试Docker仓库网络

先测试Docker仓库的网络是否通畅

curl https://download.docker.com/linux/ubuntu

如果能够正常返回,则说明网络通畅,可进行下一步。

返回的范例如下:

302 Moved Temporarily

Code: Found

Message: Resource Found

RequestId: F509FDDBC0786438

HostId: T7hUnvhqto7LWjE1TrTrRSdCApMtS0RvKDkJjeJxBAdC8TKrAziaS4FE5xJGFqEeqLAHd8CxcRA=

2. 添加源

如果第一步顺利通过,则可以添加apt源,命令如下:

$ sudo apt update
$ sudo apt install apt-transport-https ca-certificates curl software-properties-common

执行完上述命令后,添加docker.list文件

新增/etc/apt/sources.list.d/docker.list文件,并添加如下内容:

deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable

添加docker源秘钥

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –

更新一下新添加的源

$sudo apt-get update

安装docker-ce

sudo apt install docker-ce

测试docker是否安装成功

$ docker –version
Docker version 18.09.1, build 4c52b90

文章目录

1. 欧氏距离(Euclidean Distance)

欧氏距离是最易于理解的一种距离计算方法,源自欧氏空间中两点间的距离公式。

  • 二维平面上两点a(x1,y1)b(x2,y2)间的欧氏距离:

  • 三维空间两点a(x1,y1,z1)b(x2,y2,z2)间的欧氏距离:

  • 两个n维向量a(x11,x12,…,x1n) b(x21,x22,…,x2n)间的欧氏距离:

  • 也可以用表示成向量运算的形式:

2. 曼哈顿距离(Manhattan Distance)

从名字就可以猜出这种距离的计算方法了。想象你在曼哈顿要从一个十字路口开车到另外一个十字路口,驾驶距离是两点间的直线距离吗?显然不是,除非你能穿越大楼。实际驾驶距离就是这个曼哈顿距离。而这也是曼哈顿距离名称的来源, 曼哈顿距离也称为城市街区距离(City Block distance)

  • 二维平面两点a(x1,y1)b(x2,y2)间的曼哈顿距离

  • 两个n维向量a(x11,x12,…,x1n) b(x21,x22,…,x2n)间的曼哈顿距离

3. 切比雪夫距离 ( Chebyshev Distance )

国际象棋玩过么?国王走一步能够移动到相邻的8个方格中的任意一个。那么国王从格子(x1,y1)走到格子(x2,y2)最少需要多少步?这个距离就叫切比雪夫距离。

  • 二维平面两点a(x1,y1)b(x2,y2)间的切比雪夫距离

  • 两个n维向量a(x11,x12,…,x1n) b(x21,x22,…,x2n)间的切比雪夫距离

  • 这个公式的另一种等价形式是

4. 闵可夫斯基距离(Minkowski Distance)

闵氏距离不是一种距离,而是一组距离的定义,是对多个距离度量公式的概括性的表述。

两个n维变量a(x11,x12,…,x1n) b(x21,x22,…,x2n)间的闵可夫斯基距离定义为:

  其中p是一个变参数。

  当p=1时,就是曼哈顿距离

  当p=2时,就是欧氏距离

  当p时,就是切比雪夫距离

      根据变参数的不同,闵氏距离可以表示一类的距离。

5. 标准化欧氏距离 (Standardized Euclidean Distance)

标准化欧氏距离是针对简单欧氏距离的缺点而作的一种改进方案。标准欧氏距离的思路:既然数据各维分量的分布不一样,那先将各个分量都标准化到均值、方差相等。假设样本集X的均值(mean)m,标准差(standard deviation)s,那么X标准化变量表示为:

  • 标准化后的值 =  ( 标准化前的值  分量的均值 ) /分量的标准差
  • 经过简单的推导就可以得到两个n维向量a(x11,x12,…,x1n) b(x21,x22,…,x2n)间的标准化欧氏距离的公式:

  • 如果将方差的倒数看成是一个权重,这个公式可以看成是一种加权欧氏距离(Weighted Euclidean distance)

6. 马氏距离(Mahalanobis Distance)

马氏距离的引出:

上图有两个正态分布的总体,它们的均值分别为ab,但方差不一样,则图中的A点离哪个总体更近?或者说A有更大的概率属于谁?显然,A离左边的更近,A属于左边总体的概率更大,尽管Aa的欧式距离远一些。这就是马氏距离的直观解释。

  • 概念

马氏距离是基于样本分布的一种距离。物理意义就是在规范化的主成分空间中的欧氏距离。所谓规范化的主成分空间就是利用主成分分析对一些数据进行主成分分解。再对所有主成分分解轴做归一化,形成新的坐标轴。由这些坐标轴张成的空间就是规范化的主成分空间。

  • M个样本向量X1~Xm,协方差矩阵记为S,均值记为向量μ,则其中样本向量Xu的马氏距离表示为:

  • 而其中向量XiXj之间的马氏距离定义为:

  • 若协方差矩阵是单位矩阵(各个样本向量之间独立同分布),则公式就成了:

也就是欧氏距离了。若协方差矩阵是对角矩阵,公式变成了标准化欧氏距离。

7. 余弦距离(Cosine Distance)

几何中,夹角余弦可用来衡量两个向量方向的差异;

机器学习中,借用这一概念来衡量样本向量之间的差异。

  • 二维空间中向量A(x1,y1)与向量B(x2,y2)的夹角余弦公式:

  • 两个n维样本点a(x11,x12,…,x1n)b(x21,x22,…,x2n)的夹角余弦为:

即:

夹角余弦取值范围为[-1,1]。余弦越大表示两个向量的夹角越小,余弦越小表示两向量的夹角越大。当两个向量的方向重合时余弦取最大值1,当两个向量的方向完全相反余弦取最小值-1

8. 汉明距离(Hamming Distance)

  • 定义

两个等长字符串s1s2的汉明距离为:将其中一个变为另外一个所需要作的最小字符替换次数。

例如

      The Hamming distance between “1011101” and “1001001” is 2.               

      The Hamming distance between “2143896” and “2233796” is 3.               

      The Hamming distance between “toned” and “roses” is 3.                                   

  • 汉明重量

字符串相对于同样长度的零字符串的汉明距离,也就是说,它是字符串中非零的元素个数:对于二进制字符串来说,就是 1 的个数,所以 11101 的汉明重量是 4。因此,如果向量空间中的元素ab之间的汉明距离等于它们汉明重量的差a-b

  • 应用

汉明重量分析在包括信息论、编码理论、密码学等领域都有应用。比如在信息编码过程中,为了增强容错性,应使得编码间的最小汉明距离尽可能大。但是,如果要比较两个不同长度的字符串,不仅要进行替换,而且要进行插入与删除的运算,在这种场合下,通常使用更加复杂的编辑距离等算法。

9. 杰卡德距离(Jaccard Distance)

  • 杰卡德相似系数(Jaccard similarity coefficient)

两个集合AB的交集元素在AB的并集中所占的比例,称为两个集合的杰卡德相似系数,用符号J(A,B)表示:

杰卡德相似系数是衡量两个集合的相似度一种指标。

  • 杰卡德距离(Jaccard Distance)

与杰卡德相似系数相反,用两个集合中不同元素占所有元素的比例来衡量两个集合的区分度:

10. 相关距离(Correlation distance)

  • 相关系数

衡量随机变量XY相关程度的一种方法,相关系数的取值范围是[-1,1]。相关系数的绝对值越大,则表明XY相关度越高。当XY线性相关时,相关系数取值为1(正线性相关)或-1(负线性相关):

  • 相关距离

11. 信息熵(Information Entropy)

以上的距离度量方法度量的皆为两个样本(向量)之间的距离,而信息熵描述的是整个系统内部样本之间的一个距离,或者称之为系统内样本分布的集中程度(一致程度)、分散程度、混乱程度(不一致程度)。系统内样本分布越分散(或者说分布越平均),信息熵就越大。分布越有序(或者说分布越集中),信息熵就越小。

  • 计算给定的样本集X的信息熵的公式:

  • 参数的含义:

n:样本集X的分类数

piX中第 i 类元素出现的概率

信息熵越大表明样本集S的分布越分散(分布均衡),信息熵越小则表明样本集X的分布越集中(分布不均衡)。当Sn个分类出现的概率一样大时(都是1/n),信息熵取最大值log2(n)。当X只有一个分类时,信息熵取最小值0

问耕 编译自 Source Dexter

作者简介:akshay pai,数据科学工程师,热爱研究机器学习问题。Source Dexter网站创办人。

TensorFlow是Google的开源深度学习库,你可以使用这个框架以及Python编程语言,构建大量基于机器学习的应用程序。而且还有很多人把TensorFlow构建的应用程序或者其他框架,开源发布到GitHub上。

这次跟大家分享一些GitHub上令人惊奇的TensorFlow项目,你可以直接在你的应用中使用,或者根据自身所需进一步予以改进。

TensorFlow简介

如果你已经知道TensorFlow是什么,以及它是如何工作的,建议直接跳到下一节。如果你对使用Python学习TensorFlow感兴趣,网上也有相关的教程可以参考。

这一节内容比较傲适合初学者。如前所述,TensorFlow是一个深度学习库,使用这一框架,可以用来构建和测试深度神经网络。

深度学习让我们能够以极高的准确性构建复杂的应用程序。图像、视频、文本、音频等领域的问题,都可以通过深度学习解决。TensorFlow可以用于实现前述所有应用。

2015年11月9日,Google正式发布并开源TensorFlow,目前官方正式版本为TensorFlow 1.2。下面这段视频,是当年TensorFlow刚发布时,Google发布的一个官方介绍短片,Jeff Dean等出镜讲述。

TensorFlow很快成为GitHub上用户最多的深度学习框架。这个库之所以如此流行,是因为开发人员可以轻松的用其来搭建、测试和部署机器学习应用。

TensorFlow使用数据流图,上面是一个TensorFlow的示意图。我们不用关心这张图具体代表什么,但你需要知道,其中的椭圆和正方形代表节点,节点就是一些数学函数,你可以将节点分组形成各种数学计算,并得到输出。

箭头代表流动到各个节点的数据。所以TensorFlow也被成为数据流库。

OK,简介到此,下面分享四个我非常喜欢的TensorFlow GitHub项目。

项目一:Neural Style

这是最酷的TensorFlow GitHub项目之一。神经风格是将一张照片的风格迁移到另一张照片上的过程,同时保留相关的特张。简单的来说,通过这个项目,你可以使用TensorFlow创建自己的Prisma应用程序。

如上图所示,把梵高画作的风格,迁移到一张狮子的照片上,就得到一个星空风格的狮子照片,这就是所谓的风格迁移。下面再展示一组这个项目的风格迁移,能把这张狮子的照片,变成哪些非常有趣的效果。

这个项目在风格迁移的同时,还能对图像进行语义分割。进而实现对内容的前景、背景进行不同的风格迁移处理。这个过程如下图所示:

另外这个算法还能适用于视频处理。

上述图片处理使用的软硬件如下:

CPU: Intel Core i7-6800K@3.4GHz×12

GPU: NVIDIA GeForce GTX 1080/PCIe/SSE2

OS: Linux Ubuntu 16.04.1 LTS 64-bit

CUDA: 8.0

python: 2.7.12

tensorflow: 0.10.0rc

opencv: 2.4.9.1

GitHub地址如下:

https://github.com/cysmith/neural-style-tf

项目二:Mozilla Deep Speech

这个GitHub项目使用TensorFlow将语音转换为文本。语音转文本是一个热门的机器学习领域,然而各地的人们有着不同的口音,这也是一个难以解决的问题。不过仍然可以通过深度学习实现非常不错的准确性。

其实这个项目,是一个基于百度DeepSpeech架构的TensorFlow实现。换句话说,这个项目是百度的内核,Google的外貌。

百度的相关论文地址在此:

https://arxiv.org/abs/1412.5567

DeepSpeech是吴恩达带领百度团队研发出的成果,最早发布于2014年底。今年初,百度基于DeepSpeech2,开发出一款名为SwiftScribe的应用(swiftscribe.ai),可以把语音文件更为快速、便捷的转换为文字。目前只限于英文。

回到这个项目。

所需软件环境如下:

Git Large File Storage

TensorFlow 1.0 or 1.1

SciPy

PyXDG

python_speech_features (nb: deprecated)

python sox

pandas

DeepSpeech native client libraries

如果你有至少8GB显存的英伟达GPU,强烈建议安装支持GPU的TensorFlow,因为使用GPU的训练比CPU快得多。

GitHub地址如下:

https://github.com/mozilla/DeepSpeech

项目三:句子分类

句子分类就是识别句子类型的过程。例如,对于“食物非常糟糕”这个句子,你可能希望将其分类为正面句子或负面句子,这也被称为情绪分析。这个问题的难点在于:句子结构带来的复杂性。

利用卷积神经网络,我们可以尝试构建一个强大的文本分类器。

这里介绍的项目,是Yoon Kim论文《Convolutional Neural Networks for Sentence Classification(使用卷积神经网络进行句子分类)》的简单实现。这个论文的地址如下:

https://arxiv.org/abs/1408.5882

通过一个简单的CNN卷积神经网络,只进行很少的超参数调整和静态矢量,就可以得到出色的句子分类结果。

所需软件环境如下:

Python 3

Tensorflow > 0.12

Numpy

GitHub地址如下:

https://github.com/dennybritz/cnn-text-classification-tf

项目四:图像分类/物体识别

图像分类,也就是训练系统识别猫猫狗狗,或者车道、海滩、天际线等场景。计算机视觉是一个范围巨大的领域,从面部识别到情感识别,甚至可以进行视觉气体泄漏检测。虽然实现流程各异,但底层系统是相通的。

所以作者创建了一个TensorFlow GitHub库,其中包括两个相互关联的部分。第一部分:能够识别1000个对象的TensorFlow图像分类器。第二部分:创建并训练一个全新的图像分类器。

我们先来看看第一部分。

Google的TensorFlow图像识别系统是目前最准确的图像分类软件。所谓图像识别,就是对图片中的内容进行识别,然而这并非对任意图片都能识别。

只有被训练过的对象,系统才能识别。例如,我们用三个类型训练分类器:猫、狗和牛。三个分类器只能识别相应类别中的一个。如果给出一张骆驼的图片会怎样?图片仍会通过一个分类器,但是置信率会非常的低。

如何快速创建一个TensorFlow图像分类器?只需要简单的三步。当然前提是你已经装好了TensorFlow,而且懂编程、会用Python。

第一步:下载

下载预训练的模型、图片和脚本。使用如下命令即可。

git clone https://github.com/akshaypai/tfClassifiercd tfClassifier

第二步:运行脚本找到最佳预测

给定一个图片,运行脚本来进行分类。默认情况下,会显示最佳预测。

Python classifier.py –image_file file_path_to_image

为了获得top n分类,可以使用如下参数。

Python classifier.py –image_file file_path_to_image –num_top_predictions number_of_top_results

示例:比方我们给出这样一张图片。

python classifier.py –image_file ~/Pictures/fruit.jpgpomegranate (score = 0.98216)

分类器表示:这是石榴,准确率98%。

第三步:运行脚本获得top n识别分类

现在我们尝试给出一个具有更多属性的图像,如下图的房子。

python classifier.py –image_file ~/Pictures/house.jpg –num_top_predictions 5picket fence, paling (score = 0.95750)worm fence, snake fence, snake-rail fence, Virginia fence (score = 0.03615)beacon, lighthouse, beacon light, pharos (score = 0.00018)boathouse (score = 0.00013)patio, terrace (score = 0.00007)

从上面的结果可以看出,分类器以95%的可能性预测图片中有一个栅栏,另外分类器还发现了其他围栏、庭院、露台等。

到此为止,你已经学到如何设置TensorFlow图像识别系统。虽然,这个系统被限制在预训练模型的几个分类器之间。

那么我们接下来看看第二部分,如何增加一些新的分类器。

也是三个步骤。

第一步:下载预训练模型以及所需脚本

我已经把全部所需的文件整合进一个git仓库里。使用下面的命令可以下载。

git clone https://github.com/akshaypai/tfClassifiercd tfClassifier

第二步:设置图像文件夹

这个步骤用于设置文件夹结构,以便数据流图可以简单地拾取分类。假设,你想重新训练五种新的花朵分类器:玫瑰、郁金香、蒲公英、五月花和万寿菊,那么需要如下的三个步骤来创建相应的文件夹结构:

1、为每种花型创建一个文件夹,文件夹的名称就是类型的名称

2、将花的所有图像添加到各自的文件夹中,所有的玫瑰放入玫瑰花文件夹

3、将所有的文件夹,添加到一个父文件夹中,可以命名为:花

然后我们就得到如下的结构:

~/flowers~/flowers/roses/img1.jpg~/flowers/roses/img2.jpg…~/flowers/tulips/tulips_img1.jpg~/flowers/tulips/tulips_img2.jpg~/flowers/tulips/tulips_img3.jpg…

这样,文件夹结构已经OK了。

第三步:运行预训练脚本

使用如下命令运行脚本。

python retrain.py –model_dir ./inception –image_dir ~/flowers –output_graph ./output –how_many_training_steps 500

部分命令行参数:

-model_dir这个参数给出了预训练模型的位置。

-image_dir在步骤二中创建的文件夹路径

-output_graph存储新训练图的位置

-how_many_training_steps这代表要执行的迭代次数,默认为4000

好了,以上就是如何重新训练一个TensorFlow Inception模型。一旦你有了模型,就能开始用来进行分类处理。

结论

我希望你们也能体会到上述这些项目的精彩之处。学习一件事最好的方法就是动手去做。如果你还有更值得推荐的TensorFlow GitHub项目,可以留言给我们,让更多的人看到。今天就先到这里吧。休息,休息一下~

 

link: http://www.techweb.com.cn/news/2017-07-31/2566452.shtml

文章目录

Problem

You want to use different shapes and line types in your graph.

Solution

Note that with bitmap output, the filled symbols 15-18 may render without proper anti-aliasing; they can appear jagged, pixelated, and not properly centered, though this varies among platforms. Symbols 19 and 21-25 have an outline around the filled area, and will render with smoothed edges on most platforms. For symbols 21-25 to appear solid, you will also need to specify a fill (bg) color that is the same as the outline color (col); otherwise they will be hollow.

Standard graphics

Use the pch option to set the shape, and use lty and lwd to set the line type and width. The line type can be specified by name or by number.

set.seed(331)

# Plot some points with lines
# Set up the plotting area
par(mar=c(3,3,2,2))
plot(NA, xlim=c(1,4), ylim=c(0,1))

# Plot solid circles with solid lines
points(1:4, runif(4), type="b", pch=19)
# Add open squares with dashed line, with heavier line width
points(1:4, runif(4), type="b", pch=0,  lty=2, lwd=3)

points(1:4, runif(4), type="b", pch=23,   # Diamond shape
       lty="dotted", cex=2,               # Dotted line, double-size shapes
       col="#000099", bg="#FF6666")       # blue line, red fill

ggplot2

With ggplot2, shapes and line types can be assigned overall (e.g., if you want all points to be squares, or all lines to be dashed), or they can be conditioned on a variable.

# Sample data
df <- read.table(header=T, text='
  cond xval yval
     A    1  2.0
     A    2  2.5
     B    1  3.0
     B    2  2.0
')


library(ggplot2)

# Plot with standard lines and points
# group = cond tells it which points to connect with lines
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line() +
    geom_point()

# Set overall shapes and line type
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line(linetype="dashed",  # Dashed line
              size = 1.5) +       # Thicker line
    geom_point(shape = 0,         # Hollow squares
               size = 4)          # Large points

# Condition shapes and line type on variable cond
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line(aes(linetype=cond), # Line type depends on cond
              size = 1.5) +       # Thicker line
    geom_point(aes(shape=cond),   # Shape depends on cond
               size = 4)          # Large points


# Same as previous, but also change the specific linetypes and
# shapes that are used
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line(aes(linetype=cond), # Line type depends on cond
              size = 1.5) +       # Thicker line
    geom_point(aes(shape=cond),   # Shape depends on cond
               size = 4) +        # Large points
    scale_shape_manual(values=c(6,5)) +                  # Change shapes
    scale_linetype_manual(values=c("dotdash", "dotted")) # Change linetypes

By default, ggplot2 uses solid shapes. If you want to use hollow shapes, without manually declaring each shape, you can use scale_shape(solid=FALSE). Note, however, that the lines will visible inside the shape. To avoid this, you can use shapes 21-25 and specify a white fill.

# Hollow shapes
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line(aes(linetype=cond), # Line type depends on cond
              size = 1.5) +       # Thicker line
    geom_point(aes(shape=cond),   # Shape depends on cond
               size = 4)  +       # Large points
    scale_shape(solid=FALSE)

# Shapes with white fill
ggplot(df, aes(x=xval, y=yval, group = cond)) +
    geom_line(aes(linetype=cond), # Line type depends on cond
              size = 1.5) +       # Thicker line
    geom_point(aes(shape=cond),   # Shape depends on cond
               fill = "white",    # White fill
               size = 4)  +       # Large points
    scale_shape_manual(values=c(21,24))  # Shapes: Filled circle, triangle

 

Note

This code will generate the chart of line types seen at the top.

par(mar=c(0,0,0,0))

# Set up the plotting area
plot(NA, xlim=c(0,1), ylim=c(6.5, -0.5),
    xaxt="n", yaxt="n",
    xlab=NA, ylab=NA )

# Draw the lines
for (i in 0:6) {
    points(c(0.25,1), c(i,i), lty=i, lwd=2, type="l")
}
# Add labels
text(0, 0, "0. 'blank'"   ,  adj=c(0,.5))
text(0, 1, "1. 'solid'"   ,  adj=c(0,.5))
text(0, 2, "2. 'dashed'"  ,  adj=c(0,.5))
text(0, 3, "3. 'dotted'"  ,  adj=c(0,.5))
text(0, 4, "4. 'dotdash'" ,  adj=c(0,.5))
text(0, 5, "5. 'longdash'",  adj=c(0,.5))
text(0, 6, "6. 'twodash'" ,  adj=c(0,.5))

文章目录

Bioconductor version: Release (3.7)

Multivariate data analysis and graphical display of microarray data. Functions include between group analysis and coinertia analysis. It contains functions that require ADE4.

Author: Aedin Culhane

Maintainer: Aedin Culhane <Aedin at jimmy.harvard.edu>

Citation (from within R, enter citation(“made4”)):

AC C, J T, G P, DG H (2005). “MADE4:an R package for multivariate analysis of gene expression data.” Bioinformatics, 21(11), 2789-90.

Installation

To install this package, start R and enter:
## try http:// if https:// URLs are not supported
source("https://bioconductor.org/biocLite.R")
biocLite("made4")

Documentation

To view documentation for the version of this package installed in your system, start R and enter:
browseVignettes("made4")

link: https://www.bioconductor.org/packages/release/bioc/html/made4.html

京ICP备15063075号