溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

發布時間:2020-09-20 08:01:41 來源:腳本之家 閱讀:238 作者:星空比上 欄目:開發技術

本文介紹在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/選擇最新版本

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

開始安裝anaconda

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解關于win10在tensorflow的安裝及在pycharm中運行步驟詳解關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

選擇安裝位置

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

勾選后,點擊 install

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

等待一段時間

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

安裝完成,直接退出

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解 關于win10在tensorflow的安裝及在pycharm中運行步驟詳解 關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

安裝好anaconda以后,打開cmd輸入conda --version” ----->得到conda 4.7.12,安裝成功

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

anaconda3就安裝好了

開始安裝tensorflow

國外原地址下載太慢,這里設置國內鏡像源,否則特別慢。。。。:

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/    

conda config --set show_channel_urls yes

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解 關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

我們先安裝tensorflow2.0版本創建新的環境tensorflow2,輸入: conda create -n tensorflow2 python=3.7

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

輸入 y

開始自動下載文件(可以看到下載的Python版本為3.7.6版本,文件目錄在E:\anaconda3\envs中,后面配置時會用到),

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

激活剛才創建的環境,輸入 : activate tensorflow2

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

然后就開始安裝TensorFlow,輸入: pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.0.0-beta1

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

接下來自動安裝好了,出現下面提示就安裝好了,哈哈!

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

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

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

安裝過程中,如需pip9.0.1升級pip20:

輸入 python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple

運行tensorflow

既然fensorflow安裝好了,我現在用pycharm打開運行一段代碼,首先配置pycharm

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

打開設置–項目–項目編輯器–點擊Add

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

按下面步驟,設置環境就ok了

關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

我們設置一個新環境,將環境再改為剛安裝好的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, 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, [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 損失函數
loss = -tf.reduce_mean(tf.reduce_sum(y * tf.log(out + 1e-8), axis=1), axis=0)
# 準確率
correct_pred = tf.equal(tf.argmax(y, axis=1), 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], 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)

結果如下:

`[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運行內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女