關於win10在tensorflow的安裝及在pycharm中執行步驟詳解
本文介紹在win10中安裝tensorflow的步驟:
1、安裝anaconda3
2、新建conda環境變數,可建多個環境在內部安裝多個tensorflow版本,1.x和2.x版本功能差別太大,程式碼也很大區別
3、環境中安裝python和fensorflow
4、用tensorflow執行一段測試程式
安裝anaconda下載地址(清華映象):
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/選擇最新版本
開始安裝anaconda
選擇安裝位置
勾選後,點選 install
等待一段時間
安裝完成,直接退出
安裝好anaconda以後,開啟cmd輸入conda --version”
anaconda3就安裝好了
開始安裝tensorflow
國外原地址下載太慢,這裡設定國內映象源,否則特別慢。。。。:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
我們先安裝tensorflow2.0版本建立新的環境tensorflow2,輸入: conda create -n tensorflow2 python=3.7
輸入 y
開始自動下載檔案(可以看到下載的Python版本為3.7.6版本,檔案目錄在E:\anaconda3\envs中,後面配置時會用到),
啟用剛才建立的環境,輸入 : activate tensorflow2
然後就開始安裝TensorFlow,輸入: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.0.0-beta1
接下來自動安裝好了,出現下面提示就安裝好了,哈哈!
python的版本不一樣,執行環境也不一樣,如果還要安裝1.x版本,(這裡安裝tensorflow1.9.0版本),再次進入cmd中
建立新的1.x版本環境
輸入 :conda create -n tensorflow1 python=3.6 啟用新環境
輸入 : activate tensorflow1 安裝TensorFlow
輸入: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==1.9.0
安裝過程中,如需pip9.0.1升級pip20:
輸入 python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple
執行tensorflow
既然fensorflow安裝好了,我現在用pycharm開啟執行一段程式碼,首先配置pycharm
開啟設定–專案–專案編輯器–點選Add
按下面步驟,設定環境就ok了
我們設定一個新環境,將環境再改為剛安裝好的tensorflow1.9.0的版本,測試執行一個小程式。
# -*- coding: utf-8 -*- """ Created on Mon Nov 19 19:33:03 2018 @author: KUMA """ import numpy as np import tensorflow as tf import os os.environ['CUDA_VISIBLE_DEVICES'] = '0' class LinearSep: def __init__(self): self.n_train = 10 self.n_test = 50 self.x_train,self.y_train,self.x_test,self.y_test = self._gene_data() def _gene_data(self): x = np.random.uniform(-1,1,[self.n_train,2]) y = (x[:,1] > x[:,0]).astype(np.int32) x += np.random.randn(self.n_train,2) * 0.05 x_test = np.random.uniform(-1,[self.n_test,2]) y_test = (x_test[:,1] > x_test[:,0]).astype(np.int32) return x,y,x_test,y_test # 隨機生成資料 dataset = LinearSep() X_train,Y_train = dataset.x_train,dataset.y_train print(Y_train) Y_train = np.eye(2)[Y_train] X_test,Y_test = dataset.x_test,dataset.y_test Y_test = np.eye(2)[Y_test] x = tf.placeholder(tf.float32,[None,2],name='input') y = tf.placeholder(tf.float32,name='output') w1 = tf.get_variable(name='w_fc1',shape=[2,20],dtype=tf.float32) b1 = tf.get_variable(name='b_fc1',shape=[20],dtype=tf.float32) out = tf.matmul(x,w1) + b1 out = tf.nn.relu(out) w2 = tf.get_variable(name='w_fc2',shape=[20,dtype=tf.float32) b2 = tf.get_variable(name='b_fc2',shape=[2],dtype=tf.float32) out = tf.matmul(out,w2) + b2 out = tf.nn.softmax(out) # cross entropy 損失函式 loss = -tf.reduce_mean(tf.reduce_sum(y * tf.log(out + 1e-8),axis=1),axis=0) # 準確率 correct_pred = tf.equal(tf.argmax(y,tf.argmax(out,axis=1)) accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32)) # 定義優化器 train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) # 1e-3 是學習律 # 初始化網路 # BATCH_SIZE = 128 EPOCH = 7000 # 優化次數 sess = tf.Session() sess.run(tf.global_variables_initializer()) for ep in range(EPOCH): sess.run(train_op,feed_dict={x: X_train,y: Y_train}) loss_train,acc_train = sess.run([loss,accuracy],y: Y_train}) acc_test,pre_test = sess.run([accuracy,correct_pred],feed_dict={x: X_test,y: Y_test}) if ep % 1000 == 0: print(ep,loss_train,acc_train,acc_test) print(Y_test.shape) test_pre = sess.run(out,y: Y_test}) print(len(test_pre)) mask = np.argmax(test_pre,axis=1) print(mask) mask_0 = np.where(mask == 0) mask_1 = np.where(mask == 1) X_0 = X_train[mask_0] X_1 = X_train[mask_1] print(X_0)
結果如下:
`[1 0 1 0 1 1 1 0 1 1] T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
0 0.81077516 0.1 0.34 (50,2) 1000 0.013808459 1.0 0.82 (50,2) 2000 0.0025899492 1.0 0.82 (50,2) 3000 0.00088921207 1.0 0.82 (50,2) 4000 0.00038405406 1.0 0.82 (50,2) 5000 0.0001859894 1.0 0.82 (50,2) 6000 8.420033e-05 1.0 0.82 (50,2) 50 [0 1 1 1 0 0 1 1 1 1 1 1 0 1 0 0 1 1 0 1 1 0 0 0 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 0 0 1 1 0 1 1 1]`
其中出現 Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 這個沒問題,可以忽略,能正常執行出結果。
總結
到此這篇關於關於win10在tensorflow的安裝及在pycharm中執行步驟詳解的文章就介紹到這了,更多相關tensorflow安裝pycharm執行內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!