/
1_nlp_sentiment_rnn.cpp
129 lines (98 loc) · 3.25 KB
/
1_nlp_sentiment_rnn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* EDDL Library - European Distributed Deep Learning Library.
* Version: 1.1
* copyright (c) 2022, Universitat Politècnica de València (UPV), PRHLT Research Centre
* Date: March 2022
* Author: PRHLT Research Centre, UPV, (rparedes@prhlt.upv.es), (jon@prhlt.upv.es)
* All rights reserved
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "eddl/apis/eddl.h"
using namespace eddl;
//////////////////////////////////
// Embeding+RNN
// using imdb preprocessed from keras
// 2000 words
//////////////////////////////////
int main(int argc, char **argv) {
bool testing = false;
bool use_cpu = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--testing") == 0) testing = true;
else if (strcmp(argv[i], "--cpu") == 0) use_cpu = true;
}
// Download Imdb
download_imdb_2000();
// Settings
int epochs = testing ? 1 : 10;
int batch_size = 32;
int length=250;
int embdim=33;
int vocsize= 2000;
// Define network
layer in = Input({1}); //1 word
layer l = in;
layer lE = RandomUniform(Embedding(l, vocsize, 1,embdim),-0.05,0.05);
l = RNN(lE,37);
l = ReLu(Dense(l,256));
layer out = Sigmoid(Dense(l, 1));
model net = Model({in}, {out});
// dot from graphviz should be installed:
plot(net, "model.pdf");
optimizer opt=adam(0.001);
//opt->set_clip_val(0.01);
compserv cs = nullptr;
if (use_cpu) {
cs = CS_CPU();
} else {
cs = CS_GPU({1}); // one GPU
// cs = CS_GPU({1}, "low_mem"); // one GPU
// cs = CS_GPU({1,1},100); // two GPU with weight sync every 100 batches
// cs = CS_CPU();
}
// Build model
build(net,
opt, // Optimizer
{"binary_cross_entropy"}, // Losses
{"binary_accuracy"}, // Metrics
cs);
// View model
summary(net);
// Load dataset
Tensor* x_train=Tensor::load("imdb_2000_trX.bin");
Tensor* y_train=Tensor::load("imdb_2000_trY.bin");
Tensor* x_test=Tensor::load("imdb_2000_tsX.bin");
Tensor* y_test=Tensor::load("imdb_2000_tsY.bin");
x_train->reshape_({x_train->shape[0],length,1}); //batch x timesteps x input_dim
x_test->reshape_({x_test->shape[0],length,1}); //batch x timesteps x input_dim
y_train->reshape_({y_train->shape[0],1,1}); //batch x timesteps x input_dim
y_test->reshape_({y_test->shape[0],1,1}); //batch x timesteps x input_dim
if (testing) {
std::string _range_ = "0:" + std::to_string(2 * batch_size);
Tensor* x_mini_train = x_train->select({_range_, ":", ":"});
Tensor* y_mini_train = y_train->select({_range_, ":", ":"});
Tensor* x_mini_test = x_test->select({_range_, ":", ":"});
Tensor* y_mini_test = y_test->select({_range_, ":", ":"});
delete x_train;
delete y_train;
delete x_test;
delete y_test;
x_train = x_mini_train;
y_train = y_mini_train;
x_test = x_mini_test;
y_test = y_mini_test;
}
// Train model
for(int i=0;i<epochs;i++) {
fit(net, {x_train}, {y_train}, batch_size, 1);
evaluate(net,{x_test},{y_test});
}
delete net;
delete x_train;
delete y_train;
delete x_test;
delete y_test;
return EXIT_SUCCESS;
}