2021年9月30日 星期四

在Visual Studio安裝TensorFlow C語言環境

標題:在Visual Studio 2019安裝Tensorflow C語言環境

搜尋標題:Visual Studio 2019 install Tensorflow for C


今天要來用Visual Studio 2019執行C語言版本的Tensorflow。

這個教學給的範例應該不難,有問題再留言問我。


直接進入正題吧!


首先,點進去這個網址:

https://www.tensorflow.org/install/lang_c


然後依照作業系統和CPU/GPU等需求下載檔案。



然後會下載到一個檔名類似「libtensorflow-gpu-windows-x86_64-2.6.0.zip」的檔案,把它解壓縮。


解壓縮後把資料夾貼到C磁碟機或你想要的地方。

我們會看到資料夾中有個include資料夾和lib資料夾。

這兩個資料夾的名稱,要記好,等一下會使用好幾次。



然後開啟Visual Studio,建立C++版本的主控台應用程式。


建立好後按專案/<專案名稱>屬性。



在VC++目錄的「include目錄」加上include資料夾的路徑。

我的範例是「C:\libtensorflow-gpu-windows-x86_64-2.6.0\include」

在VC++目錄的「程式庫目錄」加上lib資料夾的路徑。

我的範例是「C:\libtensorflow-gpu-windows-x86_64-2.6.0\lib」



然後去連接器/輸入/其他相依性 的地方,輸入tensorflow.lib。

「tensorflow.lib」存在於lib資料夾。

都設定完成後按套用確定。



然後輸入以下程式碼(可以直接複製):

#include <iostream>
#include <stdio.h>
#include <tensorflow/c/c_api.h> 
using namespace std;
int main()
{
    printf("Hello from TensorFlow C library version %s\n"TF_Version());
    int a;
    cin >> a;
}

如圖:


這段程式是要把TensorFlow的版本print出來的,可用來確認有正確使用到TensorFlow。
然後cin是為了防止TensorFlow版本印出來後發生視窗立即閃退的情況。

輸入完程式碼後建置程式:



接著去專案中的「Debug/Release」資料夾開啟該程式。

可能會出現「找不到tensorflow.dll」之類的錯誤。


沒關係,我們去剛才解壓縮後的lib資料夾,找到「tensorflow.dll」檔案,複製到跟執行檔相同資料夾位置的地方。


複製dll檔過去後,再次啟動程式,就可以讀取到TensorFlow了。


顯示出來的,就是TensorFlow的版本(我的版本是2.6.0),如果看見TensorFlow的版本被顯示出來了,代表TensorFlow的環境建置成功了喔。

2021年9月20日 星期一

建立token並push程式到github上

標題:[教學]建立token並push程式到github上(2021/09)

完整標題:[教學]建立access token並push程式到github上(2021/09)


最近使用git push新版本的程式到github上時,卻發現不能push。


原來,現在github都要求要用access token登入才能push程式到github上了。原來的密碼不再被允許push。


現在來建立一個access token吧。

首先,進入github,到右邊的地方點自己的頭貼,然後按「Settings」。


按左邊的「Developer settings」


按左邊的「Personal access tokens」,然後按右邊的「Generate new token」


輸入Note(可隨意輸入),然後選擇日期,並勾選「repo」、「admin:repo_hook」等選項


按「Generate token」


然後就會看到你的「Personal access tokens」被建立了,把它複製起來,並保存下來(它只會顯示一次)。


回到git介面,再次push程式。

它會請求輸入github帳號,就把帳號輸入進去。



然後會要求輸入密碼,就貼上剛剛複製的「Personal access tokens」就好。


按下「OK」之後,就可以成功push了。