Skip to content

Commit 9a7dee9

Browse files
author
Vandenplas, Jeremie
committed
addition of a multivariate example
1 parent 2c30887 commit 9a7dee9

File tree

5 files changed

+161
-6
lines changed

5 files changed

+161
-6
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ foreach(execid mnist network_save network_sync)
7878
add_test(test_${execid} bin/test_${execid})
7979
endforeach()
8080

81-
foreach(execid mnist montesinos_uni simple sine)
81+
foreach(execid mnist montesinos_uni montesinos_multi simple sine)
8282
add_executable(example_${execid} src/tests/example_${execid}.f90)
8383
target_link_libraries(example_${execid} neural ${LIBS})
8484
add_test(example_${execid} bin/example_${execid})

README.md

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,25 +309,44 @@ for example on 16 cores using [OpenCoarrays](https://github.com/sourceryinstitut
309309
$ cafrun -n 16 ./example_mnist
310310
```
311311

312-
### Montesinos-Lopez et al. (2018) example
312+
### Montesinos-Lopez et al. (2018) univariate example
313313

314-
The Montesinos-Lopez et al. (2018) example is extracted from the study:
314+
The Montesinos-Lopez et al. (2018) univariate example is extracted from the study:
315315

316316
```
317317
Montesinos-Lopez et al. 2018. Multi-environment genomic prediction of plant traits using deep learners with dense architecture. G3, 8, 3813-3828.
318318
```
319319

320-
This example uses the data from the dataset "Data\_Maize\_1to3", and was extracted using the R code in the Appendix.
320+
This example uses the data from the dataset "Data\_Maize\_1to3", and was extracted using the R code in the Appendix of this paper.
321321

322322

323-
The Montesinos-Lopez data is included with the repo and you will have to unpack it first:
323+
The Montesinos-Lopez univariate data is included with the repo and you will have to unpack it first:
324324

325325
```
326326
cd data/montesinos_uni
327327
tar xzvf montesinos_uni.tar.gz
328328
cd -
329329
```
330330

331+
### Montesinos-Lopez et al. (2018) multivariate example
332+
333+
The Montesinos-Lopez et al. (2018) multivariate example is extracted from the study:
334+
335+
```
336+
Montesinos-Lopez et al. 2018. Multi-trait, multi-environment deep learning modeling for genomic-enabled prediction of plant traits. G3, 8, 3829-3840.
337+
```
338+
339+
This example uses the data from the dataset "Data\_Maize\_set\_1", and was extracted using the R code in the Appendix B of this paper.
340+
341+
342+
The Montesinos-Lopez multivariate data is included with the repo and you will have to unpack it first:
343+
344+
```
345+
cd data/montesinos_multi
346+
tar xzvf montesinos_multi.tar.gz
347+
cd -
348+
```
349+
331350

332351
## Contributing
333352

data/montesinos_multi.tar.gz

1.74 MB
Binary file not shown.

src/tests/example_mnist.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ program example_mnist
2020

2121
call load_mnist(tr_images, tr_labels, te_images, te_labels)
2222

23-
net = network_type([784, 10, 10])
23+
net = network_type([size(tr_images,dim=1), 10, size(label_digits(te_labels),dim=1)])
2424

2525
batch_size = 1000
2626
num_epochs = 10
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
program example_montesinos_multi
2+
use mod_kinds,only:ik,rk
3+
use mod_network,only:network_type
4+
implicit none
5+
integer(ik)::ny1_tr,ny2_tr,nx1_tr,nx2_tr
6+
integer(ik)::ny1_ts,ny2_ts,nx1_ts,nx2_ts
7+
8+
integer(ik)::batch_size,num_epochs
9+
10+
real(rk),allocatable::y_tr(:,:),x_tr(:,:)
11+
real(rk),allocatable::y_ts(:,:),x_ts(:,:)
12+
13+
type(network_type)::net
14+
15+
call readfile('../data/montesinos_multi/y_tr.dat',ny1_tr,ny2_tr,y_tr)
16+
call readfile('../data/montesinos_multi/x_tr.dat',nx1_tr,nx2_tr,x_tr)
17+
18+
net=network_type([nx1_tr,50,50,ny1_tr])
19+
20+
batch_size=50
21+
num_epochs=50
22+
23+
!training
24+
call net%fit(x_tr,y_tr,3._rk,epochs=num_epochs,batch_size=batch_size)
25+
26+
call net%sync(1)
27+
28+
!validation
29+
call readfile('../data/montesinos_multi/y_ts.dat',ny1_ts,ny2_ts,y_ts)
30+
call readfile('../data/montesinos_multi/x_ts.dat',nx1_ts,nx2_ts,x_ts)
31+
32+
if(this_image().eq.1)then
33+
write(*,*)'Correlation(s): ',corr_array(net%predict(x_ts),y_ts)
34+
endif
35+
36+
contains
37+
38+
subroutine readfile(filename,n,m,array)
39+
character(len=*),intent(in)::filename
40+
integer(ik),intent(out)::n,m
41+
real(rk),allocatable,intent(out)::array(:,:)
42+
43+
integer(ik)::un,i,io
44+
45+
open(newunit=un,file=filename,status='old',action='read')
46+
call numlines(un,m)
47+
call numcol(un,n)
48+
49+
allocate(array(n,m))
50+
rewind(un)
51+
do i=1,m
52+
read(un,*,iostat=io)array(:,i)
53+
if(io.ne.0)exit
54+
enddo
55+
close(un)
56+
57+
end subroutine
58+
59+
pure function corr_array(array1,array2) result(a)
60+
real(rk),intent(in)::array1(:,:),array2(:,:)
61+
real(rk),allocatable::a(:)
62+
63+
integer(ik)::i,n
64+
65+
n=size(array1,dim=1)
66+
67+
allocate(a(n))
68+
a=0.0_rk
69+
do i=1,n
70+
a(i)=corr(array1(i,:),array2(i,:))
71+
enddo
72+
73+
end function
74+
75+
pure real(rk) function corr(array1,array2)
76+
real(rk),intent(in)::array1(:),array2(:)
77+
78+
real(rk)::mean1,mean2
79+
80+
!brute force
81+
82+
mean1=sum(array1)/size(array1)
83+
mean2=sum(array2)/size(array2)
84+
corr=dot_product(array1-mean1,array2-mean2)/sqrt(sum((array1-mean1)**2)*sum((array2-mean2)**2))
85+
86+
end function
87+
88+
subroutine numlines(unfile,n)
89+
implicit none
90+
integer::io
91+
integer,intent(in)::unfile
92+
integer,intent(out)::n
93+
rewind(unfile)
94+
n=0
95+
do
96+
read(unfile,*,iostat=io)
97+
if (io.ne.0) exit
98+
n=n+1
99+
enddo
100+
rewind(unfile)
101+
end subroutine
102+
103+
subroutine numcol(unfile,n)
104+
implicit none
105+
integer,intent(in)::unfile
106+
character(len=1000000)::a
107+
integer,intent(out)::n
108+
integer::curr,first,last,lena,stat,i
109+
rewind(unfile)
110+
read(unfile,"(a)")a
111+
curr=1;lena=len(a);n=0
112+
do
113+
first=0
114+
do i=curr,lena
115+
if (a(i:i) /= " ") then
116+
first=i
117+
exit
118+
endif
119+
enddo
120+
if (first == 0) exit
121+
curr=first+1
122+
last=0
123+
do i=curr,lena
124+
if (a(i:i) == " ") then
125+
last=i
126+
exit
127+
endif
128+
enddo
129+
if (last == 0) last=lena
130+
n=n+1
131+
curr=last+1
132+
enddo
133+
rewind(unfile)
134+
end subroutine
135+
136+
end program

0 commit comments

Comments
 (0)