Monday, April 25, 2016

TensorFlow Installation


Tensor flow installation 

$sudo apt-get install oracle-java8-installer sudo apt-get install pkg-config zip g++ zlib1g-dev unzip wget https://github
Tried to install on GCE using two methods.
The first is the default installation instructions, when running the minst demo, it appears a bit slow (650-700ms)
Also tried this script.
Also tried to install from source: see original page

git clone --recurse-submodules https://github.com/tensorflow/tensorflow -b r0.7
$ sudo apt-get install python-numpy swig python-dev
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer sudo apt-get install pkg-config zip g++ zlib1g-dev unzip wget https://github.com/bazelbuild/bazel/releases/download/0.2.1/bazel_0.2.1-linux-x86_64.deb
 then google "how to install .deb"
./configure    --> say you don't have GPU  (my case. if it's not, you read yourself...)
bazel build -c opt --copt=-mavx //tensorflow/cc:tutorials_example_trainer


Jupiter notebook
look for their installation (pip..., including the dev too)
I also installed plots:
sudo apt-get install libfreetype6-dev libxft-dev
pip install matplotlib 
open port in GCE console:  gcloud compute firewall-rules create tcp8888 --allow=tcp:8888
run on the linux shell:  (ip 0.0.0.0 is a must, otherwise only local host will work)
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser
plotting:  if not visible, add this to the cell:  %matplotlib inline.   see a permanent solution here



installed via:
sudo pip install --upgrade https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.7.1-cp27-none-linux_x86_64.whl

$ python -m tensorflow.models.image.mnist.convolutionalSuccessfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.Extracting data/train-images-idx3-ubyte.gzExtracting data/train-labels-idx1-ubyte.gzExtracting data/t10k-images-idx3-ubyte.gzExtracting data/t10k-labels-idx1-ubyte.gzInitialized!Step 0 (epoch 0.00), 7.2 msMinibatch loss: 12.053, learning rate: 0.010000Minibatch error: 90.6%Validation error: 84.6%Step 100 (epoch 0.12), 698.7 msMinibatch loss: 3.279, learning rate: 0.010000Minibatch error: 6.2%Validation error: 7.1%Step 200 (epoch 0.23), 670.1 msMinibatch loss: 3.503, learning rate: 0.010000Minibatch error: 12.5%Validation error: 3.6%Step 300 (epoch 0.35), 666.0 msMinibatch loss: 3.199, learning rate: 0.010000Minibatch error: 7.8%Validation error: 3.4%Step 400 (epoch 0.47), 658.3 msMinibatch loss: 3.239, learning rate: 0.010000Minibatch error: 10.9%Validation error: 2.6%Step 500 (epoch 0.58), 657.4 msMinibatch loss: 3.283, learning rate: 0.010000Minibatch error: 9.4%Validation error: 2.6%

Wednesday, April 20, 2016

Python over C# / Java

Most people say the python is elegnet over Java. They are correct :)

Let's go over the basics, and I believe you will agree in the end...


Syntax


Curly brackets for scopes are gone.  we use the indentation instead.
if (x>10) :
  print("somewhat big")
  if x>50 :
     print "big"

Strings can be anything between 'x'  or "x"  or """x""" , in the later case new line in the editor is translated to new line (\n) in the string, so what you see is what you get.


and, or and not  are the actual operators(!) not &&,||,!.
"in" replaces the contains operator: if  'a' in ('a','b','c')
[not so great?]  instead of max=(a>b)?a:b  ,  use max = a if (a>b) else b
very easy way to wrap a function with a decorator on function like @args-check




loops

while and for loops have an optional "else" clause which will be triggered only when the loop exists normally on the condition failure (and not in "break").  This is very useful and save ugly code in end of java loops where you are unsure what caused you to pass the loop.

for loops are always using "in"
for odd in range(1,10,2) : print odd  #   from 1 , as long as <10 ,jump of 2
for item in list : print item
for key in dictionary: print key
for value in dic.itervalues() : print value

built in data structures

no arrays!  python asks us to use a higher level structure, like list.
list = []  is like Java ArrayList of Objectstuple=()  is immutalbe ArrayList of Objects

some syntactic sugar for all sequences data structures (list,tuple and string too!):

list[0]
list[0,3] returns a slice with elements 0,1,2
list += [ "hello" , "world"]   #adds all elements of the second list
if  "hello" in list : print "world"

