2019년 11월 25일 월요일

Is language modeling Self-supervised or Unsupervised?

도대체 Language model은 Unsupervised 모델인가? Self-supervised model인가? 둘 다인가?

Unsupervised


"Language modeling is usually framed as unsupervised distribution estimation"
https://arxiv.org/pdf/1810.04805.pdf

"Another notable family within unsupervised learning are autoregressive models, in which the data is split into a sequence of small pieces, each of which is predicted in turn. Such models can be used to generate data by successively guessing what will come next, feeding in a guess as input and guessing again. Language models, where each word is predicted from the words before it, are perhaps the best known example"
https://deepmind.com/blog/article/unsupervised-learning

"Unlike Peters et al. (2018a) and Radford et al. (2018), we do not use traditional left-to-right or right-to-left language models to pre-train BERT. Instead, we pre-train BERT using two unsupervised tasks, described in this section. This step is presented in the left part of Figure 1."
https://arxiv.org/pdf/1810.04805.pdf (BERT paper)



Self-supervised



"This idea has been widely used in language modeling. The default task for a language model is to predict the next word given the past sequence. BERT adds two other auxiliary tasks and both rely on self-generated labels."
https://lilianweng.github.io/lil-log/2019/11/10/self-supervised-learning.html

ALBERT: A Lite BERT for Self-supervised Learning of Language Representations
https://arxiv.org/abs/1909.11942

"A robustly optimized method for pretraining natural language processing (NLP) systems that improves on Bidirectional Encoder Representations from Transformers, or BERT, the self-supervised method released by Google in 2018. BERT is a revolutionary technique that achieved state-of-the-art results on a range of NLP tasks while relying on unannotated text drawn from the web, as opposed to a language corpus that’s been labeled specifically for a given task."
https://ai.facebook.com/blog/roberta-an-optimized-method-for-pretraining-self-supervised-nlp-systems/



Self-supervised is Unsupervised


Self supervised learning is an elegant subset of unsupervised learning where you can generate output labels ‘intrinsically’ from data objects by exposing a relation between parts of the object, or different views of the object.
https://towardsdatascience.com/self-supervised-learning-78bdd989c88b



결론은... 자기 마음대로 정의해서 쓴다.

2019년 11월 21일 목요일

TPU 사용 시 storage 관련 문제(HTTP 403, OutOfRangeError) 해결법

TPU에서 bert를 학습시키려고 했다.

VM 접속부터 여러 난관들이 있겠지만 ㅋㅋㅋ
Storage에 관련된 문제들이 두 개가 발생했다.


1. Checkpoint를 저장할 때 생기는 문제

Permission denied: Error executing an HTTP request: HTTP response code 403 with body '{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "my_account_email does not have storage.objects.list access to object.~~."
}

2. OutOfRangeError (see above for traceback): End of sequence 에러


해결법
해결법은 내 tpu 서비스 계정에 storage에 대한 owner 권한을 주는 것이다.
Tpu 서비스 계정은 tpu를 누르면 맨 아래쪽에 표시된다.
형식은 service-{project 번호}@cloud-tpu.iam.gserviceaccount.com 이다.
프로젝트 번호는 Google cloud console 메인 페이지에 가면 project id 밑에 보인다.

Tpu 서비스 계정을 알았으면, 로컬 터미널에서 아래 명령을 실행하면 된다.
gsutil acl -r ch -u TPU계정:OWNER STORAGE_BUCKET_주소

* 참고 사항
READER WRITER 두개의 권한을 부여했더니 안 됐고, 꼭 OWNER 권한이 있어야 되는 것 같다.

-r 옵션은 recursive 옵션 같은데, 폴더 별로 따로 권한을 설정할 수도 있겠지만 최상위 폴더에 -r 옵션을 주어서 한 번에 전체 권한을 주는게 가장 속 편한 것 같다.
-r 옵션이 없으면 해당 폴더에만 권한이 생기고 하위 폴더에는 권한이 안 생기는 것 같다.
학습용 데이터와 학습된 모델을 다른 폴더에 저장하는 게 편할 것이기 때문에 폴더에 권한을 각각 주는 것보다 -r을 쓰는 게 좋을 듯하다.

2019년 11월 20일 수요일

Flask(web server) 상에서 matplotlib.pyplot 사용시 프로세스가 죽을 때

Tensorflow의 수행 결과를 간단하게 보여줄 데모 페이지를 만들게 되었다.
환경을 Flask - TF serving으로 구성하고, flask 상에서 matplotlib로 그래프를 만들어 페이지에 띄우도록 했다.
그런데 일정 횟수 이상으로 결과를 뽑으면 maplotlib 상에서 쓰레드 또는 메모리 관련 문제가 생겨 flask 프로세스가 죽는 문제가 발생했다.

이에 검색을 좀 해보니 홈페이지에 사용법을 설명해 놓아 따라해보니 문제가 해결되었다.
링크 : https://matplotlib.org/faq/howto_faq.html

Matplotlib 3.1 이전 버전의 경우
import matplotlib
matplotlib.use('Agg')
코드만 추가해주면 해결되었다.


-------------------------------------------------------------------------------------------------
그 이후 버전의 경우, 홈페이지의 코드대로
from matplotlib.figure import Figure
를 이용하면 되는 듯했다.

홈페이지 코드를 보면 pyplot없이 Figure()만으로 그래프를 그릴 수 있는 것 같은데 정확히는 모르겠다.
코드에 show()가 없는 것으로 봐서는 클라이언트에 전송하기 위한 용도로만 코드를 짠 것 같다.
Figure를 사용한 예제는 거의 pyplot.figure()던데...