欸里🐹 | 倉鼠型國民
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `tools` # Google Colab : 輕鬆實作和與他人協作 python 程式 ###### 圖片來源:[Google Colaboratory SVG Logo](https://commons.wikimedia.org/wiki/File:Google_Colaboratory_SVG_Logo.svg) ![image](https://hackmd.io/_uploads/r1J7LLY8T.png) <p class="text-center"> *** </p> > 本文參考Google 介紹文件 [歡迎使用 Colaboratory](https://colab.research.google.com/?hl=zh-tw#)。 <p class="text-center"> *** </p> ## 關於 Google Colab Google Colab 全名為 Colaboratory,是一個基於雲端的網頁服務,讓使用者可以使用 Python 語言進行程式編輯,並且支援多人協作功能,以及免費的 GPU/TPU 運算資源,非常適合學生、教師和研究人員使用。 ## 基本使用方式 ### 安裝 1. 先登入 Google 的帳號,如果還沒有安裝過 Colab 的話,可以在 Google Workspace 的商店中搜尋安裝。 ![Untitled](https://hackmd.io/_uploads/SJq8uUFL6.png) 2. 進入雲端硬碟後,點選「新增」,便會新開啟 Colab 的頁面。Google Drive 會自動在雲端空間中建立一個 `Colab Notebooks` 資料夾,新建立的程式預設會以 `ipynb` 檔(Python Notebook,筆記本)的形式放置在這個資料夾中。 ![螢幕擷取畫面 2023-12-14 141605](https://hackmd.io/_uploads/SJ1RcItUa.png) ![螢幕擷取畫面 2023-12-14 142231](https://hackmd.io/_uploads/HJjIcLFIT.png) ### 主要功能 在Colab 上基本上都是使用 Colab 筆記本(`ipynb` 檔)來運作,它是一個動態互動網頁,使用者可以在筆記本中建立多個儲存格(cell),並利用儲存格編寫程式或文字內容。 - **程式碼儲存格** Colab 的程式碼儲存格介面類似於 Jupyter Notebook,使用者可以在儲存格中編寫 Python 程式碼,然後點擊左邊的箭頭圖示運行這段程式碼片段(或者使用快捷鍵 `cmd/ctrl` + `Enter`),執行程式碼後的結果會顯示儲存格下面。 在 Google Colab 中,不同的程式碼儲存格屬於同一個筆記本檔案,雖然每個儲存格可以獨立執行,但它們其實共享相同的執行環境和記憶體空間,因此一個儲存格中定義的變數、函數或導入的函式庫可以在同一筆記本的其他儲存格中被使用。 ![螢幕擷取畫面 2023-12-14 223341](https://hackmd.io/_uploads/HkxxFLY8T.png) > :bulb: **安裝套件與導入套件** > > 基於 Python 是由上往下執行的運行邏輯,如果某些函式是源自於導入的套件、函式庫,而他們則分屬不同的儲存格,則必須先執行導入函式庫的儲存格才能避免執行函式的儲存格出錯。(可以從上方選單中選擇「執行階段」→「全部執行」來一次執行全部的程式碼片段。) > > 程式碼儲存格也可以使用 `pip` 工具,輸入 `!pip + <command>` 即可使用(注意要在前面加一個驚嘆號 `!`)。常用的指令包含: > - `!pip list` 可以列出當前這個環境(也就是這個 `ipynb` 檔)已經安裝的套件,常見的套件如 `request`、`numpy`、`Flask`、`opencv-python`、`tensorflow` 等。 > > - `!pip <package>` 可以安裝指定套件,如果該筆記本會需要使用某個沒有預先安裝的套件,則建議將安裝套件保留在文件開頭,則未來再次執行該筆記本的程式時才會在執行時先安裝。 - **文字儲存格** Colab 筆記本也可以建立文字儲存格,並且支援 markdown 和 Latex 語法,在雙擊儲存格後可以建立具有格式的文字內容或數學式,編輯時文件會將畫面分成左右兩半,左邊為當前編輯內容,右邊為 markdown 語法預覽畫面。 ![螢幕擷取畫面 2023-12-14 173922](https://hackmd.io/_uploads/BkFftItLa.png) - **目錄** 從左側的工具欄選擇三條橫線圖示的「目錄」,可以快速查看文字儲存格的內容。 - **變數** 從左側的工具欄選擇 (x) 圖示的「變數」,可以檢視目前程式碼片段已經使用的變數名稱與其被賦予的值。 - **程式碼片段** 從左側的下方工具欄選擇 <> 圖示的「程式碼片段」,可以搜尋他人公開的程式碼片段功能,例如可以找到「importing a library that is not in Colaboratory」。 ### 使用外部檔案 如果需要使用外部檔案,如檔案中需要使用的類別或函式定義在另一個 Python 檔案中,可以將該檔案上傳到 Colab,然後導入使用。 常用的做法有三種: - **直接上傳檔案或資料夾(單次使用)** 導入自訂模組到 Colab 最簡單的方法,是直接將檔案或資料夾從本機電腦上傳到 colab 上。 1. 從左側工具欄選擇資料夾圖示的「檔案(Files)」,接著選擇「上傳至工作階段儲存空間(Upload to session storage)」。如果是上傳 python 檔,就可以直接在程式內使用 `import` 將需要的類別或函式 2. 如果上傳檔案較大,也可以先建立該檔案或資料夾的 `zip` 檔,再將其作為文件上傳到 colab 以節省網路流量。使用指令 `!unzip file_name.zip` 將檔案解壓縮便能在程式中使用(不過在儲存空間中並不會顯示,因此若想要確認 zip 檔正確被解壓縮,可以再打一行 `%ls` 列出當前工作目錄中的文件和目錄。這個指令類似於 Linux 系統中的 `ls` 命令)。 ![螢幕擷取畫面 2023-12-14 215352](https://hackmd.io/_uploads/H1R7KUtLT.png) - **從 github 將檔案/資料夾複製為 github 儲存庫(單次使用)** 如果 Python 檔案儲存在 GitHub ,則可以使用 `!git clone <https://github.com/your-repository.git>` 載入資料 。 - **從 google drive 取得檔案/資料夾(可重複使用)** 如果 Python 檔案儲存在 google drive ,則可以使用設定路徑載入資料到程式中 。 1. 使用以下程式碼片段將 colab 筆記本連接到 google drive。 ```python from google.colab import drive drive.mount('/content/drive') ``` 2. 執行這段程式碼後,系統會提示進行授權。同意授權後,Colab 會顯示將 google drive 掛載(Mount)到 `/content/drive`。此時 google drive 的內容會出現在 `/content/drive/MyDrive/` 路徑中。 3. 接著使用 `%cd /content/drive/MyDrive/檔案目錄`,搭配 `%ls` 就能看到資料出現在列表中。 使用時就可以和一般編寫 python 程式碼一樣,使用 open() 等函式來讀取檔案。 ![螢幕擷取畫面 2023-12-14 223341](https://hackmd.io/_uploads/HkxxFLY8T.png) > :bulb: **從 Kaggle 匯入 Google Colab** > > 可以參考 Geeksforgeeks 的這篇文章: [How to Import Kaggle Datasets Directly into Google Colab](https://www.geeksforgeeks.org/how-to-import-kaggle-datasets-directly-into-google-colab/) ### 使用 GPU/TPU 來運算 如果要處理深度學習模型訓練、大量數據處理和影像處理等任務,可能會需要使用 GPU(Graphic Processing Unit,圖形處理器)或 TPU(Tensor Processing Unit,張量處理器)來加速運算速度。Google Colab 提供一定等級的 GPU 和 TPU 處理器給一般使用者免費使用,也可以付費升級取得更多的運算資源。 從上方選單選擇「編輯」→「筆記本設置」,或是「執行階段(Runtime)」→「變更執行階段類型(Change runtime type)」,便能修改硬體加速器的種類。 ![螢幕擷取畫面 2023-12-14 231113](https://hackmd.io/_uploads/BJ_pKItIa.png) ![螢幕擷取畫面 2023-12-14 161917](https://hackmd.io/_uploads/HyapYLKIT.png) 想要確認 GPU 是否連接,可以使用以下程式碼片段來查看。如果顯示 `'/device:GPU:0'` 代表成功連接,若顯示 `''` 代表沒有成功連接。 ```python import tensorflow as tf tf.test.gpu_device_name() ``` 想要確認 TPU 是否連接,可以使用以下程式碼片段來查看。如果顯示 `Connected to TPU` 代表成功連接,若顯示 `Not connected to TPU` 代表沒有成功連接。 ```python import os if 'COLAB_TPU_ADDR' not in os.environ: print('Not connected to TPU') else: print("Connected to TPU") ``` ## 使用 Colab 協作程式 ### 使用 Google 帳號共享檔案與協作 Google Colab 和其他 Google 服務(如表單、文件等)一樣可以儲存在雲端硬碟中,透過設定來給予他人檢視、備註和編輯的權限。 - **即時協作** Google Colab 也支援即時協作,多名使用者可以同時在同一個筆記本上進行修改,同時也看得到別人所做的更改,每個儲存格旁邊會顯示當前正在編輯它的使用者的標記。 - **版本紀錄** Google Colab 會自動儲存改變,從上方的選單中點選「檔案」→「修訂版本紀錄」,可以查看筆記本不同版本的修改差異。 ![螢幕擷取畫面 2023-12-15 103939](https://hackmd.io/_uploads/S1jYqUtUa.png) ### 連結並和 GitHub 儲存庫同步 從上方選單中選擇「檔案」→「在 GitHub 中儲存副本」,登入 GitHub 帳號並授權之後,會顯示帳號現有的公開儲存庫,如果想要建立一個新的儲存庫,則需要先到 GitHub 上建立好後再回來儲存副本。儲存後會自動開啟 GitHub 頁面的副本,如果有勾選「加入 Colab 連結」,還可以直接從副本連結回到 Colab 。 ![螢幕擷取畫面 2023-12-14 182954](https://hackmd.io/_uploads/rJH_qIKLT.png) ![螢幕擷取畫面 2023-12-14 183529](https://hackmd.io/_uploads/S1Tu9UYUp.png) <p class="text-center"> *** </p> {%hackmd S5aKa1hTT5aFu7k37bGZMg %}

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully