In [1]:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
%matplotlib inline
print ("PACKAGES LOADED")
In [2]:
print ("Hello, world")
# THERE ARE THREE POPULAR TYPES
# 1. INTEGER
x = 3;
print ("Integer: %01d, %02d, %03d, %04d, %05d"
% (x, x, x, x, x))
# 2. FLOAT
x = 123.456;
print ("Float: %.0f, %.1f, %.2f, %1.2f, %2.2f"
% (x, x, x, x, x))
# 3. STRING
x = "Hello, world"
print ("String: [%s], [%3s], [%20s]"
% (x, x, x))
In [3]:
dlmethods = ["ANN", "MLP", "CNN", "RNN", "DAE"]
for alg, i in zip(dlmethods, range(len(dlmethods))):
if alg in ["ANN", "MLP", "CNN"]:
print ("[%d/%d] %s is a feed-forward network."
% (i, len(dlmethods), alg))
elif alg in ["RNN"]:
print ("[%d/%d] %s is a recurrent network."
% (i, len(dlmethods), alg))
else:
print ("[%d/%d] %s is an unsupervised method."
% (i, len(dlmethods), alg))
In [4]:
def sum(a, b):
return a+b
X = 10.
Y = 20.
# Usage
print ("%.1f + %.1f = %.1f" % (X, Y, sum(X, Y)))
In [5]:
head = "Deep learning"
body = "very "
tail = "HARD."
print (head + " is " + body + tail)
# Repeat words
print (head + " is " + body*3 + tail)
print (head + " is " + body*10 + tail)
# It is used in this way
print ("\n" + "="*50)
print (" "*15 + "It is used in this way")
print ("="*50 + "\n")
# Indexing characters in the string
x = "Hello, world"
for i in range(len(x)):
print ("Index: [%02d/%02d] Char: %s"
% (i, len(x), x[i]))
In [6]:
x = "20160607Cloudy"
year = x[:4]
day = x[4:8]
weather = x[8:]
print ("[%s] -> [%s] + [%s] + [%s] "
% (x, year, day, weather))
In [7]:
a = []
b = [1, 2, 3]
c = ["Hello", ",", "world"]
d = [1, 2, 3, "x", "y", "z"]
print a, b, c, d
In [8]:
x = []
print x
x.append('a')
print x
In [9]:
x.append(123)
print x
In [10]:
x.append(["a", "b"])
print x
In [11]:
dic = dict()
dic["name"] = "Sungjoon"
dic["age"] = 32
dic["job"] = "Ph.D. Candidate"
print dic
In [12]:
print dic["job"]
In [13]:
class Greeter:
def __init__(self, name='Sam'): # CONSTRUCTOR
self.name = name
def greet(self, loud=False): # MEMBER FUNCTION
if loud:
print ('HELLO, %s!' % self.name.upper())
else:
print ('Hello, %s' % self.name)
In [14]:
g = Greeter('Fred')
g.greet()
g.greet(loud=True)
In [15]:
d = Greeter()
d.greet()
d.greet(loud=True)
In [16]:
def print_np(x):
print ("TYPE IS %s" % (type(x)))
print ("SHAPE IS %s" % (x.shape,))
print ("VALUES ARE \n%s" % (x))
In [17]:
x = np.array([1, 2, 3]) # rank 1 array
print_np(x)
x[0] = 5 # INDEX STARTS WITH 0
print_np(x)
In [18]:
y = np.array([[1,2,3], [4,5,6]])
print_np(y)
In [19]:
a = np.zeros((3, 2))
print_np(a)
In [20]:
b = np.ones((1, 2))
print_np(b)
In [21]:
c = np.eye(2, 2)
print_np(c)
In [22]:
d = np.random.random((2, 2))
print_np(d)
In [23]:
e = np.random.randn(1, 10)
print_np(e)
In [24]:
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print_np(a)
# INDEXING
b = a[:2, 1:3]
print_np(b)
In [25]:
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print_np(a)
row_r1 = a[1, :] # Rank 1 view of the second row of a
row_r2 = a[1:2, :] # Rank 2 view of the second row of a
row_r3 = a[[1], :] # Rank 2 view of the second row of a
print_np(row_r1)
print_np(row_r2)
print_np(row_r3)
In [26]:
x = np.array([[1,2],[3,4]], dtype=np.float32)
y = np.array([[5,6],[7,8]], dtype=np.float32)
# Elementwise sum; both produce the array
print x + y
print np.add(x, y)
In [27]:
print x - y
print np.subtract(x, y)
In [28]:
print x * y
print np.multiply(x, y)
In [29]:
print x / y
print np.divide(x, y)
In [30]:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([9,10])
w = np.array([11, 12])
print_np(x)
print_np(y)
print_np(v)
print_np(w)
In [31]:
# VECTOR / VECTOR PRODUCT => SCALAR
print_np(np.dot(v, w)) # <= v * w'
# MATRIX / VECTOR PRODUCT => RANK 1
print_np(np.dot(x, v)) # <= x * v'
# MATRIX / MATRIX PRODUCT => RANK 2
print_np(np.dot(x, y))
In [32]:
x = np.array([[1,2],[3,4]])
print_np(x)
print x
print x.T
print np.sum(x) # Compute sum of all elements
print np.sum(x, axis=0) # Compute sum of each column
print np.sum(x, axis=1) # Compute sum of each row
In [33]:
v = np.array([1, 0, 1])
vv = np.tile(v, (4, 1)) # Stack 4 copies of v on top of each other
print_np(vv)
In [34]:
# Compute the x and y coordinates for points on a sine curve
x = np.arange(0, 3 * np.pi, 0.1)
y = np.sin(x)
# Plot the points using matplotlib
plt.plot(x, y)
Out[34]:
In [35]:
y_sin = np.sin(x)
y_cos = np.cos(x)
# Plot the points using matplotlib
plt.plot(x, y_sin)
plt.plot(x, y_cos)
plt.xlabel('x axis label')
plt.ylabel('y axis label')
plt.title('Sine and Cosine')
plt.legend(['Sine', 'Cosine'])
# Show the figure.
plt.show()
In [36]:
# Compute the x and y coordinates for points
# on sine and cosine curves
x = np.arange(0, 3 * np.pi, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
# Set up a subplot grid that has height 2 and width 1,
# and set the first such subplot as active.
plt.subplot(2, 1, 1)
# Make the first plot
plt.plot(x, y_sin)
plt.title('Sine')
# Set the second subplot as active, and make the second plot.
plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('Cosine')
# Show the figure.
plt.show()
In [37]:
whos
In [38]:
%env
Out[38]:
In [39]:
%who
In [40]:
%timeit np.random.normal(size=1000)
In [43]:
%pdb
zzz
%pdb