Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Sat Sep 28 22:57:42 2019
- @author: jeremiah
- """
- #%%
- import torch as t
- from torch import nn
- import numpy as np
- import pandas as pd
- import matplotlib.pyplot as plt
- %matplotlib inline
- #%% reading the data
- df = pd.read_csv('/home/jeremiah/data_analysis/datasets/time-series-pam/a1_temperatura.csv', index_col = 0)
- df.head()
- uba_data = np.zeros(len(df['Ubatuba']))
- uba_data = df['Ubatuba'].values
- #%%
- train_data, val_data = uba_data[:100], uba_data[100:]
- x_train, y_train = train_data[:-1], train_data[1:]
- x_val, y_val = val_data[:-1], val_data[1:]
- plt.plot(x_train, 'r')
- plt.plot(y_train, 'b')
- #%%
- class RNN(nn.Module):
- def __init__(self, input_size, hidden_size, n_layers, dropout_p = 0.5):
- super().__init__()
- self.hidden_size = hidden_size
- self.lstm = nn.LSTM(input_size, self.hidden_size, n_layers, batch_first = True)
- self.fc = nn.Linear(self.hidden_size, 1)
- self.dropout = nn.Dropout(dropout_p)
- def forward(self, x, h):
- out, h = self.lstm(x, h)
- out = self.dropout(out)
- out = out.view(-1, self.hidden_size)
- out = self.fc(out)
- return out, h
- #%%
- input_size = 1
- hidden_size = 16
- n_layers = 1
- net = RNN(input_size, hidden_size, n_layers)
- print(net)
- #%%
- from torch import optim
- criterion = nn.MSELoss()
- op = optim.Adam(net.parameters(), lr=0.001)
- #%%
- def train(net, steps, print_every, x, y):
- weight = next(net.parameters()).data
- hidden = (weight.new(1, 1, 16).zero_(), weight.new(1, 1, 16).zero_())
- for s in range(steps):
- x = t.Tensor(x)
- x = x.view(1, 99, 1)
- y = t.Tensor(y)
- hidden = tuple([each.data for each in hidden])
- print(x.shape)
- out, hidden = net(x, hidden)
- y = y.view(*out.shape)
- print(out.shape)
- print(y.shape)
- loss = criterion(out, y)
- op.zero_grad()
- loss.backward()
- op.step()
- if s % print_every == 0:
- plt.plot(x.view(-1), 'r')
- plt.plot(out.data, 'g')
- plt.plot(y, 'b')
- plt.show()
- return net
- #%%
- steps = 5
- print_every = 1
- net = train(net, steps, print_every, x_train, y_train)
Advertisement
Add Comment
Please, Sign In to add comment