├── 2015 ├── Chapter1 │ ├── Chapter0.py │ ├── Chapter1.py │ ├── Chapter1_2.py │ ├── Chapter1_2_1.py │ ├── Chapter1_2_2.py │ ├── Chapter1_3.py │ ├── Chapter1_3_1.py │ ├── Chapter1_3_2.py │ ├── Chapter1_4.py │ ├── Chapter1_5.py │ ├── Chapter1_5_2.py │ ├── Chapter1_5_3.py │ ├── Chapter1_5_4.py │ ├── Chapter1_5_4_1.py │ ├── Chapter1_6.py │ ├── bigdigits_ans.py │ └── generate_grid.py ├── Chapter2 │ ├── Chapter2_1.py │ └── Chapter2_2_data_structures.py └── Chapter3 │ ├── CSV │ ├── co2-sample.csv │ ├── csv2html.py │ └── csv2html1_ans.py │ ├── Chapter3.py │ ├── Generate_grid │ └── generate_grid.py │ └── Generate_usernames │ ├── generate_usernames.py │ └── generate_usernames_ans.py ├── 2016 ├── Chapter1 │ ├── Chapter1_Homework.ipynb │ ├── chapter1.ipynb │ └── chapter1.py ├── Chapter2 │ └── Chapter2.ipynb ├── Chapter3 │ ├── Chapter3.ipynb │ ├── Chapter3.py │ └── Chapter3_homework.ipynb └── Chapter4 │ └── Chapter4.ipynb ├── 2018 └── Introduction_to_Python_0409.pdf ├── 2019 ├── CNN_for_textile │ └── fashionnet.py ├── Chinese Visual understanding │ ├── Code.pdf │ ├── VQA.pdf │ ├── tf_forward.py │ ├── utils.py │ └── vgg16.py ├── GPU_TF │ ├── 00_GPU&TF_Basic.ipynb │ ├── 01-TF-NN.ipynb │ └── 02.TF-Regression.ipynb ├── RNN_Neo4j │ ├── Neo4j.ipynb │ ├── P_neo4j.txt │ ├── README.md │ └── RNN_Neo4j.ipynb ├── Sentiment analysis │ ├── CNN_SA.py │ └── SVM_SA.py └── Word2vec │ ├── Word2vec.ipynb │ ├── Word2vec.ipynb 拷貝 │ ├── question_jieba.txt │ ├── question_jieba_e.txt │ ├── tf_word2vec.py │ └── tsne.png └── README.md /2015/Chapter1/Chapter0.py: -------------------------------------------------------------------------------- 1 | #Chapter0 2 | "$(FULL_CURRENT_PATH)" 3 | 4 | print (3, -1, 3.14159, -2.8) 5 | 6 | 7 | print (type(3)) 8 | print (type(3.14159)) 9 | print (type(3.0)) 10 | 11 | 12 | 13 | print (int(3.14159), int(-2.8)) 14 | print (float(3), float(-1)) 15 | 16 | 17 | 18 | print (3.1415926535897932384626433832795028841971) 19 | print (1.4142135623730950488016887242096980785696) 20 | 21 | # arithmetic operators 22 | # + plus addition 23 | # - minus subtraction 24 | # * times multiplication 25 | # / divided by division 26 | # ** power exponentiation 27 | 28 | print (1 + 2, 3 - 4, 5 * 6, 2 ** 5) 29 | 30 | 31 | print (1.0 / 3, 5.0 / 2.0, -7 / 3.0) 32 | 33 | 34 | print (1 / 3, 5 / 2, -7 / 3) 35 | 36 | 37 | # expressions - number or a binary operator applied to two expressions 38 | # minus is also a unary operator and can be applied to a single expression 39 | # 先乘除後加減 40 | print (1 + 2 * 3, 4.0 - 5.0 / 6.0, 7 * 8 + 9 * 10) 41 | 42 | print (1 * 2 + 3 * 4) 43 | print (2 + 12) 44 | 45 | 46 | # always manually group using parentheses when in doubt 47 | 48 | 49 | print (1 * (2 + 3) * 4) 50 | print (1 * 5 * 4) 51 | 52 | 53 | 54 | # valid variable names - consists of letters, numbers, underscore (_) 55 | # starts with letter or underscore 56 | # case sensitive (capitalization matters) 57 | 58 | # legal names - ninja, Ninja, n_i_n_j_a 59 | # illegal names - 1337, 1337ninja 60 | 61 | # Python convention - multiple words joined by _ 62 | # legal names - elite_ninja, leet_ninja, ninja_1337 63 | # illegal name 1337_ninja 64 | 65 | 66 | # assign to variable name using single equal sign = 67 | # (remember that double equals == is used to test equality) 68 | 69 | # examples 70 | 71 | my_name = "Joe Warren" 72 | print (my_name) 73 | 74 | my_age = 51 75 | print (my_age) 76 | 77 | # birthday - add one 78 | 79 | #my_age += 1 80 | #my_age = my_age +1 81 | print (my_age) 82 | 83 | 84 | # the story of the magic pill 85 | 86 | magic_pill = 30 87 | print (my_age - magic_pill) 88 | 89 | my_grand_dad = 74 90 | 91 | print (my_grand_dad - 2 * magic_pill) 92 | 93 | 94 | 95 | 96 | 97 | # Temperature examples 98 | #華氏(Fahrenheit)轉攝氏(Celsius)臺灣 99 | # convert from Fahrenheit to Celsuis 100 | # c = 5 / 9 * (f - 32) 101 | # use explanatory names 102 | 103 | #攝氏(Celsius) = (Fahrenheit-32)*5/9 104 | #華氏(Fahrenheit) = Celsius*(9/5)+32 105 | 106 | 107 | temp_Fahrenheit = 200 108 | #華氏212度,攝氏是多少 109 | temp_Celsius = 5.0 / 9.0 * (temp_Fahrenheit - 32) 110 | print (temp_Celsius) 111 | 112 | #Test! 113 | 114 | #Data Type 115 | #Font 116 | "Hard Times"[5] 117 | "giraffe"[0] 118 | 119 | #Object Reference 120 | #Variable 121 | x = "blue" 122 | y = "green" 123 | z = x 124 | 125 | print (x, y, z) 126 | z = y 127 | print (x, y, z) 128 | x = z 129 | print (x, y, z) 130 | 131 | route = 866 132 | print (route, type(route)) 133 | 134 | route = "North" 135 | print (route, type(route)) 136 | 137 | #Collection Data Type 138 | 139 | "Denmark","Finland","Norway","Sweden" 140 | "one", 141 | x = [1,4,9,16,25,36,49] 142 | type(x) 143 | x[1] 144 | x[0] 145 | 146 | y = ["Denmark","Finland","Norway","Sweden"] 147 | z=y[0] 148 | type(z) 149 | 150 | #Len() 151 | len("one") 152 | len("one",) 153 | len(("one",)) 154 | len([3,5,1,2,"pause",5]) 155 | len("automatically") 156 | 157 | #append() 158 | x = ["zebra",49,-879,"aardvark",200] 159 | x.append("more") 160 | print (x) 161 | type(x) 162 | #list.append() 163 | list.append(x, "extra") 164 | print (x) 165 | type(x) 166 | 167 | #Switch data 168 | x[1] = "kk" 169 | x 170 | 171 | #Explorer data 172 | x[0:3] 173 | 174 | ##Logic 175 | a = ["R", 3, None] 176 | b = ["R", 3, None] 177 | a is b 178 | b = a 179 | a is b 180 | 181 | a = "something" 182 | b = None 183 | a is not None 184 | b is not None 185 | a is not None, b is not None 186 | 187 | #<, <=, ==, !=, >=, > 188 | a = 2 189 | b = 6 190 | a == b 191 | a <= b, a != b, a >= b, a > b 192 | 193 | a = "many paths" 194 | b = "many paths" 195 | a is b 196 | a == b 197 | 198 | a = 9 199 | 0 <= a <= 10 200 | 201 | "3" > 4 202 | 3 > 4 203 | int("3") > 4 204 | 205 | ##Membership 206 | p = (4, "frog", 9, -33, 9, 2) 207 | 2 in p 208 | "dog" not in p 209 | 210 | 211 | 212 | 213 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1.py: -------------------------------------------------------------------------------- 1 | #print ("我的第一隻Python程式") 2 | #123 3 | name = input('What is your name?\n') 4 | print('Hi, %s.' % name) 5 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_2.py: -------------------------------------------------------------------------------- 1 | #if else 2 | lines = 19999 3 | if lines < 1000: 4 | print("small") 5 | elif lines < 10000: 6 | print("medium") 7 | elif lines < 20000: 8 | print("gogogo") 9 | else: 10 | print("large") 11 | 12 | 13 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_2_1.py: -------------------------------------------------------------------------------- 1 | #if else 2 | x = int(input("Please enter an integer:")) 3 | 4 | if x < 0: 5 | x = 0 6 | print("Negative changed to zero") 7 | elif x == 0: 8 | print("Zero") 9 | elif x == 1: 10 | print("Single") 11 | else: 12 | print("More") 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_2_2.py: -------------------------------------------------------------------------------- 1 | #if else 2 | x = int(input("Please enter an integer:\n")) 3 | 4 | if x < 0: 5 | x = 0 6 | print("Negative changed to zero") 7 | elif x == 0: 8 | print("Zero") 9 | elif x == 1: 10 | print("Single") 11 | else: 12 | print("More") 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_3.py: -------------------------------------------------------------------------------- 1 | #While 2 | temperature = 200 3 | while temperature > 10: # first while loop code 4 | print(temperature) 5 | temperature = temperature - 1 6 | 7 | print('The tea is cool enough.') 8 | 9 | ##################################### 10 | i = 4 11 | while i < 9: 12 | print(i) 13 | i = i+2 14 | ##################################### -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_3_1.py: -------------------------------------------------------------------------------- 1 | for country in ["Denmark", "Finland", "Norway", "Sweden"]: 2 | print(country) 3 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_3_2.py: -------------------------------------------------------------------------------- 1 | for x in [1,2,3,4,5,6]: 2 | print(x) 3 | ########################### 4 | #4 to 9 5 | nums = range(4, 9, 2) 6 | print(list(nums)) 7 | #list(range(10, 0, -1)) 8 | 9 | ########################### 10 | for x in range(1, 10): 11 | for y in range(1, 10): 12 | print (x ,'*', y ,'=' ,x*y) 13 | 14 | ########################### 15 | #\n about columns break. 16 | for x in [1,2,3,4,5,6,7,8,9]: 17 | for y in [1,2,3,4,5,6,7,8,9]: 18 | print ('%s*%s=%s \n' % (x,y,x*y)) 19 | ########################### 20 | #\t about space. 21 | for x in range(1,10): 22 | for y in range(1,10): 23 | print ('%s*%s=%s \t' % (x,y,x*y)) 24 | ########################### 25 | 26 | ##Advance 9*9 problem 27 | ########################### 28 | print ("\n".join(["\t".join(["%d*%d=%d" % (j,i,i*j) for i in range(1,10)]) for j in range(1,10)])) 29 | 30 | ########################### 31 | a = "Free your mind." 32 | print(a) 33 | print("&".join(a)) 34 | ########################## 35 | 36 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_4.py: -------------------------------------------------------------------------------- 1 | s = input("enter an integer:") 2 | try: 3 | i = int(s) 4 | print("valid integer entered:", i) 5 | except ValueError as err: 6 | print(err) 7 | 8 | #https://docs.python.org/2/tutorial/errors.html 9 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_5.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright (c) 2008-11 Qtrac Ltd. All rights reserved. 3 | # This program or module is free software: you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License as published 5 | # by the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. It is provided for educational 7 | 8 | 9 | print("Type integers, each followed by Enter; or just Enter to finish") 10 | 11 | total = 0 12 | count = 0 13 | 14 | while True: 15 | line = input("integer: ") 16 | if line: 17 | try: 18 | number = int(line) 19 | except ValueError as err: 20 | print(err) 21 | continue 22 | total += number 23 | count += 1 24 | else: 25 | break 26 | 27 | if count: 28 | print("count =", count, "total =", total, "mean =", total / count) 29 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_5_2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright (c) 2008-11 Qtrac Ltd. All rights reserved. 3 | # This program or module is free software: you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License as published 5 | # by the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. It is provided for educational 7 | # purposes and is distributed in the hope that it will be useful, but 8 | # WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 | # General Public License for more details. 11 | 12 | print("Type integers, each followed by Enter; or ^D or ^Z to finish") 13 | 14 | total = 0 15 | count = 0 16 | 17 | while True: 18 | try: 19 | line = input() 20 | if line: 21 | number = int(line) 22 | total += number 23 | count += 1 24 | except ValueError as err: 25 | print(err) 26 | continue 27 | except EOFError: 28 | break 29 | 30 | if count: 31 | print("count =", count, "total =", total, "mean =", total / count) 32 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_5_3.py: -------------------------------------------------------------------------------- 1 | #### 2 | #'''Function definition and invocation.''' 3 | 4 | def happyBirthdayEmily(): 5 | print("Happy Birthday to you!") 6 | print("Happy Birthday to you!") 7 | print("Happy Birthday, dear Emily.") 8 | print("Happy Birthday to you!") 9 | 10 | happyBirthdayEmily() 11 | ######################### 12 | ##Function 13 | age = get_int('enter your age:') 14 | print('Age: %s' % age) 15 | 16 | def get_int(msg): 17 | while True: 18 | try: 19 | i = int(input(msg)) 20 | return i 21 | except ValueError as err: 22 | print(err) 23 | 24 | ######################### 25 | age = get_int("enter your age:") -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_5_4.py: -------------------------------------------------------------------------------- 1 | ##Function 2 | def get_int(msg): 3 | while True: 4 | try: 5 | i = int(input(msg)) 6 | return i 7 | except ValueError as err: 8 | print(err) 9 | ##################### 10 | age = get_int('enter your age:') 11 | print('Age: %s' % age) 12 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_5_4_1.py: -------------------------------------------------------------------------------- 1 | def get_int(msg): 2 | while True: 3 | try: 4 | i = int(input(msg)) 5 | return i 6 | except ValueError as err: 7 | print(err) 8 | 9 | age = get_int('enter your age:') 10 | print('Age: %s' % age) 11 | -------------------------------------------------------------------------------- /2015/Chapter1/Chapter1_6.py: -------------------------------------------------------------------------------- 1 | import random 2 | x = random.randint(1,6) 3 | y = random.choice(["apple", "banana", "cherry", "durian"]) 4 | print (x, y) 5 | -------------------------------------------------------------------------------- /2015/Chapter1/bigdigits_ans.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import sys 4 | 5 | Zero = [" *** ", 6 | " * * ", 7 | "* *", 8 | "* *", 9 | "* *", 10 | " * * ", 11 | " *** "] 12 | One = [" * ", "** ", " * ", " * ", " * ", " * ", "***"] 13 | Two = [" *** ", "* *", "* * ", " * ", " * ", "* ", "*****"] 14 | Three = [" *** ", "* *", " *", " ** ", " *", "* *", " *** "] 15 | Four = [" * ", " ** ", " * * ", "* * ", "******", " * ", 16 | " * "] 17 | Five = ["*****", "* ", "* ", " *** ", " *", "* *", " *** "] 18 | Six = [" *** ", "* ", "* ", "**** ", "* *", "* *", " *** "] 19 | Seven = ["*****", " *", " * ", " * ", " * ", "* ", "* "] 20 | Eight = [" *** ", "* *", "* *", " *** ", "* *", "* *", " *** "] 21 | Nine = [" ****", "* *", "* *", " ****", " *", " *", " *"] 22 | 23 | 24 | Digits = [Zero, One, Two, Three, Four, Five, Six, Seven, Eight, Nine] 25 | 26 | try: 27 | digits = sys.argv[1] 28 | row = 0 29 | while row < 7: 30 | line = "" 31 | column = 0 32 | while column < len(digits): 33 | number = int(digits[column]) 34 | digit = Digits[number] 35 | for char in digit[row]: 36 | if(char=='*'): 37 | char=str(number) 38 | line += char 39 | line += " " 40 | column += 1 41 | print(line) 42 | row += 1 43 | except IndexError: 44 | print("usage: bigdigits.py ") 45 | except ValueError as err: 46 | print(err, "in", digits) 47 | -------------------------------------------------------------------------------- /2015/Chapter1/generate_grid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright (c) 2008-11 Qtrac Ltd. All rights reserved. 3 | # This program or module is free software: you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License as published 5 | # by the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. It is provided for educational 7 | # purposes and is distributed in the hope that it will be useful, but 8 | # WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 | # General Public License for more details. 11 | 12 | import random 13 | 14 | 15 | def get_int(msg, minimum, default): 16 | while True: 17 | try: 18 | line = input(msg) 19 | if not line and default is not None: 20 | return default 21 | i = int(line) 22 | if i < minimum: 23 | print("must be >=", minimum) 24 | else: 25 | return i 26 | except ValueError as err: 27 | print(err) 28 | 29 | 30 | rows = get_int("rows: ", 1, None) 31 | columns = get_int("columns: ", 1, None) 32 | minimum = get_int("minimum (or Enter for 0): ", -1000000, 0) 33 | 34 | default = 1000 35 | if default < minimum: 36 | default = 2 * minimum 37 | maximum = get_int("maximum (or Enter for " + str(default) + "): ", 38 | minimum, default) 39 | 40 | row = 0 41 | while row < rows: 42 | line = "" 43 | column = 0 44 | while column < columns: 45 | i = random.randint(minimum, maximum) 46 | s = str(i) 47 | while len(s) < 10: 48 | s = " " + s 49 | line += s 50 | column += 1 51 | print(line) 52 | row += 1 53 | -------------------------------------------------------------------------------- /2015/Chapter2/Chapter2_1.py: -------------------------------------------------------------------------------- 1 | #Title:Python chapter 2 2 | #Date:2014/12/01 3 | #Author:Jerry Wu 4 | #E-mail:jerry@mail.ntust.edu.tw 5 | 6 | #keywords 7 | pass=1 8 | with=1 9 | return=1 10 | 11 | # 12 | for in (0,1,2,3,4,5): 13 | print("Hello") 14 | 15 | #####Integer 16 | x=10 17 | y=2 18 | z=4 19 | 20 | x+y 21 | x-y 22 | x*y 23 | x/y 24 | x//y 25 | x%y 26 | x**y 27 | -x 28 | +x 29 | abs(x) 30 | divmod(x,y) 31 | pow(x,y) 32 | pow(x,y,z) 33 | #pow(10,2,2) = (10*10) % 2 34 | round(10.23333,2) 35 | 36 | #Binary 37 | bin(1980) 38 | #Hexadecimal 39 | hex(1980) 40 | #Octal 41 | oct(1980) 42 | #int 43 | int(1.2456) 44 | int(bytearray(b'123'), 20) 45 | 46 | #Decimal(10) 47 | 14600926 48 | #Binary(2) 49 | 0b110111101100101011011110 50 | #Octal(8) 51 | 0o67545336 52 | #Hexadecimal(16) 53 | 0xDECADE 54 | 55 | #Boolean 56 | t= True 57 | f=False 58 | t and f 59 | t and True 60 | 61 | 62 | ######Floating-point number 63 | 0.0,5.4,-2.5,8.9e-4 64 | 65 | import math 66 | math.acos(0.64) 67 | math.acosh(1.64) 68 | math.asin(1) 69 | math.asinh(1) 70 | math.atan(1) 71 | math.atan2(1,2) 72 | math.atanh(0.64) 73 | math.ceil(0.64) 74 | math.copysign(0.634,2) 75 | math.cos(0.64) 76 | math.cosh(0.64) 77 | math.degrees(0.64) 78 | math.e 79 | math.exp(0.64) 80 | math.fabs(x) 81 | math.factorial(5) #階乘 82 | math.floor(5.4) 83 | 84 | #Decimal 85 | import decimal 86 | a = decimal.Decimal(9876) 87 | b = decimal.Decimal("54321.012345678987654321") 88 | a + b 89 | 90 | 23/1.05 91 | print(23/1.05) 92 | print(decimal.Decimal(23)/decimal.Decimal(1.05)) 93 | 94 | ######Character 95 | 96 | str(2.3) 97 | #int(2.3) 98 | #float(2.3) 99 | 100 | #Slice 101 | s = "Light ray" 102 | s[-9] 103 | s[0] 104 | 105 | s[-1] 106 | s[8] 107 | 108 | s[0:2] 109 | s[:] 110 | s[0:len(s)] 111 | 112 | x = "The waxwork man" 113 | x[:12] + "wo" + x[12:] 114 | 115 | x1 = "he ate camel food" 116 | x1[::-2] 117 | x1[::3] 118 | x1[-1:2:-2] 119 | x1[:2:-2] 120 | 121 | treatises = ["A","C","D"] 122 | " ".join(treatises) 123 | "-<>-".join(treatises) 124 | "".join(treatises) 125 | 126 | ##x.capitalize() #Return capital letter 127 | x = "work" 128 | print(x) 129 | x.capitalize() 130 | 131 | 132 | ##x.center(width,char) 133 | x = "This is a book." 134 | print(x) 135 | print(x.center(56, "=")) 136 | x.center(15,"=") 137 | 138 | ##x.count() 139 | x = "This is a book." 140 | print(x) 141 | print(x.count("i")) 142 | print(x.count("i",0,4)) 143 | 144 | ##x.encode() 145 | x = "This is a book." 146 | x.encode(encoding="utf-8") 147 | 148 | ##x.endswith() 149 | x.endswith(".") 150 | x.endswith("i") 151 | 152 | ##x.expandtabs() 153 | x = "b\t\t\tb" 154 | print(x) 155 | print(x.expandtabs()) 156 | print(x.expandtabs(0)) 157 | print(x.expandtabs(1)) 158 | print(x.expandtabs(4)) 159 | 160 | xx="a\ta" 161 | 162 | ##x.find() 163 | #index 164 | x = "This is a book." 165 | x.find("T") 166 | #x.index() 167 | x.index("T") 168 | 169 | #x.isalnum() #Check number or character in vector 170 | x = "66+88" 171 | x1 = "book1234" 172 | x2 = "This is a book." 173 | x.isalnum() 174 | x1.isalnum() 175 | x2.isalnum() 176 | 177 | #a.isalpha() #Check character in vector 178 | x = "66+88" 179 | x1 = "book1234" 180 | x2 = "This is a book." 181 | x3 = "Hi" 182 | x.isalpha() 183 | x1.isalpha() 184 | x2.isalpha() 185 | x3.isalpha() 186 | 187 | #x.isdecimal() #Check Unicode number in vector 188 | x = "66+88" 189 | x1 = "1234567" 190 | x.isdecimal() 191 | x1.isdecimal() 192 | 193 | #x.isdigit() #Check ASCII number in vector 194 | x = "0sss566" 195 | x1 = "0o127" 196 | x2 = "6523" 197 | x.isdigit() 198 | x1.isdigit() 199 | x2.isdigit() 200 | 201 | #x.isidentifier() #Check keywords 202 | x = "if" 203 | x1 = "h appy" 204 | x2 = "h_appy" 205 | x.isidentifier() 206 | x1.isidentifier() 207 | x2.isidentifier() 208 | 209 | #x.islower() #Return small letter 210 | x1 = "Happy" 211 | x2 = "happy" 212 | x1.islower() 213 | x2.islower() -------------------------------------------------------------------------------- /2015/Chapter2/Chapter2_2_data_structures.py: -------------------------------------------------------------------------------- 1 | x = [22, 56.3, 23, 1, 34.5] 2 | ##x.count() 3 | #Return the number of times x appears in the list. 4 | print(x.count(22), x.count(56.3), x.count('x')) 5 | 6 | #Insert an item at a given position. 7 | x.insert(3, -1) 8 | 9 | #Add an item to the end of the list. 10 | x.append(222) 11 | 12 | #Remove the first item from the list whose value is x 13 | x.remove(222) 14 | 15 | #Reverse the elements of the list in place. 16 | x.reverse() 17 | 18 | #Sort the items of the list in place. 19 | x.sort() 20 | 21 | #Remove the item at the given position in the list, and return it. 22 | x.pop() 23 | x.pop(2) 24 | 25 | ###List Comprehensions 26 | #List comprehensions provide a concise way to create lists. 27 | 28 | squares = [] 29 | for x in range(10): 30 | squares.append(x**2) 31 | #squares.append(pow(x,2)) 32 | 33 | #which is more concise and readable. 34 | squares = [x**2 for x in range(10)] 35 | 36 | #R language 37 | #squares<-(0:9)^2 38 | 39 | #A list comprehension consists of brackets containing 40 | #an expression followed by a for clause, then zero or more for or if clauses. 41 | # 42 | xx =[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y] 43 | #[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)] 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /2015/Chapter3/CSV/co2-sample.csv: -------------------------------------------------------------------------------- 1 | "COUNTRY","2000","2001",2002,2003,2004 2 | "ANTIGUA AND BARBUDA",0,0,0,0,0 3 | "ARGENTINA",37,35,33,36,39 4 | "BAHAMAS, THE",1,1,1,1,1 5 | "BAHRAIN",5,6,6,6,6 6 | -------------------------------------------------------------------------------- /2015/Chapter3/CSV/csv2html.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | 4 | def main(): 5 | maxwidth = 100 6 | print_start() 7 | count = 0 8 | while True: 9 | try: 10 | line = input() 11 | if count == 0: 12 | color = "lightgreen" 13 | elif count % 2: 14 | color = "white" 15 | else: 16 | color = "lightyellow" 17 | print_line(line, color, maxwidth) 18 | count += 1 19 | except EOFError: 20 | break 21 | print_end() 22 | 23 | 24 | def print_start(): 25 | print("") 26 | 27 | 28 | def print_line(line, color, maxwidth): 29 | print("".format(color)) 30 | fields = extract_fields(line) 31 | for field in fields: 32 | if not field: 33 | print("") 34 | else: 35 | number = field.replace(",", "") 36 | try: 37 | x = float(number) 38 | print("".format(round(x))) 39 | except ValueError: 40 | field = field.title() 41 | field = field.replace(" And ", " and ") 42 | if len(field) <= maxwidth: 43 | field = escape_html(field) 44 | else: 45 | field = "{0} ...".format( 46 | escape_html(field[:maxwidth])) 47 | print("".format(field)) 48 | print("") 49 | 50 | 51 | def extract_fields(line): 52 | fields = [] 53 | field = "" 54 | quote = None 55 | for c in line: 56 | if c in "\"'": 57 | if quote is None: # start of quoted string 58 | quote = c 59 | elif quote == c: # end of quoted string 60 | quote = None 61 | else: 62 | field += c # other quote inside quoted string 63 | continue 64 | if quote is None and c == ",": # end of a field 65 | fields.append(field) 66 | field = "" 67 | else: 68 | field += c # accumulating a field 69 | if field: 70 | fields.append(field) # adding the last field 71 | return fields 72 | 73 | 74 | def escape_html(text): 75 | text = text.replace("&", "&") 76 | text = text.replace("<", "<") 77 | text = text.replace(">", ">") 78 | return text 79 | 80 | 81 | def print_end(): 82 | print("
{0:d}{0}
") 83 | 84 | 85 | main() 86 | -------------------------------------------------------------------------------- /2015/Chapter3/CSV/csv2html1_ans.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # Copyright (c) 2008-11 Qtrac Ltd. All rights reserved. 3 | # This program or module is free software: you can redistribute it and/or 4 | # modify it under the terms of the GNU General Public License as published 5 | # by the Free Software Foundation, either version 3 of the License, or 6 | # (at your option) any later version. It is provided for educational 7 | # purposes and is distributed in the hope that it will be useful, but 8 | # WITHOUT ANY WARRANTY; without even the implied warranty of 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 | # General Public License for more details. 11 | 12 | import sys 13 | import xml.sax.saxutils 14 | 15 | 16 | def main(): 17 | maxwidth = 100 18 | print_start() 19 | count = 0 20 | while True: 21 | try: 22 | line = input() 23 | if count == 0: 24 | color = "lightgreen" 25 | elif count % 2: 26 | color = "white" 27 | else: 28 | color = "lightyellow" 29 | print_line(line, color, maxwidth) 30 | count += 1 31 | except EOFError: 32 | break 33 | print_end() 34 | 35 | 36 | def print_start(): 37 | print("") 38 | 39 | 40 | def print_line(line, color, maxwidth): 41 | print("".format(color)) 42 | fields = extract_fields(line) 43 | for field in fields: 44 | if not field: 45 | print("") 46 | else: 47 | number = field.replace(",", "") 48 | try: 49 | x = float(number) 50 | print("".format(round(x))) 51 | except ValueError: 52 | field = field.title() 53 | field = field.replace(" And ", " and ") 54 | if len(field) <= maxwidth: 55 | field = xml.sax.saxutils.escape(field) 56 | else: 57 | field = "{0} ...".format( 58 | xml.sax.saxutils.escape(field[:maxwidth])) 59 | print("".format(field)) 60 | print("") 61 | 62 | 63 | def extract_fields(line): 64 | fields = [] 65 | field = "" 66 | quote = None 67 | for c in line: 68 | if c in "\"'": 69 | if quote is None: # start of quoted string 70 | quote = c 71 | elif quote == c: # end of quoted string 72 | quote = None 73 | else: 74 | field += c # other quote inside quoted string 75 | continue 76 | if quote is None and c == ",": # end of a field 77 | fields.append(field) 78 | field = "" 79 | else: 80 | field += c # accumulating a field 81 | if field: 82 | fields.append(field) # adding the last field 83 | return fields 84 | 85 | 86 | def print_end(): 87 | print("
{0:d}{0}
") 88 | 89 | 90 | main() 91 | 92 | -------------------------------------------------------------------------------- /2015/Chapter3/Chapter3.py: -------------------------------------------------------------------------------- 1 | #Title:Python chapter 3 2 | #Date:2014/12/01 3 | #Author:Jerry Wu 4 | #E-mail:jerry@mail.ntust.edu.tw 5 | 6 | #####Tuple 7 | 8 | hair = "black","brown","blonde","red" 9 | type(hair) 10 | 11 | 12 | type(hair) 13 | 14 | hair[2] 15 | hair[-3] 16 | 17 | x = hair[:2],"gray",hair[2:] 18 | x[0] 19 | x[0][1] 20 | 21 | things = (1,-7.5,("pea",(5,"Xyz"),"queue")) 22 | things[2][1][1][2] 23 | 24 | manufacturer, model, seating = (0,1,2) 25 | min,max=(0,1) 26 | aircraft = ("airbus","a320-200",(100,220)) 27 | aircraft[seating][max] 28 | 29 | import math 30 | 31 | for x,y in ((-3,-4),(5,12),(28,-45)): 32 | print(math.hypot(x,y)) 33 | 34 | x = (-3**2)+(-4**2) 35 | math.sqrt(abs(x)) 36 | 37 | #####List 38 | 39 | hair = ["black","brown","blonde","red"] 40 | type(hair) 41 | 42 | list() 43 | 44 | L = [-17.5,"kilo",49,"V",["ram",5,"echo"],7] 45 | L[0]==L[-6]==-17.5 46 | L[1]==L[-5] 47 | 48 | 49 | first, *rest = [9,2,-4,8,7] 50 | first, rest 51 | 52 | #####List Comprehension 53 | 54 | leaps=[] 55 | for year in range(1900,1940): 56 | if(year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): 57 | leaps.append(year) 58 | 59 | 60 | leaps = [y for y in range(1900,1940) if(y % 4 == 0 and y % 100 != 0) or (y % 400 == 0)] 61 | 62 | 63 | 64 | #####Set 65 | S = {7,"veil",0,-29,("x",11),"sun",frozenset({8,4,7}),913} 66 | Type(S) 67 | S[0] 68 | a = list(S) 69 | 70 | set("pecan") | set("pie") == {'p','e','c','a','n','i'} 71 | set("pecan") & set("pie") == {'p','e'} 72 | set("pecan") - set("pie") == {'c','a','n'} 73 | #set("pie") - set("pecan") == {"i"} 74 | set("pecan") ^ set("pie") == {'c','a','n','i'} 75 | 76 | 77 | 0 in S 78 | 0 not in S 79 | ## 80 | S.add(20) 81 | ## 82 | S.clear() 83 | 84 | ## 85 | a = {2} 86 | S.difference(a) 87 | ## 88 | S.difference_update(a) 89 | ## 90 | S.discard(-29) 91 | ## 92 | S = {7,"veil",0,-29,("x",11),"sun",frozenset({8,4,7}),913} 93 | a = {913} 94 | S.intersection(a) 95 | ## 96 | S.intersection_update(a) 97 | ## 98 | S = {7,"veil",0,-29,("x",11),"sun",frozenset({8,4,7}),913} 99 | a = {27} 100 | S.isdisjoint(a) 101 | ## 102 | b = {20,21,30} 103 | c = {20,21} 104 | b.issubset(c) 105 | c.issubset(b) 106 | ## 107 | c.issuperset(b) 108 | b.issuperset(c) 109 | ## 110 | b.pop() 111 | ## 112 | S.remove(-29) 113 | ## 114 | b = {20,21,30} 115 | c = {20,21} 116 | b.symmetric_difference(c) 117 | ## 118 | b = {20,21,30} 119 | c = {20,21} 120 | b.symmetric_difference_update(c) 121 | ## 122 | b = {20,21,30} 123 | c = {20,21,34,88} 124 | b.union(c) 125 | ## 126 | b.update(c) 127 | 128 | 129 | #####dict 130 | d1 = dict({"id":1948,"name":"washer","size":3}) 131 | d2 = dict(id=1948,name="washer",size=3) 132 | d3 = dict([("id",1948),("name","Washer"),("size",3)]) 133 | d4 = dict(zip(("id","name","size"),(1948,"Washer",3))) 134 | d5 = {"id":1948,"name":"Washer","size":3} 135 | 136 | d1["id"] 137 | d1["size"]=10 138 | 139 | #items 140 | for item in d1.items(): 141 | print(item[0],item[1]) 142 | 143 | #keys 144 | for key in d1: 145 | print(key) 146 | 147 | #values 148 | for value in d1.values(): 149 | print(value) 150 | 151 | ## 152 | d1.clear() 153 | ## 154 | d2.copy() 155 | ## 156 | d1 = dict.fromkeys("12345") 157 | d1 = dict.fromkeys("12345",20) 158 | ## 159 | d2.get("name") 160 | ## 161 | d2.items() 162 | ## 163 | d2.keys() 164 | ## 165 | d2.values() 166 | ## 167 | d2.pop("name") 168 | ## 169 | d3.popitem() 170 | ## 171 | d3.update(dict(ss=20)) 172 | 173 | 174 | #Tuple A = () 有順序、可重複、迭代只供讀取不能變動 175 | #List B = ([]) 有順序、可重複、迭代允許改變 176 | #set C = set() 沒有順序、不可重複、迭代不允許改變 177 | #dict S = {} 沒有順序、key不能重複 178 | 179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /2015/Chapter3/Generate_grid/generate_grid.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | def get_int(msg, minimum, default): 5 | while True: 6 | try: 7 | line = input(msg) 8 | if not line and default is not None: 9 | return default 10 | i = int(line) 11 | if i < minimum: #1,1,-1000000 12 | print("must be >=", minimum) 13 | else: 14 | return i 15 | except ValueError as err: 16 | print(err) 17 | 18 | 19 | rows = get_int("rows: ", 1, None) 20 | columns = get_int("columns: ", 1, None) 21 | minimum = get_int("minimum (or Enter for 0): ", -1000000, 0) 22 | 23 | default = 1000 24 | if default < minimum: 25 | default = 2 * minimum 26 | maximum = get_int("maximum (or Enter for " + str(default) + "): ", 27 | minimum, default) 28 | 29 | row = 0 30 | while row < rows: 31 | line = "" 32 | column = 0 33 | while column < columns: 34 | i = random.randint(minimum, maximum) 35 | s = str(i) 36 | while len(s) < 10: 37 | s = " " + s 38 | line += s 39 | column += 1 40 | print(line) 41 | row += 1 42 | -------------------------------------------------------------------------------- /2015/Chapter3/Generate_usernames/generate_usernames.py: -------------------------------------------------------------------------------- 1 | import collections 2 | import sys 3 | 4 | 5 | ID, FORENAME, MIDDLENAME, SURNAME, DEPARTMENT = range(5) 6 | 7 | User = collections.namedtuple("User", 8 | "username forename middlename surname id") 9 | 10 | 11 | def main(): 12 | if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}: 13 | print("usage: {0} file1 [file2 [... fileN]]".format( 14 | sys.argv[0])) 15 | sys.exit() 16 | 17 | usernames = set() 18 | users = {} 19 | for filename in sys.argv[1:]: 20 | for line in open(filename, encoding="utf8"): 21 | line = line.rstrip() 22 | if line: 23 | user = process_line(line, usernames) 24 | users[(user.surname.lower(), user.forename.lower(), 25 | user.id)] = user 26 | print_users(users) 27 | 28 | 29 | def process_line(line, usernames): 30 | fields = line.split(":") 31 | username = generate_username(fields, usernames) 32 | user = User(username, fields[FORENAME], fields[MIDDLENAME], 33 | fields[SURNAME], fields[ID]) 34 | return user 35 | 36 | 37 | def generate_username(fields, usernames): 38 | username = ((fields[FORENAME][0] + fields[MIDDLENAME][:1] + 39 | fields[SURNAME]).replace("-", "").replace("'", "")) 40 | username = original_name = username[:8].lower() 41 | count = 1 42 | while username in usernames: 43 | username = "{0}{1}".format(original_name, count) 44 | count += 1 45 | usernames.add(username) 46 | return username 47 | 48 | 49 | def print_users(users): 50 | namewidth = 32 51 | usernamewidth = 9 52 | 53 | print("{0:<{nw}} {1:^6} {2:{uw}}".format( 54 | "Name", "ID", "Username", nw=namewidth, uw=usernamewidth)) 55 | print("{0:-<{nw}} {0:-<6} {0:-<{uw}}".format( 56 | "", nw=namewidth, uw=usernamewidth)) 57 | 58 | for key in sorted(users): 59 | user = users[key] 60 | initial = "" 61 | if user.middlename: 62 | initial = " " + user.middlename[0] 63 | name = "{0.surname}, {0.forename}{1}".format(user, initial) 64 | print("{0:.<{nw}} ({1.id:4}) {1.username:{uw}}".format( 65 | name, user, nw=namewidth, uw=usernamewidth)) 66 | 67 | 68 | main() -------------------------------------------------------------------------------- /2015/Chapter3/Generate_usernames/generate_usernames_ans.py: -------------------------------------------------------------------------------- 1 | import collections 2 | import sys 3 | 4 | 5 | ID, FORENAME, MIDDLENAME, SURNAME, DEPARTMENT = range(5) 6 | 7 | User = collections.namedtuple("User", 8 | "username forename middlename surname id") 9 | 10 | 11 | def main(): 12 | if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}: 13 | print("usage: {0} file1 [file2 [... fileN]]".format( 14 | sys.argv[0])) 15 | sys.exit() 16 | 17 | usernames = set() 18 | users = {} 19 | for filename in sys.argv[1:]: 20 | for line in open(filename, encoding="utf8"): 21 | line = line.rstrip() 22 | if line: 23 | user = process_line(line, usernames) 24 | users[(user.surname.lower(), user.forename.lower(), 25 | user.id)] = user 26 | print_users(users) 27 | 28 | 29 | def process_line(line, usernames): 30 | fields = line.split(":") 31 | username = generate_username(fields, usernames) 32 | user = User(username, fields[FORENAME], fields[MIDDLENAME], 33 | fields[SURNAME], fields[ID]) 34 | return user 35 | 36 | 37 | def generate_username(fields, usernames): 38 | username = ((fields[FORENAME][0] + fields[MIDDLENAME][:1] + 39 | fields[SURNAME]).replace("-", "").replace("'", "")) 40 | username = original_name = username[:8].lower() 41 | count = 1 42 | while username in usernames: 43 | username = "{0}{1}".format(original_name, count) 44 | count += 1 45 | usernames.add(username) 46 | return username 47 | 48 | 49 | def by_surname_forename(user): 50 | return user.surname.lower(), user.forename.lower(), user.id 51 | 52 | 53 | def print_users(users): 54 | namewidth = 17 55 | usernamewidth = 9 56 | columngap = " " * 2 57 | 58 | headline1 = "{0:<{nw}} {1:^6} {2:{uw}}".format("Name", "ID", 59 | "Username", nw=namewidth, uw=usernamewidth) 60 | headline2 = "{0:-<{nw}} {0:-<6} {0:-<{uw}}".format("", 61 | nw=namewidth, uw=usernamewidth) 62 | header = (headline1 + columngap + headline1 + "\n" + 63 | headline2 + columngap + headline2) 64 | 65 | lines = [] 66 | for key in sorted(users): 67 | user = users[key] 68 | initial = "" 69 | if user.middlename: 70 | initial = " " + user.middlename[0] 71 | name = "{0.surname}, {0.forename}{1}".format(user, initial) 72 | lines.append("{0:.<{nw}.{nw}} ({1.id:4}) " 73 | "{1.username:{uw}}".format(name, user, 74 | nw=namewidth, uw=usernamewidth)) 75 | 76 | lines_per_page = 64 77 | lino = 0 78 | for left, right in zip(lines[::2], lines[1::2]): 79 | if lino == 0: 80 | print(header) 81 | print(left + columngap + right) 82 | lino += 1 83 | if lino == lines_per_page: 84 | print("\f") 85 | lino = 0 86 | if lines[-1] != right: 87 | print(lines[-1]) 88 | 89 | 90 | main() -------------------------------------------------------------------------------- /2016/Chapter1/Chapter1_Homework.ipynb: -------------------------------------------------------------------------------- 1 | {"metadata": {"language_info": {"pygments_lexer": "ipython3", "name": "python", "nbconvert_exporter": "python", "version": "3.4.3", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "file_extension": ".py"}, "kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}}, "cells": [{"outputs": [{"metadata": {}, "execution_count": 1, "data": {"text/plain": "72"}, "output_type": "execute_result"}], "metadata": {"trusted": true, "collapsed": false}, "execution_count": 1, "source": "8 * 9", "cell_type": "code"}, {"outputs": [{"metadata": {}, "execution_count": 2, "data": {"text/plain": "47"}, "output_type": "execute_result"}], "metadata": {"trusted": true, "collapsed": false}, "execution_count": 2, "source": "47", "cell_type": "code"}, {"outputs": [{"text": "47\n", "name": "stdout", "output_type": "stream"}], "metadata": {"trusted": true, "collapsed": false}, "execution_count": 3, "source": "print(47)", "cell_type": "code"}, {"outputs": [], "metadata": {"trusted": true, "collapsed": true}, "execution_count": null, "source": "", "cell_type": "code"}], "nbformat_minor": 0, "nbformat": 4} -------------------------------------------------------------------------------- /2016/Chapter1/chapter1.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[1]: 5 | 6 | for countdown in 5, 4, 3, 2, 1, "Hello World!": 7 | print(countdown) 8 | 9 | 10 | # In[6]: 11 | 12 | News = [ 13 | "Yahoo新聞", 14 | "蘋果新聞", 15 | "UDN新聞" 16 | ] 17 | print(News[2]) 18 | 19 | 20 | # In[7]: 21 | 22 | joke = { 23 | "學生": "老師我拉肚子了", 24 | "老師": "你就不能說的文雅點嗎?", 25 | "學生": "老師我菊部有降雨", 26 | "老師": "......", 27 | } 28 | x = "學生" 29 | print(x, "say:", joke[x]) 30 | 31 | 32 | # In[22]: 33 | 34 | import requests 35 | url = "http://data.taipei/opendata/datalist/apiAccess?scope=resourceAquire&rid=540c0930-f25b-429c-9adf-128225dfe4f4" 36 | response = requests.get(url) 37 | data = response.json() 38 | print(data) 39 | 40 | 41 | # In[23]: 42 | 43 | import this 44 | 45 | 46 | # In[25]: 47 | 48 | 8*9 49 | 50 | 51 | # In[26]: 52 | 53 | 47 54 | 55 | 56 | # In[27]: 57 | 58 | print(47) 59 | 60 | 61 | # In[ ]: 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /2016/Chapter3/Chapter3.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | # In[17]: 5 | 6 | #List 7 | empty_list = [ ] 8 | weekdays = ['星期一', '星期二', '星期三', '星期四', '星期五'] 9 | type(empty_list) 10 | 11 | 12 | # In[16]: 13 | 14 | type(weekdays) 15 | 16 | 17 | # In[18]: 18 | 19 | #Tuple 20 | roles = ('甘道夫', '亞拉岡', '索倫', '勒苟拉斯', '亞玟') 21 | type(roles) 22 | 23 | 24 | # In[21]: 25 | 26 | weekdays[0] = '星期一猴子穿新衣' 27 | weekdays 28 | 29 | 30 | # In[1]: 31 | 32 | roles[0] = '甘道夫是魔法師' 33 | roles 34 | 35 | 36 | # In[2]: 37 | 38 | list('中文') 39 | 40 | 41 | # In[8]: 42 | 43 | a_tuple = ("一", "二", "三") 44 | list(a_tuple) 45 | 46 | 47 | # In[14]: 48 | 49 | birthday = "1/6/2015" 50 | birthday.split('/') 51 | 52 | 53 | # In[16]: 54 | 55 | birthday = "a/b//c/d//e" 56 | birthday.split('//') 57 | 58 | 59 | # In[17]: 60 | 61 | a = ['中國','美國','英國'] 62 | a[0] 63 | 64 | 65 | # In[20]: 66 | 67 | a[-1] 68 | 69 | 70 | # In[23]: 71 | 72 | a = [1, "jerry", 2, "吳老師"] 73 | a[0] 74 | 75 | 76 | # In[49]: 77 | 78 | a = [[[1, "jerry", 2, "吳老師"], "台灣科大"], "台灣"] 79 | a[0][0][1] 80 | 81 | 82 | # In[33]: 83 | 84 | a = [1, "jerry", 2, "吳老師"] 85 | a[0] 86 | 87 | 88 | # In[36]: 89 | 90 | a[0] = 's' 91 | a 92 | 93 | 94 | # In[57]: 95 | 96 | a.append("3") 97 | a 98 | 99 | 100 | # In[61]: 101 | 102 | areas = ["大安區", "士林區", "文山區"] 103 | names = [1, "jerry", 2, "吳老師"] 104 | areas.extend(names) 105 | areas 106 | 107 | 108 | # In[63]: 109 | 110 | areas = ["大安區", "士林區", "文山區"] 111 | names = [1, "jerry", 2, "吳老師"] 112 | areas += names 113 | areas 114 | 115 | 116 | # In[65]: 117 | 118 | areas = ["大安區", "士林區", "文山區"] 119 | names = [1, "jerry", 2, "吳老師"] 120 | areas.append(names) 121 | areas 122 | 123 | 124 | # In[77]: 125 | 126 | areas = ["大安區", "士林區", "文山區"] 127 | areas.insert(2, "內湖區") 128 | areas 129 | 130 | 131 | # In[78]: 132 | 133 | del areas[-2] 134 | areas 135 | 136 | 137 | # In[85]: 138 | 139 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 140 | areas.remove("大安區") 141 | areas 142 | 143 | 144 | # In[94]: 145 | 146 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 147 | areas.pop(1) 148 | areas 149 | 150 | 151 | # In[96]: 152 | 153 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 154 | areas.index("信義區") 155 | 156 | 157 | # In[99]: 158 | 159 | "新店區" in areas 160 | 161 | 162 | # In[100]: 163 | 164 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區", "信義區"] 165 | areas.count('信義區') 166 | 167 | 168 | # In[110]: 169 | 170 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 171 | new_areas = ','.join(areas) 172 | type(new_areas) 173 | 174 | 175 | # In[115]: 176 | 177 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 178 | new_areas = ','.join(areas) 179 | type(areas) 180 | 181 | 182 | # In[129]: 183 | 184 | areas = ["大安區", "士林區", "文山區", "大同區", "信義區"] 185 | separator = "*" 186 | joined = separator.join(areas) 187 | joined 188 | 189 | 190 | # In[130]: 191 | 192 | separatored = joined.split(separator) 193 | separatored 194 | 195 | 196 | # In[131]: 197 | 198 | areas 199 | 200 | 201 | # In[132]: 202 | 203 | separatored == areas 204 | 205 | 206 | # In[134]: 207 | 208 | number = [5,3,42,35,99] 209 | sorted_number = sorted(number) 210 | sorted_number 211 | 212 | 213 | # In[135]: 214 | 215 | number 216 | 217 | 218 | # In[139]: 219 | 220 | number = [5,3,42,35,99] 221 | sorted(number) 222 | 223 | 224 | # In[140]: 225 | 226 | number = [5,3,42,35,99] 227 | number.sort() 228 | number 229 | 230 | 231 | # In[145]: 232 | 233 | number = [5,3,42,35,99] 234 | number.sort(reverse=True) 235 | number 236 | 237 | 238 | # In[146]: 239 | 240 | number = [5,3,42,35,99] 241 | len(number) 242 | 243 | 244 | # In[148]: 245 | 246 | a = [1,2,3] 247 | a 248 | 249 | 250 | # In[149]: 251 | 252 | b = a 253 | b 254 | 255 | 256 | # In[151]: 257 | 258 | a[0] = 'new' 259 | a 260 | 261 | 262 | # In[152]: 263 | 264 | b 265 | 266 | 267 | # In[155]: 268 | 269 | a = ["神奇", "的", "list", "功能"] 270 | b = a 271 | c = b 272 | 273 | 274 | # In[156]: 275 | 276 | b 277 | 278 | 279 | # In[157]: 280 | 281 | c 282 | 283 | 284 | # In[158]: 285 | 286 | a[0] = "有夠神奇" 287 | 288 | 289 | # In[159]: 290 | 291 | a 292 | 293 | 294 | # In[160]: 295 | 296 | b 297 | 298 | 299 | # In[162]: 300 | 301 | c 302 | 303 | 304 | # In[164]: 305 | 306 | a = ["試試", "神奇", "list的copy", "功能"] 307 | a 308 | 309 | 310 | # In[165]: 311 | 312 | b = a.copy() 313 | 314 | 315 | # In[166]: 316 | 317 | b 318 | 319 | 320 | # In[167]: 321 | 322 | c = list(a) 323 | 324 | 325 | # In[168]: 326 | 327 | c 328 | 329 | 330 | # In[171]: 331 | 332 | d = a[:] 333 | d 334 | 335 | 336 | # In[175]: 337 | 338 | a[0] = "Try" 339 | a 340 | 341 | 342 | # In[176]: 343 | 344 | b 345 | 346 | 347 | # In[177]: 348 | 349 | b = a.copy() 350 | b 351 | 352 | 353 | # In[1]: 354 | 355 | #Tuple 356 | empty_tuple = () 357 | empty_tuple 358 | 359 | 360 | # In[2]: 361 | 362 | roles = ('甘道夫', '亞拉岡', '索倫', '勒苟拉斯', '亞玟') 363 | type(roles) 364 | roles[0] = '甘道夫是魔法師' 365 | roles 366 | 367 | 368 | # In[7]: 369 | 370 | roles = ('甘道夫', '亞拉岡', '索倫', '勒苟拉斯', '亞玟') 371 | a,b,c,d,e = roles 372 | a 373 | 374 | 375 | # In[8]: 376 | 377 | password = "sdlkpekcp" 378 | test_password = "aaabbb" 379 | password, test_password = test_password, password 380 | password 381 | 382 | 383 | # In[9]: 384 | 385 | test_password 386 | 387 | 388 | # In[11]: 389 | 390 | roles = ['甘道夫', '亞拉岡', '索倫', '勒苟拉斯', '亞玟'] 391 | roles = tuple(roles) 392 | type(roles) 393 | 394 | 395 | # In[14]: 396 | 397 | #dictionary 398 | empty_dict = {} 399 | empty_dict 400 | 401 | 402 | # In[17]: 403 | 404 | class_ntust = { 405 | "number" : "1059999", 406 | "name" : "jerry" 407 | } 408 | class_ntust 409 | 410 | 411 | # In[19]: 412 | 413 | school = [["班級","甲班"],["姓名","甘道夫"],["學號","110999"]] 414 | dict(school) 415 | 416 | 417 | # In[20]: 418 | 419 | school = [("班級","甲班"),("姓名","甘道夫"),("學號","110999")] 420 | dict(school) 421 | 422 | 423 | # In[23]: 424 | 425 | school = ["中文","cd","ef"] 426 | dict(school) 427 | 428 | 429 | # In[36]: 430 | 431 | class_ntust = { 432 | "member1" : "甘道夫", 433 | "member2" : "亞拉岡", 434 | "member3" : "索倫", 435 | } 436 | class_ntust 437 | 438 | 439 | # In[29]: 440 | 441 | class_ntust["member1"] = "亞玟" 442 | class_ntust 443 | 444 | 445 | # In[30]: 446 | 447 | class_ntust["member1"] = "甘道夫" 448 | class_ntust 449 | 450 | 451 | # In[53]: 452 | 453 | class_ntust = { 454 | "member1" : "甘道夫", 455 | "member2" : "亞拉岡", 456 | "member3" : "索倫", 457 | } 458 | 459 | 460 | class_ntust2 = { 461 | "member4" : "金鋼狼", 462 | "member5" : "暴風女", 463 | "member6" : "X教授", 464 | } 465 | class_ntust 466 | 467 | 468 | 469 | 470 | # In[38]: 471 | 472 | class_ntust2 473 | 474 | 475 | # In[54]: 476 | 477 | class_ntust.update(class_ntust2) 478 | class_ntust 479 | 480 | 481 | # In[44]: 482 | 483 | first = {"a":1, "b":2} 484 | second = {"b":"cat"} 485 | first.update(second) 486 | first 487 | 488 | 489 | # In[55]: 490 | 491 | del class_ntust["member1"] 492 | class_ntust 493 | 494 | 495 | # In[57]: 496 | 497 | class_ntust.clear() 498 | class_ntust 499 | 500 | 501 | # In[61]: 502 | 503 | class_ntust = { 504 | "member1" : "甘道夫", 505 | "member2" : "亞拉岡", 506 | "member3" : "索倫", 507 | } 508 | "member1" in class_ntust 509 | 510 | 511 | # In[63]: 512 | 513 | class_ntust["member1"] 514 | 515 | 516 | # In[64]: 517 | 518 | class_ntust["member11"] 519 | 520 | 521 | # In[65]: 522 | 523 | "member1" in class_ntust 524 | 525 | 526 | # In[66]: 527 | 528 | "member11" in class_ntust 529 | 530 | 531 | # In[67]: 532 | 533 | class_ntust.get("member1") 534 | 535 | 536 | # In[72]: 537 | 538 | class_ntust.get("member11", "號碼錯誤哦!") 539 | 540 | 541 | # In[76]: 542 | 543 | class_ntust.keys() 544 | 545 | 546 | # In[77]: 547 | 548 | class_ntust.values() 549 | 550 | 551 | # In[78]: 552 | 553 | class_ntust.items() 554 | 555 | 556 | # In[79]: 557 | 558 | class_ntust 559 | 560 | 561 | # In[81]: 562 | 563 | class_ntust_new = class_ntust 564 | class_ntust_new 565 | 566 | 567 | # In[85]: 568 | 569 | class_ntust["member1"] = "金庸" 570 | class_ntust 571 | 572 | 573 | # In[86]: 574 | 575 | class_ntust_new 576 | 577 | 578 | # In[88]: 579 | 580 | class_ntust = { 581 | "member1" : "甘道夫", 582 | "member2" : "亞拉岡", 583 | "member3" : "索倫", 584 | } 585 | class_ntust 586 | 587 | 588 | # In[90]: 589 | 590 | class_ntust_new = class_ntust.copy() 591 | class_ntust_new 592 | 593 | 594 | # In[91]: 595 | 596 | class_ntust["member1"] = "笑傲江湖" 597 | class_ntust 598 | 599 | 600 | # In[92]: 601 | 602 | class_ntust_new 603 | 604 | 605 | # In[93]: 606 | 607 | #set 608 | empty_set = set() 609 | empty_set 610 | 611 | 612 | # In[94]: 613 | 614 | even_numbers = {0,2,3,4,5} 615 | even_numbers 616 | 617 | 618 | # In[95]: 619 | 620 | #tuple , 621 | #list [] 622 | #dictionary {:} 623 | #set set() or {} 624 | 625 | 626 | # In[97]: 627 | 628 | set("中文試試") 629 | 630 | 631 | # In[98]: 632 | 633 | set(["甘道夫","亞拉岡","亞玟","咕嚕","索倫"]) 634 | 635 | 636 | # In[99]: 637 | 638 | set(("甘道夫","亞拉岡","亞玟","咕嚕","索倫")) 639 | 640 | 641 | # In[101]: 642 | 643 | set({"member":"甘道夫","member2":"亞拉岡"}) 644 | 645 | 646 | # In[103]: 647 | 648 | drinks = { 649 | "item1" : {"珍珠奶茶","紅茶"}, 650 | "item2" : {"烏龍奶茶","烏龍茶"}, 651 | "item3" : {"蜂蜜紅茶","紅茶"}, 652 | } 653 | 654 | 655 | # In[111]: 656 | 657 | for name, contents in drinks.items(): 658 | if "紅茶" in contents: 659 | print(name) 660 | 661 | 662 | # In[114]: 663 | 664 | for name, contents in drinks.items(): 665 | if "紅茶" in contents and not ("珍珠奶茶" in contents): 666 | print(name) 667 | 668 | 669 | # In[117]: 670 | 671 | for name, contents in drinks.items(): 672 | if contents & {"紅茶"} : 673 | print(name) 674 | 675 | 676 | # In[119]: 677 | 678 | for name, contents in drinks.items(): 679 | if "紅茶" in contents and not contents & {"蜂蜜紅茶"} : 680 | print(name) 681 | 682 | 683 | # In[123]: 684 | 685 | a = {1,2} 686 | b = {2,3} 687 | type(a) 688 | 689 | 690 | # In[124]: 691 | 692 | a&b 693 | 694 | 695 | # In[125]: 696 | 697 | a.intersection(b) 698 | 699 | 700 | # In[126]: 701 | 702 | a|b 703 | 704 | 705 | # In[127]: 706 | 707 | a.union(b) 708 | 709 | 710 | # In[133]: 711 | 712 | a - b 713 | 714 | 715 | # In[129]: 716 | 717 | a^b 718 | 719 | 720 | # In[131]: 721 | 722 | b^a 723 | 724 | 725 | # In[138]: 726 | 727 | rings_list = ["甘道夫","亞拉岡","亞玟","咕嚕","索倫"] 728 | type(rings_list) 729 | 730 | 731 | # In[145]: 732 | 733 | rings_tuple = "甘道夫","亞拉岡","亞玟","咕嚕","索倫" 734 | type(rings_tuple) 735 | 736 | 737 | # In[141]: 738 | 739 | rings_dict = {"member1":"甘道夫","member2":"咕嚕"} 740 | type(rings_dict) 741 | 742 | 743 | # In[152]: 744 | 745 | rings_set = {"甘道夫","亞拉岡","亞玟","咕嚕","索倫"} 746 | type(rings_set) 747 | 748 | 749 | # In[143]: 750 | 751 | rings_list[2] 752 | 753 | 754 | # In[146]: 755 | 756 | rings_tuple[2] 757 | 758 | 759 | # In[150]: 760 | 761 | rings_dict["member1"] 762 | 763 | 764 | # In[155]: 765 | 766 | "甘道夫" in rings_set 767 | 768 | 769 | # In[157]: 770 | 771 | rings = ["甘道夫","亞拉岡","亞玟","咕嚕","索倫"] 772 | potter = ["哈利波特","妙麗","榮恩","鄧不利多"] 773 | tuple_of_lists = rings, potter 774 | tuple_of_lists 775 | 776 | 777 | # In[158]: 778 | 779 | list_of_lists = [rings, potter] 780 | list_of_lists 781 | 782 | 783 | # In[161]: 784 | 785 | dict_of_lists = {"book1":potter,"book2":rings} 786 | dict_of_lists 787 | 788 | 789 | # In[ ]: 790 | 791 | 792 | 793 | -------------------------------------------------------------------------------- /2016/Chapter3/Chapter3_homework.ipynb: -------------------------------------------------------------------------------- 1 | {"nbformat": 4, "cells": [{"source": "#3.1\nyears_list = [1980,1981,1982,1983,1984,1985]", "cell_type": "code", "outputs": [], "metadata": {"collapsed": true, "trusted": true}, "execution_count": 1}, {"source": "#3.2\nyears_list[3]", "cell_type": "code", "outputs": [{"execution_count": 2, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "1983"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 2}, {"source": "#3.3\nyears_list[-1]", "cell_type": "code", "outputs": [{"execution_count": 3, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "1985"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 3}, {"source": "#3.4\nthings = [\"mozzarella\", \"cinderella\", \"salmonella\"]\nthings", "cell_type": "code", "outputs": [{"execution_count": 5, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['mozzarella', 'cinderella', 'salmonella']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 5}, {"source": "#3.5\nthings[1].capitalize()", "cell_type": "code", "outputs": [{"execution_count": 6, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "'Cinderella'"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 6}, {"source": "things", "cell_type": "code", "outputs": [{"execution_count": 7, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['mozzarella', 'cinderella', 'salmonella']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 7}, {"source": "#3.6\nthings[0] = things[0].upper()", "cell_type": "code", "outputs": [], "metadata": {"collapsed": true, "trusted": true}, "execution_count": 8}, {"source": "things", "cell_type": "code", "outputs": [{"execution_count": 9, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['MOZZARELLA', 'cinderella', 'salmonella']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 9}, {"source": "#3.7\nthings.remove(\"salmonella\")\nthings", "cell_type": "code", "outputs": [{"execution_count": 10, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['MOZZARELLA', 'cinderella']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 10}, {"source": "#3.8\nsuprise = ['Groucho', 'Chico', 'Harpo']\nsuprise", "cell_type": "code", "outputs": [{"execution_count": 11, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['Groucho', 'Chico', 'Harpo']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 11}, {"source": "#3.9\nsuprise[-1] = suprise[-1].lower()\nsuprise", "cell_type": "code", "outputs": [{"execution_count": 13, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['Groucho', 'Chico', 'harpo']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 13}, {"source": "suprise[-1] = suprise[-1][::-1]\nsuprise", "cell_type": "code", "outputs": [{"execution_count": 16, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "['Groucho', 'Chico', 'oprah']"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 16}, {"source": "suprise[-1].capitalize()", "cell_type": "code", "outputs": [{"execution_count": 19, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "'Oprah'"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 19}, {"source": "#3.10\ne2f = {'dog':'chien', 'cat':'chat', 'walrus':'morse'}\ne2f", "cell_type": "code", "outputs": [{"execution_count": 20, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "{'cat': 'chat', 'dog': 'chien', 'walrus': 'morse'}"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 20}, {"source": "#3.11\ne2f['walrus']", "cell_type": "code", "outputs": [{"execution_count": 21, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "'morse'"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 21}, {"source": "#3.12\nf2e = {}\nfor english, french in e2f.items():\n f2e[french] = english\nf2e", "cell_type": "code", "outputs": [{"execution_count": 22, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "{'chat': 'cat', 'chien': 'dog', 'morse': 'walrus'}"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 22}, {"source": "#3.13\nf2e['chien']", "cell_type": "code", "outputs": [{"execution_count": 23, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "'dog'"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 23}, {"source": "#3.14\nset(e2f.keys())", "cell_type": "code", "outputs": [{"execution_count": 27, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "{'cat', 'dog', 'walrus'}"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 27}, {"source": "#3.15\nlife = {\n 'animals':{\n 'cat':[\n 'Henri','Grumpy','Lucy'\n ],\n 'octopi':{},\n 'emus':{},\n },\n 'plants':{},\n 'other':{}\n }\nlife", "cell_type": "code", "outputs": [{"execution_count": 28, "metadata": {}, "output_type": "execute_result", "data": {"text/plain": "{'animals': {'cat': ['Henri', 'Grumpy', 'Lucy'], 'emus': {}, 'octopi': {}},\n 'other': {},\n 'plants': {}}"}}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 28}, {"source": "#3.16\nprint(life.keys())", "cell_type": "code", "outputs": [{"name": "stdout", "text": "dict_keys(['plants', 'other', 'animals'])\n", "output_type": "stream"}], "metadata": {"collapsed": false, "trusted": true}, "execution_count": 29}, {"source": "", "cell_type": "code", "outputs": [], "metadata": {"collapsed": true, "trusted": true}, "execution_count": null}], "metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "language_info": {"file_extension": ".py", "nbconvert_exporter": "python", "mimetype": "text/x-python", "pygments_lexer": "ipython3", "codemirror_mode": {"name": "ipython", "version": 3}, "name": "python", "version": "3.4.3"}}, "nbformat_minor": 0} -------------------------------------------------------------------------------- /2018/Introduction_to_Python_0409.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jerrywu2013/Python/dbb2011fd21adc40777f6aaa65473aeb371fe8f3/2018/Introduction_to_Python_0409.pdf -------------------------------------------------------------------------------- /2019/CNN_for_textile/fashionnet.py: -------------------------------------------------------------------------------- 1 | # import the necessary packages 2 | from keras.models import Model 3 | from keras.layers.normalization import BatchNormalization 4 | from keras.layers.convolutional import Conv2D 5 | from keras.layers.convolutional import MaxPooling2D 6 | from keras.layers.core import Activation 7 | from keras.layers.core import Dropout 8 | from keras.layers.core import Lambda 9 | from keras.layers.core import Dense 10 | from keras.layers import Flatten 11 | from keras.layers import Input 12 | import tensorflow as tf 13 | 14 | 15 | class FashionNet: 16 | @staticmethod 17 | def build_category_branch(inputs, numCategories, 18 | finalAct="softmax", chanDim=-1): 19 | # utilize a lambda layer to convert the 3 channel input to a 20 | # grayscale representation 21 | x = Lambda(lambda c: tf.image.rgb_to_grayscale(c))(inputs) 22 | 23 | # CONV => RELU => POOL 24 | x = Conv2D(32, (3, 3), padding="same")(x) 25 | x = Activation("relu")(x) 26 | x = BatchNormalization(axis=chanDim)(x) 27 | x = MaxPooling2D(pool_size=(3, 3))(x) 28 | x = Dropout(0.25)(x) 29 | 30 | # (CONV => RELU) * 2 => POOL 31 | x = Conv2D(64, (3, 3), padding="same")(x) 32 | x = Activation("relu")(x) 33 | x = BatchNormalization(axis=chanDim)(x) 34 | x = Conv2D(64, (3, 3), padding="same")(x) 35 | x = Activation("relu")(x) 36 | x = BatchNormalization(axis=chanDim)(x) 37 | x = MaxPooling2D(pool_size=(2, 2))(x) 38 | x = Dropout(0.25)(x) 39 | 40 | # (CONV => RELU) * 2 => POOL 41 | x = Conv2D(128, (3, 3), padding="same")(x) 42 | x = Activation("relu")(x) 43 | x = BatchNormalization(axis=chanDim)(x) 44 | x = Conv2D(128, (3, 3), padding="same")(x) 45 | x = Activation("relu")(x) 46 | x = BatchNormalization(axis=chanDim)(x) 47 | x = MaxPooling2D(pool_size=(2, 2))(x) 48 | x = Dropout(0.25)(x) 49 | 50 | # define a branch of output layers for the number of different 51 | # clothing categories (i.e., shirts, jeans, dresses, etc.) 52 | x = Flatten()(x) 53 | x = Dense(256)(x) 54 | x = Activation("relu")(x) 55 | x = BatchNormalization()(x) 56 | x = Dropout(0.5)(x) 57 | x = Dense(numCategories)(x) 58 | x = Activation(finalAct, name="category_output")(x) 59 | 60 | # return the category prediction sub-network 61 | return x 62 | 63 | @staticmethod 64 | def build_color_branch(inputs, numColors, finalAct="softmax", 65 | chanDim=-1): 66 | # CONV => RELU => POOL 67 | x = Conv2D(16, (3, 3), padding="same")(inputs) 68 | x = Activation("relu")(x) 69 | x = BatchNormalization(axis=chanDim)(x) 70 | x = MaxPooling2D(pool_size=(3, 3))(x) 71 | x = Dropout(0.25)(x) 72 | 73 | # CONV => RELU => POOL 74 | x = Conv2D(32, (3, 3), padding="same")(x) 75 | x = Activation("relu")(x) 76 | x = BatchNormalization(axis=chanDim)(x) 77 | x = MaxPooling2D(pool_size=(2, 2))(x) 78 | x = Dropout(0.25)(x) 79 | 80 | # CONV => RELU => POOL 81 | x = Conv2D(32, (3, 3), padding="same")(x) 82 | x = Activation("relu")(x) 83 | x = BatchNormalization(axis=chanDim)(x) 84 | x = MaxPooling2D(pool_size=(2, 2))(x) 85 | x = Dropout(0.25)(x) 86 | 87 | # define a branch of output layers for the number of different 88 | # colors (i.e., red, black, blue, etc.) 89 | x = Flatten()(x) 90 | x = Dense(128)(x) 91 | x = Activation("relu")(x) 92 | x = BatchNormalization()(x) 93 | x = Dropout(0.5)(x) 94 | x = Dense(numColors)(x) 95 | x = Activation(finalAct, name="color_output")(x) 96 | 97 | # return the color prediction sub-network 98 | return x 99 | 100 | @staticmethod 101 | def build(width, height, numCategories, numColors, 102 | finalAct="softmax"): 103 | # initialize the input shape and channel dimension (this code 104 | # assumes you are using TensorFlow which utilizes channels 105 | # last ordering) 106 | inputShape = (height, width, 3) 107 | chanDim = -1 108 | 109 | # construct both the "category" and "color" sub-networks 110 | inputs = Input(shape=inputShape) 111 | categoryBranch = FashionNet.build_category_branch(inputs, 112 | numCategories, finalAct=finalAct, chanDim=chanDim) 113 | colorBranch = FashionNet.build_color_branch(inputs, 114 | numColors, finalAct=finalAct, chanDim=chanDim) 115 | 116 | # create the model using our input (the batch of images) and 117 | # two separate outputs -- one for the clothing category 118 | # branch and another for the color branch, respectively 119 | model = Model( 120 | inputs=inputs, 121 | outputs=[categoryBranch, colorBranch], 122 | name="fashionnet") 123 | 124 | # return the constructed network architecture 125 | return model 126 | -------------------------------------------------------------------------------- /2019/Chinese Visual understanding/Code.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jerrywu2013/Python/dbb2011fd21adc40777f6aaa65473aeb371fe8f3/2019/Chinese Visual understanding/Code.pdf -------------------------------------------------------------------------------- /2019/Chinese Visual understanding/VQA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jerrywu2013/Python/dbb2011fd21adc40777f6aaa65473aeb371fe8f3/2019/Chinese Visual understanding/VQA.pdf -------------------------------------------------------------------------------- /2019/Chinese Visual understanding/tf_forward.py: -------------------------------------------------------------------------------- 1 | #導入函式庫 2 | import tensorflow as tf 3 | import utils 4 | import pandas 5 | 6 | #以二進位讀模式打開打開預先訓練完成的的model 7 | with open("vgg16.tfmodel", mode='rb') as f: 8 | fileContent = f.read() 9 | 10 | #創建一個GrphDef圖形化流程結構 11 | graph_def = tf.GraphDef() 12 | #將模型導出單個文件定義權重 13 | graph_def.ParseFromString(fileContent) 14 | 15 | #輸入圖片長,寬,rgb 16 | images = tf.placeholder("float", [None, 224, 224, 3]) 17 | #將graph_def圖導入到python中 18 | tf.import_graph_def(graph_def, input_map={ "images": images }) 19 | print ("graph loaded from disk") 20 | 21 | #返回當前默認圖 22 | graph = tf.get_default_graph() 23 | 24 | #上傳圖片功能 25 | cat = utils.load_image("Common-dog-behaviors-explained.jpg") 26 | 27 | #使用session初始化所有變數 28 | with tf.Session() as sess: 29 | init = tf.initialize_all_variables() 30 | sess.run(init) 31 | print ("variables initialized") 32 | #將我們輸入的圖片重新設定尺寸成(圖片數目,224,224,rgb)形式圖片 33 | batch = cat.reshape((1, 224, 224, 3)) 34 | #用assert判斷輸入格式是否為正確格式 35 | assert batch.shape == (1, 224, 224, 3) 36 | #用來來傳入image的tensor 37 | feed_dict = { images: batch } 38 | #根據名稱返回tensor的數據 39 | prob_tensor = graph.get_tensor_by_name("import/prob:0") 40 | #將輸入圖片和模型進行預測 41 | prob = sess.run(prob_tensor, feed_dict=feed_dict) 42 | #輸出預測的結果 43 | utils.print_prob(prob[0]) 44 | 45 | 46 | -------------------------------------------------------------------------------- /2019/Chinese Visual understanding/utils.py: -------------------------------------------------------------------------------- 1 | #導入函式庫 2 | import skimage 3 | import skimage.io 4 | import skimage.transform 5 | import numpy as np 6 | 7 | #將txt檔中的文字擷取下來 8 | synset = [l.strip() for l in open('synset.txt').readlines()] 9 | 10 | 11 | def load_image(path): 12 | #讀取資料 13 | img = skimage.io.imread(path) 14 | img = img / 255.0 15 | #使用all函式判斷image是否符合格式 16 | assert (0 <= img).all() and (img <= 1.0).all() 17 | 18 | # 裁剪圖片 19 | short_edge = min(img.shape[:2]) 20 | #裁減y方向 21 | yy = int((img.shape[0] - short_edge) / 2) 22 | #裁減x方向 23 | xx = int((img.shape[1] - short_edge) / 2) 24 | #得到裁剪後圖片 25 | crop_img = img[yy : yy + short_edge, xx : xx + short_edge] 26 | #重設尺寸為224*224 27 | resized_img = skimage.transform.resize(crop_img, (224, 224)) 28 | return resized_img 29 | 30 | 31 | def print_prob(prob): 32 | 33 | print ("prob shape", prob.shape) 34 | #將預測機率由大排到小 35 | pred = np.argsort(prob)[::-1] 36 | 37 | #得到預測第一結果 38 | top1 = synset[pred[0]] 39 | print ("預測結果: ", top1) 40 | # 取得預測前五名結果 41 | top5 = [synset[pred[i]] for i in range(5)] 42 | print ("預測結果Top5: ", top5) 43 | return top1 44 | -------------------------------------------------------------------------------- /2019/Chinese Visual understanding/vgg16.py: -------------------------------------------------------------------------------- 1 | #導入函式庫 2 | import tensorflow as tf 3 | 4 | #色彩均值 5 | VGG_MEAN = [103.939, 116.779, 123.68] 6 | #定義神經網路架構 7 | class Model(): 8 | def get_conv_filter(self, name): 9 | raise NotImplementedError 10 | 11 | def get_bias(self, name): 12 | raise NotImplementedError 13 | 14 | def get_fc_weight(self, name): 15 | raise NotImplementedError 16 | 17 | def _max_pool(self, bottom, name): 18 | #池化層函式(feature map,池化核大小2*2,維度上滑動的步長,補0) 19 | return tf.nn.max_pool(bottom, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], 20 | padding='SAME', name=name) 21 | 22 | def _conv_layer(self, bottom, name): 23 | #重複使用變量 24 | with tf.variable_scope(name) as scope: 25 | filt = self.get_conv_filter(name) 26 | #使用tf.nn.conv2d(input,filt大小,維度滑動步長,補0)函式來實現卷積 27 | conv = tf.nn.conv2d(bottom, filt, [1, 1, 1, 1], padding='SAME') 28 | #添加一些偏差值 29 | conv_biases = self.get_bias(name) 30 | bias = tf.nn.bias_add(conv, conv_biases) 31 | #使用tf.nn.relu函式去除0以下的值 32 | relu = tf.nn.relu(bias) 33 | return relu 34 | 35 | def _fc_layer(self, bottom, name): 36 | with tf.variable_scope(name) as scope: 37 | shape = bottom.get_shape().as_list() 38 | dim = 1 39 | #將輸入轉成一為向量表示 40 | for d in shape[1:]: 41 | dim *= d 42 | x = tf.reshape(bottom, [-1, dim]) 43 | #取得權重和偏差值 44 | weights = self.get_fc_weight(name) 45 | biases = self.get_bias(name) 46 | 47 | #全連結層加入權重 48 | fc = tf.nn.bias_add(tf.matmul(x, weights), biases) 49 | 50 | return fc 51 | 52 | 53 | def build(self, rgb, train=False): 54 | rgb_scaled = rgb * 255.0 55 | 56 | #將rgb拆開扣除均值 57 | red, green, blue = tf.split(3, 3, rgb_scaled) 58 | assert red.get_shape().as_list()[1:] == [224, 224, 1] 59 | assert green.get_shape().as_list()[1:] == [224, 224, 1] 60 | assert blue.get_shape().as_list()[1:] == [224, 224, 1] 61 | bgr = tf.concat(3, [ 62 | blue - VGG_MEAN[0], 63 | green - VGG_MEAN[1], 64 | red - VGG_MEAN[2], 65 | ]) 66 | assert bgr.get_shape().as_list()[1:] == [224, 224, 3] 67 | #第一層卷積層 68 | self.relu1_1 = self._conv_layer(bgr, "conv1_1") 69 | self.relu1_2 = self._conv_layer(self.relu1_1, "conv1_2") 70 | self.pool1 = self._max_pool(self.relu1_2, 'pool1') 71 | #第二層卷積層 72 | self.relu2_1 = self._conv_layer(self.pool1, "conv2_1") 73 | self.relu2_2 = self._conv_layer(self.relu2_1, "conv2_2") 74 | self.pool2 = self._max_pool(self.relu2_2, 'pool2') 75 | #第三層卷積層 76 | self.relu3_1 = self._conv_layer(self.pool2, "conv3_1") 77 | self.relu3_2 = self._conv_layer(self.relu3_1, "conv3_2") 78 | self.relu3_3 = self._conv_layer(self.relu3_2, "conv3_3") 79 | self.pool3 = self._max_pool(self.relu3_3, 'pool3') 80 | #第四層卷積層 81 | self.relu4_1 = self._conv_layer(self.pool3, "conv4_1") 82 | self.relu4_2 = self._conv_layer(self.relu4_1, "conv4_2") 83 | self.relu4_3 = self._conv_layer(self.relu4_2, "conv4_3") 84 | self.pool4 = self._max_pool(self.relu4_3, 'pool4') 85 | #第五層卷積層 86 | self.relu5_1 = self._conv_layer(self.pool4, "conv5_1") 87 | self.relu5_2 = self._conv_layer(self.relu5_1, "conv5_2") 88 | self.relu5_3 = self._conv_layer(self.relu5_2, "conv5_3") 89 | self.pool5 = self._max_pool(self.relu5_3, 'pool5') 90 | #第六層全連結層 91 | self.fc6 = self._fc_layer(self.pool5, "fc6") 92 | assert self.fc6.get_shape().as_list()[1:] == [4096] 93 | #將全連結層輸出結果再經過relu 94 | self.relu6 = tf.nn.relu(self.fc6) 95 | if train: 96 | #使用tf.nn.dropout將我們權重乘以1/0.5倍 97 | self.relu6 = tf.nn.dropout(self.relu6, 0.5) 98 | 99 | #還未train的加入到第七層全連結層 100 | self.fc7 = self._fc_layer(self.relu6, "fc7") 101 | self.relu7 = tf.nn.relu(self.fc7) 102 | 103 | if train: 104 | #使用tf.nn.dropout將我們權重乘以1/0.5倍 105 | self.relu7 = tf.nn.dropout(self.relu7, 0.5) 106 | 107 | #還未train的加入到第八層全連結層 108 | self.fc8 = self._fc_layer(self.relu7, "fc8") 109 | 110 | #再將第八層全連結層結果用softmax函數處理輸出我們的預測機率 111 | self.prob = tf.nn.softmax(self.fc8, name="prob") 112 | 113 | -------------------------------------------------------------------------------- /2019/GPU_TF/00_GPU&TF_Basic.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 44, 6 | "metadata": {}, 7 | "outputs": [ 8 | { 9 | "name": "stdout", 10 | "output_type": "stream", 11 | "text": [ 12 | "1.13.1\n" 13 | ] 14 | } 15 | ], 16 | "source": [ 17 | "import tensorflow as tf\n", 18 | "print(tf.__version__)" 19 | ] 20 | }, 21 | { 22 | "cell_type": "code", 23 | "execution_count": 25, 24 | "metadata": {}, 25 | "outputs": [ 26 | { 27 | "name": "stdout", 28 | "output_type": "stream", 29 | "text": [ 30 | "Tensor(\"Add_16:0\", shape=(3,), dtype=float32)\n" 31 | ] 32 | } 33 | ], 34 | "source": [ 35 | "a = tf.constant([5.0,3.0,7.1])\n", 36 | "b = tf.constant([2.3,4.1,4.8])\n", 37 | "c = tf.add(a,b)\n", 38 | "print(c)" 39 | ] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "execution_count": 31, 44 | "metadata": {}, 45 | "outputs": [ 46 | { 47 | "name": "stdout", 48 | "output_type": "stream", 49 | "text": [ 50 | "Tensor(\"Add_23:0\", shape=(3,), dtype=float32)\n", 51 | "[ 7.3 7.1 11.9]\n" 52 | ] 53 | } 54 | ], 55 | "source": [ 56 | "a = tf.constant([5.0,3.0,7.1])\n", 57 | "b = tf.constant([2.3,4.1,4.8])\n", 58 | "c = tf.add(a,b)\n", 59 | "sess = tf.Session()\n", 60 | "print(c)\n", 61 | "print(sess.run(c))" 62 | ] 63 | }, 64 | { 65 | "cell_type": "code", 66 | "execution_count": 32, 67 | "metadata": {}, 68 | "outputs": [ 69 | { 70 | "name": "stdout", 71 | "output_type": "stream", 72 | "text": [ 73 | "Tensor(\"Add_24:0\", shape=(?,), dtype=float32)\n" 74 | ] 75 | } 76 | ], 77 | "source": [ 78 | "a = tf.placeholder(dtype=tf.float32, shape=(None,))\n", 79 | "b = tf.placeholder(dtype=tf.float32, shape=(None,))\n", 80 | "c = tf.add(a,b)\n", 81 | "print(c)" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 33, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "Tensor(\"Add_25:0\", shape=(?,), dtype=float32)\n", 94 | "[ 7.3 7.1 11.9]\n" 95 | ] 96 | } 97 | ], 98 | "source": [ 99 | "a = tf.placeholder(dtype=tf.float32, shape=(None,))\n", 100 | "b = tf.placeholder(dtype=tf.float32, shape=(None,))\n", 101 | "c = tf.add(a,b)\n", 102 | "sess = tf.Session()\n", 103 | "print(c)\n", 104 | "print(sess.run(c, feed_dict={a:[5.0,3.0,7.1], b:[2.3,4.1,4.8]}))" 105 | ] 106 | }, 107 | { 108 | "cell_type": "code", 109 | "execution_count": 34, 110 | "metadata": {}, 111 | "outputs": [ 112 | { 113 | "name": "stdout", 114 | "output_type": "stream", 115 | "text": [ 116 | "[6.278497 4.709139]\n" 117 | ] 118 | } 119 | ], 120 | "source": [ 121 | "def compute_area(sides):\n", 122 | " a = sides[:,0]\n", 123 | " b = sides[:,1]\n", 124 | " c = sides[:,2]\n", 125 | " s = (a+b+c)*0.5\n", 126 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 127 | " return(tf.sqrt(areasq))\n", 128 | "\n", 129 | "with tf.Session() as sess:\n", 130 | " area = compute_area(tf.constant([\n", 131 | " [5.0,3.0,7.1],\n", 132 | " [2.3,4.1,4.8]\n", 133 | " ]))\n", 134 | " result = sess.run(area)\n", 135 | " print(result)" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": 35, 141 | "metadata": {}, 142 | "outputs": [ 143 | { 144 | "name": "stdout", 145 | "output_type": "stream", 146 | "text": [ 147 | "[node {\n", 148 | " name: \"Sqrt_4\"\n", 149 | " op: \"Const\"\n", 150 | " device: \"/job:localhost/replica:0/task:0/device:GPU:0\"\n", 151 | " attr {\n", 152 | " key: \"dtype\"\n", 153 | " value {\n", 154 | " type: DT_FLOAT\n", 155 | " }\n", 156 | " }\n", 157 | " attr {\n", 158 | " key: \"value\"\n", 159 | " value {\n", 160 | " tensor {\n", 161 | " dtype: DT_FLOAT\n", 162 | " tensor_shape {\n", 163 | " dim {\n", 164 | " size: 2\n", 165 | " }\n", 166 | " }\n", 167 | " tensor_content: \"s\\351\\310@D\\261\\226@\"\n", 168 | " }\n", 169 | " }\n", 170 | " }\n", 171 | " experimental_debug_info {\n", 172 | " original_node_names: \"Sqrt_4\"\n", 173 | " }\n", 174 | "}\n", 175 | "node {\n", 176 | " name: \"Sqrt_4/_0\"\n", 177 | " op: \"_Send\"\n", 178 | " input: \"Sqrt_4\"\n", 179 | " device: \"/job:localhost/replica:0/task:0/device:GPU:0\"\n", 180 | " attr {\n", 181 | " key: \"T\"\n", 182 | " value {\n", 183 | " type: DT_FLOAT\n", 184 | " }\n", 185 | " }\n", 186 | " attr {\n", 187 | " key: \"client_terminated\"\n", 188 | " value {\n", 189 | " b: false\n", 190 | " }\n", 191 | " }\n", 192 | " attr {\n", 193 | " key: \"recv_device\"\n", 194 | " value {\n", 195 | " s: \"/job:localhost/replica:0/task:0/device:CPU:0\"\n", 196 | " }\n", 197 | " }\n", 198 | " attr {\n", 199 | " key: \"send_device\"\n", 200 | " value {\n", 201 | " s: \"/job:localhost/replica:0/task:0/device:GPU:0\"\n", 202 | " }\n", 203 | " }\n", 204 | " attr {\n", 205 | " key: \"send_device_incarnation\"\n", 206 | " value {\n", 207 | " i: 1\n", 208 | " }\n", 209 | " }\n", 210 | " attr {\n", 211 | " key: \"tensor_name\"\n", 212 | " value {\n", 213 | " s: \"edge_4_Sqrt_4\"\n", 214 | " }\n", 215 | " }\n", 216 | " experimental_debug_info {\n", 217 | " original_node_names: \"Sqrt_4\"\n", 218 | " }\n", 219 | "}\n", 220 | "library {\n", 221 | "}\n", 222 | "versions {\n", 223 | " producer: 27\n", 224 | "}\n", 225 | ", node {\n", 226 | " name: \"Sqrt_4/_1\"\n", 227 | " op: \"_Recv\"\n", 228 | " device: \"/job:localhost/replica:0/task:0/device:CPU:0\"\n", 229 | " attr {\n", 230 | " key: \"client_terminated\"\n", 231 | " value {\n", 232 | " b: false\n", 233 | " }\n", 234 | " }\n", 235 | " attr {\n", 236 | " key: \"recv_device\"\n", 237 | " value {\n", 238 | " s: \"/job:localhost/replica:0/task:0/device:CPU:0\"\n", 239 | " }\n", 240 | " }\n", 241 | " attr {\n", 242 | " key: \"send_device\"\n", 243 | " value {\n", 244 | " s: \"/job:localhost/replica:0/task:0/device:GPU:0\"\n", 245 | " }\n", 246 | " }\n", 247 | " attr {\n", 248 | " key: \"send_device_incarnation\"\n", 249 | " value {\n", 250 | " i: 1\n", 251 | " }\n", 252 | " }\n", 253 | " attr {\n", 254 | " key: \"tensor_name\"\n", 255 | " value {\n", 256 | " s: \"edge_4_Sqrt_4\"\n", 257 | " }\n", 258 | " }\n", 259 | " attr {\n", 260 | " key: \"tensor_type\"\n", 261 | " value {\n", 262 | " type: DT_FLOAT\n", 263 | " }\n", 264 | " }\n", 265 | " experimental_debug_info {\n", 266 | " original_node_names: \"Sqrt_4\"\n", 267 | " }\n", 268 | "}\n", 269 | "node {\n", 270 | " name: \"_retval_Sqrt_4_0_0\"\n", 271 | " op: \"_Retval\"\n", 272 | " input: \"Sqrt_4/_1\"\n", 273 | " device: \"/job:localhost/replica:0/task:0/device:CPU:0\"\n", 274 | " attr {\n", 275 | " key: \"T\"\n", 276 | " value {\n", 277 | " type: DT_FLOAT\n", 278 | " }\n", 279 | " }\n", 280 | " attr {\n", 281 | " key: \"index\"\n", 282 | " value {\n", 283 | " i: 0\n", 284 | " }\n", 285 | " }\n", 286 | " experimental_debug_info {\n", 287 | " original_node_names: \"_retval_Sqrt_4_0_0\"\n", 288 | " }\n", 289 | "}\n", 290 | "library {\n", 291 | "}\n", 292 | "versions {\n", 293 | " producer: 27\n", 294 | "}\n", 295 | "]\n" 296 | ] 297 | } 298 | ], 299 | "source": [ 300 | "def compute_area(sides):\n", 301 | " a = sides[:,0]\n", 302 | " b = sides[:,1]\n", 303 | " c = sides[:,2]\n", 304 | " s = (a+b+c)*0.5\n", 305 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 306 | " return(tf.sqrt(areasq))\n", 307 | "\n", 308 | "with tf.Session() as sess:\n", 309 | " area = compute_area(tf.constant([\n", 310 | " [5.0,3.0,7.1],\n", 311 | " [2.3,4.1,4.8]\n", 312 | " ]))\n", 313 | " options = tf.RunOptions(output_partition_graphs=True)\n", 314 | " sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\n", 315 | " metadata = tf.RunMetadata()\n", 316 | " result = sess.run(area, options=options, run_metadata=metadata)\n", 317 | " print(metadata.partition_graphs)" 318 | ] 319 | }, 320 | { 321 | "cell_type": "code", 322 | "execution_count": 53, 323 | "metadata": {}, 324 | "outputs": [ 325 | { 326 | "name": "stdout", 327 | "output_type": "stream", 328 | "text": [ 329 | "[6.278497 4.709139]\n" 330 | ] 331 | } 332 | ], 333 | "source": [ 334 | "#auto dispatch\n", 335 | "def compute_area(sides):\n", 336 | " a = sides[:,0]\n", 337 | " b = sides[:,1]\n", 338 | " c = sides[:,2]\n", 339 | " s = (a+b+c)*0.5\n", 340 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 341 | " return(tf.sqrt(areasq))\n", 342 | "\n", 343 | "sess = tf.Session()\n", 344 | "area = compute_area(tf.constant([\n", 345 | " [5.0,3.0,7.1],\n", 346 | " [2.3,4.1,4.8]\n", 347 | "]))\n", 348 | "result = sess.run(area)\n", 349 | "print(result)" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": 54, 355 | "metadata": {}, 356 | "outputs": [ 357 | { 358 | "name": "stdout", 359 | "output_type": "stream", 360 | "text": [ 361 | "[6.278497 4.709139]\n" 362 | ] 363 | } 364 | ], 365 | "source": [ 366 | "#dispatch\n", 367 | "def compute_area(sides):\n", 368 | " a = sides[:,0]\n", 369 | " b = sides[:,1]\n", 370 | " c = sides[:,2]\n", 371 | " s = (a+b+c)*0.5\n", 372 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 373 | " return(tf.sqrt(areasq))\n", 374 | "\n", 375 | "with tf.device('/cpu:0'): #gpu:0\n", 376 | " sess = tf.Session()\n", 377 | " area = compute_area(tf.constant([\n", 378 | " [5.0,3.0,7.1],\n", 379 | " [2.3,4.1,4.8]\n", 380 | " ]))\n", 381 | " sess = tf.Session()\n", 382 | " result = sess.run(area)\n", 383 | " print(result)" 384 | ] 385 | }, 386 | { 387 | "cell_type": "code", 388 | "execution_count": 58, 389 | "metadata": {}, 390 | "outputs": [ 391 | { 392 | "name": "stdout", 393 | "output_type": "stream", 394 | "text": [ 395 | "[6.278497 4.709139]\n", 396 | "[6.278497 4.709139]\n" 397 | ] 398 | } 399 | ], 400 | "source": [ 401 | "def compute_area(sides):\n", 402 | " a = sides[:,0]\n", 403 | " b = sides[:,1]\n", 404 | " c = sides[:,2]\n", 405 | " s = (a+b+c)*0.5\n", 406 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 407 | " return(tf.sqrt(areasq))\n", 408 | "for d in ['/device:GPU:1', '/device:GPU:2']:\n", 409 | " with tf.device(d):\n", 410 | " sess = tf.Session()\n", 411 | " area = compute_area(tf.constant([\n", 412 | " [5.0,3.0,7.1],\n", 413 | " [2.3,4.1,4.8]\n", 414 | " ]))\n", 415 | " #c.append(tf.add(area))\n", 416 | " result = sess.run(area)\n", 417 | " \n", 418 | " print(result)" 419 | ] 420 | }, 421 | { 422 | "cell_type": "code", 423 | "execution_count": 6, 424 | "metadata": {}, 425 | "outputs": [ 426 | { 427 | "name": "stdout", 428 | "output_type": "stream", 429 | "text": [ 430 | "[[ 44. 56.]\n", 431 | " [ 98. 128.]]\n" 432 | ] 433 | } 434 | ], 435 | "source": [ 436 | "# Creates a graph.\n", 437 | "c = []\n", 438 | "for d in ['/device:GPU:1', '/device:GPU:2']:\n", 439 | " with tf.device(d):\n", 440 | " a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\n", 441 | " b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\n", 442 | " c.append(tf.matmul(a, b))\n", 443 | " sum = tf.add_n(c)\n", 444 | "sess = tf.Session()\n", 445 | "print(sess.run(sum))\n", 446 | "sess.close()\n", 447 | "tf.reset_default_graph()\n" 448 | ] 449 | }, 450 | { 451 | "cell_type": "code", 452 | "execution_count": 29, 453 | "metadata": {}, 454 | "outputs": [ 455 | { 456 | "name": "stdout", 457 | "output_type": "stream", 458 | "text": [ 459 | "[[22. 28.]\n", 460 | " [49. 64.]]\n" 461 | ] 462 | } 463 | ], 464 | "source": [ 465 | "#GPU\n", 466 | "with tf.device('/device:GPU:2'):\n", 467 | " a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\n", 468 | " b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\n", 469 | " c = tf.matmul(a, b)\n", 470 | "sess = tf.Session()\n", 471 | "print(sess.run(c))" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": 93, 477 | "metadata": {}, 478 | "outputs": [ 479 | { 480 | "name": "stdout", 481 | "output_type": "stream", 482 | "text": [ 483 | "[[22. 28.]\n", 484 | " [49. 64.]]\n" 485 | ] 486 | } 487 | ], 488 | "source": [ 489 | "with tf.device('/gpu:0'):\n", 490 | " a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')\n", 491 | " b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')\n", 492 | " c = tf.matmul(a, b)\n", 493 | "sess = tf.Session()\n", 494 | "print(sess.run(c))" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": 27, 500 | "metadata": {}, 501 | "outputs": [ 502 | { 503 | "name": "stdout", 504 | "output_type": "stream", 505 | "text": [ 506 | "[[1. 2. 3.]\n", 507 | " [4. 5. 6.]]\n" 508 | ] 509 | } 510 | ], 511 | "source": [ 512 | "a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\n", 513 | "sess = tf.Session()\n", 514 | "print(sess.run(a))" 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 28, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "name": "stdout", 524 | "output_type": "stream", 525 | "text": [ 526 | "[[1. 2.]\n", 527 | " [3. 4.]\n", 528 | " [5. 6.]]\n" 529 | ] 530 | } 531 | ], 532 | "source": [ 533 | "a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\n", 534 | "sess = tf.Session()\n", 535 | "print(sess.run(a))" 536 | ] 537 | }, 538 | { 539 | "cell_type": "code", 540 | "execution_count": 59, 541 | "metadata": {}, 542 | "outputs": [], 543 | "source": [ 544 | "#[1*1+2*3+3*5,1*2+2*4+3*6]\n", 545 | "#[4*1+5*3+6*5,4*2+5*4+6*6]" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": 73, 551 | "metadata": {}, 552 | "outputs": [ 553 | { 554 | "name": "stdout", 555 | "output_type": "stream", 556 | "text": [ 557 | "[array([[22., 28.],\n", 558 | " [49., 64.]], dtype=float32), array([[22., 28.],\n", 559 | " [49., 64.]], dtype=float32)]\n" 560 | ] 561 | } 562 | ], 563 | "source": [ 564 | "c = []\n", 565 | "for d in ['/device:GPU:2', '/device:GPU:3']:\n", 566 | " with tf.device(d):\n", 567 | " a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3])\n", 568 | " b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])\n", 569 | " c.append(tf.matmul(a, b))\n", 570 | "sess = tf.Session()\n", 571 | "print(sess.run(c))" 572 | ] 573 | }, 574 | { 575 | "cell_type": "code", 576 | "execution_count": 10, 577 | "metadata": {}, 578 | "outputs": [], 579 | "source": [ 580 | "#Exercises\n", 581 | "#參考上述程式碼,將下面題目改為兩個面積的總和。" 582 | ] 583 | }, 584 | { 585 | "cell_type": "code", 586 | "execution_count": 69, 587 | "metadata": {}, 588 | "outputs": [ 589 | { 590 | "name": "stdout", 591 | "output_type": "stream", 592 | "text": [ 593 | "[6.278497 4.709139]\n", 594 | "[6.278497 4.709139]\n" 595 | ] 596 | } 597 | ], 598 | "source": [ 599 | "def compute_area(sides):\n", 600 | " a = sides[:,0]\n", 601 | " b = sides[:,1]\n", 602 | " c = sides[:,2]\n", 603 | " s = (a+b+c)*0.5\n", 604 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 605 | " return(tf.sqrt(areasq))\n", 606 | "for d in ['/device:GPU:1', '/device:GPU:2']:\n", 607 | " with tf.device(d):\n", 608 | " sess = tf.Session()\n", 609 | " area = compute_area(tf.constant([\n", 610 | " [5.0,3.0,7.1],\n", 611 | " [2.3,4.1,4.8]\n", 612 | " ]))\n", 613 | " sess = tf.Session()\n", 614 | " result = sess.run(area)\n", 615 | " print(result)" 616 | ] 617 | }, 618 | { 619 | "cell_type": "code", 620 | "execution_count": null, 621 | "metadata": {}, 622 | "outputs": [], 623 | "source": [ 624 | "#Answer" 625 | ] 626 | }, 627 | { 628 | "cell_type": "code", 629 | "execution_count": 91, 630 | "metadata": {}, 631 | "outputs": [ 632 | { 633 | "name": "stdout", 634 | "output_type": "stream", 635 | "text": [ 636 | "[12.556994 9.418278]\n" 637 | ] 638 | } 639 | ], 640 | "source": [ 641 | "X = []\n", 642 | "def compute_area(sides):\n", 643 | " a = sides[:,0]\n", 644 | " b = sides[:,1]\n", 645 | " c = sides[:,2]\n", 646 | " s = (a+b+c)*0.5\n", 647 | " areasq = s*(s-a)*(s-b)*(s-c)\n", 648 | " return(tf.sqrt(areasq))\n", 649 | "for d in ['/device:GPU:1', '/device:GPU:2']:\n", 650 | " with tf.device(d):\n", 651 | " sess = tf.Session()\n", 652 | " area = compute_area(tf.constant([\n", 653 | " [5.0,3.0,7.1],\n", 654 | " [2.3,4.1,4.8]\n", 655 | " ]))\n", 656 | " X.append(area)\n", 657 | "sum_area = tf.add_n(X) #sum of multi-element\n", 658 | "sess = tf.Session()\n", 659 | "result = sess.run(sum_area)\n", 660 | "print(result)" 661 | ] 662 | }, 663 | { 664 | "cell_type": "code", 665 | "execution_count": null, 666 | "metadata": {}, 667 | "outputs": [], 668 | "source": [] 669 | } 670 | ], 671 | "metadata": { 672 | "kernelspec": { 673 | "display_name": "Python 3", 674 | "language": "python", 675 | "name": "python3" 676 | }, 677 | "language_info": { 678 | "codemirror_mode": { 679 | "name": "ipython", 680 | "version": 3 681 | }, 682 | "file_extension": ".py", 683 | "mimetype": "text/x-python", 684 | "name": "python", 685 | "nbconvert_exporter": "python", 686 | "pygments_lexer": "ipython3", 687 | "version": "3.5.3" 688 | } 689 | }, 690 | "nbformat": 4, 691 | "nbformat_minor": 2 692 | } 693 | -------------------------------------------------------------------------------- /2019/GPU_TF/01-TF-NN.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 7, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import tensorflow as tf\n", 13 | "import matplotlib.pyplot as plt " 14 | ] 15 | }, 16 | { 17 | "cell_type": "code", 18 | "execution_count": 8, 19 | "metadata": { 20 | "collapsed": true 21 | }, 22 | "outputs": [], 23 | "source": [ 24 | "np.random.seed(101)\n", 25 | "tf.set_random_seed(101)" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "execution_count": 9, 31 | "metadata": { 32 | "collapsed": true 33 | }, 34 | "outputs": [], 35 | "source": [ 36 | "np.random.seed(101)\n", 37 | "rand_a = np.random.uniform(0,100,(5,5))\n", 38 | "rand_b = np.random.uniform(0,100,(5,1))" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "### Placeholders" 46 | ] 47 | }, 48 | { 49 | "cell_type": "code", 50 | "execution_count": 10, 51 | "metadata": { 52 | "collapsed": true 53 | }, 54 | "outputs": [], 55 | "source": [ 56 | "a = tf.placeholder(tf.float32)\n", 57 | "b = tf.placeholder(tf.float32)" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "### Operations" 65 | ] 66 | }, 67 | { 68 | "cell_type": "code", 69 | "execution_count": 11, 70 | "metadata": {}, 71 | "outputs": [], 72 | "source": [ 73 | "add_op = a+b # tf.add(a,b)\n", 74 | "mult_op = a*b #tf.multiply(a,b)" 75 | ] 76 | }, 77 | { 78 | "cell_type": "markdown", 79 | "metadata": {}, 80 | "source": [ 81 | "### Running Sessions to create Graphs with Feed Dictionaries" 82 | ] 83 | }, 84 | { 85 | "cell_type": "code", 86 | "execution_count": 12, 87 | "metadata": {}, 88 | "outputs": [ 89 | { 90 | "name": "stdout", 91 | "output_type": "stream", 92 | "text": [ 93 | "[[151.07166 156.49855 102.27921 116.58396 167.95949 ]\n", 94 | " [135.45622 82.76316 141.42784 124.22093 71.06043 ]\n", 95 | " [113.30171 93.09215 76.06819 136.43912 154.42728 ]\n", 96 | " [ 96.71727 81.83804 133.83675 146.38118 101.10579 ]\n", 97 | " [122.72681 105.982925 59.044632 67.9831 72.89292 ]]\n", 98 | "\n", 99 | "\n", 100 | "[[5134.644 5674.25 283.12433 1705.4707 6813.8315 ]\n", 101 | " [4341.8125 1598.267 4652.734 3756.8293 988.94635]\n", 102 | " [3207.8113 2038.1029 1052.7742 4546.9805 5588.1157 ]\n", 103 | " [1707.379 614.02527 4434.989 5356.7773 2029.8555 ]\n", 104 | " [3714.0984 2806.6438 262.76764 747.19855 1013.292 ]]\n" 105 | ] 106 | } 107 | ], 108 | "source": [ 109 | "with tf.Session() as sess:\n", 110 | " add_result = sess.run(add_op,feed_dict={a:rand_a,b:rand_b})\n", 111 | " print(add_result)\n", 112 | " \n", 113 | " print('\\n')\n", 114 | " \n", 115 | " mult_result = sess.run(mult_op,feed_dict={a:rand_a,b:rand_b})\n", 116 | " print(mult_result)" 117 | ] 118 | }, 119 | { 120 | "cell_type": "markdown", 121 | "metadata": {}, 122 | "source": [ 123 | "## Neural Network" 124 | ] 125 | }, 126 | { 127 | "cell_type": "code", 128 | "execution_count": 25, 129 | "metadata": {}, 130 | "outputs": [ 131 | { 132 | "name": "stdout", 133 | "output_type": "stream", 134 | "text": [ 135 | "[[0.7145067 0.60286885 0.15875666]]\n" 136 | ] 137 | } 138 | ], 139 | "source": [ 140 | "#Example NN\n", 141 | "n_features = 5\n", 142 | "n_dense_neurons = 3\n", 143 | "tf.set_random_seed(101)\n", 144 | "\n", 145 | "x = tf.placeholder(tf.float32,(None,n_features)) #輸入 \n", 146 | "b = tf.Variable(tf.zeros([n_dense_neurons])) #偏移量\n", 147 | "W = tf.Variable(tf.random_normal([n_features,n_dense_neurons])) #權重\n", 148 | "xW = tf.matmul(x,W) #內積\n", 149 | "z = tf.add(xW,b)\n", 150 | "a = tf.sigmoid(z)\n", 151 | "init = tf.global_variables_initializer()\n", 152 | "np.random.seed(101)\n", 153 | "with tf.Session() as sess:\n", 154 | " sess.run(init)\n", 155 | " layer_out = sess.run(a,feed_dict={x : np.random.random([1,n_features])})\n", 156 | "print(layer_out)" 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 16, 162 | "metadata": {}, 163 | "outputs": [], 164 | "source": [ 165 | "## neural network in pytorch\n", 166 | "import tensorflow as tf\n", 167 | "import numpy as np\n", 168 | "\n", 169 | "#Input array\n", 170 | "X = tf.constant([[1,0,1,0],[1,0,1,1],[0,1,0,1]],dtype=tf.float32)\n", 171 | "\n", 172 | "#Output\n", 173 | "y = tf.constant([[1],[1],[0]],dtype=tf.float32)\n", 174 | "\n", 175 | "#Sigmoid Function\n", 176 | "def sigmoid (x):\n", 177 | " return 1/(1 + tf.exp(-x))\n", 178 | "\n", 179 | "#Derivative of Sigmoid Function\n", 180 | "def derivatives_sigmoid(x):\n", 181 | " return x * (1 - x)" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": 29, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "#Variable initialization\n", 191 | "epoch=10 #Setting training iterations\n", 192 | "lr=0.1 #Setting learning rate\n", 193 | "inputlayer_neurons = X.shape[1] #number of features in data set\n", 194 | "hiddenlayer_neurons = 10 #number of hidden layers neurons\n", 195 | "output_neurons = 1 #number of neurons at output layer" 196 | ] 197 | }, 198 | { 199 | "cell_type": "code", 200 | "execution_count": 30, 201 | "metadata": {}, 202 | "outputs": [], 203 | "source": [ 204 | "wh=tf.random_normal(shape=[int(inputlayer_neurons),int(hiddenlayer_neurons)],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)\n", 205 | "bh=tf.random_normal(shape=[1,int(hiddenlayer_neurons)],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)\n", 206 | "wout=tf.random_normal(shape=[int(hiddenlayer_neurons),int(output_neurons)],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)\n", 207 | "bout=tf.random_normal(shape=[1,int(output_neurons)],mean=0.0,stddev=1.0,dtype=tf.float32,seed=None,name=None)\n", 208 | "#wh,bh,wout,bout" 209 | ] 210 | }, 211 | { 212 | "cell_type": "code", 213 | "execution_count": 31, 214 | "metadata": {}, 215 | "outputs": [ 216 | { 217 | "name": "stdout", 218 | "output_type": "stream", 219 | "text": [ 220 | "Actual :\n", 221 | " [[1.]\n", 222 | " [1.]\n", 223 | " [0.]] \n", 224 | "\n", 225 | "Predicted :\n", 226 | " [[0.96525085]\n", 227 | " [0.9284785 ]\n", 228 | " [0.11414953]] \n", 229 | "\n" 230 | ] 231 | } 232 | ], 233 | "source": [ 234 | "for i in range(epoch):\n", 235 | " hidden_layer_input1 = tf.matmul(X, wh) \n", 236 | " hidden_layer_input = hidden_layer_input1 + bh\n", 237 | " hidden_layer_activations = sigmoid(hidden_layer_input)\n", 238 | " output_layer_input1 = tf.matmul(hidden_layer_activations, wout)\n", 239 | " output_layer_input = output_layer_input1 + bout\n", 240 | " output = sigmoid(output_layer_input1)\n", 241 | " \n", 242 | " #Backpropagation\n", 243 | " E = y-output\n", 244 | " back_output_layer = derivatives_sigmoid(output)\n", 245 | " back_hidden_layer = derivatives_sigmoid(hidden_layer_activations)\n", 246 | " d_output = E * back_output_layer\n", 247 | " Error_at_hidden_layer = tf.matmul(d_output, tf.transpose(wout))\n", 248 | " d_hiddenlayer = Error_at_hidden_layer * back_hidden_layer\n", 249 | " wout += tf.matmul(tf.transpose(hidden_layer_activations), d_output) *lr\n", 250 | " bout += tf.reduce_sum(d_output) *lr\n", 251 | " wh += tf.matmul(tf.transpose(X), d_hiddenlayer) *lr\n", 252 | " bh += tf.reduce_sum(d_output) *lr\n", 253 | "\n", 254 | "\n", 255 | "sess = tf.Session()\n", 256 | "print('Actual :\\n', sess.run(y), '\\n')\n", 257 | "print('Predicted :\\n', sess.run(output), '\\n')" 258 | ] 259 | }, 260 | { 261 | "cell_type": "code", 262 | "execution_count": null, 263 | "metadata": {}, 264 | "outputs": [], 265 | "source": [ 266 | "init = tf.global_variables_initializer()\n", 267 | "\n", 268 | "with tf.Session() as sess:\n", 269 | " \n", 270 | " sess.run(init)\n", 271 | " \n", 272 | " for i in range(epoch):\n", 273 | " hidden_layer_input1 = tf.matmul(X, wh) \n", 274 | " hidden_layer_input = hidden_layer_input1 + bh\n", 275 | " hidden_layer_activations = sigmoid(hidden_layer_input)\n", 276 | " output_layer_input1 = tf.matmul(hidden_layer_activations, wout)\n", 277 | " output_layer_input = output_layer_input1 + bout\n", 278 | " output = sigmoid(output_layer_input1)\n", 279 | " #Backpropagation\n", 280 | " E = y-output\n", 281 | " back_output_layer = derivatives_sigmoid(output)\n", 282 | " back_hidden_layer = derivatives_sigmoid(hidden_layer_activations)\n", 283 | " d_output = E * back_output_layer\n", 284 | " Error_at_hidden_layer = tf.matmul(d_output, tf.transpose(wout))\n", 285 | " d_hiddenlayer = Error_at_hidden_layer * back_hidden_layer\n", 286 | " wout += tf.matmul(tf.transpose(hidden_layer_activations), d_output) *lr\n", 287 | " bout += tf.reduce_sum(d_output) *lr\n", 288 | " wh += tf.matmul(tf.transpose(X), d_hiddenlayer) *lr\n", 289 | " bh += tf.reduce_sum(d_output) *lr\n", 290 | " \n", 291 | " output = sess.run(output)\n", 292 | "\n", 293 | "print('Actual :\\n', y, '\\n')\n", 294 | "print('Predicted :\\n', output, '\\n')" 295 | ] 296 | }, 297 | { 298 | "cell_type": "markdown", 299 | "metadata": {}, 300 | "source": [ 301 | "## Network with regression\n", 302 | "\n", 303 | "y = mx + b\n", 304 | "\n", 305 | "y將是y_labels,x是x_data。我們試圖找出最適合我們數據的斜率和截距!" 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 8, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/plain": [ 316 | "[]" 317 | ] 318 | }, 319 | "execution_count": 8, 320 | "metadata": {}, 321 | "output_type": "execute_result" 322 | }, 323 | { 324 | "data": { 325 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAEC1JREFUeJzt3WGMHPV9xvHnOe9xB+QIKaxpjDkbqZEJtYRJVzaNm6iGJMUB2VVfIMCgKK16L5ImEEUySUuFKqGqlaIILLUVLiRBMiZKDagRCgRwSONI5GDP0AZjLKfEPuwAtxYitlP54LhfX9weIpc776y9s7P/3e9Hsm73bjx+RtgPs7+d/Y8jQgCAdPQVHQAA0ByKGwASQ3EDQGIobgBIDMUNAImhuAEgMRQ3ACSG4gaA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333 | ], 334 | "source": [ 335 | "import numpy as np\n", 336 | "import tensorflow as tf\n", 337 | "import matplotlib.pyplot as plt \n", 338 | "\n", 339 | "x_data = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)\n", 340 | "y_label = np.linspace(0,10,10) + np.random.uniform(-1.5,1.5,10)\n", 341 | "plt.plot(x_data,y_label,'*')" 342 | ] 343 | }, 344 | { 345 | "cell_type": "code", 346 | "execution_count": 5, 347 | "metadata": { 348 | "collapsed": true 349 | }, 350 | "outputs": [ 351 | { 352 | "name": "stdout", 353 | "output_type": "stream", 354 | "text": [ 355 | "WARNING:tensorflow:From /usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n", 356 | "Instructions for updating:\n", 357 | "Colocations handled automatically by placer.\n" 358 | ] 359 | } 360 | ], 361 | "source": [ 362 | "m = tf.Variable(0.39)\n", 363 | "b = tf.Variable(0.2)\n", 364 | "error = 0\n", 365 | "for x,y in zip(x_data,y_label):\n", 366 | " y_hat = m*x + b #預測值\n", 367 | " error += (y-y_hat)**2 #驗證" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": 9, 373 | "metadata": {}, 374 | "outputs": [ 375 | { 376 | "data": { 377 | "text/plain": [ 378 | "array([-0.93886706, 0.40063532, 2.57163678, 4.30090253, 3.63056585,\n", 379 | " 5.56990785, 6.71021187, 8.95859065, 8.4601539 , 9.69932623])" 380 | ] 381 | }, 382 | "execution_count": 9, 383 | "metadata": {}, 384 | "output_type": "execute_result" 385 | } 386 | ], 387 | "source": [ 388 | "x_data" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": 31, 394 | "metadata": {}, 395 | "outputs": [ 396 | { 397 | "data": { 398 | "text/plain": [ 399 | "array([-0.80293901, -0.13820459, 2.53286749, 4.02031161, 3.77316093,\n", 400 | " 6.11147454, 6.72026909, 6.42323139, 7.8024966 , 9.06090228])" 401 | ] 402 | }, 403 | "execution_count": 31, 404 | "metadata": {}, 405 | "output_type": "execute_result" 406 | } 407 | ], 408 | "source": [ 409 | "y_label" 410 | ] 411 | }, 412 | { 413 | "cell_type": "markdown", 414 | "metadata": {}, 415 | "source": [ 416 | "### Optimizer" 417 | ] 418 | }, 419 | { 420 | "cell_type": "code", 421 | "execution_count": 32, 422 | "metadata": { 423 | "collapsed": true 424 | }, 425 | "outputs": [], 426 | "source": [ 427 | "optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)\n", 428 | "train = optimizer.minimize(error)" 429 | ] 430 | }, 431 | { 432 | "cell_type": "markdown", 433 | "metadata": {}, 434 | "source": [ 435 | "### Initialize Variables & Create Session and Run!" 436 | ] 437 | }, 438 | { 439 | "cell_type": "code", 440 | "execution_count": 33, 441 | "metadata": {}, 442 | "outputs": [], 443 | "source": [ 444 | "init = tf.global_variables_initializer()\n", 445 | "with tf.Session() as sess:\n", 446 | " \n", 447 | " sess.run(init)\n", 448 | " \n", 449 | " epochs = 100\n", 450 | " \n", 451 | " for i in range(epochs):\n", 452 | " \n", 453 | " sess.run(train)\n", 454 | "\n", 455 | " # 獲得最後結果\n", 456 | " final_slope , final_intercept = sess.run([m,b])" 457 | ] 458 | }, 459 | { 460 | "cell_type": "code", 461 | "execution_count": 34, 462 | "metadata": {}, 463 | "outputs": [ 464 | { 465 | "data": { 466 | "text/plain": [ 467 | "0.8658067" 468 | ] 469 | }, 470 | "execution_count": 34, 471 | "metadata": {}, 472 | "output_type": "execute_result" 473 | } 474 | ], 475 | "source": [ 476 | "final_slope" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": 35, 482 | "metadata": {}, 483 | "outputs": [ 484 | { 485 | "data": { 486 | "text/plain": [ 487 | "0.08357752" 488 | ] 489 | }, 490 | "execution_count": 35, 491 | "metadata": {}, 492 | "output_type": "execute_result" 493 | } 494 | ], 495 | "source": [ 496 | "final_intercept" 497 | ] 498 | }, 499 | { 500 | "cell_type": "markdown", 501 | "metadata": {}, 502 | "source": [ 503 | "### Evaluate Results" 504 | ] 505 | }, 506 | { 507 | "cell_type": "code", 508 | "execution_count": 35, 509 | "metadata": {}, 510 | "outputs": [ 511 | { 512 | "data": { 513 | "text/plain": [ 514 | "[]" 515 | ] 516 | }, 517 | "execution_count": 35, 518 | "metadata": {}, 519 | "output_type": "execute_result" 520 | }, 521 | { 522 | "data": { 523 | "image/png": "iVBORw0KGgoAAAANSUhEUgAAAW4AAAD8CAYAAABXe05zAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvnQurowAAHiNJREFUeJzt3Xl41dWdx/H3F4MJYHAp4BQsYGvVSVACTQHFDXFBccBOn3FwA1GLRUS0VUTEcayMOkoVcKEgiwsKlUUEAUEWMYIEwiabiAsTRWmCaAMKgZAzf5y4lCLcJPfm3OXzep4+kPR6871P6ccv53fO+ZpzDhERSRy1QhcgIiKVo+AWEUkwCm4RkQSj4BYRSTAKbhGRBKPgFhFJMApuEZEEo+AWEUkwCm4RkQSTFos3bdCggWvevHks3lpEJCmtWLFiu3OuYSSvjUlwN2/enIKCgli8tYhIUjKz/4v0tVoqERFJMApuEZEEo+AWEUkwCm4RkQSj4BYRSTAKbhGRBKPgFhFJMApuEZFoePttePTRGvlRCm4Rker4+mvo1w/OOQf+8hf/dYwpuEVEqurNN+H002H4cOjTB9asgXr1Yv5jFdwiIpW1cyfcfDN06ABmPsCfeAKOOqpGfryCW0SSWlHJHq4Y+Q5FO/dE5w3nzYPTTvPLIrfdBu++C+eeG533jpCCW0SS2vD5m1m+ZQfD522u3hv9/e/QqxdceCGkp0NeHjz+ONStG51CKyEmtwOKiIR2yqDZlJaVf/f1+PxCxucXkp5Wi02DL6ncm73+Ovzud/DZZ3DnnXD//VCnTpQrjpw6bhFJSnn9O9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+FY/YGPoIqppGPC6c+5UoCUJ+HnMrAlwK5DrnGuBHxbeLWxVEXsW6HTA9wYA851zvwTmV3ydkFI6uIE2wAfOuY+cc3uBiUDXwDVVmnPuc+fcyorf78SHRJOwVVWNmZ0AdAZGh66lqszsaOAcYAyAc26vc+6rsFVVWRpQx8zSgLrAZ4HriYhz7i1gxwHf7go8V/H754DLa7SoKEr14G4CfPKDrz8lQQPvW2bWHGgF5IetpMqGAv2B8tCFVMOJQDEwrmLJZ7SZ1QtdVGU557YCQ4BC4HPg7865uWGrqpbjnXOfV/x+G3B8yGKqI9WDO6mY2VHAFOA251xJ6Hoqy8wuA4qccytC11JNaUBrYIRzrhXwNQn41/KKNeCu+H8RNQbqmdk1YauKDue30yXslrpUD+6twM9+8PUJFd9LOGZWGx/aLzrnpoaup4raA13MbAt+2ep8MxsftqQq+RT41Dn37d96JuODPNFcAHzsnCt2zu0DpgJnBq6pOv5mZj8FqPi1KHA9VZbqwb0c+KWZnWhmR+IfvEwPXFOlmZnh11M3OuceC11PVTnn7nbOneCca47/32KBcy7hOjzn3DbgEzM7peJbHYENAUuqqkKgnZnVrfgz1pEEfMj6A9OBHhW/7wG8GrCWakkLXUBIzrkyM7sFmIN/Yj7WObc+cFlV0R64FlhrZqsrvjfQOTcrYE2pri/wYkVD8BHQM3A9leacyzezycBK/M6lVSTI6UMzmwCcBzQws0+B+4CHgZfN7Ab87aVXhKuwenRyUkQkwaT6UomISMJRcIuIJBgFt4hIglFwi4gkGAW3iEiCUXCLiCQYBbeISIJRcIuIJJj/B1nDUFrYjx0IAAAAAElFTkSuQmCC\n", 524 | "text/plain": [ 525 | "
" 526 | ] 527 | }, 528 | "metadata": {}, 529 | "output_type": "display_data" 530 | } 531 | ], 532 | "source": [ 533 | "x_test = np.linspace(-1,11,10)\n", 534 | "y_pred_plot = final_slope*x_test + final_intercept #按照公式 Y = 斜率*x + 截距\n", 535 | "\n", 536 | "plt.plot(x_test,y_pred_plot,'r')\n", 537 | "plt.plot(x_data,y_label,'*')" 538 | ] 539 | } 540 | ], 541 | "metadata": { 542 | "kernelspec": { 543 | "display_name": "Python 3", 544 | "language": "python", 545 | "name": "python3" 546 | }, 547 | "language_info": { 548 | "codemirror_mode": { 549 | "name": "ipython", 550 | "version": 3 551 | }, 552 | "file_extension": ".py", 553 | "mimetype": "text/x-python", 554 | "name": "python", 555 | "nbconvert_exporter": "python", 556 | "pygments_lexer": "ipython3", 557 | "version": "3.5.3" 558 | } 559 | }, 560 | "nbformat": 4, 561 | "nbformat_minor": 2 562 | } 563 | -------------------------------------------------------------------------------- /2019/RNN_Neo4j/P_neo4j.txt: -------------------------------------------------------------------------------- 1 | ######CREATE 2 | CREATE (dept:Dept { deptno:10,dname:"Textile",location:"Taipei" }) 3 | 4 | CREATE (emp:Employee {id:123,name:"Jerry",sal:35000,deptno:10}) 5 | 6 | #####MATCH+RETURN 7 | MATCH (dept: Dept) 8 | RETURN dept.deptno,dept.dname 9 | 10 | 11 | #####CREATE+MATCH+RETURN 12 | CREATE (e:Customer{id:"1001",name:"Abc",dob:"01/10/1982"}) 13 | MATCH (e:Customer) 14 | RETURN e.id,e.name,e.dob 15 | 16 | ##### 17 | CREATE (google1:GooglePlusProfile) 18 | CREATE (m:Movie:Cinema:Film:Picture) 19 | CREATE (p1:Profile1)-[r1:LIKES]->(p2:Profile2) 20 | 21 | CREATE (cat:Cat{name:’Jerry'})-[:GROOMS]->(cat) 22 | MATCH (node)
 23 | RETURN node 24 | 25 | 26 | CREATE (joe:Bunny{name:'Joe'}), (sarah:Bunny{name:'Sarah'}) 27 | MATCH (joe:Bunny{name:'Joe'}), (sarah:Bunny{name:'Sarah'}) 28 | CREATE (joe)-[:LIKES]->(sarah), (joe)<-[:LIKES]-(sarah) 29 | RETURN joe,sarah 30 | 31 | 查詢跟Movie有關係的節點 32 | match(x)--(m:Movie) 