dict = { "key1" : "value1" , "key2" : value2 }
len(dict)  # return 2
dict.update( { "key1": "updated1" , "key100":"value100"}) #update by another dict
if "key100" in dict:  print ("this was expected")
dict["key100"] = "updated100"
del(dict["key100"])  #both key and value are deleted
dict.items = list of key,value tuples   [ ("key1","value1", "key2,value2")]
dict(dict.items)  # transform a list with tuples into a dictionary

set is similiar to hashset
mySet = { "key1" , "key2" , "key3")
mySet = set(["key1","key2","key3" )


Better than the ugly :  if (list!=null && list.length>0)

if  list :   #nil=false and empty list = false too
  #do something with the list
else
  print("empty list or nil list, do exception path!")


Super cool documentation and unit-testing feature

the first declaration inside a method can be a string, with code examples.
def add(x,y)
  """ adds x to y and return the sum
  >>> add(1,1)
  2
 >>> add(-1,101)
 100
"""
  return x+y

the documentation is accessed using add.__doc__
doctest can run all the code samples in the documentation.

import doctest
if __name__ == "__main__" ;  #optional "if" ,means run this code only if you don't import this
  doctest.testmod()
it will search for all the mehod documentation, and run each example.  if there is a problem, it will output the expected and the actual value.


printing


simple, in this case we indent to the left the name, for column size of 20, then price as float.
print("Name={name:<20s} Price:{price:8.2f}. format(a="golden-crown", b=2000.01) )
print("Name={name:<20s} Price:{price:8.2f}. format("golden-crown", 2000.01) )  #same


in many case, you have a varialbes instead, you can do this trick and use the general method locals() which pass the dictionary of current scope values:
print("Name={name:<20s} Price:{price:8.2f}. format(**locals() ))
As a sidenote: You can use the same trick with your own dictionary.
  def foo(a,b,c,d) :  print (a,b,c,d) #method with four arguments
  myDic = { "c":0.4 , d:"description" , "a": 55 , "b": true , }  #dictionay somewhere in the code
  foo(**myDic)  #will pass the right values from the dictionary to the right arguments
  myList = [ 55, true, 0.4 , "description"]
  foo(*myList) #does the same, but here order is important

print itlsef is quite strong
print( a,b, sep="\n")  will seperate with newline, default is one space.
fh = open ("data.txt", "w")
print("lets write to file. why should it be difficult or different then regular print?", file=fh)
fh.close()



exceptions (see the else part)

try:
   f = open("file.txt")
except IOError as (errnum, errstr):
  print("IO exception number {0} : {1}".format(errnum,errstr)
except (ValueError, InventedError)
  print("other known error happened")
except:  #other unknown error
   print("unknown error, for this, we propogate up", sys.exc_info()[0]))
   raise
else:
    #read the file
    print( f.readlines())
    f.close()

finally:
   print("just like in java, very optional"

assert is a synatactic sugar to if  not <condition>: raise AssertionError("msg")
assert  x>0 , "x must be above zero, it wasn't! fix this for assertion to pass"




Thursday, April 14, 2016

Choosing a Home Router

It's remarkable how many options there are, and how unorganized the information is.
Let's jump to the bottom line, and then get back to details and spec:
As of 2016 my recommendations:
If your modem speed is 50-100MBs, buy VDSL2, 802.11n (or better) and 10/100Mbs or better
If your modem speed is better, or if you pass large files inside the nwetwork, make sure your wireless and wire speeds match it. 

And for the gory details:

Wireless speed (this is total, if you have 2 clients, they will share bandwidth)
2.4Ghz/5Ghz need to be supported on your laptop/phone.  Most support 2.4Ghz and the newer (>2014) also support 5GHz, which is much less crowded.
In my neighborhood, using the first caps me at 6-10MBs, due to interfeences, and the later can achieve 30MBs.

 
802.11 ac -(both 2.4 and 5Ghz in parallel)  - >> 2GBs
802.11 n - (both 2.4 and 5Ghz in parallel) max of 150MB (1 antena) to 450MB(3 antenas)
802.11 a - (only 5GHz) - 54MB (lots of time less)
802.11 g - (only 2.4GHZ)  max 54MB  (sometimes more, lots of time less)
802.11 b - 11MB 

Wire speed
10/100/1000 Mbs - means supports network cards of 10Mbs and of 100Mbs and of 1000Mbs.
10/100 Mbps - only supports up to 100Mbps
I suggest, As of 2016, to buy at least 10/100Mbs, 

Modem speed
Do you have cable-type or DSL type?  see below for DSL type only:
VDSL, VDSL2   : max of 100MB download and upload (usually lower with distance)
ADSL2+ : max of 24MB download and 3.3 upload.
ADSL     : slower