diff --git a/ST_LSTM.py b/ST_LSTM.py index 5e9617e..adafa78 100644 --- a/ST_LSTM.py +++ b/ST_LSTM.py @@ -9,6 +9,8 @@ import math +DIM_value = "" #? + class SpatioTemporal_LSTM(nn.Module): """docstring for SpatioTemporal_LSTM""" def __init__(self, hidden_size, input_size): @@ -70,7 +72,7 @@ def _compute_cell(self, x, h, c, M): o = torch.sigmoid(F.conv2d(x, self.weight_xo) + F.conv2d(M, self.weight_mo) + F.conv2d(c, self.weight_co) + F.conv2d(h, self.weight_ho) + self.bias_o) - h = o * torch.tanh(F.conv2d(torch.cat((c,M), dim= ),self.weight_1x1)) + h = o * torch.tanh(F.conv2d(torch.cat((c,M), dim=DIM_value ),self.weight_1x1)) return h,c,M diff --git a/data_generator.py b/data_generator.py index 33123dd..2f94b68 100644 --- a/data_generator.py +++ b/data_generator.py @@ -21,7 +21,7 @@ def __init__(self, data_pb): try: f = h5py.File('/home/ujjax/Downloads/mnist.h5') except: - print 'Please set the correct path to MNIST dataset' + print('Please set the correct path to MNIST dataset') sys.exit() self.data_ = f['train'].value.reshape(-1, 28, 28) @@ -152,7 +152,7 @@ def DisplayData(self, data, rec=None, fut=None, fig=1, case_id=0, output_file=No plt.axis('off') plt.draw() if output_file1 is not None: - print output_file1 + print(output_file1) plt.savefig(output_file1, bbox_inches='tight') # create figure for reconstuction and future sequences @@ -168,7 +168,7 @@ def DisplayData(self, data, rec=None, fut=None, fig=1, case_id=0, output_file=No plt.axis('off') plt.draw() if output_file2 is not None: - print output_file2 + print(output_file2) plt.savefig(output_file2, bbox_inches='tight') else: plt.pause(0.1) diff --git a/main.py b/main.py index df46e96..d9c369b 100644 --- a/main.py +++ b/main.py @@ -21,20 +21,20 @@ def __init__(self): self.H = dict() self.h = dict() - self.c = dict() - self.m = { 0 : Parameter(torch.Tensor())} + self.c = dict() + self.m = { 0 : Parameter(torch.Tensor())} - self.h[0] = Parameter(torch.Tensor(shape)) - self.h[1] = Parameter(torch.Tensor(shape)) - self.h[2] = Parameter(torch.Tensor(shape)) - self.h[3] = Parameter(torch.Tensor(shape)) + self.h[0] = Parameter(torch.Tensor(shape)) + self.h[1] = Parameter(torch.Tensor(shape)) + self.h[2] = Parameter(torch.Tensor(shape)) + self.h[3] = Parameter(torch.Tensor(shape)) - self.c[0] = Parameter(torch.Tensor(shape)) - self.c[1] = Parameter(torch.Tensor(shape)) - self.c[2] = Parameter(torch.Tensor(shape)) - self.c[3] = Parameter(torch.Tensor(shape)) + self.c[0] = Parameter(torch.Tensor(shape)) + self.c[1] = Parameter(torch.Tensor(shape)) + self.c[2] = Parameter(torch.Tensor(shape)) + self.c[3] = Parameter(torch.Tensor(shape)) self.cells = nn.ModuleList([]) for i in self.n_layers: @@ -44,9 +44,9 @@ def __init__(self): self._reset_parameters() def _reset_parameters(self): - stdv = 1.0 / math.sqrt(self.hidden_size) - for weight in self.parameters(): - weight.data.uniform_(-stdv, stdv) + stdv = 1.0 / math.sqrt(self.hidden_size) + for weight in self.parameters(): + weight.data.uniform_(-stdv, stdv) def forward(self, input_, first_timestep = False): @@ -68,11 +68,11 @@ def forward(self, input_, first_timestep = False): return self.H , self. C, self.M def initHidden(self): - result = Variable(torch.zeros(1, 1, self.hidden_size)) #################SHAPE - if use_cuda: - return result.cuda() - else: - return result + result = Variable(torch.zeros(1, 1, self.hidden_size)) #################SHAPE + if use_cuda: + return result.cuda() + else: + return result class Decoder(nn.Module): @@ -105,8 +105,8 @@ def forward(self, input_, C,H,M): return output def initHidden(self): - result = Variable(torch.zeros(1, 1, self.hidden_size)) - if use_cuda: - return result.cuda() - else: - return result + result = Variable(torch.zeros(1, 1, self.hidden_size)) + if use_cuda: + return result.cuda() + else: + return result diff --git a/train.py b/train.py index a3d6bc0..6c55927 100644 --- a/train.py +++ b/train.py @@ -15,7 +15,8 @@ SOS_token = 0 EOS_token = 1 - +FILE_PATH = '' #¿? +MAX_LENGTH = '' #¿?¿? def showPlot(points): plt.figure() @@ -41,9 +42,9 @@ def timeSince(since, percent): def train(input_variable, target_variable, encoder, decoder, encoder_optimizer, decoder_optimizer, criterion, max_length=MAX_LENGTH,teacher_forcing_ratio = 0.5): - encoder_hidden = encoder.initHidden() + encoder_hidden = encoder.initHidden() - encoder_optimizer.zero_grad() + encoder_optimizer.zero_grad() decoder_optimizer.zero_grad() input_length = input_variable.size()[0] @@ -76,10 +77,10 @@ def train(input_variable, target_variable, encoder, decoder, encoder_optimizer, else: for di in range(target_length): - decoder_output, decoder_hidden, decoder_attention = decoder( + decoder_output, decoder_hidden, decoder_attention = decoder( decoder_input, decoder_hidden, encoder_outputs) - topv, topi = decoder_output.data.topk(1) + topv, topi = decoder_output.data.topk(1) ni = topi[0][0] decoder_input = Variable(torch.LongTensor([[ni]])) @@ -98,10 +99,10 @@ def train(input_variable, target_variable, encoder, decoder, encoder_optimizer, def trainIters(encoder, decoder, n_iters,data_generator, print_every=1000, plot_every=100, learning_rate=0.01): - start_time = time.time() + start_time = time.time() - encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate) + encoder_optimizer = optim.SGD(encoder.parameters(), lr=learning_rate) decoder_optimizer = optim.SGD(decoder.parameters(), lr=learning_rate) training_pairs = [variablesFromPair(random.choice(pairs)) @@ -112,7 +113,7 @@ def trainIters(encoder, decoder, n_iters,data_generator, print_every=1000, plot_ for it in range(1, n_iters+1): - training_pair = next(data_generator) + training_pair = next(data_generator) input_variable = training_pair[0] target_variable = training_pair[1] @@ -176,23 +177,23 @@ def evaluate(encoder, decoder, sentence, max_length=MAX_LENGTH): def main(_): - batch_size = 128 - file_path = ##### + batch_size = 128 + file_path = FILE_PATH - data = prepare_data.load_data(file_path) + data = prepare_data.load_data(file_path) - data_generator = prepare_data.get_batches(data, batch_size) + data_generator = prepare_data.get_batches(data, batch_size) - hidden_size = 256 - encoder1 = Encoder(input_lang.n_words, hidden_size) - decoder1 = Decoder(hidden_size, output_lang.n_words, dropout_p=0.1) + hidden_size = 256 + encoder1 = Encoder(input_lang.n_words, hidden_size) + decoder1 = Decoder(hidden_size, output_lang.n_words, dropout_p=0.1) - if use_cuda: - encoder1 = encoder1.cuda() - attn_decoder1 = decoder1.cuda() + if use_cuda: + encoder1 = encoder1.cuda() + attn_decoder1 = decoder1.cuda() - trainIters(encoder1, decoder1, 75000, print_every=5000) + trainIters(encoder1, decoder1, 75000, print_every=5000) if __name__ == '__main__':