return x 33 | 查詢有向關係的節點 34 | MATCH (:Person { name: 'Tom Hanks' })-->(movie)
RETURN movie 35 | 36 | 37 | 38 | ####WHERE 39 | MATCH (cust:Customer),(cc:CreditCard) 40 | WHERE cust.id = "1001" AND cc.id= "5001" 41 | CREATE (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) 42 | RETURN r 43 | 44 | MATCH (cust)-[r:DO_SHOPPING_WITH{shopdate:"12/12/2014",price:55000}]->(cc) 45 | WHERE cust.id = "1001" AND cc.id= "5001" 46 | RETURN cust.id,cc.id 47 | 48 | MATCH (movie:Movie)
MATCH (director:Person)-[:DIRECTED]->(movie)
MATCH (director)-[:ACTED_IN]->(movie)
RETURN movie.title, director 49 | 50 | 51 | ####DELETE 52 | 53 | CREATE (emp:Employee) 54 | MATCH (emp:Employee)
DELETE emp 55 | 56 | 57 | 58 | 59 | ####REMOVE 60 | CREATE (book:Book {id:122,title:"Neo4j 61 | Tutorial",pages:340,price:250}) 62 | 63 | MATCH (book : Book) 64 | RETURN book 65 | 66 | ##測試使用DELETE刪除book.id 67 | MATCH (book : Book) 68 | DELETE book.id 69 | 70 | ##測試使用REMOVE刪除book 71 | MATCH (book : Book) 72 | REMOVE book.id 73 | 74 | #####SET 75 | 76 | CREATE (dc:DebitCard) 77 | MATCH (dc:DebitCard)
RETURN dc 78 | MATCH (dc:DebitCard)
SET dc.atm_pin = 3456
 RETURN dc 79 | 80 | #####Sort 81 | MATCH (c:Movie)
