본문 바로가기

Contact 日本語 English

【Python】 파이썬에서 tensorflow 환경 조성 및 Jupyter 사용법

 

파이썬에서 tensorflow 환경 조성 및 Jupyter 사용법

 

추천 : Python파이썬 목차  


1. 개요 [본문]

2. Anaconda 설치 [본문]

3. Anaconda Prompt (anaconda3) [본문]

4. Jupyter 설치 [본문]

5. 아나콘다 환경 공유하기 [본문]


 

1. 개요 [목차]

Tensorflow, Keras 등은 파이썬에서 인공신경망을 구현하기 위해 만들어짐

2017년부터 R에서 실행 가능한 버전이 출시됨

⑶ 맥북은 터미널을 제공하므로 자연스럽게 설치돼 있음

 

 

2. Anaconda 설치 [목차]

Windows 기준

1단계. 설치 웹사이트 

 

Anaconda | Individual Edition

Anaconda's open-source Individual Edition is the easiest way to perform Python/R data science and machine learning on a single machine.

www.anaconda.com

 

2단계. 다운로드 설치 가이드 

 

How to Download & Install Rstudio: Anaconda On Windows/Mac

What is Splunk? Splunk is a software platform widely used for monitoring, searching, analyzing and...

www.guru99.com

 

3단계. 최종적으로 다음이 설치됨 

○ Jupyter Notebook (anaconda3) 

Anaconda Navigator (anaconda3)

Spyder (anaconda3)

Anaconda Powershell Prompt (anaconda3)

Anaconda Prompt (anaconda3)

Linux 기준 

1단계. Miniconda : 여기에서 Miniconda3 Linux 64-bit를 설치

1단계 - 방법 1

단계 1. 아래 링크에서 파일을 다운로드 

 

Miniconda — conda documentation

Miniconda is a free minimal installer for conda. It is a small, bootstrap version of Anaconda that includes only conda, Python, the packages they depend on, and a small number of other useful packages, including pip, zlib and a few others. Use the conda in

docs.conda.io

 

단계 2. 로컬에 있는 해당 .sh 파일을 Linux 내 원하는 디렉토리로 이동 : 이때 cp, rsync, scp 등의 명령어를 쓸 수 있음

○ (참고) cp [OPTION] [SOURCE] [DESTINATION] : 파일을 복사하여 붙여넣기

-r : 하위 디렉토리와 파일 전체를 복사

-p : 소유주, 그룹, 권한, 시간 정보를 보존하여 복사

○ (참고) scp : 파일이 깨질 가능성이 낮음

scp -P ${port} ${srcFile} ${id}@${destIP}:${destPath}

${port} : destination의 포트 번호 

${srcFile} : source에서 옮기고자 하는 파일의 경로. 예 : ~/Downloads/file.txt 

${id} : destination에서 한 사용자의 ID 

${destIP} : destination의 ip 주소. 예 : #.#.#.# (IPv4)

${destPath} : destination에서 파일을 저장할 디렉토리 주소. 예 : ~/DATA1/1.txt 

코드 자동생성기

○ (참고) rsync (remote sync) : 속도가 좀 더 빠름

rsync --rsh=’ssh -p ${port}’ -avzhP ${srcFile} ${id}@${destIP}:${destPath}

${port} : destination의 포트 번호 

${srcFile} : source에서 옮기고자 하는 파일의 경로. 예 : ~/Downloads/file.txt 

${id} : destination에서 한 사용자의 ID 

${destIP} : destination의 ip 주소. 예 : #.#.#.# (IPv4)

${destPath} : destination에서 파일을 저장할 디렉토리 주소. 예 : ~/DATA1/1.txt 

코드 자동생성기 

1단계 - 방법 2 

단계 1. 위 링크에서 Miniconda3 Linux 64-bit 다운로드 링크 복사

단계 2. curl -o 명령어를 사용

 

curl -o my.sh "https://repo.anaconda.com/miniconda/Miniconda3-py38_23.3.1-0-Linux-x86_64.sh"

 

