Showing
5 changed files
with
104 additions
and
6 deletions
ReadMe.md
0 → 100644
1 | +# 비지도 학습을 이용한 소스 코드 유사성 검증 | ||
2 | + | ||
3 | +### **Author** | ||
4 | +2017103967 김성주 | ||
5 | + | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | +### 1. Dataset | ||
10 | + | ||
11 | +학습에 사용할 소스 코드 데이터셋이 필요합니다. | ||
12 | + | ||
13 | +본 연구에서는 Github에서 MNIST dataset을 사용한 python 코드를 크롤링하여 수집했고, | ||
14 | + | ||
15 | +```code/crawler/main.py``` 혹은 ```code/crawler/cralwer.py```를 참고하시기 바랍니다. | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +데이터셋의 소스 코드들이 unique한 경우에 표절 데이터셋을 추가적으로 생성할 수 있습니다. | ||
20 | + | ||
21 | +본 연구에서는 original dataset에 대해 함수/클래스/조건문 등의 구문 단위로 shuffle하거나, | ||
22 | + | ||
23 | +고유 키워드나 주로 쓰이는 키워드를 최대한 제외하고 변수명과 함수명을 난독화하거나 (obfuscate), | ||
24 | + | ||
25 | +original source code A와 B를 merge한 데이터셋을 생성하였습니다. | ||
26 | + | ||
27 | +자세한 것은 ```code/dataset_generator/main.py```를 참고하시기 바랍니다. | ||
28 | + | ||
29 | + | ||
30 | + | ||
31 | +### 2. Embedding Vector | ||
32 | + | ||
33 | +단어 임베딩 모델에 학습시키기 위해서는, raw dataset에 대한 tokenized vocabulary가 필요합니다. | ||
34 | + | ||
35 | +소스 코드는 일반적인 텍스트와 달리 공백이나 줄 단위로 토큰화할 수 없으므로, | ||
36 | + | ||
37 | +syntax를 분석하여 파싱할 필요가 있습니다. 본 연구에서는 astminer 도구를 사용해 수행했습니다. | ||
38 | + | ||
39 | +자세한 과정은 [링크](https://github.com/JetBrains-Research/astminer)를 참고하시기 바랍니다. | ||
40 | + | ||
41 | + | ||
42 | + | ||
43 | +tokenized dataset은 모델에 입력하기 전 별도의 전처리가 필요합니다. | ||
44 | + | ||
45 | +```code/code2vec/preprocess_py.sh```를 참고하시기 바랍니다. | ||
46 | + | ||
47 | + | ||
48 | + | ||
49 | +전처리된 파일은 c2v format입니다. c2v 파일을 이용해 학습을 진행할 수 있습니다. | ||
50 | + | ||
51 | +```code/code2vec/train.sh```를 참고하시기 바랍니다. | ||
52 | + | ||
53 | + | ||
54 | + | ||
55 | +### 3. Similarity Plotting | ||
56 | + | ||
57 | +학습이 진행된 모델을 사용해 소스 코드 간의 cosine similarity를 계산할 수 있습니다. | ||
58 | + | ||
59 | +```code/similarity_plotter/code2vec_tester.py```를 참고하시기 바랍니다. | ||
60 | + | ||
61 | +해당 코드에서 불러오는 모델 파일은 ```code/code2vec/release.sh```를 이용해 생성할 수 있습니다. | ||
62 | + | ||
63 | + | ||
64 | + | ||
65 | +### 4. Siamese Network | ||
66 | + | ||
67 | +여러 가지의 시도가 혼재되어 있어 진행 중 에러가 발생할 수 있습니다. | ||
68 | + | ||
69 | +전체적인 과정은 ```code/siamese_network/train.py```를 참고하시기 바랍니다. | ||
70 | + | ||
71 | +Siamese Network 학습을 위해서는 pair dataset과 embedding matrix가 필요합니다. | ||
72 | + | ||
73 | +```code/siamese_network/dataset.py```에서 2에서 학습한 모델의 출력 벡터를 불러와 pair dataset을 생성합니다. 또한 해당 코드에서 embedding matrix를 불러옵니다. | ||
74 | + | ||
75 | +embedding matrix는 다음과 같은 과정으로 생성할 수 있습니다. | ||
76 | + | ||
77 | +``` | ||
78 | +embedding_index = {} | ||
79 | +words = model.index_to_key | ||
80 | +vocab_size = len(words) + 1 | ||
81 | +embedding_matrix = np.zeros((vocab_size, 384)) | ||
82 | + | ||
83 | +for i in range(len(words)): | ||
84 | + word - words[i] | ||
85 | + if word in model: | ||
86 | + embedding_matrix[i] = model[word] | ||
87 | + | ||
88 | +# save data here | ||
89 | +``` | ||
90 | + | ||
91 | +네트워크 구조는 ```code/siamese_network/model.py```를 참고하시기 바랍니다. | ||
92 | + | ||
93 | +train/test/predict는 각각 ```code/siamese_network/train.py``` , ```code/siamese_network/test.py```, | ||
94 | + | ||
95 | +```code/siamese_network/predict.py```로 진행할 수 있습니다. | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
code/code2vec/release.sh
0 → 100644
... | @@ -19,7 +19,7 @@ def avg_feature_vector(text, model, num_features, index2word_set): | ... | @@ -19,7 +19,7 @@ def avg_feature_vector(text, model, num_features, index2word_set): |
19 | feature_vec = np.divide(feature_vec, n_words) | 19 | feature_vec = np.divide(feature_vec, n_words) |
20 | return feature_vec | 20 | return feature_vec |
21 | 21 | ||
22 | -def compare(c2v_model, model, dir1, dir2): | 22 | +def compare(t2v_model, model, dir1, dir2): |
23 | files = [f for f in readdir(dir1) if is_extension(f, 'py')] | 23 | files = [f for f in readdir(dir1) if is_extension(f, 'py')] |
24 | 24 | ||
25 | plt.ylabel('cos_sim') | 25 | plt.ylabel('cos_sim') |
... | @@ -28,7 +28,7 @@ def compare(c2v_model, model, dir1, dir2): | ... | @@ -28,7 +28,7 @@ def compare(c2v_model, model, dir1, dir2): |
28 | idx = 0 | 28 | idx = 0 |
29 | L = len(files) | 29 | L = len(files) |
30 | data = [] | 30 | data = [] |
31 | - index2word_set = set(c2v_model.index_to_key) | 31 | + index2word_set = set(t2v_model.index_to_key) |
32 | 32 | ||
33 | for f in files: | 33 | for f in files: |
34 | print(idx,"/",L) | 34 | print(idx,"/",L) |
... | @@ -47,8 +47,8 @@ def compare(c2v_model, model, dir1, dir2): | ... | @@ -47,8 +47,8 @@ def compare(c2v_model, model, dir1, dir2): |
47 | print(result) | 47 | print(result) |
48 | 48 | ||
49 | vectors_text_path = 'data/targets.txt' | 49 | vectors_text_path = 'data/targets.txt' |
50 | -c2v_model = KeyedVectors.load_word2vec_format(vectors_text_path, binary=False) | 50 | +t2v_model = KeyedVectors.load_word2vec_format(vectors_text_path, binary=False) |
51 | model = load_model(config.MODEL_PATH) | 51 | model = load_model(config.MODEL_PATH) |
52 | 52 | ||
53 | # Usage | 53 | # Usage |
54 | -# compare(c2v_model, model, 'data/refined', 'data/shuffled') | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
54 | +# compare(t2v_model, model, 'data/refined', 'data/shuffled') | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -4,7 +4,7 @@ import random | ... | @@ -4,7 +4,7 @@ import random |
4 | from utils import * | 4 | from utils import * |
5 | import matplotlib.pyplot as plt | 5 | import matplotlib.pyplot as plt |
6 | 6 | ||
7 | -vectors_text_path = 'data/targets.txt' # w2v output file from model | 7 | +vectors_text_path = 'data/targets.txt' # t2v output file from model |
8 | model = KeyedVectors.load_word2vec_format(vectors_text_path, binary=False) | 8 | model = KeyedVectors.load_word2vec_format(vectors_text_path, binary=False) |
9 | 9 | ||
10 | def compare(dir1, dir2): | 10 | def compare(dir1, dir2): | ... | ... |
-
Please register or login to post a comment