原因是 N1 盒子 emmc 出现坏块导致,解决方法:用 U 盘启动 N1 盒子进入 Armbian,然后输入下面的命令:
e2fsck /dev/mmcblk1p2
修复成功后拔掉 U 盘重启 N1 盒子。
原因是 N1 盒子 emmc 出现坏块导致,解决方法:用 U 盘启动 N1 盒子进入 Armbian,然后输入下面的命令:
e2fsck /dev/mmcblk1p2
修复成功后拔掉 U 盘重启 N1 盒子。
现在(2020年3月28日,March 28th, 2020)网上能搜到的内容都过时了,正解如下:
dd
to burn ISO to USB——用 dd
命令把 ISO 烧进 U 盘那么第一步,怎么把 DMG 转为 ISO 呢?这里需要用到一个工具叫 dmg2img
,下面是 brew
安装方法:
brew install dmg2img
然后,
dmg2img <path_to_your_dmg_file> <path_to_your_iso_file>
就把 DMG 转为 ISO了。
第二步,把 ISO 烧进 U 盘:
sudo dd if=<path_to_your_iso_file> of=<your_destination_disk> bs=4m
具体可以用 diskutil list
命令查看自己 U 盘的标识
五步走:
Git Server
,安装https://packages.synocommunity.com
,名称写 社群
,确定Z shell (with modules)
,安装sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
chsh
,所以新建~/.profile
文件,粘贴以下内容后保存if [[ -x /usr/local/bin/zsh ]]; then export SHELL=/usr/local/bin/zsh exec /usr/local/bin/zsh fi
lsof -n -i4TCP:$PORT | grep LISTEN lsof -n -iTCP:$PORT | grep LISTEN lsof -n -i:$PORT | grep LISTEN
其中$PORT
替换为指定的端口号,或者是用英文逗号连接的一组端口号。
git config --global core.quotepath false
macOS 升级到 High Sierra 后,telnet
命令竟然不见了!身为码畜,是可忍孰不可忍……
如果你还没有安装过 Homebrew
,先用下面的命令安装上:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
也是一条命令的事儿……
brew install telnet
equal(
x,
y,
name=None
)
对输入的 x
和 y
两个 Tensor
逐元素(element-wise)做 (x == y) 逻辑比较,返回 bool
类型的 Tensor
。
x
只支持以下类型:half
, float32
, float64
, uint8
, int8
, int16
, int32
, int64
, complex64
, quint8
, qint8
, qint32
, string
, bool
, complex128
y
的类型必须与 x
相同name
给这个操作取一个名称,可选bool
类型的 Tensor
broadcasting
,详见 Numpy
文档。x
和 y
拥有相同的 shape
import tensorflow as tf a = tf.constant([1, 2], tf.int32) b = tf.constant([2, 2], tf.int32) with tf.Session() as sess: print(sess.run(tf.equal(a, b))) # 输出 [False True]
broadcasting
用法:x
和 y
不同 shape
x = tf.constant(["hehe", "haha", "hoho", "kaka"], tf.string) y = tf.constant("hoho", tf.string) with tf.Session() as sess: print(sess.run(tf.equal(x, y))) # 输出 [False False True False]
注意观察上面这个栗子,实际解决了在一个数组中查找某个元素索引(index
)的问题,这个特性配合 tf.cast
在生成 one-hot
向量时将会特别有用。
a = tf.constant([[1], [2]], tf.int32) b = tf.constant([[2, 1]], tf.int32) with tf.Session() as sess: print(sess.run(tf.equal(a, b))) # 输出 # [[False True] # [ True False]]
学一门新的语言,第一个实战例子总是打印 hello world
;在机器学习领域,也有一个 hello world,那就是 MNIST
手写数字识别——当然,它可比打印字符串有意义多了。
MNIST 是深度学习巨巨 Yann LeCun 维护的一套手写数字图像数据库,包含 60000 个训练样本和 10000 个测试样本,所有这些图像已经做过规范化及居中处理,拥有同样的固定尺寸,使用起来非常方便。下面是其中几个例子。
很明显,我们马上要开始编写第一个 TensorFlow
程序,一个有实际应用价值的深度神经网络(DNN)
——虽然只有 2 个隐层,做 MNIST 手写数字识别,484 很鸡冻……
万事开头难,迈出第一步非常关键。为了避免陷入对无限发散的未知知识点(技能树)的恐慌,我们尽量将注意力集中在主线任务上,旁枝末节以后慢慢看,当然,楼主假设你还是会写基本的 Python
的。废话不多说,动手吧!
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
input_data
封装了 MNIST 数据的下载、解析功能,read_data_sets()
返回三部分数据:55000 个训练样本 mnist.train
,5000 个验证样本 mnist.validation
(注意到没有,这 5000 是从原始的 60000 个训练样本上拿出来的),10000 个测试样本 mnist.test
。
每个样本都包含一个手写数字图像 x
和一个对应的标签 y
,训练集的图像和标签可以通过 mnist.train.images
和 mnist.train.labels
读取,验证集和测试集同理。图像的尺寸是 28x28 像素,每个像素值代表 [0,1] 的笔划强度,我们可以把图像数据理解为一个长度为 784 的数组,也就是一个 784 维的向量。标签的取值为 [0,9] ,表示从 0 到 9 这 10 个数字,这里我们把标签处理成 10 维 one-hot
向量以方便对应 DNN 的输出。
接下来我们搭建一个拥有 2 个隐层的 DNN
模型,第一层拥有 1024 个神经元,第二层拥有 625 个神经元,使用 ReLU
作为激活函数,并在输入层和隐层各层都使用 dropout
机制避免模型发生过拟合,这些概念暂时不懂也没有关系,把实验做完,不懂的再去查。
import tensorflow as tf X = tf.placeholder(tf.float32, [None, 784]) Y = tf.placeholder(tf.float32, [None, 10]) p_keep_input = tf.placeholder(tf.float32) p_keep_hidden = tf.placeholder(tf.float32) def init_weights(shape): return tf.Variable(tf.random_normal(shape, stddev=0.01)) w_h = init_weights([784, 1024]) w_h2 = init_weights([1024, 625]) w_o = init_weights([625, 10]) def model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden): X = tf.nn.dropout(X, p_keep_input) h = tf.nn.relu(tf.matmul(X, w_h)) h = tf.nn.dropout(h, p_keep_hidden) h2 = tf.nn.relu(tf.matmul(h, w_h2)) h2 = tf.nn.dropout(h2, p_keep_hidden) return tf.matmul(h2, w_o) py_x = model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden)
为了训练模型,必须有一个指标衡量模型的好坏,这个指标就是损失(loss 或 cost)
,loss 越接近于 0 表明模型的输出越接近于真实的标签,我们选择最常用的交叉熵 cross-entropy
作为损失函数, RMSProp
优化算法做梯度下降
。
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)
我们把模型输出的标签与真实的标签进行比较,并将比较结果转换为一个取值 [0, 1] 的浮点数作为准确率
指标。
predict_acc = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(py_x, 1), tf.argmax(Y, 1)), tf.float32))
接下来,我们该启动程序了:
epoch_count = 20000 batch_size = 50 keep_input = 0.8 keep_hidden = 0.75 with tf.Session() as sess: sess.run(tf.global_variables_initializer()) step = 0 for i in xrange(epoch_count): step += 1 batch_x, batch_y = mnist.train.next_batch(batch_size) sess.run(train_op, feed_dict={X: batch_x, Y: batch_y, p_keep_input: keep_input, p_keep_hidden: keep_hidden}) if step % 100 == 0: loss, acc = sess.run([cost, predict_acc], feed_dict={X: batch_x, Y: batch_y, p_keep_input: 1., p_keep_hidden: 1.}) print("Epoch: {}".format(step), "\tLoss: {:.6f}".format(loss), "\tTraining Accuracy: {:.5f}".format(acc)) print("Testing Accuracy: {:0.5f}".format(sess.run(predict_acc, feed_dict={X: mnist.test.images, Y: mnist.test.labels, p_keep_input: 1., p_keep_hidden: 1.})))
整个训练过程共 20000 步,每步使用 50 个一组的随机样本做训练,每训练 100 步输出一次训练准确率,全部训练结束后使用测试集 mnist.test
评估准确率。
我们的 DNN 模型很简单,同时也很争气,准确率大概是 98.26%,有没有感觉到一种成就感……
虽然实验做完了,结果也还算不错,但此刻我们应该冷静下来认真做一下回顾,把整篇文章中不太明白的概念整理出来,深入地去做一下理论知识学习。这里推荐一本免费的电子书:Neural Networks and Deep Learning,拿来入门非常合适。
这里将介绍如何在 Ubuntu 16.04 LTS
系统上搭建 支持 GPU 的 TensorFlow 1.4.0
开发环境。
这取决于你有没有一块儿支持 CUDA 的 NVIDIA 显卡。如果没有,只能选择 CPU 版本。如果有,继续往下看。
deb (network)
,最后会给出一个下载链接和一系列的命令,类似:sudo dpkg -i cuda-repo-ubuntu1604_9.0.176-1_amd64.deb
sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub
sudo apt-get update
sudo apt-get install cuda
修改 PATH
环境变量export PATH=/usr/local/cuda-9.0/bin${PATH:+:${PATH}}
修改 LD_LIBRARY_PATH
环境变量export LD_LIBRARY_PATH=/usr/local/cuda-9.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
sudo apt install nvidia-384
驱动安装成功后,可以使用下面的命令查看显卡状态:nvidia-smi
sudo dpkg -i libcudnn7_7.0.3.11-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-dev_7.0.3.11-1+cuda9.0_amd64.deb
sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb
sudo apt install libcupti-dev
修改 LD_LIBRARY_PATH
环境变量export LD_LIBRARY_PATH=/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
安装预编译的包或者从源码编译都是可行的。
python 2.7/3.n
都可以。
# for Python 2.7
sudo apt-get install python-pip python-dev
sudo pip install -U pip setuptools
# for Python 3.n
sudo apt-get install python3-pip python3-dev
sudo pip3 install -U pip setuptools
pip install tensorflow # Python 2.7; CPU support (no GPU support)
pip3 install tensorflow # Python 3.n; CPU support (no GPU support)
pip install tensorflow-gpu # Python 2.7; GPU support
pip3 install tensorflow-gpu # Python 3.n; GPU support
r1.4
分支git clone https://github.com/tensorflow/tensorflow
git checkout r1.4
sudo apt-get install openjdk-8-jdk
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
sudo apt-get update && sudo apt-get install bazel
sudo apt-get install python-numpy python-dev python-pip python-wheel # for Python 2.7
sudo apt-get install python3-numpy python3-dev python3-pip python3-wheel # for Python 3.n
cd tensorflow # 进入第 1 步克隆的仓库根目录 ./configure Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7 Found possible Python library paths: /usr/local/lib/python2.7/dist-packages /usr/lib/python2.7/dist-packages Please input the desired Python library path to use. Default is [/usr/lib/python2.7/dist-packages] Using python library path: /usr/local/lib/python2.7/dist-packages Do you wish to build TensorFlow with MKL support? [y/N] No MKL support will be enabled for TensorFlow Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: Do you wish to use jemalloc as the malloc implementation? [Y/n] jemalloc enabled Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] No Google Cloud Platform support will be enabled for TensorFlow Do you wish to build TensorFlow with Hadoop File System support? [y/N] No Hadoop File System support will be enabled for TensorFlow Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] No XLA support will be enabled for TensorFlow Do you wish to build TensorFlow with VERBS support? [y/N] No VERBS support will be enabled for TensorFlow Do you wish to build TensorFlow with OpenCL support? [y/N] No OpenCL support will be enabled for TensorFlow Do you wish to build TensorFlow with CUDA support? [y/N] Y CUDA support will be enabled for TensorFlow Do you want to use clang as CUDA compiler? [y/N] nvcc will be used as CUDA compiler Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 8.0]: 9.0 Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 6.0]: 7 Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: Please specify a list of comma-separated Cuda compute capabilities you want to build with. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: "3.5,5.2"]: 6.1 Do you wish to build TensorFlow with MPI support? [y/N] MPI support will not be enabled for TensorFlow Configuration finished
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
whl
包在 /tmp/tensorflow_pkg
目录下,文件名可能略有不同sudo pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp27-cp27mu-linux_x86_64.whl
$ python
# Python import tensorflow as tf hello = tf.constant('Hello, TensorFlow!') sess = tf.Session() print(sess.run(hello))如果能看到下面的输出
Hello, TensorFlow!
恭喜你,安装成功了……请参考详细的官方文档
wget -r -np -nH --cut-dirs=3 -R index.html http://hostname/aaa/bbb/ccc/ddd/