2단계. .sh 파일 실행 .sh 파일 처리

파일 실행 : .sh 파일을 실행하려면 ./[My_File].sh, sh [My_File].sh, 혹은 bash [My_File].sh와 같이 할 수 있음 

파일 수정 : zshbash 두 종류의 shell이 있음 

$ vi script.sh

$ nano backup

$ sudo apt-get install zsh

권한 부여

○ 예 : $ chmod +x run-md5sum.sh 

3단계. Miniconda .sh 파일을 실행 시 약관을 읽어야 설치를 실행시킬 수 있음 : 계속 엔터 누르기 

4단계. 첫 번째 yes

license agreement에 대한 응답

○ 반드시 yes를 하여야 함 

5단계. 두 번째 yes 

install init을 할지에 대한 응답

○ no가 default이고 no로 하면 뒤에 있는 /bin/bash가 작동하지 않음

○ 필자는 yes로 명시적으로 입력해 주는 것을 권장

6단계. 리눅스 재부팅 혹은 /bin/bash

 

 

3. Anaconda Prompt (anaconda3) [목차]

Windows 기준

① anaconda3를 실행 : 관리자 권한으로 실행하는 것이 권장됨 

패키지를 설치하고 가상환경을 설정해 주어야 : 가상환경은 패키지 버전을 관리하기 위한 패키지 묶음을 의미함 

 

# 0. 설치할 디렉토리로 이동
cd C:/Users/sun/

# 1. 패키지 설치 예시
conda install r-essentials
conda uninstall r-essentials
conda install -c conda-forge r-essentials=4.0

conda install tensorflow
conda install -c conda-forge keras
python -m pip install --upgrade pip

# 2. base 업데이트
conda update -n base conda
conda update --all

# 3. 가상환경 tensorflow, tensorflow2 생성
conda create --name tensorflow python=3.7
conda create --name tensorflow2 python=3.7

# 4. 가상환경 리스트 확인 (base는 기본으로 있음)
conda info --envs

# 5. 파이썬 버전 확인
python --version

# 6. 가상환경 tensorflow 업데이트
pip install --ignore-installed --upgrade tensorflow

# 7. 가상환경 tensorflow2 삭제
conda remove --name tensorflow2 --all

# 8. 가상환경 tensorflow 활성화
activate tensorflow

 

Linux 기준

① Linux는 한 번 conda가 설치되면 이름 앞에 원래는 없었던 (base)가 떠서 쉽게 설치 여부를 알 수 있음

② base는 기본 환경의 이름으로, 새로운 환경을 설치하면 (아래 참고) base 대신 다른 환경명으로 바뀜

③ 참고로 환경은 ./miniconda3/envs/에 저장돼 있음

트러블 슈팅 

사용자 이름 또는 파일경로에 한글이 들어가면 안 됨 

(가능) 방법 1. 사용자 이름과 파일경로가 영어가 되도록 사용자 계정을 새로 하나 만듦 

○ 설정 → 가족 및 다른 사용자 → 기타 사용자 → 이 PC에 다른 사용자 추가 

(금지) 방법 2. 사용자 이름을 바꾸는 방법 

 

사용자 계정 이름과 사용자 폴더 이름 바꾸기《1/2》 - Windows 10

Windows 10에서는 새로운 계정 이름으로 변경하여도 처음 등록 할 때 작성한 사용자 폴더 이름은 원칙적으로 변경할 수가 없습니다. 사용자 폴더 이름은 계정 이름이 변경되어도 영향을 받지 않습

www.tabmode.com

위 방법을 쓰면 "윈도우 계정에 로그인할 수 없음" 문제가 발생할 수 있음