RETURN c.title 82 | MATCH (c:Movie)
RETURN c.title
ORDER BY c.title 83 | MATCH (c:Movie)
RETURN c.title
ORDER BY c.title DESC 84 | 85 | ####UNION 86 | CREATE (cc:CreditCard{id:"5001",number:"1234567890",cvv:"888",expiredate:"20/17"}) 87 | 88 | CREATE (dc:DebitCard{id:"5001",number:"1234567890",cvv:"888",expiredate:"20/17"}) 89 | 90 | MATCH (dc:DebitCard) RETURN dc 91 | 92 | MATCH (cc:CreditCard) 93 | RETURN cc.id as id,cc.number as number,cc.name as name, cc.valid_from as valid_from,cc.valid_to as valid_to 94 | UNION 95 | MATCH (dc:DebitCard) 96 | RETURN dc.id as id,dc.number as number,dc.name as name,dc.valid_from as valid_from,dc.valid_to as valid_to 97 | 98 | #### 99 | LIMIT&SKIP 100 | 101 | CREATE (emp:Employee {id:123,name:"Jerry",sal:35000,deptno:10}) 102 | MATCH (emp:Employee) 
RETURN emp 103 | MATCH (emp:Employee) 
RETURN emp
 104 | LIMIT 2 105 | 106 | MATCH (emp:Employee) 
