<jupyter_start><jupyter_text>[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/jun-hyeok/SUP5001-41_Deep-Neural-Networks_2022Spring/blob/main/DNN_HW4/pt_1.ipynb)  DNN HW4 : Part 12022.03.16박준혁<jupyter_code>from util import *<jupyter_output><empty_output><jupyter_text>Part 1: Python  1-1. Python: List<jupyter_code>print("1-1", color=GRAY)

# define
list1 = [1, 2, 3]

# index
print(f"{type(list1) = }")
print(f"{list1[0], list1[-1] = }")

# element
list1[1] = "str1"
print(f"{list1[1] = }")

# range
list2 = range(10)
print(f"{type(list2) = }")

# type cast
list2 = list(list2)
print(f"{type(list2) = }")<jupyter_output>[90m1-1
[0mtype(list1) = <class 'list'>
list1[0], list1[-1] = (1, 3)
list1[1] = 'str1'
type(list2) = <class 'range'>
type(list2) = <class 'list'><jupyter_text>1-2. Python: List<jupyter_code>print("1-2", color=GRAY)

# slice range
print(f"{list2[2:4] = }")

# slice from # to end
print(f"{list2[2:] = }")

# slice from start to #
print(f"{list2[:2] = }")

# slice all (from start to end)
print(f"{list2[:] = }")

# slice step
print(f"{list2[::2] = }")

# slice neg index
print(f"{list2[:-1] = }")

# assign new list to sliced list
print()
print(f"{list2 = } : before")
list2[2:4] = [8, 9]
print("list2[2:4] = [8, 9]", color=GRAY, input=True)
print(f"{list2 = } : after")

print()
print(f"{list2 = } : before")
list2[2:5] = [8, 9]
print("list2[2:4] = [8, 9]", color=GRAY, input=True)
print(f"{list2 = } : after")<jupyter_output>[90m1-2
[0mlist2[2:4] = [2, 3]
list2[2:] = [2, 3, 4, 5, 6, 7, 8, 9]
list2[:2] = [0, 1]
list2[:] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
list2[::2] = [0, 2, 4, 6, 8]
list2[:-1] = [0, 1, 2, 3, 4, 5, 6, 7, 8]

list2 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] : before
[90m>>>list2[2:4] = [8, 9]
[0mlist2 = [0, 1, 8, 9, 4, 5, 6, 7, 8, 9] : after

list2 = [0, 1, 8, 9, 4, 5, 6, 7, 8, 9] : before
[90m>>>list2[2:4] = [8, 9]
[0mlist2 = [0, 1, 8, 9, 5, 6, 7, 8, 9] : after<jupyter_text>1-3. Python: For Loop with List<jupyter_code>print("1-3", color=GRAY)

# define animals list
animals = ["cat", "dog", "monkey"]

# print for loop
for animal in animals:
    print(f"{animal = }")
print()

# define nums list
nums = [0, 1, 2, 3, 4]
# empty list
squares = []

# calculate square of nums
for num in nums:
    squares.append(num**2)
print(f"{squares = }")<jupyter_output>[90m1-3
[0manimal = 'cat'
animal = 'dog'
animal = 'monkey'

squares = [0, 1, 4, 9, 16]<jupyter_text>1-4. Python: Function<jupyter_code>print("1-4", color=GRAY)

# define function sign(x)
def sign(x):
    if x > 0:
        return "positive"
    elif x < 0:
        return "negative"
    else:
        return "zero"


# print sign of [-1, 0, 1]
for x in [-1, 0, 1]:
    print(f"{x = :2}, {sign(x) = }")<jupyter_output>[90m1-4
[0mx = -1, sign(x) = 'negative'
x =  0, sign(x) = 'zero'
x =  1, sign(x) = 'positive'<jupyter_text>1-5. Python: Function<jupyter_code>print("1-5", color=GRAY)

# define function hello(name, loud=False)
def hello(name, loud=False):
    if loud:
        print(f"HELLO, {name.upper()}!")
    else:
        print(f"Hello, {name}!")


# hello loud default
print("hello('Bob')", color=GRAY, input=True)
hello("Bob")

# hello loud
print("hello('Fred', loud=True)", color=GRAY, input=True)
hello("Fred", loud=True)<jupyter_output>[90m1-5
[0m[90m>>>hello('Bob')
[0mHello, Bob!
[90m>>>hello('Fred', loud=True)
[0mHELLO, FRED!<jupyter_text>1-6. Python: zip<jupyter_code>print("1-6", color=GRAY)

# print 1 4 7  / 2 5 8 / 3 6 9
for x, y, z in zip(range(1, 4), range(4, 7), range(7, 10)):
    print(f"{x, y, z = }")
print()

# error
try:
    for x, y in zip(range(1, 4), range(4, 7), range(7, 10)):
        print(f"{x, y, z = }")
except Exception as e:
    print(f"x, y, z = {type(e).__name__}: {e}", color=RED)<jupyter_output>[90m1-6
[0mx, y, z = (1, 4, 7)
x, y, z = (2, 5, 8)
x, y, z = (3, 6, 9)

[91mx, y, z = ValueError: too many values to unpack (expected 2)
[0m<jupyter_text>1-7. Python: Magic method \_\_init\_\_()<jupyter_code>print("1-7", color=GRAY)

# define class HelloWorld
class HelloWorld:
    def __init__(self):
        print("init")


print("helloworld = HelloWorld()", color=GRAY, input=True)
helloworld = HelloWorld()<jupyter_output>[90m1-7
[0m[90m>>>helloworld = HelloWorld()
[0minit<jupyter_text>1-8. Python: Magic method \_\_call\_\_()<jupyter_code>print("1-8", color=GRAY)


class HelloWorld:
    def __init__(self):
        print("init", color=GRAY)


print("helloworld = HelloWorld()", color=GRAY, input=True)
helloworld = HelloWorld()

# helloworld is callable?
print(f"{callable(helloworld) = }")
try:
    print(f"{helloworld() = }")
except Exception as e:
    print(f"helloworld() = {type(e).__name__}: {e}", color=RED)
print()


class HelloWorld:
    def __init__(self):
        print("init", color=GRAY)

    def __call__(self):
        print("Hello world")


print("helloworld = HelloWorld()", color=GRAY, input=True)
helloworld = HelloWorld()

print("helloworld()", color=GRAY, input=True)
helloworld()<jupyter_output>[90m1-8
[0m[90m>>>helloworld = HelloWorld()
[0m[90minit
[0mcallable(helloworld) = False
[91mhelloworld() = TypeError: 'HelloWorld' object is not callable
[0m
[90m>>>helloworld = HelloWorld()
[0m[90minit
[0m[90m>>>helloworld()
[0mHello world<jupyter_text>Part 1: NumPy  NumPy: Install<jupyter_code>try:
    import numpy as np
except ImportError:
    print("numpy is not installed", color=RED)
    %pip install numpy
else:
    print("numpy is installed", color=GREEN)<jupyter_output>[92mnumpy is installed
[0m<jupyter_text>1-9. Numpy: np.array<jupyter_code>print("1-9", color=GRAY)

import numpy as np

# rank 1 array
a = np.array([1, 2, 3])

print(f"{type(a) = }")
print(f"{a.shape = }")
print(f"{a.ndim = }")
print(f"{a[0], a[1], a[2] = }")
print()

print(f"{a = } : before")
a[0] = 5
print("a[0] = 5", color=GRAY, input=True)
print(f"{a = } : after")
print()

# rank 2 array
b = np.array([[1, 2, 3], [4, 5, 6]])


print(f"{b.shape = }")
print(f"{b.ndim = }")
print(f"{b[0, 0], b[0, 1], b[0, 2] = }")<jupyter_output>[90m1-9
[0mtype(a) = <class 'numpy.ndarray'>
a.shape = (3,)
a.ndim = 1
a[0], a[1], a[2] = (1, 2, 3)

a = array([1, 2, 3]) : before
[90m>>>a[0] = 5
[0ma = array([5, 2, 3]) : after

b.shape = (2, 3)
b.ndim = 2
b[0, 0], b[0, 1], b[0, 2] = (1, 2, 3)<jupyter_text>1-10. Numpy: np.zeros, np.ones, np.full, np.eye, np.random<jupyter_code>print("1-10", color=GRAY)

import numpy as np

# array with all zeros
a = np.zeros((2, 2))
print(f"{a = }")

# array with all ones
b = np.ones((1, 2))
print(f"{b = }")

# array with the specific value
c = np.full((2, 2), 7)
print(f"{c = }")

# 2x2 identity matrix
d = np.eye(2)
print(f"{d = }")

# array with random values
e = np.random.random((2, 2))
print(f"{e = }")<jupyter_output>[90m1-10
[0ma = array([[0., 0.],
       [0., 0.]])
b = array([[1., 1.]])
c = array([[7, 7],
       [7, 7]])
d = array([[1., 0.],
       [0., 1.]])
e = array([[0.36245692, 0.22826985],
       [0.04302142, 0.27145544]])<jupyter_text>1-11. Numpy: np.where<jupyter_code>print("1-11", color=GRAY)

import numpy as np

# array with 0 to 9
a = np.arange(10)

# np.where(condition)
print(f"{np.where(a < 5) = }")

# np.where(condition, x, y)
print(f"{np.where(a < 5, a, 10 * a) = }")

# np.where(condition, x, y)
print(f"{np.where(a < 4, a, -1) = }")<jupyter_output>[90m1-11
[0mnp.where(a < 5) = (array([0, 1, 2, 3, 4]),)
np.where(a < 5, a, 10 * a) = array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])
np.where(a < 4, a, -1) = array([ 0,  1,  2,  3, -1, -1, -1, -1, -1, -1])<jupyter_text>1-12. Numpy: Array slice<jupyter_code>print("1-12", color=GRAY)

import numpy as np

# 3x4 array
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# slice the array a, the first 2 rows and column 1, 2
# shape of sub-array is (2, 2)
b = a[:2, 1:3]

# sub-array refer to the same array as original array, so
# change the sub-array will change the original array as well
# b[0, 0] == a[0, 1]
print(f"{b[0,0] == a[0,1] = }", color=GRAY)
print(f"{a[0, 1] = } : before")
b[0, 0] = 77
print("b[0, 0] = 77", color=GRAY, input=True)
print(f"{a[0, 1] = } : after")<jupyter_output>[90m1-12
[0m[90mb[0,0] == a[0,1] = True
[0ma[0, 1] = 2 : before
[90m>>>b[0, 0] = 77
[0ma[0, 1] = 77 : after<jupyter_text>1-13. Numpy: Array reshape<jupyter_code>print("1-13", color=GRAY)

import numpy as np

# 3x4 array
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

# reshape the array a to be a 2x6 array
b = np.reshape(a, (2, 6))
print(f"{b = }")

# reshape also refer to the same array as original array, so
# change the reshaped-array will change the original array as well
# b[0, 0] == a[0, 0]
print(f"{b[0,0] == a[0,0] = }", color=GRAY)
print(f"{a[0, 0] = } : before")
b[0, 0] = 77
print("b[0, 0] = 77", color=GRAY, input=True)
print(f"{a[0, 0] = } : after")<jupyter_output>[90m1-13
[0mb = array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]])
[90mb[0,0] == a[0,0] = True
[0ma[0, 0] = 1 : before
[90m>>>b[0, 0] = 77
[0ma[0, 0] = 77 : after<jupyter_text>1-14. Numpy: Array reshape<jupyter_code>print("1-14", color=GRAY)

import numpy as np

# 2 arrays with shape (2, 2)
a = np.array([[1, 0], [0, 1]])
print(f"{a = }", color=GRAY, input=True)
b = np.array([[4, 1], [2, 2]])
print(f"{b = }", color=GRAY, input=True)

# dot product of a and b
print(f"{np.dot(a, b) = }")<jupyter_output>[90m1-14
[0m[90m>>>a = array([[1, 0],
       [0, 1]])
[0m[90m>>>b = array([[4, 1],
       [2, 2]])
[0mnp.dot(a, b) = array([[4, 1],
       [2, 2]])<jupyter_text>Part 1: Pytorch  Pytorch: Install<jupyter_code>try:
    import torch
except ImportError:
    print("torch is not installed", color=RED)
    %pip install torch
else:
    print("torch is installed", color=GREEN)<jupyter_output>[92mtorch is installed
[0m<jupyter_text>1-15. Pytorch: Tensor<jupyter_code>print("1-15", color=GRAY)

import numpy as np
import torch

# float type tensor
t1 = torch.FloatTensor([0, 1, 2, 3, 4, 5, 6])
t2 = torch.tensor(np.arange(7))

print(f"{t1.shape = }")
print(f"{t2.shape = }")
print(f"{t1.dim() = }")
print(f"{t1.size() = }")
print(f"{t1[:2] = }")
print(f"{t1[3:] = }")<jupyter_output>[90m1-15
[0mt1.shape = torch.Size([7])
t2.shape = torch.Size([7])
t1.dim() = 1
t1.size() = torch.Size([7])
t1[:2] = tensor([0., 1.])
t1[3:] = tensor([3., 4., 5., 6.])<jupyter_text>1-16. Pytorch: NumPy array vs PyTorch tensor<jupyter_code>print("1-16", color=GRAY)

import numpy as np
import torch

b = np.arange(7)
t1 = torch.FloatTensor([0, 1, 2, 3, 4, 5, 6])
print(f"{b = }")
print(f"{t1 = }")
print(f"{type(b) = }")
print(f"{type(t1) = }")
print()

# transform numpy array to torch tensor
tt = torch.tensor(b)
t_from = torch.from_numpy(b)
print(f"{tt = }")
print(f"{t_from = }")
print(f"{type(tt) = }")
print(f"{type(t_from) = }")<jupyter_output>[90m1-16
[0mb = array([0, 1, 2, 3, 4, 5, 6])
t1 = tensor([0., 1., 2., 3., 4., 5., 6.])
type(b) = <class 'numpy.ndarray'>
type(t1) = <class 'torch.Tensor'>

tt = tensor([0, 1, 2, 3, 4, 5, 6])
t_from = tensor([0, 1, 2, 3, 4, 5, 6])
type(tt) = <class 'torch.Tensor'>
type(t_from) = <class 'torch.Tensor'><jupyter_text>1-17. Pytorch: NumPy array vs PyTorch tensor<jupyter_code>print("1-17", color=GRAY)

print(f"{tt = } : before")
print(f"{t_from = } : before")
b[0] = -10
print("b[0]= -10", color=GRAY, input=True)
print(f"{tt = } : after")
print(f"{t_from = }: after")
print()

# transform torch tensor to numpy array
t_to_np = t_from.numpy()
print(f"{t_to_np = }")
print(f"{type(t_to_np) = }")<jupyter_output>[90m1-17
[0mtt = tensor([0, 1, 2, 3, 4, 5, 6]) : before
t_from = tensor([0, 1, 2, 3, 4, 5, 6]) : before
[90m>>>b[0]= -10
[0mtt = tensor([0, 1, 2, 3, 4, 5, 6]) : after
t_from = tensor([-10,   1,   2,   3,   4,   5,   6]): after

t_to_np = array([-10,   1,   2,   3,   4,   5,   6])
type(t_to_np) = <class 'numpy.ndarray'><jupyter_text>1-18. Pytorch: Broadcasting<jupyter_code>print("1-18", color=GRAY)

import numpy as np
import torch

# addition of two tensors
m1 = torch.FloatTensor([[3, 3]])
print(f"{m1 = }", color=GRAY, input=True)
m2 = torch.FloatTensor([[2, 2]])
print(f"{m2 = }", color=GRAY, input=True)
print(f"{m1 + m2 = }")

# vector + scalar
m1 = torch.FloatTensor([[1, 2]])
print(f"{m1 = }", color=GRAY, input=True)
m2 = torch.FloatTensor([3])
print(f"{m2 = }", color=GRAY, input=True)
print(f"{m1 + m2 = }")

# 2x1 vector + 1x2 vector
m1 = torch.FloatTensor([[1, 2]])
print(f"{m1 = }", color=GRAY, input=True)
m2 = torch.FloatTensor([[3], [4]])
print(f"{m2 = }", color=GRAY, input=True)
print(f"{m1 + m2 = }")<jupyter_output>[90m1-18
[0m[90m>>>m1 = tensor([[3., 3.]])
[0m[90m>>>m2 = tensor([[2., 2.]])
[0mm1 + m2 = tensor([[5., 5.]])
[90m>>>m1 = tensor([[1., 2.]])
[0m[90m>>>m2 = tensor([3.])
[0mm1 + m2 = tensor([[4., 5.]])
[90m>>>m1 = tensor([[1., 2.]])
[0m[90m>>>m2 = tensor([[3.],
        [4.]])
[0mm1 + m2 = tensor([[4., 5.],
        [5., 6.]])<jupyter_text>1-19. Pytorch: torch.mul vs torch.matmul<jupyter_code>print("1-19", color=GRAY)

m1 = torch.FloatTensor([[1, 2], [3, 4]])
print(f"{m1 = }", color=GRAY, input=True)
m2 = torch.FloatTensor([[1], [2]])
print(f"{m2 = }", color=GRAY, input=True)
print(f"{m1 * m2 = }")
print(f"{m1.mul(m2) = }")
print(f"{m1.matmul(m2) = }")<jupyter_output>[90m1-19
[0m[90m>>>m1 = tensor([[1., 2.],
        [3., 4.]])
[0m[90m>>>m2 = tensor([[1.],
        [2.]])
[0mm1 * m2 = tensor([[1., 2.],
        [6., 8.]])
m1.mul(m2) = tensor([[1., 2.],
        [6., 8.]])
m1.matmul(m2) = tensor([[ 5.],
        [11.]])<jupyter_text>1-20. Pytorch: torch.view (np.reshape in PyTorch)<jupyter_code>print("1-20", color=GRAY)

import numpy as np
import torch

t = np.arange(12).reshape(-1, 2, 3)
floatT = torch.FloatTensor(t)

print(f"{floatT.shape = }")
print(f"{floatT.view([-1, 3]) = }")<jupyter_output>[90m1-20
[0mfloatT.shape = torch.Size([2, 2, 3])
floatT.view([-1, 3]) = tensor([[ 0.,  1.,  2.],
        [ 3.,  4.,  5.],
        [ 6.,  7.,  8.],
        [ 9., 10., 11.]])
{'fraction_non_alphanumeric': 0.24194933447831687, 'fraction_numerical': 0.08036353227422356, 'mean_word_length': 4.21455223880597, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 147, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '23', 'dir': 'jupyter-structured-clean-dedup', 'id': '72251', 'license': 'MIT', 'n_tokens_mistral': 7194, 'n_tokens_neox': 6567, 'n_words': 1472, 'path': 'DNN_HW4/pt_1.ipynb', 'repo_name': 'jun-hyeok/SUP5001-41_Deep-Neural-Networks_2022Spring', 'size': '13974'}