[python練習] numpy

引用

import numpy as np;

1維陣列

a = np.array([1, 2, 3]);

2維陣列

a = np.array([ [1, 2, 3], [4, 5, 6] ]);

3維陣列

a = np.array([ [ [1, 2, 3], [4, 5, 6] ], [ [7, 8, 9], [10, 11, 12] ] ]);

內積 (vector dot)

vector dot

v1 = np.array([1, 2, 3]);
v2 = np.array([4, 5, 6]);
v3 = np.dot(v1,v2); #v3 = 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32
print(v3);

矩陣相乘 (matrix multiplication)

matrix_multiplication

m1 = np.array([ [1, 2, 3], [4, 5, 6] ]);      #size: 2*3
m2 = np.array([ [1, 2 ], [3, 4], [5, 6] ]);   #size: 3*2
m3 = np.matmul(m1,m2); # m3= [ [ 1*1+2*3+3*5 1*2+2*4+3*6] [ 4*1+5*3+6*5 4*2+5*4+6*6 ] ] = [ [ 22 28 ] [ 49 64 ] ]
print(m3);

元素相乘 (multiply)

  • 不常用
m1 = np.arange(1,5).reshape(2,2); # m1 = [ [ 1 2 ] [ 3 4 ] ]
m2 = np.arange(0,4).reshape(2,2); # m2 = [ [ 0 1 ] [ 2 3 ] ]
m3 = np.multiply(m1,m2);          # m3 = [ [ 1*0 2*1 ] [ 3*2 4*3 ] ] = [ [ 0 2 ] [ 6 12 ] ]
print(m3);

原始碼

[python練習] pyplot

import matplotlib.pyplot as plt
import numpy as np

#產生隨機資料
x_data = np.linspace(-100, 100, 300)[:, np.newaxis];
#加入雜訊
noise = np.random.normal(0, 300, x_data.shape);
y_data = np.square(x_data) - 0.5 + noise;

plt.plot(x_data, y_data, 'g.');

#動態繪製sin曲線
x = np.arange(-100,100) 
for i in range(1000):
  #嘗試移除畫過的sin曲線,第一次會發生錯誤,因此用try/except
  try:
    lines.pop(0).remove()
  except Exception:
    pass
  #重新計算y值
  y = 1000*np.sin((x+i) * np.pi/ 9.0)
  #繪製sin曲線
  lines=plt.plot(x,y,'r-')
  #延遲0.1秒
  plt.pause(0.1)

原始碼

在centos 7上安裝nvidia docker

#安裝 docker-ce
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce-18.06.0.ce -y
systemctl enable docker && systemctl start docker


# 移除舊版nvidia-docker
docker volume ls -q -f driver=nvidia-docker | xargs -r -I{} -n1 docker ps -q -a -f volume={} | xargs -r docker rm -f
sudo yum remove nvidia-docker

# 加入repositories
distribution=$(. /etc/os-release;echo $ID$VERSION_ID)
curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.repo | sudo tee /etc/yum.repos.d/nvidia-docker.repo

# 安裝 nvidia-docker2
sudo yum install -y nvidia-docker2
sudo pkill -SIGHUP dockerd

# 測試
docker run --runtime=nvidia --rm nvidia/cuda:9.0-base nvidia-smi

refer: https://github.com/NVIDIA/nvidia-docker

install cuda 10.0 on centos 7

如下:

#安裝必要套件
yum –y update
yum install kernel-devel kernel-headers gcc dkms acpid libglvnd-glx libglvnd-opengl libglvnd-devel pkgconfig pciutils gcc-c++ wget
yum groupinstall "Development Tools" -y

#Nvidia 硬體檢查
lspci | grep -E "VGA|3D"

#Blacklist nouveau
echo "blacklist nouveau" >> /etc/modprobe.d/nvidia-installer-disable-nouveau.conf && echo "options nouveau modeset=0" >> /etc/modprobe.d/nvidia-installer-disable-nouveau.conf

#regenerate the initramfs
dracut /boot/initramfs-$(uname -r).img $(uname -r) --force
reboot

wget http://tw.download.nvidia.com/XFree86/Linux-x86_64/410.78/NVIDIA-Linux-x86_64-410.78.run
bash NVIDIA-Linux-x86_64-410.78.run

#下載cuda
wget https://developer.nvidia.com/compute/cuda/10.0/Prod/local_installers/cuda_10.0.130_410.48_linux
mv cuda_10.0.130_410.48_linux cuda_10.0.130_410.48_linux.run

#安裝cuda
bash cuda_10.0.130_410.48_linux.run

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-10.0/lib64
export CUDA_HOME=/usr/local/cuda-10.0
export PATH=$PATH:/usr/local/cuda-10.0/bin

cd ~/NVIDIA_CUDA-10.0_Samples/1_Utilities/deviceQuery
make
./deviceQuery

cd ~/NVIDIA_CUDA-10.0_Samples/0_Simple/vectorAdd
make
./vectorAdd

Latent Semantic Analysis (LSA) 測試