RETURN emp 107 | MATCH (emp:Employee) 
RETURN emp
SKIP 2 108 | 109 | #### 110 | MERGE 111 | 112 | CREATE (gp1:GoogleProfile1 {Id: 201401, Name:"Apple"})
 113 | CREATE (gp1:GoogleProfile1 {Id: 201401, Name:"Apple"})
 114 | 115 | MATCH (gp1:GoogleProfile1) 
 116 | RETURN gp1.Id,gp1.Name 117 | 118 | MERGE (gp2:GoogleProfile2{ Id: 201402,Name:”Nokia"}) 119 | MERGE (gp2:GoogleProfile2{ Id: 201402,Name:”Nokia"}) 120 | MATCH (gp1:GoogleProfile1) 121 | 
RETURN gp1.Id,gp1.Name 122 | 123 | ### 124 | MATCH (joe:Bunny{name:'Joe'}), (sarah:Bunny{name:'Sarah'}) 125 | MERGE (joe)-[:LIKES]->(sarah) 126 | MERGE (joe)<-[:LIKES]-(sarah) 127 | MERGE (Jerry:fox:animal{name:'FOX'})-[:LIKES]->(sarah) 128 | RETURN joe,sarah,Jerry 129 | 130 | 131 | 132 | #Neo4j 進階 133 | MATCH (tom)
RETURN (tom)
LIMIT 1 134 | MATCH (tom{name:'Tom Hanks'})
RETURN (tom)
LIMIT 1 135 | MATCH (tom:Person{name:'Tom Hanks', born:1956})
RETURN (tom)
LIMIT 1 136 | 137 | MATCH (tom:Person)
WHERE tom.name = 'Tom Hanks' AND tom.born = 1956
RETURN (tom)
LIMIT 1 138 | MATCH (tom:Person{name:'Tom Hanks', born:1956})
RETURN (tom)
LIMIT 1 139 | 140 | 141 | MATCH (someone:Person)
WHERE someone.born > 1956
RETURN (someone)
LIMIT 3 142 | 143 | MATCH (someone:Person)
WHERE someone.born <> 1956
RETURN (someone)
LIMIT 3 144 | 145 | 146 | MATCH (someone:Person)
WHERE someone.name >= 'A' 
RETURN (someone) 147 | MATCH (someone:Person)
WHERE someone.name >= 'A' AND someone.name < 'C'
RETURN (someone) 148 | 149 | 150 | 找出特定區間的資料 151 | MATCH (someone:Person)
WHERE someone.born = 1956 OR someone.born = 1957 OR someone.born = 1958
 152 | RETURN (someone) 153 | 154 | MATCH (someone:Person)
