yuml / python > Deep learning用ネットワーク描画 > v0.1-v0.5: N層+dropoutまで

```txt:動作環境
Ubuntu 14.04 LTS desktop amd64
Python 2.7.6
```

Deep Learningのネットを説明する時に描画をするのが面倒くさい。
コードで簡単に作れないだろうか。　

yUML形式で試してみる。
参考 http://qiita.com/tukiyo3/items/840b52027be03d270b0a


### v0.1: 1層だけ

```draw.py
print "![](http://yuml.me/diagram/class/",
for elem in ('b','c','d','e'):
	print "[a]-->[" + elem + "],",

print ")"
```

`python draw.py`の出力結果が以下。

```
![](http://yuml.me/diagram/class/ [a]-->[b], [a]-->[c], [a]-->[d], [a]-->[e], )
```

上記のテキストをQiitaにそのまま貼ると以下となる。

![](http://yuml.me/diagram/class/ [a]-->[b], [a]-->[c], [a]-->[d], [a]-->[e], )

10層とか作れるのだろうか。レンダリングが重くなるかもしれない。


### v0.2: 2層だけ


```draw.py
print # for empty line

print "![](http://yuml.me/diagram/class/",
for outer in ('x','y','z'):
	for inner in ('b','c','d','e'):
		print "[" + outer + "]-->[" + inner + "],",

print ")"

print # for empty line
```

```
![](http://yuml.me/diagram/class/ [x]-->[b], [x]-->[c], [x]-->[d], [x]-->[e], [y]-->[b], [y]-->[c], [y]-->[d], [y]-->[e], [z]-->[b], [z]-->[c], [z]-->[d], [z]-->[e], )
```

![](http://yuml.me/diagram/class/ [x]-->[b], [x]-->[c], [x]-->[d], [x]-->[e], [y]-->[b], [y]-->[c], [y]-->[d], [y]-->[e], [z]-->[b], [z]-->[c], [z]-->[d], [z]-->[e], )


Fully connected networkのみ。

### v0.3: 3層だけ

層を増やすのは面倒くさい。

```draw.py
print # for empty line


hidden1=('x','y','z')
hidden2=('a','b','c','d')
hidden3=('l','m','n','o')

print "![](http://yuml.me/diagram/class/",

for outer in hidden1:
	for inner in hidden2:
		print "[" + outer + "]-->[" + inner + "],",

for outer in hidden2:
	for inner in hidden3:
		print "[" + outer + "]-->[" + inner + "],",

print ")"

print # for empty line
```

```
![](http://yuml.me/diagram/class/ [x]-->[a], [x]-->[b], [x]-->[c], [x]-->[d], [y]-->[a], [y]-->[b], [y]-->[c], [y]-->[d], [z]-->[a], [z]-->[b], [z]-->[c], [z]-->[d], [a]-->[l], [a]-->[m], [a]-->[n], [a]-->[o], [b]-->[l], [b]-->[m], [b]-->[n], [b]-->[o], [c]-->[l], [c]-->[m], [c]-->[n], [c]-->[o], [d]-->[l], [d]-->[m], [d]-->[n], [d]-->[o], )
```

![](http://yuml.me/diagram/class/ [x]-->[a], [x]-->[b], [x]-->[c], [x]-->[d], [y]-->[a], [y]-->[b], [y]-->[c], [y]-->[d], [z]-->[a], [z]-->[b], [z]-->[c], [z]-->[d], [a]-->[l], [a]-->[m], [a]-->[n], [a]-->[o], [b]-->[l], [b]-->[m], [b]-->[n], [b]-->[o], [c]-->[l], [c]-->[m], [c]-->[n], [c]-->[o], [d]-->[l], [d]-->[m], [d]-->[n], [d]-->[o], )


### v0.4: N層

```draw.py
print # for empty line


hidden=[
	('x','y','z'),
	('a','b','c','d'),
	('e','f','g','h'),
	('i','j','k','l'),
	('m','n','o','p')
	]

print "![](http://yuml.me/diagram/class/",

for idx in range(4):
	for outer in hidden[idx]:
		for inner in hidden[idx+1]:
			print "[" + outer + "]-->[" + inner + "],",
print ")"

print # for empty line
```

`
![](http://yuml.me/diagram/class/ [x]-->[a], [x]-->[b], [x]-->[c], [x]-->[d], [y]-->[a], [y]-->[b], [y]-->[c], [y]-->[d], [z]-->[a], [z]-->[b], [z]-->[c], [z]-->[d], [a]-->[e], [a]-->[f], [a]-->[g], [a]-->[h], [b]-->[e], [b]-->[f], [b]-->[g], [b]-->[h], [c]-->[e], [c]-->[f], [c]-->[g], [c]-->[h], [d]-->[e], [d]-->[f], [d]-->[g], [d]-->[h], [e]-->[i], [e]-->[j], [e]-->[k], [e]-->[l], [f]-->[i], [f]-->[j], [f]-->[k], [f]-->[l], [g]-->[i], [g]-->[j], [g]-->[k], [g]-->[l], [h]-->[i], [h]-->[j], [h]-->[k], [h]-->[l], [i]-->[m], [i]-->[n], [i]-->[o], [i]-->[p], [j]-->[m], [j]-->[n], [j]-->[o], [j]-->[p], [k]-->[m], [k]-->[n], [k]-->[o], [k]-->[p], [l]-->[m], [l]-->[n], [l]-->[o], [l]-->[p], )
`

![](http://yuml.me/diagram/class/ [x]-->[a], [x]-->[b], [x]-->[c], [x]-->[d], [y]-->[a], [y]-->[b], [y]-->[c], [y]-->[d], [z]-->[a], [z]-->[b], [z]-->[c], [z]-->[d], [a]-->[e], [a]-->[f], [a]-->[g], [a]-->[h], [b]-->[e], [b]-->[f], [b]-->[g], [b]-->[h], [c]-->[e], [c]-->[f], [c]-->[g], [c]-->[h], [d]-->[e], [d]-->[f], [d]-->[g], [d]-->[h], [e]-->[i], [e]-->[j], [e]-->[k], [e]-->[l], [f]-->[i], [f]-->[j], [f]-->[k], [f]-->[l], [g]-->[i], [g]-->[j], [g]-->[k], [g]-->[l], [h]-->[i], [h]-->[j], [h]-->[k], [h]-->[l], [i]-->[m], [i]-->[n], [i]-->[o], [i]-->[p], [j]-->[m], [j]-->[n], [j]-->[o], [j]-->[p], [k]-->[m], [k]-->[n], [k]-->[o], [k]-->[p], [l]-->[m], [l]-->[n], [l]-->[o], [l]-->[p], )


## v0.5: N層 + dropout

dropoutを破線で表す。
描画しないというのでも良い。

参考: http://qiita.com/HirofumiYashima/items/7fbf76e3f5dfdf3bc7a3

```draw.py
import numpy as np
from numpy.random import *

print # for empty line

hidden=[
	('x','y','z'),
	('a','b','c','d'),
	('e','f','g','h'),
	('i','j','k','l'),
	('m','n','o','p')
	]

print "![](http://yuml.me/diagram/class/",

rand(10)

for idx in range(4):
	for outer in hidden[idx]:
		for inner in hidden[idx+1]:
			val = rand()
			if val > 0.5:
				print "[" + outer + "]-->[" + inner + "],",
			else:
				print "[" + outer + "]-.->[" + inner + "],",				
print ")"

print # for empty line
```

`![](http://yuml.me/diagram/class/ [x]-.->[a], [x]-.->[b], [x]-.->[c], [x]-->[d], [y]-->[a], [y]-.->[b], [y]-->[c], [y]-->[d], [z]-.->[a], [z]-.->[b], [z]-->[c], [z]-->[d], [a]-.->[e], [a]-.->[f], [a]-->[g], [a]-->[h], [b]-.->[e], [b]-.->[f], [b]-->[g], [b]-.->[h], [c]-.->[e], [c]-.->[f], [c]-.->[g], [c]-->[h], [d]-.->[e], [d]-.->[f], [d]-.->[g], [d]-.->[h], [e]-.->[i], [e]-.->[j], [e]-.->[k], [e]-.->[l], [f]-->[i], [f]-->[j], [f]-.->[k], [f]-.->[l], [g]-->[i], [g]-->[j], [g]-.->[k], [g]-->[l], [h]-.->[i], [h]-->[j], [h]-->[k], [h]-->[l], [i]-.->[m], [i]-.->[n], [i]-.->[o], [i]-->[p], [j]-->[m], [j]-->[n], [j]-->[o], [j]-->[p], [k]-->[m], [k]-->[n], [k]-.->[o], [k]-->[p], [l]-.->[m], [l]-->[n], [l]-->[o], [l]-->[p], )`

![](http://yuml.me/diagram/class/ [x]-.->[a], [x]-.->[b], [x]-.->[c], [x]-->[d], [y]-->[a], [y]-.->[b], [y]-->[c], [y]-->[d], [z]-.->[a], [z]-.->[b], [z]-->[c], [z]-->[d], [a]-.->[e], [a]-.->[f], [a]-->[g], [a]-->[h], [b]-.->[e], [b]-.->[f], [b]-->[g], [b]-.->[h], [c]-.->[e], [c]-.->[f], [c]-.->[g], [c]-->[h], [d]-.->[e], [d]-.->[f], [d]-.->[g], [d]-.->[h], [e]-.->[i], [e]-.->[j], [e]-.->[k], [e]-.->[l], [f]-->[i], [f]-->[j], [f]-.->[k], [f]-.->[l], [g]-->[i], [g]-->[j], [g]-.->[k], [g]-->[l], [h]-.->[i], [h]-->[j], [h]-->[k], [h]-->[l], [i]-.->[m], [i]-.->[n], [i]-.->[o], [i]-->[p], [j]-->[m], [j]-->[n], [j]-->[o], [j]-->[p], [k]-->[m], [k]-->[n], [k]-.->[o], [k]-->[p], [l]-.->[m], [l]-->[n], [l]-->[o], [l]-->[p], )