Skip to content

Commit 784607d

Browse files
committed
Upload AE1d
1 parent 53f2c55 commit 784607d

23 files changed

+1635
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
# Project Name
4+
project(AE1d CXX)
5+
6+
# Directory Name
7+
set(SUB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../utils)
8+
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
9+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
10+
11+
# Create Executable File
12+
set(SRCS
13+
${SRC_DIR}/main.cpp
14+
${SRC_DIR}/train.cpp
15+
${SRC_DIR}/valid.cpp
16+
${SRC_DIR}/test.cpp
17+
${SRC_DIR}/loss.cpp
18+
${SRC_DIR}/networks.cpp
19+
)
20+
21+
add_subdirectory(${SUB_DIR} build)
+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# AE1d
2+
This is the implementation of "Autoencoder" corresponding to 1-dimensional shape.<br>
3+
Original paper: G. E. Hinton and R. R. Salakhutdinov. Reducing the dimensionality of data with neural networks. Science, 2006. [link](https://science.sciencemag.org/content/313/5786/504.abstract)
4+
5+
## Usage
6+
7+
### 1. Build
8+
Please build the source file according to the procedure.
9+
~~~
10+
$ mkdir build
11+
$ cd build
12+
$ cmake ..
13+
$ make -j4
14+
$ cd ..
15+
~~~
16+
17+
### 2. Dataset Setting
18+
19+
#### Recommendation
20+
- Normal Distribution Dataset<br>
21+
This is the dataset of 300-dimensional data of 1-dimensional shape that has a training set of 100,000 pieces and a test set of 1,000 pieces.<br>
22+
Link: [official](https://github.com/koba-jon/normal_distribution_dataset)
23+
24+
- THE MNIST DATABASE of handwritten digits<br>
25+
This is the dataset of 28x28 grayscale for handwritten digits in 10 classes that has a training set of 60000 images and a test set of 10000 images.<br>
26+
Link: [official](http://yann.lecun.com/exdb/mnist/)
27+
28+
#### Setting
29+
30+
Please create a link for the dataset.<br>
31+
The following hierarchical relationships are recommended.
32+
33+
~~~
34+
datasets
35+
|--Dataset1
36+
| |--train
37+
| | |--data1.dat
38+
| | |--data2.csv
39+
| | |--data3.txt
40+
| |
41+
| |--valid
42+
| |--test
43+
|
44+
|--Dataset2
45+
|--Dataset3
46+
~~~
47+
48+
You should substitute the path of training data for "<training_path>", test data for "<test_path>", respectively.<br>
49+
The following is an example for "NormalDistribution".
50+
~~~
51+
$ cd datasets
52+
$ git clone https://github.com/koba-jon/normal_distribution_dataset.git
53+
$ ln -s normal_distribution_dataset/NormalDistribution ./NormalDistribution
54+
$ cd ..
55+
~~~
56+
57+
### 3. Training
58+
59+
#### Setting
60+
Please set the shell for executable file.
61+
~~~
62+
$ vi scripts/train.sh
63+
~~~
64+
The following is an example of the training phase.<br>
65+
If you want to view specific examples of command line arguments, please view "src/main.cpp" or add "--help" to the argument.
66+
~~~
67+
#!/bin/bash
68+
69+
DATA='NormalDistribution'
70+
71+
./AE1d \
72+
--train true \
73+
--epochs 300 \
74+
--dataset ${DATA} \
75+
--nd 300 \
76+
--nz 1 \
77+
--batch_size 64 \
78+
--gpu_id 0
79+
~~~
80+
81+
#### Run
82+
Please execute the following to start the program.
83+
~~~
84+
$ sh scripts/train.sh
85+
~~~
86+
87+
### 4. Test
88+
89+
#### Setting
90+
Please set the shell for executable file.
91+
~~~
92+
$ vi scripts/test.sh
93+
~~~
94+
The following is an example of the test phase.<br>
95+
If you want to view specific examples of command line arguments, please view "src/main.cpp" or add "--help" to the argument.
96+
~~~
97+
#!/bin/bash
98+
99+
DATA='NormalDistribution'
100+
101+
./AE1d \
102+
--test true \
103+
--dataset ${DATA} \
104+
--test_in_dir "test" \
105+
--test_out_dir "test" \
106+
--nd 300 \
107+
--nz 1 \
108+
--gpu_id 0
109+
~~~
110+
If you want to test the reconstruction error of the data, the above settings will work.<br>
111+
If you want to test the denoising of the data, set "test_in_dir" to "directory of noisy data" and "test_out_dir" to "directory of output ground truth".<br>
112+
However, the two file names must correspond.
113+
114+
#### Run
115+
Please execute the following to start the program.
116+
~~~
117+
$ sh scripts/test.sh
118+
~~~
119+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
1. What kind of dataset do you need?
2+
3+
Please prepare files of 1-dimensional shape in which the data is written line-by-line.
4+
5+
Example (7-dimensional data of 1-dimensional shape):
6+
---------------------------------------------------------
7+
0.125
8+
1.552
9+
2.452
10+
4.552
11+
2.563
12+
4.532
13+
2.413
14+
---------------------------------------------------------
15+
16+
17+
2. What kind of hierarchical relationship do you need?
18+
19+
The following hierarchical relationships are recommended.
20+
21+
---------------------------------------------------------
22+
datasets
23+
|--Dataset1
24+
| |--train
25+
| | |--data1.dat
26+
| | |--data2.csv
27+
| | |--data3.txt
28+
| |
29+
| |--valid
30+
| | |--data4.dat
31+
| | |--data5.csv
32+
| | |--data6.txt
33+
| |
34+
| |--test
35+
| |--data7.dat
36+
| |--data8.csv
37+
| |--data9.txt
38+
|
39+
|--Dataset2
40+
|--Dataset3
41+
---------------------------------------------------------
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
DATA='NormalDistribution'
4+
5+
./AE1d \
6+
--test true \
7+
--dataset ${DATA} \
8+
--test_in_dir "test" \
9+
--test_out_dir "test" \
10+
--nd 300 \
11+
--nz 1 \
12+
--gpu_id 0
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
DATA='NormalDistribution'
4+
5+
./AE1d \
6+
--train true \
7+
--epochs 300 \
8+
--dataset ${DATA} \
9+
--nd 300 \
10+
--nz 1 \
11+
--batch_size 64 \
12+
--gpu_id 0
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <string>
3+
// For External Library
4+
#include <torch/torch.h>
5+
// For Original Header
6+
#include "loss.hpp"
7+
#include "losses.hpp"
8+
9+
10+
// -----------------------------------
11+
// class{Loss} -> constructor
12+
// -----------------------------------
13+
Loss::Loss(const std::string loss){
14+
if (loss == "l1"){
15+
this->judge = 0;
16+
}
17+
else if (loss == "l2"){
18+
this->judge = 1;
19+
}
20+
else{
21+
std::cerr << "Error : The loss fuction isn't defined right." << std::endl;
22+
std::exit(1);
23+
}
24+
}
25+
26+
27+
// -----------------------------------
28+
// class{Loss} -> operator
29+
// -----------------------------------
30+
torch::Tensor Loss::operator()(torch::Tensor &input, torch::Tensor &target){
31+
if (this->judge == 0){
32+
static auto criterion = torch::nn::L1Loss(torch::nn::L1LossOptions().reduction(torch::kMean));
33+
return criterion(input, target);
34+
}
35+
static auto criterion = torch::nn::MSELoss(torch::nn::MSELossOptions().reduction(torch::kMean));
36+
return criterion(input, target);
37+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#ifndef LOSS_HPP
2+
#define LOSS_HPP
3+
4+
#include <string>
5+
// For External Library
6+
#include <torch/torch.h>
7+
8+
9+
// -------------------
10+
// class{Loss}
11+
// -------------------
12+
class Loss{
13+
private:
14+
int judge;
15+
public:
16+
Loss(){}
17+
Loss(const std::string loss);
18+
torch::Tensor operator()(torch::Tensor &input, torch::Tensor &target);
19+
};
20+
21+
22+
#endif

0 commit comments

Comments
 (0)