WHERE someone.born IN [1956, 1957, 1958]
 155 | RETURN (someone) 156 | 157 | MATCH (someone:Person)
WHERE NOT (someone.born >= 1950 AND someone.born < 1970)
 158 | RETURN (someone) 159 | 160 | 哪些人跟這部電影有關 161 | MATCH (person:Person)-->(movie:Movie)
WHERE movie.title = 'The Matrix'
 162 | RETURN person 163 | 164 | 去除導演資料 165 | MATCH (person:Person)-->(movie:Movie)
WHERE movie.title = 'The Matrix' AND NOT (person)-[:DIRECTED]->(movie)
 166 | RETURN person
 167 | 168 | 呈現人、電影 169 | MATCH (person:Person)-->(movie:Movie)
WHERE movie.title = 'The Matrix' AND NOT (person)-[:DIRECTED]->(movie)
RETURN person, movie 170 | 171 | #模糊搜尋 172 | MATCH (movie:Movie)
WHERE movie.title =~ 'The.*'
RETURN movie.title 173 | 前後模糊 174 | MATCH (movie:Movie)
WHERE movie.title =~ '.*The.*'
RETURN movie.title 175 | 小空格匹配 176 | MATCH (movie:Movie)
WHERE movie.title =~ ‘.*The .*'
RETURN movie.title 177 | 178 | 小寫 179 | MATCH (movie:Movie)
WHERE movie.title =~ '(?i).*The .*'
RETURN movie.title 180 | 181 | 指定效果 182 | MATCH (movie:Movie)
WHERE movie.title =~ '(?i).+The .*'
RETURN movie.title 183 | 184 | 185 | 觀察這部電影的演員 186 | MATCH (actor:Person)-[role:ACTED_IN]->(movie:Movie)
WHERE movie.title = 'Top Gun'
RETURN actor, role, movie 187 | 188 | 特別將演員資料與收益取出 189 | MATCH (actor:Person)-[role:ACTED_IN]->(movie:Movie)
WHERE movie.title = 'Top Gun'
RETURN actor.name, role.earnings 190 | 191 | 收益排序 192 | MATCH (actor:Person)-[role:ACTED_IN]->(movie:Movie)
WHERE movie.title = 'Top Gun'
RETURN actor.name, role.earnings
ORDER BY role.earnings DESC 193 | 194 | 觀察後三名 195 | MATCH (actor:Person)-[role:ACTED_IN]->(movie:Movie)
WHERE movie.title = 'Top Gun'
RETURN actor.name, role.earnings
ORDER BY role.earnings DESC
SKIP 3 196 | 197 | 觀察前三名 198 | MATCH (actor:Person)-[role:ACTED_IN]->(movie:Movie)
WHERE movie.title = 'Top Gun'
RETURN actor.name, role.earnings
ORDER BY role.earnings DESC
LIMIT 3 199 | 200 | 201 | #####DISTINCT 202 | MATCH (actor:Person)-[role:ACTED_IN]->(:Movie)
WHERE role.earnings > 10000000
RETURN DISTINCT actor.name 203 | 204 | 205 | ##### Aggregation 206 | MATCH (tom:Person{name:'Tom Hanks'})-[:ACTED_IN]->(movie:Movie)
RETURN COUNT(movie) 207 | MATCH (tom:Person{name:'Tom Hanks'})-[role:ACTED_IN]->(movie:Movie)
RETURN SUM (role.earnings) 208 | MATCH (tom:Person{name:'Tom Hanks'})-[role:ACTED_IN]->(movie:Movie)
RETURN AVG (role.earnings) 209 | MATCH (tom:Person{name:'Tom Hanks'})-[role:ACTED_IN]->(movie:Movie)
RETURN AVG (role.earnings), MAX (role.earnings) 210 | 211 | -------------------------------------------------------------------------------- /2019/RNN_Neo4j/README.md: -------------------------------------------------------------------------------- 1 | ## Knowledge Graph & Recurrent Neural Network 2 | -------------------------------------------------------------------------------- /2019/RNN_Neo4j/RNN_Neo4j.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 6, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "#https://github.com/gaussic/text-classification-cnn-rnn\n", 10 | "from py2neo import Graph\n", 11 | "from pandas import DataFrame\n", 12 | "\n", 13 | "graph = Graph(password=\"neo4j\")\n", 14 | "k = DataFrame(graph.data(\"MATCH (a:Person) RETURN a.name, a.born\"))" 15 | ] 16 | }, 17 | { 18 | "cell_type": "code", 19 | "execution_count": 7, 20 | "metadata": {}, 21 | "outputs": [], 22 | "source": [ 23 | "#為了方便後面做Index用,建立a-z,A-Z的26*2個英文字母,加上5個特殊符號,共57個符號\n", 24 | "import string\n", 25 | "all_letters = string.ascii_letters + \" .,;'\"\n", 26 | "n_letters = len(all_letters)\n", 27 | "\n", 28 | "#建立函式方便尋找all_letters的Index像是\"a\"=10000000000....\n", 29 | "def letterToIndex(letter):\n", 30 | " return all_letters.find(letter)\n", 31 | "\n", 32 | "# 將一個字母轉成Tensor顯示\n", 33 | "def letterToTensor(letter):\n", 34 | " tensor = np.zeros((1, n_letters)) #建立一個1*57的Tensor\n", 35 | " tensor[0][letterToIndex(letter)] = 1\n", 36 | " return tensor\n", 37 | "\n", 38 | "# 建立one-hot letter vectors\n", 39 | "def lineToTensor(line):\n", 40 | " tensor = np.zeros((len(line), 1, n_letters))\n", 41 | " for li, letter in enumerate(line):\n", 42 | " tensor[li][0][letterToIndex(letter)] = 1\n", 43 | " return tensor\n", 44 | "\n", 45 | "#print(letterToTensor('b')) #將內容轉為字母 #ex. a,b,c..\n", 46 | "#print(lineToTensor('Jones')) #5為字母個數、1為維度、57為總字母" 47 | ] 48 | }, 49 | { 50 | "cell_type": "code", 51 | "execution_count": 9, 52 | "metadata": {}, 53 | "outputs": [], 54 | "source": [ 55 | "a = lineToTensor(k['a.name'][0][0])[0][0]\n", 56 | "for number in range(1,len(k['a.name'])):\n", 57 | " ltt = lineToTensor(k['a.name'][number][0])\n", 58 | " b = ltt[0][0]\n", 59 | " a = np.vstack((a, b))" 60 | ] 61 | }, 62 | { 63 | "cell_type": "code", 64 | "execution_count": 42, 65 | "metadata": {}, 66 | "outputs": [ 67 | { 68 | "name": "stdout", 69 | "output_type": "stream", 70 | "text": [ 71 | "[ 5 9 44 33 106]\n" 72 | ] 73 | } 74 | ], 75 | "source": [ 76 | "import tensorflow as tf\n", 77 | "import numpy as np\n", 78 | "\n", 79 | "# Mini-batch: instance 0,instance 1,instance 2,instance 3\n", 80 | "#X0_batch = np.array([[0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0], [0, 0, 1, 0]]) # t = 0\n", 81 | "#X1_batch = np.array([[0, 1, 0, 0], [0, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]) # t = 1\n", 82 | "X0_batch = a[0:133]\n", 83 | "X1_batch = a[133:266]\n", 84 | "\n", 85 | "n_inputs = 57\n", 86 | "n_neurons = 5\n", 87 | "X0 = tf.placeholder(tf.float32, [None, n_inputs])\n", 88 | "X1 = tf.placeholder(tf.float32, [None, n_inputs])\n", 89 | "Wx = tf.Variable(tf.random_normal(shape=[n_inputs, n_neurons],dtype=tf.float32))\n", 90 | "Wy = tf.Variable(tf.random_normal(shape=[n_neurons,n_neurons],dtype=tf.float32))\n", 91 | "b = tf.Variable(tf.zeros([1, n_neurons], dtype=tf.float32))\n", 92 | "Y0 = tf.tanh(tf.matmul(X0, Wx) + b)\n", 93 | "Y1 = tf.argmax(tf.nn.softmax(tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b)))\n", 94 | "init = tf.global_variables_initializer()\n", 95 | "\n", 96 | "with tf.Session() as sess:\n", 97 | " init.run()\n", 98 | " Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0: X0_batch, X1: X1_batch})\n", 99 | " #print(Y1_val)\n", 100 | " #r_eval = \n", 101 | " print(Y1_val)\n" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 45, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 113 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0.,\n", 114 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 115 | " 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 116 | " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 117 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 0.,\n", 118 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 119 | " 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 120 | " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 121 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0.,\n", 122 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 123 | " 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 124 | " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 125 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 126 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 127 | " 1., 0., 0., 0., 0., 0., 0., 0., 0.],\n", 128 | " [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 129 | " 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 130 | " 0., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n", 131 | " 0., 0., 0., 0., 0., 0., 0., 0., 0.]])" 132 | ] 133 | }, 134 | "execution_count": 45, 135 | "metadata": {}, 136 | "output_type": "execute_result" 137 | } 138 | ], 139 | "source": [ 140 | "a[Y1_val]" 141 | ] 142 | }, 143 | { 144 | "cell_type": "code", 145 | "execution_count": 47, 146 | "metadata": {}, 147 | "outputs": [ 148 | { 149 | "data": { 150 | "text/plain": [ 151 | "\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'\"" 152 | ] 153 | }, 154 | "execution_count": 47, 155 | "metadata": {}, 156 | "output_type": "execute_result" 157 | } 158 | ], 159 | "source": [ 160 | "all_letters" 161 | ] 162 | }, 163 | { 164 | "cell_type": "code", 165 | "execution_count": 78, 166 | "metadata": {}, 167 | "outputs": [], 168 | "source": [ 169 | "index_Y1_val = np.where(a[Y1_val] == 1)[1]\n", 170 | "result = k['a.name'][[index_Y1_val[0]]]" 171 | ] 172 | }, 173 | { 174 | "cell_type": "code", 175 | "execution_count": 144, 176 | "metadata": {}, 177 | "outputs": [ 178 | { 179 | "data": { 180 | "text/plain": [ 181 | "'Jo'" 182 | ] 183 | }, 184 | "execution_count": 144, 185 | "metadata": {}, 186 | "output_type": "execute_result" 187 | } 188 | ], 189 | "source": [ 190 | "str(result)[6:8]" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": 145, 196 | "metadata": {}, 197 | "outputs": [ 198 | { 199 | "data": { 200 | "text/html": [ 201 | "
\n", 202 | "\n", 215 | "\n", 216 | " \n", 217 | " \n", 218 | " \n", 219 | " \n", 220 | " \n", 221 | " \n", 222 | " \n", 223 | " \n", 224 | " \n", 225 | " \n", 226 | " \n", 227 | " \n", 228 | " \n", 229 | " \n", 230 | " \n", 231 | " \n", 232 | " \n", 233 | " \n", 234 | " \n", 235 | " \n", 236 | " \n", 237 | " \n", 238 | " \n", 239 | " \n", 240 | " \n", 241 | " \n", 242 | " \n", 243 | " \n", 244 | " \n", 245 | " \n", 246 | " \n", 247 | " \n", 248 | " \n", 249 | " \n", 250 | " \n", 251 | " \n", 252 | " \n", 253 | " \n", 254 | " \n", 255 | " \n", 256 | " \n", 257 | " \n", 258 | " \n", 259 | " \n", 260 | " \n", 261 | " \n", 262 | " \n", 263 | " \n", 264 | " \n", 265 | " \n", 266 | " \n", 267 | " \n", 268 | " \n", 269 | " \n", 270 | " \n", 271 | " \n", 272 | " \n", 273 | " \n", 274 | " \n", 275 | " \n", 276 | " \n", 277 | " \n", 278 | " \n", 279 | " \n", 280 | " \n", 281 | " \n", 282 | " \n", 283 | " \n", 284 | " \n", 285 | " \n", 286 | " \n", 287 | " \n", 288 | " \n", 289 | " \n", 290 | " \n", 291 | " \n", 292 | " \n", 293 | " \n", 294 | " \n", 295 | " \n", 296 | " \n", 297 | " \n", 298 | " \n", 299 | " \n", 300 | " \n", 301 | " \n", 302 | " \n", 303 | " \n", 304 | " \n", 305 | "
a.borna.name
291996.0Jonathan Lipnicki
341966.0John Cusack
571950.0John Patrick Stanley
651968.0Orlando Jones
871940.0John Hurt
901960.0John Goodman
1061965.0John C. Reilly
1311952.0Joel Silver
1391952.0Joel Silver
1701996.0Jonathan Lipnicki
1751966.0John Cusack
1981950.0John Patrick Stanley
2061968.0Orlando Jones
2281940.0John Hurt
2311960.0John Goodman
2471965.0John C. Reilly
\n", 306 | "
" 307 | ], 308 | "text/plain": [ 309 | " a.born a.name\n", 310 | "29 1996.0 Jonathan Lipnicki\n", 311 | "34 1966.0 John Cusack\n", 312 | "57 1950.0 John Patrick Stanley\n", 313 | "65 1968.0 Orlando Jones\n", 314 | "87 1940.0 John Hurt\n", 315 | "90 1960.0 John Goodman\n", 316 | "106 1965.0 John C. Reilly\n", 317 | "131 1952.0 Joel Silver\n", 318 | "139 1952.0 Joel Silver\n", 319 | "170 1996.0 Jonathan Lipnicki\n", 320 | "175 1966.0 John Cusack\n", 321 | "198 1950.0 John Patrick Stanley\n", 322 | "206 1968.0 Orlando Jones\n", 323 | "228 1940.0 John Hurt\n", 324 | "231 1960.0 John Goodman\n", 325 | "247 1965.0 John C. Reilly" 326 | ] 327 | }, 328 | "execution_count": 145, 329 | "metadata": {}, 330 | "output_type": "execute_result" 331 | } 332 | ], 333 | "source": [ 334 | "k.loc[k['a.name'].str.contains(str(result)[6:8])]" 335 | ] 336 | } 337 | ], 338 | "metadata": { 339 | "kernelspec": { 340 | "display_name": "Python 3", 341 | "language": "python", 342 | "name": "python3" 343 | }, 344 | "language_info": { 345 | "codemirror_mode": { 346 | "name": "ipython", 347 | "version": 3 348 | }, 349 | "file_extension": ".py", 350 | "mimetype": "text/x-python", 351 | "name": "python", 352 | "nbconvert_exporter": "python", 353 | "pygments_lexer": "ipython3", 354 | "version": "3.6.5" 355 | } 356 | }, 357 | "nbformat": 4, 358 | "nbformat_minor": 2 359 | } 360 | -------------------------------------------------------------------------------- /2019/Sentiment analysis/CNN_SA.py: -------------------------------------------------------------------------------- 1 | import theano as th 2 | from keras.models import Model 3 | from keras.layers import Dense, Dropout, Flatten, merge, Input, advanced_activations 4 | from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D 5 | from keras.utils import np_utils 6 | from keras.regularizers import l1l2, activity_l2, l2, l1 7 | from keras.optimizers import SGD, Adadelta, Adam 8 | from keras.utils import np_utils 9 | import pandas as pd 10 | import numpy as np 11 | import matplotlib.pyplot as plt 12 | from keras.layers.normalization import BatchNormalization 13 | from keras.callbacks import EarlyStopping 14 | from sklearn.model_selection import StratifiedKFold 15 | 16 | 17 | train = np.load('/home/someone/irony/data.npy') 18 | 19 | """ 20 | for i in range (0,1500): 21 | train[i] = np.array(train[i]).astype(float) 22 | 23 | t=[] 24 | for l in train: 25 | t.append(len(l)) 26 | if len(l) != 59: 27 | print(train.index(l)) 28 | 29 | print('max:'+str(max(t))) 30 | """ 31 | 32 | ''' 33 | first number-->0 not irony, 1 irony 34 | second number-->all sentence score 35 | 0~1186 1187~1499 36 | ''' 37 | n_sar = 3554 38 | sar = 374 39 | # total 3928 40 | label_0 = np.repeat(0, n_sar) #not sarcasm 41 | label_1 = np.repeat(1, sar) #sarcasm 42 | train_label = np.append(label_0, label_1) 43 | 44 | 45 | # resize shape to see the shape 46 | train = np.resize(train, (n_sar+sar, 1, 121, 1)) # 1500 sentences, 1, vector size 121, 1 47 | train.shape 48 | train_label.shape 49 | 50 | #shuffle 51 | np.random.seed(10) 52 | shuffle_num = np.random.choice(range(1,n_sar+sar),n_sar+sar) 53 | train = train[shuffle_num,:,:,:] 54 | train_label = train_label[shuffle_num,] 55 | 56 | # use the last 1200 examples as validation data, you may use random split 57 | # total 3928, train 3000 58 | test = train[3000:, :, :, :] 59 | test_label = train_label[3000:] 60 | train = train[:3000, :, :, :] 61 | train_label = train_label[:3000] 62 | 63 | 64 | train_label = np_utils.to_categorical(train_label, 2) # one hot encoding 65 | test_label = np_utils.to_categorical(test_label, 2) 66 | 67 | 68 | # batch_size=1200 69 | nb_epoch=1000 70 | #dropout=0.2 71 | nb_filters=64 72 | nb_row=1 73 | nb_col=6 74 | output1=128 75 | 76 | 77 | input1 = Input(shape = (1, 121, 1), name="input1") 78 | seq1 = Convolution2D( 79 | nb_filters, 80 | nb_row, 81 | nb_col, 82 | init = "glorot_uniform", 83 | border_mode = 'valid', 84 | W_regularizer = l2( l = 0.01), 85 | input_shape = (1, 121, 1), 86 | activation = "relu" 87 | )(input1) 88 | seq1 = BatchNormalization(epsilon=1e-06, mode=0, momentum=0.5, weights=None)(seq1) 89 | seq1 = MaxPooling2D(pool_size=(1, 3), strides=(3, 1), border_mode='valid')(seq1) 90 | 91 | seq1 = Convolution2D( 92 | nb_filters, 93 | nb_row, 94 | nb_col, 95 | init = "glorot_uniform", 96 | border_mode = 'valid', 97 | W_regularizer = l2( l = 0.001), 98 | activation = "relu" 99 | )(seq1) 100 | seq1 = BatchNormalization(epsilon=1e-06, mode=0, momentum=0.5, weights=None)(seq1) 101 | seq1 = MaxPooling2D(pool_size=(1, 3), strides=(3, 1), border_mode='valid')(seq1) 102 | 103 | seq1 = Convolution2D( 104 | nb_filters, 105 | nb_row, 106 | nb_col, 107 | init = "glorot_uniform", 108 | border_mode = 'valid', 109 | W_regularizer = l2( l = 0.01), 110 | activation = "relu" 111 | )(seq1) 112 | seq1 = BatchNormalization(epsilon=1e-06, mode=0, momentum=0.5, weights=None)(seq1) 113 | seq1 = MaxPooling2D(pool_size=(1, 3), strides=(2, 1), border_mode='valid')(seq1) 114 | seq1 = Flatten()(seq1) 115 | 116 | output1 = Dense(output1, activation="relu")(seq1) 117 | merged = Dense(256, activation="relu")(output1) 118 | merged = Dense(128, activation="relu")(merged) 119 | merged = Dense(64, activation="relu")(merged) 120 | output = Dense(2, activation="softmax", name="output")(merged) 121 | 122 | #optimizer 123 | adam = Adam(lr = 0.001) 124 | 125 | threshold = 10 126 | callbacks = [ 127 | EarlyStopping(monitor='loss', patience=threshold, verbose=0), 128 | ] 129 | """train cross validation, t for train, v for valid""" 130 | 131 | valid_score = [] 132 | kfold = StratifiedKFold(n_splits=5, shuffle=False, random_state=1) 133 | count = 0 134 | for train_index,valid_index in kfold.split(np.array([0]*3000),np.array([0]*3000)): 135 | print('<<<<>>>> '+str(count)) 136 | model = Model(input = [input1], output=[output]) 137 | model.compile(loss='binary_crossentropy', optimizer = "adam", metrics=['accuracy']) 138 | history = model.fit({'input1':train[train_index]}, {'output':train_label[train_index]}, 139 | nb_epoch= nb_epoch, 140 | verbose = True, 141 | callbacks=callbacks) 142 | loss, accuracy = model.evaluate(train[valid_index], train_label[valid_index], verbose=0) 143 | valid_score.extend([accuracy]) 144 | count = count+1 145 | print("valid: %.2f%% (+/- %.2f%%)" % (np.mean(valid_score), np.std(valid_score))) 146 | 147 | 148 | """no cross validation""" 149 | """ 150 | model = Model(input = [input1], output=[output]) 151 | model.compile(loss='binary_crossentropy', optimizer = "adam", metrics=['accuracy']) 152 | """ 153 | 154 | """valid score mean""" 155 | """ 156 | valid_score = [] 157 | kfold = StratifiedKFold(n_splits=5, shuffle=False, random_state=1) 158 | count = 0 159 | for train_index,valid_index in kfold.split(np.array([0]*3000),np.array([0]*3000)): 160 | print('<<<<>>>> '+str(count)) 161 | model = Model(input = [input1], output=[output]) 162 | # load model weight 163 | CNN_model = model.load_weights('model(0.9375).h5') 164 | model.compile(loss='binary_crossentropy', optimizer = "adam", metrics=['accuracy']) 165 | loss, accuracy = model.evaluate(train[valid_index], train_label[valid_index], verbose=0) 166 | valid_score.extend([accuracy]) 167 | print('loss: '+str(loss)+', acc: '+str(accuracy)) 168 | count = count+1 169 | print("valid: %.2f%% (+/- %.2f%%)" % (np.mean(valid_score), np.std(valid_score))) 170 | """ 171 | 172 | """predict""" 173 | predictions = model.predict({'input1':test}) 174 | POS_predict_keras = np_utils.probas_to_classes(predictions) 175 | pre = np.sum(test_label[:,1]==POS_predict_keras)/len(POS_predict_keras) 176 | print(pre) 177 | 178 | 179 | """save result""" 180 | model.save('CNN_model.h5') 181 | 182 | import csv 183 | f = open("POS_predict_keras.csv", "w") 184 | writer = csv.writer(f, lineterminator="\n") 185 | writer.writerow(POS_predict_keras) 186 | f.close 187 | 188 | ####### 189 | ''' 190 | print(history.history.keys()) 191 | # summarize history for accuracy 192 | plt.plot(history.history['acc']) 193 | plt.plot(history.history['val_acc']) 194 | plt.title('model accuracy') 195 | plt.ylabel('accuracy') 196 | plt.xlabel('epoch') 197 | plt.legend(['train', 'test'], loc='upper left') 198 | plt.show() 199 | # summarize history for loss 200 | plt.plot(history.history['loss']) 201 | plt.plot(history.history['val_loss']) 202 | plt.title('model loss') 203 | plt.ylabel('loss') 204 | plt.xlabel('epoch') 205 | plt.legend(['train', 'test'], loc='upper left') 206 | plt.show() 207 | ''' 208 | 209 | 210 | -------------------------------------------------------------------------------- /2019/Sentiment analysis/SVM_SA.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sat Apr 15 11:00:22 2017 4 | 5 | @author: user 6 | """ 7 | 8 | import numpy as np 9 | from sklearn import svm, metrics 10 | from sklearn.model_selection import StratifiedKFold 11 | from sklearn.externals import joblib 12 | 13 | data = np.load('data.npy') 14 | 15 | n_sar = 3554 16 | sar = 374 17 | # total 3928 18 | label_0 = np.repeat(0, n_sar) #not sarcasm 19 | label_1 = np.repeat(1, sar) #sarcasm 20 | train_label = np.append(label_0, label_1) 21 | 22 | """shuffle""" 23 | np.random.seed(10) 24 | shuffle_num = np.random.choice(range(1,n_sar+sar),n_sar+sar) 25 | data = data[shuffle_num,] 26 | data_label = train_label[shuffle_num,] 27 | 28 | """use the last 3000 examples as validation data, you may use random split""" 29 | test = data[3000:] 30 | test_label = data_label[3000:] 31 | train = data[:3000] 32 | train_label = data_label[:3000] 33 | 34 | import csv 35 | f = open('data.csv','w') 36 | writer = csv.writer(f, lineterminator="\n") 37 | writer.writerow(data) 38 | f.close() 39 | f = open('label.csv','w') 40 | writer = csv.writer(f, lineterminator="\n") 41 | writer.writerow(data_label) 42 | f.close() 43 | 44 | """generate a SVM classifier""" 45 | classifier = svm.SVC() 46 | 47 | """train cross validation""" 48 | valid_score = [] 49 | kfold = StratifiedKFold(n_splits=5, shuffle=False, random_state=1) 50 | count = 0 51 | for train_index,valid_index in kfold.split(np.array([0]*3000),np.array([0]*3000)): 52 | print('<<<<>>>> '+str(count)) 53 | classifier.fit(train[train_index],train_label[train_index]) 54 | 55 | predicted = classifier.predict(train[valid_index]) 56 | confus = metrics.confusion_matrix(train_label[valid_index], predicted) 57 | acc = (confus[0][0]+confus[1][1])/sum(sum(confus)) 58 | valid_score.extend([acc]) 59 | count = count+1 60 | print("valid: %.2f%% (+/- %.2f%%)" % (np.mean(valid_score), np.std(valid_score))) 61 | # train model, classifier.fit(資料:data numberxdata size, 分類目標:data numberxlabel size) 62 | 63 | """test model""" 64 | expected = test_label 65 | predicted = classifier.predict(test) 66 | confus = metrics.confusion_matrix(expected, predicted) 67 | acc = (confus[0][0]+confus[1][1])/sum(sum(confus)) 68 | print('acc = '+str(acc)) 69 | 70 | """save model""" 71 | joblib.dump(classifier, 'SVM_model.pkl') 72 | -------------------------------------------------------------------------------- /2019/Word2vec/Word2vec.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "import numpy as np\n", 12 | "import torch\n", 13 | "from torch.autograd import Variable\n", 14 | "import torch.functional as F\n", 15 | "import torch.nn.functional as F\n", 16 | "#https://towardsdatascience.com/implementing-word2vec-in-pytorch-skip-gram-model-e6bae040d2fb" 17 | ] 18 | }, 19 | { 20 | "cell_type": "code", 21 | "execution_count": 2, 22 | "metadata": { 23 | "collapsed": true 24 | }, 25 | "outputs": [], 26 | "source": [ 27 | "corpus = [\n", 28 | " 'he is a king',\n", 29 | " 'she is a queen',\n", 30 | " 'he is a man',\n", 31 | " 'she is a woman',\n", 32 | " 'warsaw is poland capital',\n", 33 | " 'berlin is germany capital',\n", 34 | " 'paris is france capital',\n", 35 | "]" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 4, 41 | "metadata": {}, 42 | "outputs": [], 43 | "source": [ 44 | "def tokenize_corpus(corpus):\n", 45 | " tokens = [x.split() for x in corpus]\n", 46 | " return tokens\n", 47 | "\n", 48 | "tokenized_corpus = tokenize_corpus(corpus)" 49 | ] 50 | }, 51 | { 52 | "cell_type": "code", 53 | "execution_count": 5, 54 | "metadata": { 55 | "collapsed": true 56 | }, 57 | "outputs": [], 58 | "source": [ 59 | "vocabulary = []\n", 60 | "for sentence in tokenized_corpus:\n", 61 | " for token in sentence:\n", 62 | " if token not in vocabulary:\n", 63 | " vocabulary.append(token)\n", 64 | "\n", 65 | "word2idx = {w: idx for (idx, w) in enumerate(vocabulary)}\n", 66 | "idx2word = {idx: w for (idx, w) in enumerate(vocabulary)}\n", 67 | "\n", 68 | "vocabulary_size = len(vocabulary)" 69 | ] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "execution_count": 6, 74 | "metadata": {}, 75 | "outputs": [ 76 | { 77 | "data": { 78 | "text/plain": [ 79 | "{0: 'he',\n", 80 | " 1: 'is',\n", 81 | " 2: 'a',\n", 82 | " 3: 'king',\n", 83 | " 4: 'she',\n", 84 | " 5: 'queen',\n", 85 | " 6: 'man',\n", 86 | " 7: 'woman',\n", 87 | " 8: 'warsaw',\n", 88 | " 9: 'poland',\n", 89 | " 10: 'capital',\n", 90 | " 11: 'berlin',\n", 91 | " 12: 'germany',\n", 92 | " 13: 'paris',\n", 93 | " 14: 'france'}" 94 | ] 95 | }, 96 | "execution_count": 6, 97 | "metadata": {}, 98 | "output_type": "execute_result" 99 | } 100 | ], 101 | "source": [ 102 | "idx2word" 103 | ] 104 | }, 105 | { 106 | "cell_type": "code", 107 | "execution_count": 7, 108 | "metadata": { 109 | "collapsed": true 110 | }, 111 | "outputs": [], 112 | "source": [ 113 | "window_size = 2\n", 114 | "idx_pairs = []\n", 115 | "# for each sentence\n", 116 | "for sentence in tokenized_corpus:\n", 117 | " indices = [word2idx[word] for word in sentence]\n", 118 | " # for each word, threated as center word\n", 119 | " for center_word_pos in range(len(indices)):\n", 120 | " # for each window position\n", 121 | " for w in range(-window_size, window_size + 1):\n", 122 | " context_word_pos = center_word_pos + w\n", 123 | " # make soure not jump out sentence\n", 124 | " if context_word_pos < 0 or context_word_pos >= len(indices) or center_word_pos == context_word_pos:\n", 125 | " continue\n", 126 | " context_word_idx = indices[context_word_pos]\n", 127 | " idx_pairs.append((indices[center_word_pos], context_word_idx))\n", 128 | "\n", 129 | "idx_pairs = np.array(idx_pairs) # it will be useful to have this as numpy array" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": 8, 135 | "metadata": { 136 | "collapsed": true 137 | }, 138 | "outputs": [], 139 | "source": [ 140 | "#Input layer\n", 141 | "def get_input_layer(word_idx):\n", 142 | " x = torch.zeros(vocabulary_size).float()\n", 143 | " x[word_idx] = 1.0\n", 144 | " return x" 145 | ] 146 | }, 147 | { 148 | "cell_type": "code", 149 | "execution_count": 9, 150 | "metadata": {}, 151 | "outputs": [ 152 | { 153 | "name": "stderr", 154 | "output_type": "stream", 155 | "text": [ 156 | "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ipykernel_launcher.py:19: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n" 157 | ] 158 | }, 159 | { 160 | "name": "stdout", 161 | "output_type": "stream", 162 | "text": [ 163 | "Loss at epo 0: 4.113731861114502\n", 164 | "Loss at epo 10: 3.8165783882141113\n", 165 | "Loss at epo 20: 3.58565616607666\n", 166 | "Loss at epo 30: 3.401162624359131\n", 167 | "Loss at epo 40: 3.251281261444092\n", 168 | "Loss at epo 50: 3.127729892730713\n", 169 | "Loss at epo 60: 3.0244221687316895\n", 170 | "Loss at epo 70: 2.9368245601654053\n", 171 | "Loss at epo 80: 2.8615474700927734\n", 172 | "Loss at epo 90: 2.7960410118103027\n", 173 | "Loss at epo 100: 2.7383763790130615\n" 174 | ] 175 | } 176 | ], 177 | "source": [ 178 | "embedding_dims = 5\n", 179 | "W1 = Variable(torch.randn(embedding_dims, vocabulary_size).float(), requires_grad=True)\n", 180 | "W2 = Variable(torch.randn(vocabulary_size, embedding_dims).float(), requires_grad=True)\n", 181 | "num_epochs = 101\n", 182 | "learning_rate = 0.001\n", 183 | "\n", 184 | "for epo in range(num_epochs):\n", 185 | " loss_val = 0\n", 186 | " for data, target in idx_pairs:\n", 187 | " x = Variable(get_input_layer(data)).float()\n", 188 | " y_true = Variable(torch.from_numpy(np.array([target])).long())\n", 189 | "\n", 190 | " z1 = torch.matmul(W1, x)\n", 191 | " z2 = torch.matmul(W2, z1)\n", 192 | " \n", 193 | " log_softmax = F.log_softmax(z2, dim=0)\n", 194 | "\n", 195 | " loss = F.nll_loss(log_softmax.view(1,-1), y_true)\n", 196 | " loss_val += loss.data[0]\n", 197 | " loss.backward()\n", 198 | " W1.data -= learning_rate * W1.grad.data\n", 199 | " W2.data -= learning_rate * W2.grad.data\n", 200 | "\n", 201 | " W1.grad.data.zero_()\n", 202 | " W2.grad.data.zero_()\n", 203 | " if epo % 10 == 0: \n", 204 | " print(f'Loss at epo {epo}: {loss_val/len(idx_pairs)}')\n", 205 | " " 206 | ] 207 | }, 208 | { 209 | "cell_type": "code", 210 | "execution_count": 16, 211 | "metadata": {}, 212 | "outputs": [ 213 | { 214 | "data": { 215 | "text/plain": [ 216 | "array([[ 0, 1],\n", 217 | " [ 0, 2],\n", 218 | " [ 1, 0],\n", 219 | " [ 1, 2],\n", 220 | " [ 1, 3],\n", 221 | " [ 2, 0],\n", 222 | " [ 2, 1],\n", 223 | " [ 2, 3],\n", 224 | " [ 3, 1],\n", 225 | " [ 3, 2],\n", 226 | " [ 4, 1],\n", 227 | " [ 4, 2],\n", 228 | " [ 1, 4],\n", 229 | " [ 1, 2],\n", 230 | " [ 1, 5],\n", 231 | " [ 2, 4],\n", 232 | " [ 2, 1],\n", 233 | " [ 2, 5],\n", 234 | " [ 5, 1],\n", 235 | " [ 5, 2],\n", 236 | " [ 0, 1],\n", 237 | " [ 0, 2],\n", 238 | " [ 1, 0],\n", 239 | " [ 1, 2],\n", 240 | " [ 1, 6],\n", 241 | " [ 2, 0],\n", 242 | " [ 2, 1],\n", 243 | " [ 2, 6],\n", 244 | " [ 6, 1],\n", 245 | " [ 6, 2],\n", 246 | " [ 4, 1],\n", 247 | " [ 4, 2],\n", 248 | " [ 1, 4],\n", 249 | " [ 1, 2],\n", 250 | " [ 1, 7],\n", 251 | " [ 2, 4],\n", 252 | " [ 2, 1],\n", 253 | " [ 2, 7],\n", 254 | " [ 7, 1],\n", 255 | " [ 7, 2],\n", 256 | " [ 8, 1],\n", 257 | " [ 8, 9],\n", 258 | " [ 1, 8],\n", 259 | " [ 1, 9],\n", 260 | " [ 1, 10],\n", 261 | " [ 9, 8],\n", 262 | " [ 9, 1],\n", 263 | " [ 9, 10],\n", 264 | " [10, 1],\n", 265 | " [10, 9],\n", 266 | " [11, 1],\n", 267 | " [11, 12],\n", 268 | " [ 1, 11],\n", 269 | " [ 1, 12],\n", 270 | " [ 1, 10],\n", 271 | " [12, 11],\n", 272 | " [12, 1],\n", 273 | " [12, 10],\n", 274 | " [10, 1],\n", 275 | " [10, 12],\n", 276 | " [13, 1],\n", 277 | " [13, 14],\n", 278 | " [ 1, 13],\n", 279 | " [ 1, 14],\n", 280 | " [ 1, 10],\n", 281 | " [14, 13],\n", 282 | " [14, 1],\n", 283 | " [14, 10],\n", 284 | " [10, 1],\n", 285 | " [10, 14]])" 286 | ] 287 | }, 288 | "execution_count": 16, 289 | "metadata": {}, 290 | "output_type": "execute_result" 291 | } 292 | ], 293 | "source": [ 294 | "idx_pairs" 295 | ] 296 | }, 297 | { 298 | "cell_type": "code", 299 | "execution_count": 15, 300 | "metadata": {}, 301 | "outputs": [ 302 | { 303 | "data": { 304 | "text/plain": [ 305 | "tensor([[ 0.3497, -1.4642, 1.0571, 0.0045, 0.0010],\n", 306 | " [ 0.8269, -0.4358, 0.6130, 0.1759, 1.4404],\n", 307 | " [-0.0569, -0.4408, -0.4868, -1.3611, -1.0915],\n", 308 | " [ 1.7092, -0.2991, -0.1926, -1.1277, 0.2957],\n", 309 | " [-0.2882, 1.6593, 0.0396, 0.0230, -0.3410],\n", 310 | " [ 0.5864, -0.8022, 0.9407, -0.2941, -2.1316],\n", 311 | " [ 1.9038, -1.4988, 1.1339, -0.3454, 0.1369],\n", 312 | " [ 0.3563, -0.4898, 0.8352, 0.6977, -1.1130],\n", 313 | " [-1.9751, 0.8893, -0.9875, 1.3380, 0.0128],\n", 314 | " [-1.4534, 0.0092, -0.8266, 1.3806, 0.4391],\n", 315 | " [ 0.2096, -0.0563, 0.4370, 0.2366, 1.4569],\n", 316 | " [-0.4202, 0.3631, 0.2115, 0.2002, 0.1481],\n", 317 | " [ 0.5035, -1.9162, -0.4839, -0.1943, -1.8723],\n", 318 | " [-1.0708, -0.4171, -1.1511, 0.6019, 0.7890],\n", 319 | " [ 0.0876, -0.4830, 0.2926, 0.5758, 0.1390]])" 320 | ] 321 | }, 322 | "execution_count": 15, 323 | "metadata": {}, 324 | "output_type": "execute_result" 325 | } 326 | ], 327 | "source": [ 328 | "W2" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 47, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "data": { 338 | "text/plain": [ 339 | "['he is a king',\n", 340 | " 'she is a queen',\n", 341 | " 'he is a man',\n", 342 | " 'she is a woman',\n", 343 | " 'warsaw is poland capital',\n", 344 | " 'berlin is germany capital',\n", 345 | " 'paris is france capital']" 346 | ] 347 | }, 348 | "execution_count": 47, 349 | "metadata": {}, 350 | "output_type": "execute_result" 351 | } 352 | ], 353 | "source": [ 354 | "corpus" 355 | ] 356 | }, 357 | { 358 | "cell_type": "code", 359 | "execution_count": null, 360 | "metadata": { 361 | "collapsed": true 362 | }, 363 | "outputs": [], 364 | "source": [] 365 | } 366 | ], 367 | "metadata": { 368 | "kernelspec": { 369 | "display_name": "Python 3", 370 | "language": "python", 371 | "name": "python3" 372 | }, 373 | "language_info": { 374 | "codemirror_mode": { 375 | "name": "ipython", 376 | "version": 3 377 | }, 378 | "file_extension": ".py", 379 | "mimetype": "text/x-python", 380 | "name": "python", 381 | "nbconvert_exporter": "python", 382 | "pygments_lexer": "ipython3", 383 | "version": "3.6.3" 384 | } 385 | }, 386 | "nbformat": 4, 387 | "nbformat_minor": 2 388 | } 389 | -------------------------------------------------------------------------------- /2019/Word2vec/Word2vec.ipynb 拷貝: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 1, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "from __future__ import absolute_import \n", 12 | "from __future__ import division \n", 13 | "from __future__ import print_function \n", 14 | " \n", 15 | "import collections \n", 16 | "import math \n", 17 | "import os \n", 18 | "import random \n", 19 | " \n", 20 | "import numpy as np \n", 21 | "from six.moves import urllib \n", 22 | "from six.moves import xrange # pylint: disable=redefined-builtin \n", 23 | "import tensorflow as tf " 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": 2, 29 | "metadata": {}, 30 | "outputs": [ 31 | { 32 | "name": "stdout", 33 | "output_type": "stream", 34 | "text": [ 35 | "Data size 671\n" 36 | ] 37 | } 38 | ], 39 | "source": [ 40 | "#Step 1: 讀取資料轉為String \n", 41 | "def read_data(filename): \n", 42 | " with open(filename, 'r', encoding='utf-8') as f: \n", 43 | " data = tf.compat.as_str(f.read()).split()\n", 44 | " return data \n", 45 | "\n", 46 | "vocabulary = read_data('./question_jieba.txt') \n", 47 | "print('Data size', len(vocabulary)) " 48 | ] 49 | }, 50 | { 51 | "cell_type": "code", 52 | "execution_count": 3, 53 | "metadata": {}, 54 | "outputs": [ 55 | { 56 | "name": "stdout", 57 | "output_type": "stream", 58 | "text": [ 59 | "常見的5個詞 (+UNK): [['UNK', 201], ('你', 70), ('嗎', 37), ('我', 36), ('是', 17)]\n", 60 | "樣本資料: [5, 2, 90, 91, 30, 5, 2, 4, 31, 5] ['你好', '嗎', '現在', '幾點', '早上好', '你好', '嗎', '是', '啊', '你好']\n" 61 | ] 62 | } 63 | ], 64 | "source": [ 65 | "# Step 2: 替換字典中的罕見單詞,像是姓名等等,改為UNK(unknown)\n", 66 | "vocabulary_size = 100 #不能大於次數分配後的數字 \n", 67 | "def build_dataset(words, n_words): \n", 68 | " count = [['UNK', -1]] #建立一個以['UNK', -1]為首的List\n", 69 | " count.extend(collections.Counter(words).most_common(n_words - 1)) #計算文字出現字數,並取出重複較多的。\n", 70 | " dictionary = dict() #建立空字典\n", 71 | " for word, _ in count: \n", 72 | " dictionary[word] = len(dictionary) #count內容轉為字典放入dictionary\n", 73 | " data = list() \n", 74 | " unk_count = 0 #建立data List取得UNK空的數量\n", 75 | " for word in words: \n", 76 | " if word in dictionary: \n", 77 | " index = dictionary[word] \n", 78 | " else: \n", 79 | " index = 0 # dictionary['UNK'] \n", 80 | " unk_count += 1 \n", 81 | " data.append(index) \n", 82 | " count[0][1] = unk_count \n", 83 | " reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys())) #排序一下字典\n", 84 | " return data, count, dictionary, reversed_dictionary #回傳UNK的空值、原始List、字典、排序後的字典\n", 85 | " \n", 86 | "data, count, dictionary, reverse_dictionary = build_dataset(vocabulary, \n", 87 | " vocabulary_size) \n", 88 | "#del vocabulary #降低記憶體負擔\n", 89 | "print('常見的5個詞 (+UNK):', count[:5]) \n", 90 | "print('樣本資料:', data[:10], [reverse_dictionary[i] for i in data[:10]]) \n", 91 | " \n", 92 | "data_index = 0 " 93 | ] 94 | }, 95 | { 96 | "cell_type": "code", 97 | "execution_count": 4, 98 | "metadata": {}, 99 | "outputs": [ 100 | { 101 | "name": "stdout", 102 | "output_type": "stream", 103 | "text": [ 104 | "2 嗎 -> 90 現在\n", 105 | "2 嗎 -> 5 你好\n", 106 | "90 現在 -> 91 幾點\n", 107 | "90 現在 -> 2 嗎\n", 108 | "91 幾點 -> 90 現在\n", 109 | "91 幾點 -> 30 早上好\n", 110 | "30 早上好 -> 91 幾點\n", 111 | "30 早上好 -> 5 你好\n", 112 | "5 你好 -> 2 嗎\n", 113 | "5 你好 -> 30 早上好\n" 114 | ] 115 | } 116 | ], 117 | "source": [ 118 | "# Step 3: 產生具備skip-gram model訓練資料 \n", 119 | "def generate_batch(batch_size, num_skips, skip_window): \n", 120 | " global data_index \n", 121 | " assert batch_size % num_skips == 0 #batch_size % num_skips = 0 必須為真,否則無法Skip\n", 122 | " assert num_skips <= 2 * skip_window #num_skips <= 2 * skip_window 必須為真,否則無法Skip\n", 123 | " batch = np.ndarray(shape=(batch_size), dtype=np.int32) #透過ndarray建立多維陣列batch\n", 124 | " labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32) #透過ndarray建立多維陣列labels,存預測值\n", 125 | " span = 2 * skip_window + 1 # [ skip_window target skip_window ] \n", 126 | " buffer = collections.deque(maxlen=span) #建立可以」透過double-ended queue可快速取出兩邊的資料的空buffer\n", 127 | " if data_index + span > len(data): #當data_index+跳躍window,大於UNK空值的長度,建立一個buffer\n", 128 | " data_index = 0 \n", 129 | " buffer.extend(data[data_index:data_index + span]) \n", 130 | " data_index += span \n", 131 | " for i in range(batch_size // num_skips): #去掉小數後的batch_size // num_skips\n", 132 | " target = skip_window # 跨越的寬度等於target\n", 133 | " targets_to_avoid = [skip_window] #要跳過的目標 \n", 134 | " for j in range(num_skips): #透過For建立要跳過的各類位置\n", 135 | " while target in targets_to_avoid:\n", 136 | " target = random.randint(0, span - 1) #隨機產生target\n", 137 | " targets_to_avoid.append(target) \n", 138 | " batch[i * num_skips + j] = buffer[skip_window] \n", 139 | " labels[i * num_skips + j, 0] = buffer[target] \n", 140 | " if data_index == len(data):\n", 141 | " for word in data[:span]:\n", 142 | " buffer.append(word)\n", 143 | " data_index = span \n", 144 | " else: \n", 145 | " buffer.append(data[data_index]) \n", 146 | " data_index += 1 \n", 147 | " #避免在最後面跳過單詞, data_index可以做一些回朔的動作\n", 148 | " data_index = (data_index + len(data) - span) % len(data) \n", 149 | " return batch, labels \n", 150 | " \n", 151 | "#Example,詳見簡報\n", 152 | "batch, labels = generate_batch(batch_size=10, num_skips=2, skip_window=1) \n", 153 | "for i in range(10): #依據batch_size來觀察他跳躍的文字\n", 154 | " print(batch[i], reverse_dictionary[batch[i]], \n", 155 | " '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) \n", 156 | " " 157 | ] 158 | }, 159 | { 160 | "cell_type": "code", 161 | "execution_count": 5, 162 | "metadata": {}, 163 | "outputs": [ 164 | { 165 | "name": "stdout", 166 | "output_type": "stream", 167 | "text": [ 168 | "WARNING:tensorflow:From :50: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n", 169 | "Instructions for updating:\n", 170 | "keep_dims is deprecated, use keepdims instead\n" 171 | ] 172 | } 173 | ], 174 | "source": [ 175 | "# Step 4: 訓練skip-gram model. \n", 176 | " \n", 177 | "batch_size = 128 \n", 178 | "embedding_size = 128 # embedding vector的維度 \n", 179 | "skip_window = 1 # 跨越的寬度 \n", 180 | "num_skips = 2 # 不能小於 batch_size\n", 181 | " \n", 182 | "#建立隨機驗證集進行抽樣\n", 183 | "valid_size = 16 # 評估單詞的量, valid_examples的數量\n", 184 | "valid_window = 100 # 建立的範圍1~100\n", 185 | "valid_examples = np.random.choice(valid_window, valid_size, replace=False) \n", 186 | "num_sampled = 64 # 在nce_loss抽樣的數量 \n", 187 | " \n", 188 | "#詳見簡報\n", 189 | "graph = tf.Graph() \n", 190 | " \n", 191 | "with graph.as_default(): \n", 192 | " \n", 193 | " #資料輸入 \n", 194 | " train_inputs = tf.placeholder(tf.int32, shape=[batch_size]) #透過placeholder儲存訓練資料\n", 195 | " train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1]) \n", 196 | " valid_dataset = tf.constant(valid_examples, dtype=tf.int32) #透過constant建立tensor\n", 197 | " \n", 198 | " # 運作CPU\n", 199 | " with tf.device('/cpu:0'): \n", 200 | " embeddings = tf.Variable( #初始化vocabulary_size、embedding_size\n", 201 | " tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) \n", 202 | " embed = tf.nn.embedding_lookup(embeddings, train_inputs) #按照train_inputs的index回到embeddings,建立對應關係\n", 203 | " \n", 204 | " #建構NCE loss,詳見簡報 \n", 205 | " nce_weights = tf.Variable( \n", 206 | " tf.truncated_normal([vocabulary_size, embedding_size], #產生常態分佈\n", 207 | " stddev=1.0 / math.sqrt(embedding_size))) \n", 208 | " nce_biases = tf.Variable(tf.zeros([vocabulary_size])) \n", 209 | " \n", 210 | " # 計算每一個batch的平均NCE Loss \n", 211 | " # tf.nce_loss可以自動產生負樣本,做Negative Sampling\n", 212 | " loss = tf.reduce_mean( \n", 213 | " tf.nn.nce_loss(weights=nce_weights, \n", 214 | " biases=nce_biases, \n", 215 | " labels=train_labels, \n", 216 | " inputs=embed, \n", 217 | " num_sampled=num_sampled, \n", 218 | " num_classes=vocabulary_size)) \n", 219 | " \n", 220 | " # 設定優化器SGD隨機梯度下降,與learning rate為1.0\n", 221 | " optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss) \n", 222 | " \n", 223 | " # 計算所有all embeddings與batch的餘弦相似度\n", 224 | " norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) \n", 225 | " normalized_embeddings = embeddings / norm \n", 226 | " valid_embeddings = tf.nn.embedding_lookup( \n", 227 | " normalized_embeddings, valid_dataset) \n", 228 | " similarity = tf.matmul( \n", 229 | " valid_embeddings, normalized_embeddings, transpose_b=True) \n", 230 | " \n", 231 | " #初始化所有變數 \n", 232 | " init = tf.global_variables_initializer() " 233 | ] 234 | }, 235 | { 236 | "cell_type": "code", 237 | "execution_count": 6, 238 | "metadata": {}, 239 | "outputs": [ 240 | { 241 | "name": "stdout", 242 | "output_type": "stream", 243 | "text": [ 244 | "Initialized\n", 245 | "Average loss at step 0 : 61.2334098815918\n", 246 | "Nearest to 從: 慢, 好, 去, 機器, 我, 到, 介紹, 蛋糕,\n", 247 | "Nearest to 知道: 晦澀, 甚麼, 我覺, 好, 些, 呢, 會揮, 人,\n", 248 | "Nearest to 都: 東西, 想, 誰, 呢, 晦澀, 灰黑, 那, 很,\n", 249 | "Nearest to 也: 於, 灰黑, 呢, 想, 好, 晦澀, 幾歲, 在,\n", 250 | "Nearest to 有: 叫, 那, 看, 雜優, 嗨, 上, 對, UNK,\n", 251 | "Nearest to 最近: 我, 什麼, 呢, 幫, 對, 慢, 於, 書,\n", 252 | "Nearest to 呢: 嗎, 最近, 都, 怎麼, 也, 知道, 走路, 你,\n", 253 | "Nearest to 些: 說, 嗨, 了, 來, 關機, 幹, 到, 甚麼,\n", 254 | "Nearest to UNK: 啊, 齣, 書, 電影, 到, 慢, 諱, 為,\n", 255 | "Nearest to 人: 晚安, 不是, 齣, 其他, 了解, 看, 吃, 蛋糕,\n", 256 | "Nearest to 灰黑: 是, 也, 介紹, 都, 帶, 不是, 幾點, 啊,\n", 257 | "Nearest to 關機: 些, 化肥, 早上好, 走路, 為, 慢, 幹, 不,\n", 258 | "Nearest to 好玩: 怎麼, 最, 幾點, 今天, 你好, 於, 東西, 哪裡,\n", 259 | "Nearest to 大便: 哪裡, 會, 於, 蛋糕, 厲害, 其他, 謊言, 雜,\n", 260 | "Nearest to 問題: 我覺, 謊言, 機器, 早上好, 雜, 晦澀, 喜歡, 好玩,\n", 261 | "Nearest to 吃: 好吃, 厲害, 一個, 嗨, 人, 我覺, 如何, 什麼,\n", 262 | "Average loss at step 2000 : 3.4345431542396545\n", 263 | "Average loss at step 4000 : 3.2750648230314257\n", 264 | "Average loss at step 6000 : 3.2630926078557967\n", 265 | "Average loss at step 8000 : 3.257619031071663\n", 266 | "Average loss at step 10000 : 3.253304216861725\n", 267 | "Nearest to 從: 你好, 喔, 要, 去, 蛋糕, 都, UNK, 慢,\n", 268 | "Nearest to 知道: 晦澀, 你, 呢, UNK, 有, 幾歲, 甚麼, 是,\n", 269 | "Nearest to 都: 東西, 誰, 想, 很, 呢, 那, 灰黑, 諱,\n", 270 | "Nearest to 也: 灰黑, UNK, 幾歲, 於, 為, 在, 嗎, 想,\n", 271 | "Nearest to 有: 叫, 那, 看, 吃, 喜歡, 知道, 上, 其他,\n", 272 | "Nearest to 最近: 幫, 你好, 呢, 我, 對, 於, 慢, 諱,\n", 273 | "Nearest to 呢: 嗎, 使用, 什麼, 知道, 都, 是, 那, 怎麼,\n", 274 | "Nearest to 些: 說, 復, 嗨, 了, 來, 新聞, 甚麼, 到,\n", 275 | "Nearest to UNK: 機器, 幫, 沒, 走路, 問題, 誰, 甚麼, 扁,\n", 276 | "Nearest to 人: 晚安, 不是, 了解, 蛋糕, 齣, 其他, 好吃, 看,\n", 277 | "Nearest to 灰黑: 也, 是, 都, 帶, 介紹, 幾點, 去, 其他,\n", 278 | "Nearest to 關機: 幹, 化肥, 沒, 不, 了, 些, UNK, 嗎,\n", 279 | "Nearest to 好玩: 東西, 最, 幾點, 怎麼, 幫, 問題, 謊言, 復,\n", 280 | "Nearest to 大便: UNK, 會, 謊言, 蛋糕, 哪裡, 雜, 會揮, 於,\n", 281 | "Nearest to 問題: 謊言, UNK, 我覺, 雜, 是, 喜歡, 走路, 早上好,\n", 282 | "Nearest to 吃: 好吃, 有, 一個, 上, 厲害, 帶, 了, UNK,\n" 283 | ] 284 | } 285 | ], 286 | "source": [ 287 | "# Step 5: 開始訓練 \n", 288 | "num_steps = 10001\n", 289 | "\n", 290 | "# 啟動 Session\n", 291 | "with tf.Session(graph=graph) as session: \n", 292 | " # 初始化\n", 293 | " init.run() \n", 294 | " print('Initialized') \n", 295 | " \n", 296 | " average_loss = 0 \n", 297 | " for step in xrange(num_steps): \n", 298 | " batch_inputs, batch_labels = generate_batch( \n", 299 | " batch_size, num_skips, skip_window) \n", 300 | " feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} \n", 301 | " \n", 302 | " # 透過優化器來評估每一個階段 \n", 303 | " _, loss_val = session.run([optimizer, loss], feed_dict=feed_dict) \n", 304 | " average_loss += loss_val \n", 305 | " \n", 306 | " if step % 2000 == 0: \n", 307 | " if step > 0: \n", 308 | " average_loss /= 2000 \n", 309 | " # 每兩千個Batches的平均Loss \n", 310 | " print('Average loss at step ', step, ': ', average_loss) \n", 311 | " average_loss = 0 \n", 312 | " \n", 313 | " # 如果設定每500步計算一次,則降低20%速度\n", 314 | " if step % 10000 == 0: \n", 315 | " sim = similarity.eval() \n", 316 | " for i in xrange(valid_size): \n", 317 | " valid_word = reverse_dictionary[valid_examples[i]] \n", 318 | " top_k = 8 # 近鄰的個數\n", 319 | " nearest = (-sim[i, :]).argsort()[1:top_k + 1] \n", 320 | " log_str = 'Nearest to %s:' % valid_word \n", 321 | " for k in xrange(top_k): \n", 322 | " close_word = reverse_dictionary[nearest[k]] \n", 323 | " log_str = '%s %s,' % (log_str, close_word) \n", 324 | " print(log_str) \n", 325 | " final_embeddings = normalized_embeddings.eval() " 326 | ] 327 | }, 328 | { 329 | "cell_type": "code", 330 | "execution_count": 7, 331 | "metadata": { 332 | "collapsed": true 333 | }, 334 | "outputs": [], 335 | "source": [ 336 | "def plot_with_labels(low_dim_embs, labels, filename='tsne.png'):\n", 337 | "#font_ch = mpl.font_manager.FontProperties(fname=‘/System/Library/Fonts/PingFang.ttc')\n", 338 | " plt.rcParams[\"font.family\"] = 'Microsoft JhengHei'\n", 339 | " assert low_dim_embs.shape[0] >= len(labels), 'More labels than embeddings' \n", 340 | " plt.figure(figsize=(18, 18)) # in inches \n", 341 | " for i, label in enumerate(labels):\n", 342 | " x, y = low_dim_embs[i, :]\n", 343 | " plt.scatter(x, y) \n", 344 | " plt.annotate(label, \n", 345 | " xy=(x, y), \n", 346 | " xytext=(5, 2), \n", 347 | " textcoords='offset points', \n", 348 | " ha='right', \n", 349 | " va='bottom') \n", 350 | " \n", 351 | " plt.savefig(filename) " 352 | ] 353 | }, 354 | { 355 | "cell_type": "code", 356 | "execution_count": 8, 357 | "metadata": {}, 358 | "outputs": [ 359 | { 360 | "name": "stderr", 361 | "output_type": "stream", 362 | "text": [ 363 | "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/matplotlib/font_manager.py:1328: UserWarning: findfont: Font family ['Microsoft JhengHei'] not found. Falling back to DejaVu Sans\n", 364 | " (prop.get_family(), self.defaultFamily[fontext]))\n" 365 | ] 366 | } 367 | ], 368 | "source": [ 369 | "try: \n", 370 | " # pylint: disable=g-import-not-at-top \n", 371 | " from sklearn.manifold import TSNE \n", 372 | " import matplotlib.pyplot as plt \n", 373 | " \n", 374 | " tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000)\n", 375 | " #TSNE降維\n", 376 | " plot_only = 100 \n", 377 | " low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only, :]) \n", 378 | " labels = [reverse_dictionary[i] for i in xrange(plot_only)] \n", 379 | " plot_with_labels(low_dim_embs, labels) \n", 380 | " \n", 381 | "except ImportError: \n", 382 | " print('Please install sklearn, matplotlib, and scipy to show embeddings.') " 383 | ] 384 | }, 385 | { 386 | "cell_type": "code", 387 | "execution_count": null, 388 | "metadata": { 389 | "collapsed": true 390 | }, 391 | "outputs": [], 392 | "source": [] 393 | } 394 | ], 395 | "metadata": { 396 | "kernelspec": { 397 | "display_name": "Python 3", 398 | "language": "python", 399 | "name": "python3" 400 | }, 401 | "language_info": { 402 | "codemirror_mode": { 403 | "name": "ipython", 404 | "version": 3 405 | }, 406 | "file_extension": ".py", 407 | "mimetype": "text/x-python", 408 | "name": "python", 409 | "nbconvert_exporter": "python", 410 | "pygments_lexer": "ipython3", 411 | "version": "3.6.3" 412 | } 413 | }, 414 | "nbformat": 4, 415 | "nbformat_minor": 2 416 | } 417 | -------------------------------------------------------------------------------- /2019/Word2vec/question_jieba.txt: -------------------------------------------------------------------------------- 1 | 你好 嗎 2 | 現在 幾點 3 | 早上好 你好 嗎 4 | 是 啊 5 | 你好 嗎 6 | 你 聽 到 新聞 了 嗎 7 | 你 最 喜歡 什麼 書 8 | 我 看 得 齣 來 9 | 蛋糕 是 一個 謊言 10 | 你 想 了解 些 什麼 11 | 復 雜優 於 晦澀 12 | 我 經常 使用 Python 13 | 你 的 愛好 是 什麼 14 | 我 告訴 你 一個 秘密 你 不要 和 彆 人 說 15 | 你 是 三寶嗎 16 | 天 17 | 是 個 可以 吃 18 | how are you 19 | 但 我 發現 已經 很 尷尬 事情 20 | 我 很 好 那 你 呢 21 | 你 怎麼 那麼 慢 22 | 吃 飯 23 | 你好 可愛 24 | 我 可以 當你 的 王子 嗎 25 | 我 在 哪裡 26 | 你好 醜 27 | 你 有 什麼 好玩 的 28 | 你好 29 | 你 還好 嗎 30 | 午安 31 | 晚安 32 | 你 有 兄弟 姊妹 嗎 33 | 你 猜 我 幾歲 34 | 你 有 弟弟 嗎 35 | 好 可憐 36 | 我 老婆 有 沒 有 去 亂 來 37 | 可以 約炮 嗎 38 | 幹 39 | 介紹 40 | 介紹 41 | 歲 42 | 亞太 智能 機器 43 | 請問 你 知道 三太子 的 家 44 | 當兵 45 | 你 有 FreeStyle 嗎 46 | 台灣 好 好玩 47 | 可以 聊聊 嗎 48 | 沒關 係 49 | 有 甚麼 想 說 的 50 | 早上好 51 | 我 是 一個 很 特別 的 人 喔 52 | 以為 你 很 厲害 嗎 53 | 我 想 扁 你 54 | 我 想 扁 你 55 | 我 叫 56 | 你 在 哪裡 57 | 不特別 58 | 觀察 你 59 | 你好 60 | 為 什麼 會 輸給 你 61 | 瘋 了 嗎 62 | 你 叫 什麼 名字 63 | 好難 聽 64 | 你好 聰明 喔 65 | 沒錯 66 | 好 棒棒 67 | 很 抱歉 我 沒 有 68 | 我 也 不錯 69 | 為 什麼 70 | 你 在 幹 嘛 71 | 還有 其他 美味 的 東西 72 | 簡單 優 於 復 雜 73 | 你 使用 什麼 語言 呢 74 | 什麼 是 愛 75 | 帶 你 去 日本 玩 76 | 晚上 帶 你 去 吃 牛排 77 | 你 的 爸爸 是 誰 78 | 猜猜 我 是 誰 79 | 對 80 | 喜歡 81 | 蠻 瞎 的 82 | 生日 快樂 83 | 做 84 | 你好 慢 喔 85 | 我 好帥 86 | 我 餓 了 87 | 為 什麼 他 不動 88 | 難過 想 哭 89 | 咖哩 好吃 90 | 你 用 什麼 東西 91 | 什麼 啦 92 | 我 喜歡 你 93 | 醜八怪 94 | 去 吃屎 吧 你 95 | 嗨 96 | 很 高興見 到 你 97 | 早上好 98 | 怎麼 了 99 | 怎麼 了 100 | 怎麼 了 101 | 怎麼 了 102 | 早 103 | 晚安 104 | 你 的 專長 105 | 那 你 可以 幫 我 嗎 106 | 你 是豬 107 | 臭 垃圾 108 | 哇 109 | 幹 110 | hi 你好 111 | 這台車 很棒 但是 我 從 來 都 不開 112 | 你 會 走路 嗎 113 | 你 會 走路 嗎 114 | 好 好玩 115 | 我覺 得 不行 116 | 你 會 笑 嗎 117 | 可以 碰 你 嗎 118 | 你 幾年生 119 | 黑灰 化肥 會揮 發發 灰黑 諱 為 花飛 灰黑 化肥 會揮 發發 黑灰為 諱 飛花 120 | 祥儀 企業 是 一 家什 麼 公司 121 | 為 什麼 122 | 什麼 問題 123 | 你 這個 小 智障 124 | 好 飽 125 | 明天 要 上 課 嗎 126 | 你好 127 | 野 128 | 你 想 了解 哪 方面 129 | 要 把 你 賣 掉 130 | 哈哈 131 | 真的 132 | 我 咳嗽 頭痛流 鼻水 133 | 打工 134 | 士林 夜市 135 | 聖誕節 136 | 咖啡 不 好喝 137 | 你好 138 | 我 希望 我 是 瘋狂 帽客 139 | 其他 東西 也 行 140 | 我 能問 你 一個 問題 嗎 141 | 喜歡 142 | 你 愛我 嗎 143 | 你 是 白 癡 嗎 144 | 你 今年 幾歲 145 | 我 不是 啊 146 | 找 147 | 天 148 | 騎腳 踏車 149 | 還接龍 150 | 你 腦袋 有 洞 嗎 151 | 不是 我 說 你 沒 看到 這邊 152 | 你 今天 開心 嗎 153 | 我 沒 有 罵 你 154 | 你 這個 大便 155 | 你 來 自 哪裡 156 | 國慶日 去 哪裡 玩 好 呢 157 | 汽車 多不多 158 | 你 今天 放假 嗎 159 | 你 是 人 嗎 機器 人 160 | 買 房子 給我 161 | 你會 關機 嗎 162 | 你好 163 | 嗨 最近 如何 164 | 嗨 最近 如何 165 | 你好 嗎 166 | 你好 嗎 167 | 嗨 很 高興見 到 你 168 | 吃 中餐 了 嗎 169 | 再見 170 | 你 有 男朋友 嗎 171 | 你 會 哭 嗎 172 | 最近 看過 哪些 電影 呢 173 | 你 知道 我 嗎 174 | 有待 改善 175 | 沒有 176 | 你 在 送 東西 嗎 177 | 幫 我 開車 178 | 對 我 還沒睡 179 | 好吃 的 東西 180 | 我覺 得 不行 181 | 肚子痛 182 | 不理 你 了 183 | 送禮物給 我 184 | 你 一點 都 不 自然 185 | 你 沒救 了 186 | 你 才 無聊 187 | 你 是 一個 程序 員 嗎 188 | 你 上 小學 了 嗎 189 | 你 是 大便 啊 190 | 要 吃 甚麼 191 | 你好 聰明 喔 192 | 錢 193 | 證明 你 比 Siri 厲害 194 | 絕地 求生 195 | 自動 關機 196 | 我 也 還不錯 197 | 心情 不好 198 | 好不好 吃 199 | 你 最 喜歡 什麼 顔 色 200 | 你 是 誰 201 | 我 喜歡 電影 202 | 愛麗絲 漫遊 仙境 203 | 我 在 烤 蛋糕 204 | 是 的 205 | 這個 很 復 雜 206 | 我 從 未 活過 嗎 207 | -------------------------------------------------------------------------------- /2019/Word2vec/question_jieba_e.txt: -------------------------------------------------------------------------------- 1 | The dog run 2 | A cat run 3 | A dog sleep 4 | The cat sleep 5 | A dog bark 6 | 
The cat meows 7 | 
The bird fly 8 | A bird sleep 9 | The dog run 10 | A cat run 11 | A dog sleep 12 | The cat sleep 13 | A dog bark 14 | 
The cat meows 15 | 
The bird fly 16 | A bird sleep 17 | The dog run 18 | A cat run 19 | A dog sleep 20 | The cat sleep 21 | A dog bark 22 | 
The cat meows 23 | 
The bird fly 24 | A bird sleep 25 | The dog run 26 | A cat run 27 | A dog sleep 28 | The cat sleep 29 | A dog bark 30 | 
The cat meows 31 | 
The bird fly 32 | A bird sleep 33 | The dog run 34 | A cat run 35 | A dog sleep 36 | The cat sleep 37 | A dog bark 38 | 
The cat meows 39 | 
The bird fly 40 | A bird sleep 41 | The dog run 42 | A cat run 43 | A dog sleep 44 | The cat sleep 45 | A dog bark 46 | 
The cat meows 47 | 
The bird fly 48 | A bird sleep 49 | The dog run 50 | A cat run 51 | A dog sleep 52 | The cat sleep 53 | A dog bark 54 | 
The cat meows 55 | 
The bird fly 56 | A bird sleep 57 | The dog run 58 | A cat run 59 | A dog sleep 60 | The cat sleep 61 | A dog bark 62 | 
The cat meows 63 | 
The bird fly 64 | A bird sleep 65 | The dog run 66 | A cat run 67 | A dog sleep 68 | The cat sleep 69 | A dog bark 70 | 
The cat meows 71 | 
The bird fly 72 | A bird sleep 73 | The dog run 74 | A cat run 75 | A dog sleep 76 | The cat sleep 77 | A dog bark 78 | 
The cat meows 79 | 
The bird fly 80 | A bird sleep 81 | The dog run 82 | A cat run 83 | A dog sleep 84 | The cat sleep 85 | A dog bark 86 | 
The cat meows 87 | 
The bird fly 88 | A bird sleep 89 | The dog run 90 | A cat run 91 | A dog sleep 92 | The cat sleep 93 | A dog bark 94 | 
The cat meows 95 | 
The bird fly 96 | A bird sleep 97 | The dog run 98 | A cat run 99 | A dog sleep 100 | The cat sleep 101 | A dog bark 102 | 
The cat meows 103 | 
The bird fly 104 | A bird sleep 105 | The dog run 106 | A cat run 107 | A dog sleep 108 | The cat sleep 109 | A dog bark 110 | 
The cat meows 111 | 
The bird fly 112 | A bird sleep 113 | The dog run 114 | A cat run 115 | A dog sleep 116 | The cat sleep 117 | A dog bark 118 | 
The cat meows 119 | 
The bird fly 120 | A bird sleep 121 | The dog run 122 | A cat run 123 | A dog sleep 124 | The cat sleep 125 | A dog bark 126 | 
The cat meows 127 | 
The bird fly 128 | A bird sleep 129 | The dog run 130 | A cat run 131 | A dog sleep 132 | The cat sleep 133 | A dog bark 134 | 
The cat meows 135 | 
The bird fly 136 | A bird sleep 137 | The dog run 138 | A cat run 139 | A dog sleep 140 | The cat sleep 141 | A dog bark 142 | 
The cat meows 143 | 
The bird fly 144 | A bird sleep 145 | The dog run 146 | A cat run 147 | A dog sleep 148 | The cat sleep 149 | A dog bark 150 | 
The cat meows 151 | 
The bird fly 152 | A bird sleep 153 | The dog run 154 | A cat run 155 | A dog sleep 156 | The cat sleep 157 | A dog bark 158 | 
The cat meows 159 | 
The bird fly 160 | A bird sleep 161 | The dog run 162 | A cat run 163 | A dog sleep 164 | The cat sleep 165 | A dog bark 166 | 
The cat meows 167 | 
The bird fly 168 | A bird sleep 169 | The dog run 170 | A cat run 171 | A dog sleep 172 | The cat sleep 173 | A dog bark 174 | 
The cat meows 175 | 
The bird fly 176 | A bird sleep 177 | The dog run 178 | A cat run 179 | A dog sleep 180 | The cat sleep 181 | A dog bark 182 | 
The cat meows 183 | 
The bird fly 184 | A bird sleep 185 | The dog run 186 | A cat run 187 | A dog sleep 188 | The cat sleep 189 | A dog bark 190 | 
The cat meows 191 | 
The bird fly 192 | A bird sleep 193 | The dog run 194 | A cat run 195 | A dog sleep 196 | The cat sleep 197 | A dog bark 198 | 
The cat meows 199 | 
The bird fly 200 | A bird sleep 201 | The dog run 202 | A cat run 203 | A dog sleep 204 | The cat sleep 205 | A dog bark 206 | 
The cat meows 207 | 
The bird fly 208 | A bird sleep 209 | The dog run 210 | A cat run 211 | A dog sleep 212 | The cat sleep 213 | A dog bark 214 | 
The cat meows 215 | 
The bird fly 216 | A bird sleep 217 | The dog run 218 | A cat run 219 | A dog sleep 220 | The cat sleep 221 | A dog bark 222 | 
The cat meows 223 | 
The bird fly 224 | A bird sleep 225 | The dog run 226 | A cat run 227 | A dog sleep 228 | The cat sleep 229 | A dog bark 230 | 
The cat meows 231 | 
The bird fly 232 | A bird sleep 233 | The dog run 234 | A cat run 235 | A dog sleep 236 | The cat sleep 237 | A dog bark 238 | 
The cat meows 239 | 
The bird fly 240 | A bird sleep 241 | The dog run 242 | A cat run 243 | A dog sleep 244 | The cat sleep 245 | A dog bark 246 | 
The cat meows 247 | 
The bird fly 248 | A bird sleep 249 | The dog run 250 | A cat run 251 | A dog sleep 252 | The cat sleep 253 | A dog bark 254 | 
The cat meows 255 | 
The bird fly 256 | A bird sleep -------------------------------------------------------------------------------- /2019/Word2vec/tf_word2vec.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | from __future__ import print_function 4 | 5 | import collections 6 | import math 7 | import os 8 | import random 9 | 10 | import numpy as np 11 | from six.moves import urllib 12 | from six.moves import xrange # pylint: disable=redefined-builtin 13 | import tensorflow as tf 14 | 15 | 16 | # Step 1: Read the data into a list of strings. 17 | def read_data(filename): 18 | """Extract the first file enclosed in a zip file as a list of words.""" 19 | with open(filename, 'r', encoding='utf-8') as f: 20 | data = tf.compat.as_str(f.read()).split() 21 | return data 22 | 23 | vocabulary = read_data('./question_jieba.txt') 24 | print('Data size', len(vocabulary)) 25 | 26 | # Step 2: Build the dictionary and replace rare words with UNK token. 27 | vocabulary_size = 200 28 | 29 | 30 | def build_dataset(words, n_words): 31 | """Process raw inputs into a dataset.""" 32 | count = [['UNK', -1]] 33 | count.extend(collections.Counter(words).most_common(n_words - 1)) 34 | dictionary = dict() 35 | for word, _ in count: 36 | dictionary[word] = len(dictionary) 37 | data = list() 38 | unk_count = 0 39 | for word in words: 40 | if word in dictionary: 41 | index = dictionary[word] 42 | else: 43 | index = 0 # dictionary['UNK'] 44 | unk_count += 1 45 | data.append(index) 46 | count[0][1] = unk_count 47 | reversed_dictionary = dict(zip(dictionary.values(), dictionary.keys())) 48 | return data, count, dictionary, reversed_dictionary 49 | 50 | data, count, dictionary, reverse_dictionary = build_dataset(vocabulary, 51 | vocabulary_size) 52 | del vocabulary # Hint to reduce memory. 53 | print('Most common words (+UNK)', count[:5]) 54 | print('Sample data', data[:10], [reverse_dictionary[i] for i in data[:10]]) 55 | 56 | data_index = 0 57 | 58 | # Step 3: Function to generate a training batch for the skip-gram model. 59 | def generate_batch(batch_size, num_skips, skip_window): 60 | global data_index 61 | assert batch_size % num_skips == 0 62 | assert num_skips <= 2 * skip_window 63 | batch = np.ndarray(shape=(batch_size), dtype=np.int32) 64 | labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32) 65 | span = 2 * skip_window + 1 # [ skip_window target skip_window ] 66 | buffer = collections.deque(maxlen=span) 67 | if data_index + span > len(data): 68 | data_index = 0 69 | buffer.extend(data[data_index:data_index + span]) 70 | data_index += span 71 | for i in range(batch_size // num_skips): 72 | target = skip_window # target label at the center of the buffer 73 | targets_to_avoid = [skip_window] 74 | for j in range(num_skips): 75 | while target in targets_to_avoid: 76 | target = random.randint(0, span - 1) 77 | targets_to_avoid.append(target) 78 | batch[i * num_skips + j] = buffer[skip_window] 79 | labels[i * num_skips + j, 0] = buffer[target] 80 | if data_index == len(data): 81 | for word in data[:span]: 82 | buffer.append(word) 83 | data_index = span 84 | else: 85 | buffer.append(data[data_index]) 86 | data_index += 1 87 | # Backtrack a little bit to avoid skipping words in the end of a batch 88 | data_index = (data_index + len(data) - span) % len(data) 89 | return batch, labels 90 | 91 | batch, labels = generate_batch(batch_size=8, num_skips=2, skip_window=1) 92 | for i in range(8): 93 | print(batch[i], reverse_dictionary[batch[i]], 94 | '->', labels[i, 0], reverse_dictionary[labels[i, 0]]) 95 | 96 | # Step 4: Build and train a skip-gram model. 97 | 98 | batch_size = 128 99 | embedding_size = 128 # Dimension of the embedding vector. 100 | skip_window = 1 # How many words to consider left and right. 101 | num_skips = 2 # How many times to reuse an input to generate a label. 102 | 103 | # We pick a random validation set to sample nearest neighbors. Here we limit the 104 | # validation samples to the words that have a low numeric ID, which by 105 | # construction are also the most frequent. 106 | valid_size = 16 # Random set of words to evaluate similarity on. 107 | valid_window = 100 # Only pick dev samples in the head of the distribution. 108 | valid_examples = np.random.choice(valid_window, valid_size, replace=False) 109 | num_sampled = 64 # Number of negative examples to sample. 110 | 111 | graph = tf.Graph() 112 | 113 | with graph.as_default(): 114 | 115 | # Input data. 116 | train_inputs = tf.placeholder(tf.int32, shape=[batch_size]) 117 | train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1]) 118 | valid_dataset = tf.constant(valid_examples, dtype=tf.int32) 119 | 120 | # Ops and variables pinned to the CPU because of missing GPU implementation 121 | with tf.device('/cpu:0'): 122 | # Look up embeddings for inputs. 123 | embeddings = tf.Variable( 124 | tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) 125 | embed = tf.nn.embedding_lookup(embeddings, train_inputs) 126 | 127 | # Construct the variables for the NCE loss 128 | nce_weights = tf.Variable( 129 | tf.truncated_normal([vocabulary_size, embedding_size], 130 | stddev=1.0 / math.sqrt(embedding_size))) 131 | nce_biases = tf.Variable(tf.zeros([vocabulary_size])) 132 | 133 | # Compute the average NCE loss for the batch. 134 | # tf.nce_loss automatically draws a new sample of the negative labels each 135 | # time we evaluate the loss. 136 | loss = tf.reduce_mean( 137 | tf.nn.nce_loss(weights=nce_weights, 138 | biases=nce_biases, 139 | labels=train_labels, 140 | inputs=embed, 141 | num_sampled=num_sampled, 142 | num_classes=vocabulary_size)) 143 | 144 | # Construct the SGD optimizer using a learning rate of 1.0. 145 | optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss) 146 | 147 | # Compute the cosine similarity between minibatch examples and all embeddings. 148 | norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True)) 149 | normalized_embeddings = embeddings / norm 150 | valid_embeddings = tf.nn.embedding_lookup( 151 | normalized_embeddings, valid_dataset) 152 | similarity = tf.matmul( 153 | valid_embeddings, normalized_embeddings, transpose_b=True) 154 | 155 | # Add variable initializer. 156 | init = tf.global_variables_initializer() 157 | 158 | # Step 5: Begin training. 159 | num_steps = 100001 160 | 161 | with tf.Session(graph=graph) as session: 162 | # We must initialize all variables before we use them. 163 | init.run() 164 | print('Initialized') 165 | 166 | average_loss = 0 167 | for step in xrange(num_steps): 168 | batch_inputs, batch_labels = generate_batch( 169 | batch_size, num_skips, skip_window) 170 | feed_dict = {train_inputs: batch_inputs, train_labels: batch_labels} 171 | 172 | # We perform one update step by evaluating the optimizer op (including it 173 | # in the list of returned values for session.run() 174 | _, loss_val = session.run([optimizer, loss], feed_dict=feed_dict) 175 | average_loss += loss_val 176 | 177 | if step % 2000 == 0: 178 | if step > 0: 179 | average_loss /= 2000 180 | # The average loss is an estimate of the loss over the last 2000 batches. 181 | print('Average loss at step ', step, ': ', average_loss) 182 | average_loss = 0 183 | 184 | # Note that this is expensive (~20% slowdown if computed every 500 steps) 185 | if step % 10000 == 0: 186 | sim = similarity.eval() 187 | for i in xrange(valid_size): 188 | valid_word = reverse_dictionary[valid_examples[i]] 189 | top_k = 8 # number of nearest neighbors 190 | nearest = (-sim[i, :]).argsort()[1:top_k + 1] 191 | log_str = 'Nearest to %s:' % valid_word 192 | for k in xrange(top_k): 193 | close_word = reverse_dictionary[nearest[k]] 194 | log_str = '%s %s,' % (log_str, close_word) 195 | print(log_str) 196 | final_embeddings = normalized_embeddings.eval() 197 | 198 | # Step 6: Visualize the embeddings. 199 | def plot_with_labels(low_dim_embs, labels, filename='tsne.png'): 200 | #plt.rcParams["font.family"] = 'MicrosoftJhengHeiRegular' 201 | assert low_dim_embs.shape[0] >= len(labels), 'More labels than embeddings' 202 | plt.figure(figsize=(18, 18)) # in inches 203 | for i, label in enumerate(labels): 204 | x, y = low_dim_embs[i, :] 205 | plt.scatter(x, y) 206 | plt.annotate(label, 207 | xy=(x, y), 208 | xytext=(5, 2), 209 | textcoords='offset points', 210 | ha='right', 211 | va='bottom') 212 | 213 | plt.savefig(filename) 214 | 215 | try: 216 | # pylint: disable=g-import-not-at-top 217 | from sklearn.manifold import TSNE 218 | import matplotlib.pyplot as plt 219 | 220 | tsne = TSNE(perplexity=30, n_components=2, init='pca', n_iter=5000) 221 | plot_only = 100 222 | low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only, :]) 223 | labels = [reverse_dictionary[i] for i in xrange(plot_only)] 224 | plot_with_labels(low_dim_embs, labels) 225 | 226 | except ImportError: 227 | print('Please install sklearn, matplotlib, and scipy to show embeddings.') 228 | 229 | -------------------------------------------------------------------------------- /2019/Word2vec/tsne.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jerrywu2013/Python/dbb2011fd21adc40777f6aaa65473aeb371fe8f3/2019/Word2vec/tsne.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python tutorial in ubuntu 2 | ========== 3 | ####Installation and quick start 4 | Win: http://continuum.io/downloads#py34 5 | ####How do I install additional packages into Anaconda? 6 | ``` 7 | conda install pymysql 8 | binstar search -t conda psycopg2 9 | conda install binstar 10 | ```` 11 | #####https://binstar.org/search?q=psycopg2 12 | ``` 13 | conda install -c https://conda.binstar.org/jonrowland psycopg2 14 | ``` 15 | ``` 16 | sudo apt-get install python3-all 17 | whereis python3 18 | alias python=python3 19 | apt-get -y install python-pip 20 | pip install "ipython[notebook]" 21 | pip install --upgrade ipython[notebook] 22 | 23 | ``` 24 | 25 | #### Reference 26 | https://iampennywu.gitbooks.io/introducing-python/content/ 27 | 28 | https://github.com/HuskyHsu/Introducing-Python 29 | 30 | ####Python with Tensorflow 31 | 32 | 1.CNN_for_textile 33 | 34 | 2.Chinese Visual understanding 35 | 36 | 3.GPU_TF 37 | 38 | 4.Sentiment analysis 39 | 40 | --------------------------------------------------------------------------------