(가능) 방법 3. AccountProfilerFixer.exe (ref

 

윈도우10 계정에 로그인할 수 없음, 해결방법

갑자기 뜬 위와 같은 메세지"계정에 로그인할 수 없음"이라니...이 때문에 얼마나 헤맸는지 모른...

blog.naver.com

 

 

4. Jupyter 설 [목차]

1단계. anaconda3를 실행하고 다음 코드를 입력 (ref

 

위키독스

온라인 책을 제작 공유하는 플랫폼 서비스

wikidocs.net

activate tensorflow
pip install jupyter
jupyter notebook

 

2단계. jupyter lab (선택)

 

pip install jupyterlab
jupyter lab

 

① jupyter lab은 디렉토리 확인이 용이함

② jupyter lab은 파일 업로드, 삭제, 이름 바꾸기 등이 용이함 

3단계. 패키지 (e.g., tensorflow) 설치

 

# install tensorflow package
### tensorflow package version should be matched to python version 
pip install tensorflow

# You can specify the exact version of the package for the environment
pip install keras==2.2.4
# You can even remove previous packages
pip uninstall keras
pip install keras

# execute jupyter
jupyter notebook

 

① 필자는 위와 같이 설치하는 것을 선호함

② 다만, keras보다는 tensorflow.keras를 설치하는 게 버전 관리에 유리할 수 있음

4단계. jupyter ipykernel 설치

 

pip install ipykernel
python -m ipykernel install --user --name JEONGBIN_ENV --display-name JEONGBIN_ENV

 

① JEONGBIN_ENV 대신 자신의 환경명을 입력하면 됨 

② 한 번 띄운 jupyter notebook의 환경을 쉽게 변경할 수 있음

5단계. jupyter notebook 실행

단계 1. 브라우저 우측 상단에 New → Python 3 클릭

jupyter 상에 R을 설치한 경우, 단계 1에서 Python 3 밑에 R도 같이 표시돼 있음

단계 2. 코드 입력 → Run 

jupyter 주요 단축키 

○ a : 현재 cell 위로 새로운 cell 추가

○ b : 현재 cell 아래로 새로운 cell 추가

○ z : 작업 되돌리기
○ shift + 위아래 화살표 : 연속된 cell 여러 개 선택
○ command (⌘) + enter : 현재 cell 실행

○ command (⌘) + f : 문자열 찾기(find)

○ command (⌘) + s : 저장

 

 

5. 아나콘다 환경 공유하기 [목차]

⑴ 환경 파일(.yml) : 가장 단순한 방법

단계 1. 환경 파일 추출하기 : JEONGBIN_ENV라는 환경으로부터 .yml 파일을 추출

 

conda activate JEONGBIN_ENV
conda env export > environment.yml
# Now you can see environment.yml at the mother directory.

 

단계 2. 환경 파일로 환경 생성하기

 

conda env create -f environment.yml

 

트러블슈팅 1. Solving environment: failed. ResolvedPackageNotFound: (ref)

○ 기본적으로 윈도우에서 위 conda 명령어를 사용하면 에러가 나는 것으로 보임 

○ 방법 1. build info를 제거하여 실행 

방법 2. platform-specific version tag를 제거 후 실행

도커(docker) : 가장 권장되는 방법

Binder 

requirements.txt : pip list, pip freeze 등을 활용하여 패키지를 리스트업 할 수 있음. 이들 패키지를 requirements.txt에 저장하면 이후 다음 명령어를 통해 일괄적으로 패키지를 다운로드 받을 수 있음

 

pip install -r requirements.txt

 

⑸ 깃허브 repo를 통한 환경 공유 

 

# Example 

conda create -n CellDART python=3.8  
conda activate CellDART  
pip install git+https://github.com/mexchy1000/CellDART.git  
python -m ipykernel install --user --name CellDART --display-name CellDART

 

README.md : 단순히 plain text로 specification을 입력하는 방식

.sh 파일 : .sh 파일을 실행하려면 ./[My_File].sh, sh [My_File].sh, 혹은 bash [My_File].sh와 같이 할 수 있음

 

bash ./MyEnvironment.sh

 

입력: 2021.02.28 17:36

수정: 2023.12.20 20:14