Skip to content

Commit 8c9878a

Browse files
Merge pull request #205 from deephealthproject/develop
Develop
2 parents 4f6d6de + 7d4dfe1 commit 8c9878a

File tree

13 files changed

+49
-37
lines changed

13 files changed

+49
-37
lines changed

examples/nn/4_NLP/4_nlp_text_generation.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ Tensor *onehot(Tensor *in, int vocs)
6363

6464
int main(int argc, char **argv) {
6565

66+
download_flickr();
67+
6668
// Settings
6769
int epochs = 10;
6870
int batch_size = 8;
@@ -112,7 +114,7 @@ int main(int argc, char **argv) {
112114
// Build model
113115
build(net,
114116
opt, // Optimizer
115-
{"cross_entropy"}, // Losses
117+
{"soft_cross_entropy"}, // Losses
116118
{"accuracy"}, // Metrics
117119
//CS_GPU({1}) // one GPU
118120
//CS_GPU({1,1},100) // two GPU with weight sync every 100 batches
@@ -126,16 +128,17 @@ int main(int argc, char **argv) {
126128
// Load dataset
127129
Tensor *x_train=Tensor::load("flickr_trX.bin","bin");
128130
x_train->info();
131+
129132
Tensor *y_train=Tensor::load("flickr_trY.bin","bin");
130133
y_train->info();
131-
//y_train->print();
134+
132135
y_train=onehot(y_train,outvs);
133136
y_train->reshape_({y_train->shape[0],olength,outvs}); //batch x timesteps x input_dim
137+
y_train->info();
134138

135139
// Train model
136140
for(int i=0;i<epochs;i++) {
137141
fit(net, {x_train}, {y_train}, batch_size, 1);
138-
//evaluate(net,{x_test},{y_test});
139142
}
140143

141144
}

include/eddl/apis/eddl.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ typedef NetLoss * metric;
466466
* @return (void)
467467
*/
468468
void eval_batch(model net, vector<Tensor *> in, vector<Tensor *> out, vector<int> indices);
469-
469+
470470
/**
471471
* @brief Loads the next batch of random samples from the input vector to the output vector
472472
*
@@ -1829,6 +1829,15 @@ typedef NetLoss * metric;
18291829
*/
18301830
void download_eutrans();
18311831

1832+
/**
1833+
* @brief Downloads Flickr Dataset (small partition)
1834+
*
1835+
* @see
1836+
*
1837+
* @return (void) The binary files of Flickr
1838+
*/
1839+
void download_flickr();
1840+
18321841

18331842
}
18341843
#endif

include/eddl/tensor/tensor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class Tensor {
159159
void updateShape(const vector<int> &new_shape);
160160
void updateSize();
161161
void updateStrides();
162-
void updateData(float* ptr, void *ptr2=NULL);
162+
void updateData(float* ptr, void *ptr2=NULL,bool setshared=true);
163163
void deleteData();
164164

165165
/**

src/apis/eddl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,10 @@ namespace eddl {
12631263
download_dataset("eutrans","bin",{"2w0p7f4un6ci94v","g4k1bc6p4bow9tf","egcfin16gl9t92y","n8ks3lyqyhxx1e8"});
12641264
}
12651265

1266+
void download_flickr(){
1267+
download_dataset("flickr","bin",{"452pyxe9x5jpnwb","24c2d5bm6pug8gg"});
1268+
}
1269+
12661270
void download_drive(){
12671271
download_dataset("drive","npy",{"sbd8eu32adcf5oi","qp0j8oiqzf6tc1a"});
12681272
}

src/descriptors/descriptor_conv.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ void ConvolDescriptor::build(Tensor *A) {
130130
// mem for ptr, lowering im2col
131131
ptrI=get_fmem(A->shape[0] * r * c * kr * kc * kz,"ConvolDescriptor::build");
132132
_profile_add_tensor(A->shape[0] * r * c * kr * kc * kz);
133-
new(&matK) Eigen::Map<Eigen::MatrixXf>(K->ptr, kr * kc * kz, nk);
134-
new(&matgK) Eigen::Map<Eigen::MatrixXf>(gK->ptr, kr * kc * kz, nk);
133+
//new(&matK) Eigen::Map<Eigen::MatrixXf>(K->ptr, kr * kc * kz, nk);
134+
//new(&matgK) Eigen::Map<Eigen::MatrixXf>(gK->ptr, kr * kc * kz, nk);
135135
// convolution: matC=matA*matK
136136
}
137137
#ifdef cGPU

src/layers/conv/layer_conv.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,21 @@ void LConv::apply_accumulated_gradients() {
128128
}
129129

130130
Layer *LConv::share(int c, int bs, vector<Layer *> p) {
131-
// TODO: share ComvDescriptor
132-
LConv *n = new LConv(p[0], cd->ksize, cd->stride, cd->pad, name, dev,mem_level);
131+
LConv *n = new LConv(p[0], cd->ksize, cd->stride, cd->pad, "share_"+name, dev,mem_level);
133132
n->orig = this;
134133
n->isshared=true;
135134
n->trainable = trainable;
136135

137136
n->cd->use_bias=cd->use_bias;
138137

139138
//share params
139+
140140
for (int i = 0; i < n->params.size(); i++) delete n->params[i];
141141
n->params.clear();
142142

143143

144144
n->cd->K = cd->K;
145145
n->cd->bias = cd->bias;
146-
n->cd->matK = cd->matK;
147146

148147
n->params.push_back(n->cd->K);
149148
n->params.push_back(n->cd->bias);

src/net/net.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
#include <fstream>
1616
#include <string>
1717
#include <chrono>
18-
#include <thread>
1918
#include "eddl/net/net.h"
20-
#include <pthread.h>
2119
#include "eddl/utils.h"
2220
#include "eddl/random.h"
2321

src/net/net_api.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,12 @@ void Net::prepare_recurrent(vtensor tin, vtensor tout, int &inl, int &outl, vten
805805
for(j=1;j<xtd[i]->ndim;j++)
806806
shape.push_back(xtd[i]->shape[j]);
807807

808-
tinr.push_back(Z);
808+
vector<int>zero_shape;
809+
for(j=0;j<tout[i]->ndim;j++)
810+
if (j!=1) zero_shape.push_back(tout[i]->shape[j]);
811+
812+
if (!isencoder) tinr.push_back(new Tensor(tin[0]->shape,tin[0]->ptr,tin[0]->device));
813+
tinr.push_back(Tensor::zeros(zero_shape,tout[i]->device));
809814
for(j=0;j<outl-1;j++)
810815
tinr.push_back(new Tensor(shape,xtd[i]->ptr+(j*offset),xtd[i]->device));
811816
}
@@ -837,12 +842,7 @@ void Net::fit_recurrent(vtensor tin, vtensor tout, int batch, int epochs) {
837842
int inl;
838843
int outl;
839844

840-
vector<int>shape;
841-
for(j=0;j<tout[0]->ndim;j++)
842-
if (j!=1) shape.push_back(tout[0]->shape[j]);
843-
Tensor *Z=Tensor::zeros(shape,tout[0]->device);
844-
845-
prepare_recurrent(tin,tout,inl,outl,xt,xtd,yt,tinr,toutr,Z);
845+
prepare_recurrent(tin,tout,inl,outl,xt,xtd,yt,tinr,toutr);
846846

847847
if (rnet==nullptr) build_rnet(inl,outl);
848848

@@ -851,10 +851,13 @@ void Net::fit_recurrent(vtensor tin, vtensor tout, int batch, int epochs) {
851851
else if (isencoder)
852852
rnet->fit(tinr,tout,batch,epochs);
853853
else if (isdecoder)
854-
rnet->fit(tin,toutr,batch,epochs);
854+
rnet->fit(tinr,toutr,batch,epochs);
855855

856856
if (snets[0]->dev!=DEV_CPU) rnet->sync_weights();
857857

858+
for(i=0;i<tinr.size();i++) delete(tinr[i]);
859+
for(i=0;i<toutr.size();i++) delete(toutr[i]);
860+
858861
if (isencoder) {
859862
for(i=0;i<xt.size();i++)
860863
delete xt[i];
@@ -870,8 +873,6 @@ void Net::fit_recurrent(vtensor tin, vtensor tout, int batch, int epochs) {
870873
yt.clear();
871874
}
872875

873-
delete Z;
874-
875876
}
876877

877878
// TODO: train_batch_recurrent

src/net/net_build.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include <fstream>
1515
#include <string>
1616
#include <chrono>
17-
#include <thread>
1817
#include "eddl/net/net.h"
19-
#include <pthread.h>
2018
#include "eddl/utils.h"
2119
#include "eddl/random.h"
2220

src/net/net_func.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
#include <fstream>
1515
#include <string>
1616
#include <chrono>
17-
#include <thread>
1817
#include "eddl/net/net.h"
19-
#include <pthread.h>
2018
#include "eddl/utils.h"
2119
#include "eddl/random.h"
2220
#include "eddl/layers/core/layer_core.h"

0 commit comments

Comments
 (0)