參考下列幾個網址寫出來的code (for Matlab)
http://lsa.colorado.edu/papers/dp1.LSAintro.pdf
https://blog.csdn.net/pipisorry/article/details/42560331
https://read01.com/zh-tw/0zeKRM.html#.W7C8PhMzbKZ

C=[ [ 0,0,1,1,0,0,0,0,0 ];
    [ 0,0,0,0,0,1,0,0,1 ];
    [ 0,1,0,0,0,0,0,1,0 ];
    [ 0,0,0,0,0,0,1,0,1 ];
    [ 1,0,0,0,0,1,0,0,0 ];
    [ 1,1,1,1,1,1,1,1,1 ];
    [ 1,0,1,0,0,0,0,0,0 ];
    [ 0,0,0,0,0,0,1,0,1 ];
    [ 0,0,0,0,0,2,0,0,1 ];
    [ 1,0,1,0,0,0,0,1,0 ];
    [ 0,0,0,1,1,0,0,0,0 ]; ];

[w,s,p]=svd(C);

plot(-p(:,2),-p(:,3),'.')
hold on
plot(-w(:,2),-w(:,3),'.')


Label_1={'book';'dads';'dummies';'estate';'guide';'investing';'market';'real';'rich';'stock';'value'};
text(-w(:,2),-w(:,3),Label_1,'Color','red','FontSize',14);

Label_2={'T1';'T2';'T3';'T4';'T5';'T6';'T7';'T8';'T9';};
text(-p(:,2),-p(:,3),Label_2,'Color','blue','FontSize',14);
hold off

結果呈現如下:

MongoDB Replica Set高可用性的建置

  1. 建立三個MongoDB
echo "[info] 建立測試環境";
docker network create mongo-cluster >/dev/null 2>&1
docker rm -f mongo1 mongo2 mongo3 >/dev/null 2>&1
for i in 1 2 3
do
  echo "       mongo${i} 建立"
  docker run \
    -d \
    -p 3000$i:27017 \
    --name mongo$i \
    --net mongo-cluster \
    mongo mongod --replSet my-mongo-set >/dev/null 2>&1
done
  1. 接著設定三者關係,並強制設定electionTimeoutMillis為500ms.
echo "       設定mongo cluster"
docker exec -it mongo3 mongo --eval "config={\"_id\":\"my-mongo-set\",\"members\":[{\"_id\":0,\"host\":\"mongo1:27017\"},{\"_id\":1,\"host\":\"mongo2:27017\"},{\"_id\":2,\"host\":\"mongo3:27017\"}],\"settings\": { \"electionTimeoutMillis\": 500 }}; rs.initiate(config)"
  1. PHP測試
#https://github.com/mongodb/mongo-php-library
require_once __DIR__ . "/vendor/autoload.php";

try{
  $client = new MongoDB\Client("mongodb://mongo1:27017,mongo2:27017,mongo3:27017",[],[
    'typeMap' => [
      'array' => 'array',
      'document' => 'array',
      'root' => 'array',
    ],
  ]);
  $db = $client->selectDatabase('test');
  $cursor = $db->command(['isMaster' => 1]);
  echo "[info] 成功連線至: {$cursor->toArray()[0]['primary']}\n";

} catch(Exception $e) {
  echo "[error] 無法線連\n";
  exit;
}

{  //若Collection:testA不存在則建立.
  if((iterator_count($db->listCollections([
    'filter'=>[
      'name'=>'testA'
    ]
  ])))==0) //當數量為0時,建立testA
    $db->createCollection('testA');
}

//連線至Collection:testA
$collection=$db->selectCollection('testA');

//若沒有任何資料,則建立100則隨機資料
if($collection->count()==0){
  for($i=0;$i<100;$i++){
    $insertOneResult = $collection->insertOne([
      'idx' => $i,
      'value' => "{$i}, rand: ".rand(0,9999),
    ]);
  }
}

//取得最後三筆資料
foreach($collection->find([],[
  'limit' => 3,
  'projection' => ['_id'=>0],
  'sort' => [
    'idx'=>-1
  ]
]) as $rs){
  echo "\tidx:{$rs['idx']}\tvalue:{$rs['value']}\n";
}

使用google map geolocation api對raspberry pi進行地理定位

下面範例使用了Google Map GeoLocation Api對raspberry pi進行地理定位,
其中key可在https://console.developers.google.com申請,每日免費額度是2500(每100秒限制10000次)

/*
npm i pi-wifi google-geolocation
node main.js
*/
function geolocation(callback){
  var geolocation = require ('google-geolocation') ({
    key: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  });
  var piWifi = require('pi-wifi');

  piWifi.scan(function(err, networks) {
    if (err) {
      return console.error(err.message);
    }
    //console.log(networks);

    var items=[];
    for(i in networks){
      items.push({
        'macAddress': networks[i].bssid,
        'signalStrength': networks[i].signalLevel,
      });
    }

    var params={
      wifiAccessPoints: items,
    };

    geolocation (params, (err, data) => {
      if (err) {
        return;
      }

      var result={
        'lat': data.location.lat,
        'lng': data.location.lng,
        'accuracy': data.accuracy,
        'type': 'wifi',
      };

      if(callback &&  typeof(callback) == "function")
        callback(result);
    });
  });
}

