https://www.leiphone.com/news/201606/ORlQ7uK3TIW8xVGF.htmlhtml
1.官方網站
目前最新版本Anaconda 4.3.1 For Linux。分Python3.6和2.7版本,32和64位 。選擇合適版本
速度慢些
2.清華大學的Anaconda
速度快python
在清華大學 TUNA 鏡像源選擇對應的操做系統與所需的Python版本下載Anaconda安裝包。Windows環境下的安裝包直接執行.exe文件進行安裝便可,Ubuntu環境下在終端執行react
$ bash Anaconda2-4.3.1-Linux-x86_64.sh #Python 2.7版本
或者linux
$ bash Anaconda3-4.3.1-Linux-x86_64.sh #Python 3.5 版本
打開終端,進入Anaconda所在的文件夾下
運行該命令:bash Anaconda3-4.3.0-Linux-x86_64.shgit
在清華大學 TUNA 鏡像源選擇對應的操做系統與所需的Python版本下載Anaconda安裝包。Windows環境下的安裝包直接執行.exe文件進行安裝便可,Ubuntu環境下在終端執行github
$ bash Anaconda2-4.3.1-Linux-x86_64.sh #Python 2.7版本
或者shell
$ bash Anaconda3-4.3.1-Linux-x86_64.sh #Python 3.5 版本
在安裝的過程當中,會詢問安裝路徑,按回車便可。以後會詢問是否將Anaconda安裝路徑加入到環境變量(.bashrc)中,輸入yes,這樣之後在終端中輸入python便可直接進入Anaconda的Python版本(若是你的系統中以前安裝過Python,自行選擇yes or no)。安裝成功後,會有當前用戶根目錄下生成一個anaconda2的文件夾,裏面就是安裝好的內容express
查詢安裝信息apache
$ conda info
查詢當前已經安裝的庫ubuntu
$ conda list
安裝庫(***表明庫名稱)
$ conda install ***
更新庫
$ conda update ***
官方下載更新工具包的速度很慢,因此繼續添加清華大學 TUNA提供的Anaconda倉庫鏡像,在終端或cmd中輸入以下命令進行添加
$ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ $ conda config --set show_channel_urls yes $ conda install numpy #測試是否添加成功
以後會自動在用戶根目錄生成「.condarc」文件,Ubuntu環境下路徑爲~/.condarc,Windows環境下路徑爲C:\用戶\your_user_name\.condarc
channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ - defaults show_channel_urls: yes
若是要刪除鏡像,直接刪除「.condarc」文件便可
1.安裝好以後便可在終端輸入spyder,相似pycharm的一個IDE
2.自帶notebook IDE。打開方式,終端輸入ipython notebook便可打開該IDE。關於notebook,可自行了解
3.Linux 自己安裝的2.7版本仍然存在,Python3和anaconda合併
4.關於conda命令,終端輸入conda list 便可查看已經安裝的模塊 。conda install * conda update *
5.綜上,咱們安裝好之後,從此使用ipython便可,方便好用
在ubuntu上卸載anaconda的步驟 :
(一)刪除整個anaconda目錄:
因爲Anaconda的安裝文件都包含在一個目錄中,因此直接將該目錄刪除便可。到包含整個anaconda目錄的文件夾下,刪除整個Anaconda目錄:
rm -rf anaconda文件夾名
(二)建議清理下.bashrc中的Anaconda路徑:
1.到根目錄下,打開終端並輸入:
sudo gedit ~/.bashrc
2.在.bashrc文件末尾用#號註釋掉以前添加的路徑(或直接刪除):
#export PATH=/home/lq/anaconda3/bin:$PATH
保存並關閉文件
3.使其當即生效,在終端執行:
source ~/.bashrc
4.關閉終端,而後再重啓一個新的終端,這一步很重要,否則在原終端上仍是綁定有anaconda.
$ conda create -n tensorflow $ source activate tensorflow $ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.1.0-cp36-cp36m-linux_x86_64.whl
這裏,若是想安裝其餘版本(CPU only 和 GPU support 都有),參考
tensorflow on Ubuntu
$ sudo chown -R 用戶 anaconda3/ $ sudo chgrp -R 用戶 anaconda3/ (用戶爲本身用戶名)
在conda環境中安裝tensorflow的好處是能夠便捷的管理tensorflow的依賴包。分爲兩個步驟:激活上一步創建的名爲tensorflow的conda環境;用conda或者pip工具安裝Tensorflow,做者選擇的是pip方式。
pip方式須要首先激活conda環境
而後根據要安裝的不一樣tensorflow版本選擇對應的一條環境變量設置export語句(操做系統,Python版本,CPU版本仍是CPU+GPU版本)
最後根據是python 2仍是3版本選擇一句進行安裝。
conda上面目前有人已經作好了tensorflow的pkg,可是版本不必定最新,且只有CPU版本,不支持GPU。
步驟也是首先激活conda環境,而後調用conda install 語句安裝.
上面的步驟完成後,從conda環境中退出:
tensorflow自帶幾個示例程序,詳細位置以下:
…/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/models
Anaconda2是Anaconda的安裝位置,第一個tensorflow爲conda建立的名字爲tensorflow的環境,第二個tensorflow即安裝的tensorflow包。進入image下的mnist目錄,而後運行python convolutional.py就是對mnist的訓練過程。
5. spyder裏import tensorflow報錯顯示沒有tensorflow模塊解決
安裝完anaconda後anaconda會自帶spyder這個IDE,可是咱們在打開這個IDE輸入import tensorflow會顯示沒有TensorFlow這個模塊的錯誤,不對啊,明明咱們已經安裝了TensorFlow,並且在終端下也測試了沒問題啊。這裏我也查看了網上關於這個問題地許多教程,感受都不太明朗,這裏給出我不知在哪兒看到的解決方案:
之因此會出現這個問題大概是anaconda自帶的spyder沒有工做在tensorflow環境下的緣由。想要在TensorFlow環境下使用spyder,那就在tensorflow環境下再安裝一個spyder嘛。好,那咱們開始安裝。
在tensorflow環境下,在終端用conda安裝spyder。輸入一下命令:
conda install spyder
而後會看見安裝目錄位於咱們以前建好的tensorflow環境下,也會看到安裝spyder會安裝許多依賴包,而後輸入y,大概10分鐘後就安裝好了。這時咱們在終端輸入spyder打開spyder,在IPython console下輸入import tensorflow,若是沒有報錯則代表安裝成功了。
總結:
注意每次要使用tensorflow時,必定要先在終端激活tensorflow環境:source activate tensorflow
而後輸入spyder打開spyder,這裏的這個spyder是tensorflow環境下的spyder,不使用tensorflow時使用如下命令關掉tensorflow環境source deactivate
在anaconda中建立了一個環境tensorflow,裏面安裝了tensorflow。
# 建立一個環境 conda create --name tensorflow python=2.7 # 使用一個環境 source activate tensorflow
而後在這個環境下打開spyder。而後import tensorflow
,報錯,說找不到tensorflow。
解決辦法是在tensorflow這個環境下,安裝spyder。
而後在這個環境下打開spyder:
這樣就能夠在spyder中使用tensorflow了:
sudo pip install spyder conda install QtPy conda install Pylint
test code:
from skimage import io img=io.imread('d:/dog.jpg') io.imshow(img)
將其中的d:/dog.jpg 改爲你的圖片位置
而後點擊上面工具欄裏的綠色三角進行運行,最終顯示
若是右下角「 Ipython console" 能顯示出圖片,說明咱們的運行環境安裝成功。
咱們能夠選擇右上角的 」 variable explorer" 來查看圖片信息,如
咱們能夠把這個程序保存起來,注意python腳本文件的後綴名爲py.
若是沒有遇到和 protobuf pip 包相關的問題,你能夠跳過這個部分。
注意: 若是你的 TensorFlow 運行緩慢,你可能遇到了一個 protobuf pip 包相關的問題。
TensorFlow pip 包依賴於 protobuf pip 包 3.1。 從 PyPI 下載的 protobuf 是 proto 序列化/反序列化的純 Python 實現的庫, 其速度比 C++ 實現慢10-50倍。Protobuf 支持二進制擴展,速度更快,基於 C++。 但該擴展沒法在純Python實現的 pip 包中獲取。咱們製做了包含該二進制擴展的protobuf pip 包。 以下命令可安裝該自定義的 protobuf pip 包:
for Python 2.7:
$ pip install --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.1.0-cp27-none-linux_x86_64.whl
for Python 3.5:
$ pip3 install --upgrade \ https://storage.googleapis.com/tensorflow/linux/cpu/protobuf-3.1.0-cp35-none-linux_x86_64.whl
安裝這個 protobuf 包會覆蓋已經存在的 protobuf 包。 注意該二進制 pip 包已經支持大於 64MB 的問題,修復了以下問題:
[libprotobuf ERROR google/protobuf/src/google/protobuf/io/coded_stream.cc:207]
Protocol message 被拒絕,由於太大(大於 67108864 字節)。
爲增大限制或禁用報警,
在 google/protobuf/io/coded_stream.h 中查看 CodedInputStream::SetTotalBytesLimit()
Spyder報錯:
import tensorflow as tf
Traceback (most recent call last):
File "<ipython-input-1-41389fad42b5>", line 1, in <module>
import tensorflow as tf
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/__init__.py", line 24, in <module>
from tensorflow.python import *
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 52, in <module>
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 28, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "/home/jz/anaconda2/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
問題描述:
進入python3,而後import tensorflow,沒法import,錯誤代碼:libcudnn.so.6:cannot open sharedobject file: No such file or directory
問題解決:
pip uninstall tensorflow-gpupip install tensorflow-gpu==1.2
問題解釋:
根據錯誤代碼,應該是找不到libcudnn.so.6。這個時候筆者就沒有注意最後是6而不是5,就開始各類試路徑啊,硬連接軟連接啥的。實質上是由於當前時間(2017.8),雖然tensorflow官網install教程裏面說的是安裝cudnn5.1,而tensorflow-gpu1.3已經開始去找cudnn6了(也就是說是用cudnn6編譯的)。。。 理論上能夠安裝cudnn6試試看,但筆者自己沒有追逐最新版的習慣(天知道還有什麼新坑),因此直接換到了tensorflow-gpu1.2版本,就解決問題了。
另注:一般狀況下這類問題是真的找不到文件,而不是找錯文件。這個時候筆者一般會先查一下各類路徑有沒有設置錯誤(寫錯啊,路徑之間的冒號啊),而後去指定路徑看看有沒有這個文件,最後去查一下連接問題(這個就有些複雜了,並且改動起來有風險,建議備份先)。
雖然谷歌官網推薦使用anaconda的安全環境安裝,可是我覺的太麻煩,想用ipython notebook 或者 spyder 就很麻煩,不如直接安裝進去 site-packages
因此這裏 不用直接 進入終端,
# Python 2.7
$ conda create -n tensorflow python=2.7
$ source activate tensorflow
(tensorflow)$ # Your prompt should change
# Ubuntu/Linux 64-bit, CPU only:
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled. Requires CUDA toolkit 7.5 and CuDNN v4. For
# other versions, see "Install from sources" below.
(tensorflow)$ pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
打開你的anaconda文件夾,找到envs 打開tensorflow 將sitepack-ages裏面的東西都考到 anaconda/lib/python2.7/sitepack-ages
4.運行tensorflow
打開終端,輸入 spyder 或者ipython notebook 就能夠實驗了
5.今天發現新辦法,安裝了anaconda以後
在終端直接輸入:conda install -c https://conda.anaconda.org/jjhelmus tensorflow
就能夠了,不用那麼麻煩了,由於將虛擬環境中一出來可能使conda命令失效,安裝了anaconda以後
若是說權限不夠的, 能夠 使用指令: sudo chmod 777 -R anaconda2 以後就能夠安裝tensorflow了
conda install -c https://conda.anaconda.org/jjhelmus tensorflow
若是你試了上面幾種辦法都很差用,那麼能夠下載tensorflow
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl
,將whl文件放到你的anaconda2/lib/python2.7/site-packages/ 裏面,而後打開終端: pip install
tensorflow-0.8.0rc0-cp27-none-linux_x86_64.whl 就能夠了
13:test
在test以前,開啓gpu支持
$ export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64" $ export CUDA_HOME=/usr/local/cuda
2017-9-15日更(成功)
下載1.2版本的whl文件:https://pypi.python.org/pypi/tensorflow-gpu/1.2.1
或者https://anaconda.org/anaconda/tensorflow-gpu
TensorBoard的官網教程以下:
https://www.tensorflow.org/versions/r0.7/how_tos/summaries_and_tensorboard/index.html
簡單解釋下:TensorBoard是個可視化工具,能夠用來查看TensorFlow的圖以及過程當中的各類值和圖像等。
1. 在tensorflow程序中給須要的節點添加「summary operations」,「summary operations」會收集該節點的數據,並標記上第幾步、時間戳等標識,寫入事件文件。
事件文件的形式以下所示:
2. TensorBoard讀取事件文件,並可視化Tensorflow的流程。
python mnist_with_summaries.py
mnist_with_summaries.py的源碼以下:
# Copyright 2015 The TensorFlow Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the 'License'); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an 'AS IS' BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== """A simple MNIST classifier which displays summaries in TensorBoard. This is an unimpressive MNIST model, but it is a good example of using tf.name_scope to make a graph legible in the TensorBoard graph explorer, and of naming summary tags so that they are grouped meaningfully in TensorBoard. It demonstrates the functionality of every TensorBoard dashboard. """ from __future__ import absolute_import from __future__ import division from __future__ import print_function import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data flags = tf.app.flags FLAGS = flags.FLAGS flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data ' 'for unit testing.') flags.DEFINE_integer('max_steps', 1000, 'Number of steps to run trainer.') flags.DEFINE_float('learning_rate', 0.001, 'Initial learning rate.') flags.DEFINE_float('dropout', 0.9, 'Keep probability for training dropout.') flags.DEFINE_string('data_dir', '/tmp/data', 'Directory for storing data') flags.DEFINE_string('summaries_dir', '/tmp/mnist_logs', 'Summaries directory') def train(): # Import data mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True, fake_data=FLAGS.fake_data) sess = tf.InteractiveSession() # Create a multilayer model. # Input placehoolders with tf.name_scope('input'): x = tf.placeholder(tf.float32, [None, 784], name='x-input') y_ = tf.placeholder(tf.float32, [None, 10], name='y-input') with tf.name_scope('input_reshape'): image_shaped_input = tf.reshape(x, [-1, 28, 28, 1]) tf.image_summary('input', image_shaped_input, 10) # We can't initialize these variables to 0 - the network will get stuck. def weight_variable(shape): """Create a weight variable with appropriate initialization.""" initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial) def bias_variable(shape): """Create a bias variable with appropriate initialization.""" initial = tf.constant(0.1, shape=shape) return tf.Variable(initial) def variable_summaries(var, name): """Attach a lot of summaries to a Tensor.""" with tf.name_scope('summaries'): mean = tf.reduce_mean(var) tf.scalar_summary('mean/' + name, mean) with tf.name_scope('stddev'): stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean))) tf.scalar_summary('sttdev/' + name, stddev) tf.scalar_summary('max/' + name, tf.reduce_max(var)) tf.scalar_summary('min/' + name, tf.reduce_min(var)) tf.histogram_summary(name, var) def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu): """Reusable code for making a simple neural net layer. It does a matrix multiply, bias add, and then uses relu to nonlinearize. It also sets up name scoping so that the resultant graph is easy to read, and adds a number of summary ops. """ # Adding a name scope ensures logical grouping of the layers in the graph. with tf.name_scope(layer_name): # This Variable will hold the state of the weights for the layer with tf.name_scope('weights'): weights = weight_variable([input_dim, output_dim]) variable_summaries(weights, layer_name + '/weights') with tf.name_scope('biases'): biases = bias_variable([output_dim]) variable_summaries(biases, layer_name + '/biases') with tf.name_scope('Wx_plus_b'): preactivate = tf.matmul(input_tensor, weights) + biases tf.histogram_summary(layer_name + '/pre_activations', preactivate) activations = act(preactivate, 'activation') tf.histogram_summary(layer_name + '/activations', activations) return activations hidden1 = nn_layer(x, 784, 500, 'layer1') with tf.name_scope('dropout'): keep_prob = tf.placeholder(tf.float32) tf.scalar_summary('dropout_keep_probability', keep_prob) dropped = tf.nn.dropout(hidden1, keep_prob) y = nn_layer(dropped, 500, 10, 'layer2', act=tf.nn.softmax) with tf.name_scope('cross_entropy'): diff = y_ * tf.log(y) with tf.name_scope('total'): cross_entropy = -tf.reduce_mean(diff) tf.scalar_summary('cross entropy', cross_entropy) with tf.name_scope('train'): train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize( cross_entropy) with tf.name_scope('accuracy'): with tf.name_scope('correct_prediction'): correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) with tf.name_scope('accuracy'): accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.scalar_summary('accuracy', accuracy) # Merge all the summaries and write them out to /tmp/mnist_logs (by default) merged = tf.merge_all_summaries() train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train', sess.graph) test_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test') tf.initialize_all_variables().run() # Train the model, and also write summaries. # Every 10th step, measure test-set accuracy, and write test summaries # All other steps, run train_step on training data, & add training summaries def feed_dict(train): """Make a TensorFlow feed_dict: maps data onto Tensor placeholders.""" if train or FLAGS.fake_data: xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data) k = FLAGS.dropout else: xs, ys = mnist.test.images, mnist.test.labels k = 1.0 return {x: xs, y_: ys, keep_prob: k} for i in range(FLAGS.max_steps): if i % 10 == 0: # Record summaries and test-set accuracy summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False)) test_writer.add_summary(summary, i) print('Accuracy at step %s: %s' % (i, acc)) else: # Record train set summaries, and train if i % 100 == 99: # Record execution stats run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True), options=run_options, run_metadata=run_metadata) train_writer.add_run_metadata(run_metadata, 'step%d' % i) train_writer.add_summary(summary, i) print('Adding run metadata for', i) else: # Record a summary summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True)) train_writer.add_summary(summary, i) def main(_): if tf.gfile.Exists(FLAGS.summaries_dir): tf.gfile.DeleteRecursively(FLAGS.summaries_dir) tf.gfile.MakeDirs(FLAGS.summaries_dir) train() if __name__ == '__main__': tf.app.run()
其中
flags.DEFINE_string('summaries_dir', '/tmp/mnist_logs', 'Summaries directory')
標識了事件文件的輸出路徑。該例中,輸出路徑爲/tmp/mnist_logs
tensorboard --logdir=/tmp/mnist_logs/
WARNING:tensorflow:IOError [Errno 2] No such file or directory: '/usr/local/lib/python2.7/dist-packages/tensorflow/tensorboard/TAG' on path /usr/local/lib/python2.7/dist-packages/tensorflow/tensorboard/TAG WARNING:tensorflow:Unable to read TensorBoard tag Starting TensorBoard on port 6006
解決方案:
下載tensorflow的github的源代碼,將tensorflow的tensorboard目錄下的TAG文件拷貝到python下面的tensorboard目錄下便可,個人目錄以下:
sudo cp ~/libsource/tensorflow/tensorflow/tensorflow/tensorboard/TAG /usr/local/lib/python2.7/dist-packages/tensorflow/tensorboard/