Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mostly syntax problems #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ST_LSTM.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import math

DIM_value = "" #?

class SpatioTemporal_LSTM(nn.Module):
"""docstring for SpatioTemporal_LSTM"""
def __init__(self, hidden_size, input_size):
Expand Down Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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)
46 changes: 23 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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
39 changes: 20 additions & 19 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

SOS_token = 0
EOS_token = 1

FILE_PATH = '' #¿?
MAX_LENGTH = '' #¿?¿?

def showPlot(points):
plt.figure()
Expand All @@ -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]
Expand Down Expand Up @@ -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]]))
Expand All @@ -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))
Expand All @@ -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]

Expand Down Expand Up @@ -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__':
Expand Down