geolocation(function(location){
  console.log(location);
  var url ="https://www.google.com.tw/maps/search/"+location.lat+","+location.lng;
  console.log(url);
});

install nodejs 6 on raspberry pi

armv6的raspberry pi似乎無法直接用apt-get install方式安裝.
所以改用下列語法安裝

wget http://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-armv6l.tar.gz && \
tar -xzf node-v6.9.5-linux-armv6l.tar.gz && \
mv node-v6.9.5-linux-armv6l nodejs && \
rm node-v6.9.5-linux-armv6l.tar.gz && \
cp -R * /usr/local/ && \
rm -r -f nodejs && \
ln -s /usr/local/nodejs/bin/node /usr/bin/node && \
ln -s /usr/local/nodejs/bin/npm /usr/bin/npm

install & test cuda 9.0 on ubuntu 16.04

安裝

wget https://developer.nvidia.com/compute/cuda/9.0/Prod/local_installers/cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo service lightdm stop
sudo dpkg -i cuda-repo-ubuntu1604-9-0-local_9.0.176-1_amd64-deb
sudo apt-key add /var/cuda-repo-9-0-local/7fa2af80.pub
sudo apt-get update && sudo apt-get install cuda -y
sudo reboot
sudo ln -s /usr/local/cuda/bin/nvcc /usr/bin/nvcc

設定環境變數

編輯.bashrc

vim ~/.bashrc

加入

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64:/usr/lib/nvidia-367
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin

測試編譯

方法1

cd /usr/local/cuda/samples/0_Simple/vectorAdd
make
./vectorAdd

方法2

建立測試檔案vectorAdd.cu

#include <stdio.h>
#include <cuda_runtime.h>

__global__ void vectorAdd(const float *A, const float *B, float *C, int numElements){
  int i = blockDim.x * blockIdx.x + threadIdx.x;
  if (i < numElements){
    C[i] = A[i] + B[i];
  }
}

int main(void){
  int numElements = 50000;

  //初始化測試資料
  float *h_A=new float[numElements];
  float *h_B=new float[numElements];
  float *h_C=new float[numElements];
  for (int i = 0; i < numElements; ++i) {
    h_A[i] = rand()/(float)RAND_MAX;
    h_B[i] = rand()/(float)RAND_MAX;
  }

  //配置GPU記憶體空間,並從記憶體中複製資料至GPU中
  size_t size = numElements * sizeof(float);
  float *d_A = NULL; cudaMalloc((void **)&d_A, size);
  float *d_B = NULL; cudaMalloc((void **)&d_B, size);
  float *d_C = NULL; cudaMalloc((void **)&d_C, size);
  cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice);
  cudaMemcpy(d_B, h_B, size, cudaMemcpyHostToDevice);

  //運算
  int threadsPerBlock = 256;
  int blocksPerGrid =(numElements + threadsPerBlock – 1) / threadsPerBlock;
  vectorAdd<<<blocksPerGrid, threadsPerBlock>>>(d_A, d_B, d_C, numElements);

  //取回運算結果
  cudaMemcpy(h_C, d_C, size, cudaMemcpyDeviceToHost);

  //清除GPU記憶體空間
  cudaFree(d_A);
  cudaFree(d_B);
  cudaFree(d_C);

  //驗證資料
  for (int i = 0; i < numElements; ++i) {
    if (fabs(h_A[i] + h_B[i] – h_C[i]) > 1e-5) {
      fprintf(stderr, "Result verification failed at element %d!\n", i);
      exit(EXIT_FAILURE);
    }
  }

  //清除記憶體
  delete d_A;
  delete d_B;
  delete d_C;

  printf("Test PASSED\n");
  return 0;
}

接著手動編譯~

/usr/local/cuda-9.0/bin/nvcc \
  -ccbin g++  \
  -m64 \
  -gencode arch=compute_30,code=sm_30 \
  -gencode arch=compute_35,code=sm_35 \
  -gencode arch=compute_37,code=sm_37 \
  -gencode arch=compute_50,code=sm_50 \
  -gencode arch=compute_52,code=sm_52 \
  -gencode arch=compute_60,code=sm_60 \
  -gencode arch=compute_70,code=sm_70 \
  -gencode arch=compute_70,code=compute_70 \
  -c vectorAdd.cu -o vectorAdd.o

/usr/local/cuda-9.0/bin/nvcc \
  -ccbin g++ \
  -m64 \
  -gencode arch=compute_30,code=sm_30 \
  -gencode arch=compute_35,code=sm_35 \
  -gencode arch=compute_37,code=sm_37 \
  -gencode arch=compute_50,code=sm_50 \
  -gencode arch=compute_52,code=sm_52 \
  -gencode arch=compute_60,code=sm_60 \
  -gencode arch=compute_70,code=sm_70 \
  -gencode arch=compute_70,code=compute_70 \
  vectorAdd.o -o vectorAdd