[AI] day1. 텐서플로우 시작



휴학하기 전에 인공지능 수업을 들었던 적이 있었는데, 그 때 당시에 의사결정트리라는 것을 만들어 보면서 나랑은 관련이 없을 것만 같던 인공지능에 대해 조금 더 관심을 갖게 되었다. 그래서 인공지능에 대해서 공부를 조금씩 해보려고 한다.
그런데 이 분야는 매우 광범위하여 대체 어디서부터, 어떻게 공부를 시작해야 하는지 감이 잡히지 않았다. 구글링을 하면서 조금 뒤적여봤는데 이런 글이 있더라.

http://media.fastcampus.co.kr/knowledge/how-beginners-wrong-machine-learning/

물론 저 글이 무조건 옳다고 생각하지는 않으며, 사람에 따라 의견이 분분할 수 있다. 당연한 것이다. 배우는데에 있어서는 사람마다 방법이 다 다르니까. 하지만 나 같은 범재는 본인보다 훨씬 뛰어난 사람들과의 차이를 빠르게 인정하고 그들의 기술을 빠르게 내 것으로 만들어야한다는게 본인의 생각이다. 따라서 공용 라이브러리를 이용해 머신러닝을 공부하기로 마음먹었다. 과연 어디까지 할 수 있을지는 모르지만 뭐 시작하지 않으면 아무것도 아니지 않은가. 중간에 좌절하는 일이 있더라도 일단은 시작을 해보자.

현재 '인공지능을 위한 텐서플로우 애플리케이션 프로그래밍' 이라는 책으로 인공지능을 공부하고 있다. 개요 부분은 읽어도 당장은 어떻게 쓰는지 감이 안오기 때문에 일단 직접 써보면서 정리하기로 했다.
따라서 책의 "chapter 02. 텐서플로우 시작" 부터 시작한다.



1. 텐서플로우(TensorFlow)란


구글이 만든 머신러닝 오픈 소스 라이브러리이다. 심층 신경망 DistBelief를 개선하여 확장성과 유연성이 뛰어나며 많은 운영체제에서 구동할 수 있도록 지원한다.



2. 텐서플로우 개발 환경


텐서플로우에 대한 구체적인 설명에 앞서, 일단 어떻게 사용하는지부터 알아야하지 않겠는가. 시작이 반이다. 개발 환경부터 구축을 해놓고 텐서플로우에 대해서 알아보자.

1) 아나콘다 (anaconda) 설치


아나콘다는 파이썬 데이터 과학 플랫폼으로 패키지 설치 및 종속성, 환경 관리를 쉽게 할 수 있다.

https://www.anaconda.com/distribution/

홈페이지에 들어가서 우측 상단의 download를 클릭하고 python 3.7 version을 다운로드하자.

맥북 기준으로 다운로드를 하면 위와 같은 창이 나오는데, 설치는 어렵지 않으니까 쭉 진행하자.



2) Anaconda에서 텐서플로우 설치


맥 유저라면 터미널 정도는 써봤을 것이다. 터미널에서 conda 명령어를 입력하자.



1> 가상환경 생성


다음과 같은 명령어를 입력하자.

>conda create -n tensorflow python=3.7

파이썬 버전은 자신이 다운로드한 버전에 맞게 입력하면 된다. 별 일이 없으면 다운로드가 진행될텐데, 중간에 "y /n" 에서 y 입력 후 엔터하는 과정이 한번 있고 그 다음부터는 잘 진행될 것이다.
여기까지 했으면 가상 환경 생성이 완료된 것이다.


2> 가상 환경 실행 및 종료


가상 환경 실행 및 종료는 아래의 코드로 진행할 수 있다.

#가상 환경 실행
>conda activate tensorflow

#가상 환경 종료
>conda deactivate

정상적으로 진행이 되었다면
이렇게 왼쪽에 (tensorflow)가 생길 것이며 이게 생성한 가상 환경 이름이다.


