%pylab inline
from tfs.models import LeNet
net = LeNet()

"""
Explanation: Load LeNet Model

some famous models have already been distributed together with the package
we provide some tools to look into the models
End of explanation
"""


netout = net.build()
print netout

"""
Explanation: Build the network

before we use the network object, we need to build() it first. 
by default, any network object only contains definitions about the network.  we need to call build() function to construct the computational graph.
End of explanation
"""


print net

print net.print_shape()

"""
Explanation: Explore the network object

we can see the network structure easily
End of explanation
"""


print net.initializer

print net.losser

print net.optimizer

"""
Explanation: each network object also has the following components binding with it:
initializer
loss
optimizer
End of explanation
"""


from tfs.dataset import Mnist
dataset = Mnist()

"""
Explanation: Load and explore the data

after we have construct the model, what we need to do next is to load data.
our package has provided some frequently used dataset, such as Mnist, and Cifar10
End of explanation
"""


import numpy as np
idx = np.random.randint(0,60000) # we have 60000 images in the training dataset
img = dataset.train.data[idx,:,:,0]
lbl = dataset.train.labels[idx]
imshow(img,cmap='gray')
print 'index:',idx,'\t','label:',lbl

"""
Explanation: we can explore the image inside the mnist dataset
End of explanation
"""


net.monitor

"""
Explanation: Train the network

It's very easy to train a network, just use fit function, which is a bit like sklearn
If you want to record some information during training, you can define a monitor, and plug it onto the network
The default monitor would only print some information every 10 steps.
End of explanation
"""


from tfs.core.monitor import *
net.monitor['default'].interval=20
net.monitor['var'] = LayerInputVarMonitor(net,interval=10)

net.fit(dataset,batch_size=200,n_epoch=1)

var_result = net.monitor['var'].results

import pandas as pd
var = pd.DataFrame(var_result,columns=[n.name for n in net.nodes])

var

"""
Explanation: now we change the print step to 20, and add a monitor that record the variance of each layer's input and output.
End of explanation
"""


