Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
deeplearningzerotoall
GitHub Repository: deeplearningzerotoall/PyTorch
Path: blob/master/RNN/4-longseq.ipynb
618 views
Kernel: Python 3
import torch import torch.optim as optim import numpy as np
# Random seed to make results deterministic and reproducible torch.manual_seed(0)
<torch._C.Generator at 0x116cfab50>
sentence = ("if you want to build a ship, don't drum up people together to " "collect wood and don't assign them tasks and work, but rather " "teach them to long for the endless immensity of the sea.")
# make dictionary char_set = list(set(sentence)) char_dic = {c: i for i, c in enumerate(char_set)}
# hyper parameters dic_size = len(char_dic) hidden_size = len(char_dic) sequence_length = 10 # Any arbitrary number learning_rate = 0.1
# data setting x_data = [] y_data = [] for i in range(0, len(sentence) - sequence_length): x_str = sentence[i:i + sequence_length] y_str = sentence[i + 1: i + sequence_length + 1] print(i, x_str, '->', y_str) x_data.append([char_dic[c] for c in x_str]) # x str to index y_data.append([char_dic[c] for c in y_str]) # y str to index x_one_hot = [np.eye(dic_size)[x] for x in x_data]
0 if you wan -> f you want 1 f you want -> you want 2 you want -> you want t 3 you want t -> ou want to 4 ou want to -> u want to 5 u want to -> want to b 6 want to b -> want to bu 7 want to bu -> ant to bui 8 ant to bui -> nt to buil 9 nt to buil -> t to build 10 t to build -> to build 11 to build -> to build a 12 to build a -> o build a 13 o build a -> build a s 14 build a s -> build a sh 15 build a sh -> uild a shi 16 uild a shi -> ild a ship 17 ild a ship -> ld a ship, 18 ld a ship, -> d a ship, 19 d a ship, -> a ship, d 20 a ship, d -> a ship, do 21 a ship, do -> ship, don 22 ship, don -> ship, don' 23 ship, don' -> hip, don't 24 hip, don't -> ip, don't 25 ip, don't -> p, don't d 26 p, don't d -> , don't dr 27 , don't dr -> don't dru 28 don't dru -> don't drum 29 don't drum -> on't drum 30 on't drum -> n't drum u 31 n't drum u -> 't drum up 32 't drum up -> t drum up 33 t drum up -> drum up p 34 drum up p -> drum up pe 35 drum up pe -> rum up peo 36 rum up peo -> um up peop 37 um up peop -> m up peopl 38 m up peopl -> up people 39 up people -> up people 40 up people -> p people t 41 p people t -> people to 42 people to -> people tog 43 people tog -> eople toge 44 eople toge -> ople toget 45 ople toget -> ple togeth 46 ple togeth -> le togethe 47 le togethe -> e together 48 e together -> together 49 together -> together t 50 together t -> ogether to 51 ogether to -> gether to 52 gether to -> ether to c 53 ether to c -> ther to co 54 ther to co -> her to col 55 her to col -> er to coll 56 er to coll -> r to colle 57 r to colle -> to collec 58 to collec -> to collect 59 to collect -> o collect 60 o collect -> collect w 61 collect w -> collect wo 62 collect wo -> ollect woo 63 ollect woo -> llect wood 64 llect wood -> lect wood 65 lect wood -> ect wood a 66 ect wood a -> ct wood an 67 ct wood an -> t wood and 68 t wood and -> wood and 69 wood and -> wood and d 70 wood and d -> ood and do 71 ood and do -> od and don 72 od and don -> d and don' 73 d and don' -> and don't 74 and don't -> and don't 75 and don't -> nd don't a 76 nd don't a -> d don't as 77 d don't as -> don't ass 78 don't ass -> don't assi 79 don't assi -> on't assig 80 on't assig -> n't assign 81 n't assign -> 't assign 82 't assign -> t assign t 83 t assign t -> assign th 84 assign th -> assign the 85 assign the -> ssign them 86 ssign them -> sign them 87 sign them -> ign them t 88 ign them t -> gn them ta 89 gn them ta -> n them tas 90 n them tas -> them task 91 them task -> them tasks 92 them tasks -> hem tasks 93 hem tasks -> em tasks a 94 em tasks a -> m tasks an 95 m tasks an -> tasks and 96 tasks and -> tasks and 97 tasks and -> asks and w 98 asks and w -> sks and wo 99 sks and wo -> ks and wor 100 ks and wor -> s and work 101 s and work -> and work, 102 and work, -> and work, 103 and work, -> nd work, b 104 nd work, b -> d work, bu 105 d work, bu -> work, but 106 work, but -> work, but 107 work, but -> ork, but r 108 ork, but r -> rk, but ra 109 rk, but ra -> k, but rat 110 k, but rat -> , but rath 111 , but rath -> but rathe 112 but rathe -> but rather 113 but rather -> ut rather 114 ut rather -> t rather t 115 t rather t -> rather te 116 rather te -> rather tea 117 rather tea -> ather teac 118 ather teac -> ther teach 119 ther teach -> her teach 120 her teach -> er teach t 121 er teach t -> r teach th 122 r teach th -> teach the 123 teach the -> teach them 124 teach them -> each them 125 each them -> ach them t 126 ach them t -> ch them to 127 ch them to -> h them to 128 h them to -> them to l 129 them to l -> them to lo 130 them to lo -> hem to lon 131 hem to lon -> em to long 132 em to long -> m to long 133 m to long -> to long f 134 to long f -> to long fo 135 to long fo -> o long for 136 o long for -> long for 137 long for -> long for t 138 long for t -> ong for th 139 ong for th -> ng for the 140 ng for the -> g for the 141 g for the -> for the e 142 for the e -> for the en 143 for the en -> or the end 144 or the end -> r the endl 145 r the endl -> the endle 146 the endle -> the endles 147 the endles -> he endless 148 he endless -> e endless 149 e endless -> endless i 150 endless i -> endless im 151 endless im -> ndless imm 152 ndless imm -> dless imme 153 dless imme -> less immen 154 less immen -> ess immens 155 ess immens -> ss immensi 156 ss immensi -> s immensit 157 s immensit -> immensity 158 immensity -> immensity 159 immensity -> mmensity o 160 mmensity o -> mensity of 161 mensity of -> ensity of 162 ensity of -> nsity of t 163 nsity of t -> sity of th 164 sity of th -> ity of the 165 ity of the -> ty of the 166 ty of the -> y of the s 167 y of the s -> of the se 168 of the se -> of the sea 169 of the sea -> f the sea.
# transform as torch tensor variable X = torch.FloatTensor(x_one_hot) Y = torch.LongTensor(y_data)
# declare RNN + FC class Net(torch.nn.Module): def __init__(self, input_dim, hidden_dim, layers): super(Net, self).__init__() self.rnn = torch.nn.RNN(input_dim, hidden_dim, num_layers=layers, batch_first=True) self.fc = torch.nn.Linear(hidden_dim, hidden_dim, bias=True) def forward(self, x): x, _status = self.rnn(x) x = self.fc(x) return x
# declare RNN + FC class Net(torch.nn.Module): def __init__(self, input_dim, hidden_dim, layers): super(Net, self).__init__() self.rnn = torch.nn.RNN(input_dim, hidden_dim, num_layers=layers, batch_first=True) self.fc = torch.nn.Linear(hidden_dim, hidden_dim, bias=True) def forward(self, x): x, _status = self.rnn(x) x = self.fc(x) return x net = Net(dic_size, hidden_size, 2)
# loss & optimizer setting criterion = torch.nn.CrossEntropyLoss() optimizer = optim.Adam(net.parameters(), learning_rate)
# start training for i in range(100): optimizer.zero_grad() outputs = net(X) loss = criterion(outputs.view(-1, dic_size), Y.view(-1)) loss.backward() optimizer.step() results = outputs.argmax(dim=2) predict_str = "" for j, result in enumerate(results): # print(i, j, ''.join([char_set[t] for t in result]), loss.item()) if j == 0: predict_str += ''.join([char_set[t] for t in result]) else: predict_str += char_set[result[-1]] print(predict_str)
f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. p you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't dssign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. p you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. f you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. l you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. t you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. t you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea. m you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.