3> 텐서플로우 설치


pip 명령어를 이용하여 설치를 해보자.

>pip install tensorflow

여기까지 별 일이 없다면 텐서플로우 설치는 끝났다. 그런데 터미널에서 anaconda prompt를 이용하여 코딩하는 것은 여간 불편한 일이 아니다. 요즘 시대에 좋은 에디터가 차고 넘치는데 안 쓸 이유가 있겠는가?
파이참을 사용하도록 하자.


3) 파이참 (pyCharm for anaconda) 설치


https://www.jetbrains.com/pycharm/promo/anaconda/

요기 들어가서 community 버젼 다운로드 하자.

쭉 설치해서 이런게 나오면 끝.
새 프로젝트를 하나 생성해보자. Create New Project를 누르면 아래와 같은 화면이 나올 것이다.
원하는 Location을 선택 후 그냥 넘어가지말고 Project Interpreter를 클릭하여 Existing Interpreter로 변경하자. 안그러면 깔았던 텐서플로우가 무용지물이 된다. 즉 아래와 같이 나와야한다.

혹시 위와 같이 나오지 않는다면 conda activate tensorflow를 터미널에 다시 입력하고 프로젝트 생성을 다시 해보자. 그 후 프로젝트에서 파이썬 파일을 생성하고 tensorflow를 import했을 때 오류가 안 뜬다면 성공한 것이다.

프로젝트가 생성 되었으면  파이썬 파일을 생성한 후 아래와 같은 코드를 입력해보자.

import tensorflow as tf
res = tf.constant("Hello, tensorflow!")
sess = tf.Session()
print(sess.run(res))

그럼 정상적으로 나온다! 하고 끝내고 싶었지만... 뭔가 이상한 오류코드가 주르륵 나온다..


/anaconda3/envs/tensorflow/bin/python /Users/singiyeol/Desktop/untitled/t.py
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/anaconda3/envs/tensorflow/lib/python3.7/site-packages/tensorboard/compat/tensorflow_stub/dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING:tensorflow:From /Users/singiyeol/Desktop/untitled/t.py:3: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2019-10-14 14:25:51.692494: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2019-10-14 14:25:51.692652: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 4. Tune using inter_op_parallelism_threads for best performance.
b'Hello, tensorflow!'

Process finished with exit code 0

구글링을 해 본 결과..




1. futurewarning ... 
이 오류들은 numpy의 버젼과 관계가 있다고 한다. 버젼 간에 어떤 충돌이 일어나서 발생하는 오류인가보다.
2. the name tf.Session is deprecated.
말 그대로 session은 더 이상 쓰지 않는다는 것인데, 텐서플로우의 버젼 문제인 것 같다. 2.0버젼 이상에서 사용되지 않나보다.
3. Intel MKL-DNN ...
인텔 CPU의 instruction sets에 관련된 문제인 것 같은데, 무시해도 괜찮은 경고 메세지라고 하니 일단 보류하고, 나중에 관련 문제가 따로 발생하게 되면 고치도록 하겠다.
4. inter_op_parallelism_threads ...
https://joytk.tistory.com/15 여기를 보면 해결할 수 있다.




아마 인공지능 기술이 빠르게 발전하다보니 빈번한 업데이트 때문에 오류 또한 빈번하게 발생하는 것 같은데, 일단 이런 경고 메세지에 불구하고 결과가 출력 되기는 한다. 아래와 같이 말이다.

b'Hello, tensorflow!'

의도치 않은 'b'라는 문자가 튀어나왔다. 인코딩 문제로 발생하는 오류이며, 출력 부분을 아래와 같이 작성하면 해결할 수 있다.
print(str(sess.run(res), encoding='utf-8'))



저 오류 때문에 결국 많이는 못했지만, 시작이 반이라고 시작에 의의를 두기로 한다. 포기하지 않기를 바라면서..

댓글