關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行步驟詳解
本文介紹在win10中安裝tensorflow的步驟:
1、安裝anaconda3
2、新建conda環(huán)境變量,可建多個(gè)環(huán)境在內(nèi)部安裝多個(gè)tensorflow版本,1.x和2.x版本功能差別太大,代碼也很大區(qū)別
3、環(huán)境中安裝python和fensorflow
4、用tensorflow運(yùn)行一段測試程序
安裝anaconda下載地址(清華鏡像):
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/選擇最新版本
開始安裝anaconda
選擇安裝位置
勾選后,點(diǎn)擊 install
等待一段時(shí)間
安裝完成,直接退出
安裝好anaconda以后,打開cmd輸入conda --version”
----->得到conda 4.7.12,安裝成功
anaconda3就安裝好了
開始安裝tensorflow
國外原地址下載太慢,這里設(shè)置國內(nèi)鏡像源,否則特別慢。。。。:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
我們先安裝tensorflow2.0版本創(chuàng)建新的環(huán)境tensorflow2,輸入: conda create -n tensorflow2 python=3.7
輸入 y
開始自動下載文件(可以看到下載的Python版本為3.7.6版本,文件目錄在E:\anaconda3\envs中,后面配置時(shí)會用到),
激活剛才創(chuàng)建的環(huán)境,輸入 : activate tensorflow2
然后就開始安裝TensorFlow,輸入: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.0.0-beta1
接下來自動安裝好了,出現(xiàn)下面提示就安裝好了,哈哈!
python的版本不一樣,運(yùn)行環(huán)境也不一樣,如果還要安裝1.x版本,(這里安裝tensorflow1.9.0版本),再次進(jìn)入cmd中
創(chuàng)建新的1.x版本環(huán)境
輸入 :conda create -n tensorflow1 python=3.6 激活新環(huán)境
輸入 : 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
運(yùn)行tensorflow
既然fensorflow安裝好了,我現(xiàn)在用pycharm打開運(yùn)行一段代碼,首先配置pycharm
打開設(shè)置–項(xiàng)目–項(xiàng)目編輯器–點(diǎn)擊Add
按下面步驟,設(shè)置環(huán)境就ok了
我們設(shè)置一個(gè)新環(huán)境,將環(huán)境再改為剛安裝好的tensorflow1.9.0的版本,測試運(yùn)行一個(gè)小程序。
# -*- 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, 1, [self.n_test, 2]) y_test = (x_test[:, 1] > x_test[:, 0]).astype(np.int32) return x, y, x_test, y_test # 隨機(jī)生成數(shù)據(jù) 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, [None, 2], 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, 2], 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 損失函數(shù) loss = -tf.reduce_mean(tf.reduce_sum(y * tf.log(out + 1e-8), axis=1), axis=0) # 準(zhǔn)確率 correct_pred = tf.equal(tf.argmax(y, axis=1), tf.argmax(out, axis=1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # 定義優(yōu)化器 train_op = tf.train.AdamOptimizer(1e-3).minimize(loss) # 1e-3 是學(xué)習(xí)律 # 初始化網(wǎng)絡(luò) # BATCH_SIZE = 128 EPOCH = 7000 # 優(yōu)化次數(shù) 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], feed_dict={x: X_train, 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, feed_dict={x: X_test, 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)
結(jié)果如下:
`[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]`
其中出現(xiàn) Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 這個(gè)沒問題,可以忽略,能正常運(yùn)行出結(jié)果。
總結(jié)
到此這篇關(guān)于關(guān)于win10在tensorflow的安裝及在pycharm中運(yùn)行步驟詳解的文章就介紹到這了,更多相關(guān)tensorflow安裝pycharm運(yùn)行內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例
今天小編就為大家分享一篇pytorch-神經(jīng)網(wǎng)絡(luò)擬合曲線實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

Python中Matplotlib繪圖保存圖片時(shí)調(diào)節(jié)圖形清晰度或分辨率的方法

Python中利用all()來優(yōu)化減少判斷的實(shí)例分析

Python使用Pandas生成日報(bào)的實(shí)現(xiàn)代碼

Pytorch建模過程中的DataLoader與Dataset示例詳解