<jupyter_start><jupyter_text>prepare training and testing data<jupyter_code>def split_and_clean(sample_series,train_start,train_end,test_len):
    clean_series=[]
    test_series=[]
    for index in range(len(sample_series)):
        c=np.array(sample_series[index][train_start:train_end])
        test_series.append(sample_series[index][train_end:(train_end+test_len)])
        std=np.std(c)
        mean=np.mean(c)
        c[(c-np.mean(c))>2*std]=mean+2*std
        c[(c-np.mean(c))<-2*std]=mean-2*std
        clean_series.append(c)
    return (clean_series,test_series)
# plt.plot(clean_series[1])
def value2id(series,bin_size,biased_mean,std):
    id_series=np.array(list(map(lambda x:(x-biased_mean) // (std*bin_size),series)))
    id_series[id_series>(6/bin_size)]=(6/bin_size)
    id_series[id_series<0]=0
    return id_series
def id2value(id_series,bin_size,biased_mean,std):
    return id_series*(std*bin_size)+biased_mean
def discrify(clean_series):
    bin_size=0.005
    discrete_dic={}
    index_series=[]
    for i in range(len(clean_series)):
        if np.sum(clean_series[i])==0:
            std=-1
            biased_mean=-1
            discrete_dic[i]=(std,biased_mean)
            index_series.append(np.zeros_like(clean_series[i])+3/bin_size)
        else:
            std=np.std(clean_series[i])
            biased_mean=np.mean(clean_series[i])-3*std

            discrete_dic[i]=(std,biased_mean)
            index_series.append(value2id(clean_series[i],bin_size=bin_size,biased_mean=biased_mean,std=std))
    return (discrete_dic,index_series)
encode_start=0
encode_end=150
decode_len=63

clean_series,decode_series = split_and_clean(sample_series,encode_start,encode_end,decode_len)


train_x=clean_series
train_y=decode_series

encode_start=200
encode_end=350
decode_len=63


clean_series,decode_series = split_and_clean(sample_series,encode_start,encode_end,decode_len)

test_x=clean_series
test_y=decode_series
# for i in decode_series:
#     if sum(i[i>1199])>0:
#         print(i)
# changes=[]
# for series in range(len(sample_series)):
#     change=diff(sample_series[series][1:-2])
# #     change[change>2]=2
#     changes.append(change)
# changes=np.array(changes)
# changes.shape
# def num2index(num):
#     return int((num+1) // 0.0025)
# index
# def index2num(index):
#     return -1+(index+0.5)*0.0025

# index2num(1199)


# #discrete change
# dis_change=[]
# for i in range(len(changes)):
#     dis_change.append(list(map(lambda x:num2index(x),changes[i,:])))
# dis_change=np.array(dis_change)
# assert dis_change.shape==changes.shape
# train_x=dis_change[:,265:365]
# train_y=changes[:,365:365+63]
# test_x=dis_change[:,400:500]
# test_y=changes[:,500:500+63]
def X_loader(x,batchsize=10):
    batch=0
    x=np.array(x)
    while batch<(len(x) // batchsize):
        
        data=x[batch*batchsize:(batch+1)*batchsize,:]
        tensor=torch.FloatTensor(np.array(data, dtype=float))
        tensor=tensor.unsqueeze(2).cuda()
#         tensor=tensor.repeat(1,1,25)
        yield(tensor)
        batch+=1
def Y_loader(y,batchsize=10):
    batch=0
    y=np.array(y)
    while batch<(len(y) // batchsize):
        
        data=y[batch*batchsize:(batch+1)*batchsize,:]
        tensor=torch.FloatTensor(np.array(data, dtype=float))
        tensor=tensor.unsqueeze(2).cuda()

        yield(tensor)
        batch+=1
trainx=X_loader(train_x)
a=next(trainx)
a.shape
a.repeat(1,1,25).shape
import torch
import torch.nn as nn

class NN(nn.Module):
    def __init__(self, encoder_dim=150,out_dim=63):
        super(NN, self).__init__()

        self.encoder = nn.GRU(25, encoder_dim, batch_first=True, bidirectional=False)
        self.decoder = nn.Linear(encoder_dim, out_dim)
    def forward(self, x):
            
#         print('embedding',out)
        output, hidden = self.encoder(x)
#         print('hidden',hidden)
        output = self.decoder(hidden)
        return output.squeeze()
model=NN(encoder_dim=25,out_dim=10).cuda()
# for name, param in model.named_parameters():
#     if param.requires_grad:
#         print (name, param.data)
def SMAPE(true,predicted):
    true=true.squeeze()
    predicted=predicted.squeeze()
    epsilon = 0.1
    summ = torch.abs(true) + torch.abs(predicted) + epsilon
    smape = torch.abs(predicted - true) / summ * 2.0
    return torch.mean(smape)
from torch import optim
epoch=30
indicator=200

model=NN(encoder_dim=25,out_dim=10).cuda()
optimizer = optim.RMSprop(model.parameters(), lr=0.001, alpha=0.99, \
    eps=1e-08, weight_decay=0, momentum=0, centered=False)
# optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0)

for i in range(epoch):
    trainx=X_loader(train_x)
    trainy=Y_loader(train_y)
    running_loss = 0.0
    validation(test_x,test_y)
    for j,X_batch in enumerate(trainx):
        #Parse loaded batch
        
        model.train()

        optimizer.zero_grad()
        output = model(X_batch)
        

        target=next(trainy)
        loss = SMAPE(target,output)

        loss.backward()
#         torch.nn.utils.clip_grad_norm_(model.parameters(), 0.25)
        optimizer.step()

        running_loss += loss.item()

        if (j>0) and (j % indicator == 0):
            print("Epoch: {}; iterations: {}; Loss: {}\n".format(i, j, running_loss / indicator))
            running_loss = 0.0
#             for name, param in model.named_parameters():
#                 if param.requires_grad:
#                     print (name, param.data,param.grad.data)
#             print(output)

def validation(test_x,test_y):
    testx=X_loader(test_x)
    testy=Y_loader(test_y)
    running_loss = 0.0
    for j,X_batch in enumerate(testx):
        #Parse loaded batch
        
        model.eval()
        output = model(X_batch)
#         MSE = nn.MSELoss()
        target=next(testy)
        loss = SMAPE(target,output)
        running_loss += loss.item()
        print(j,end='\r')
        if j >500:
            break
    print("VALIDATION: Epoch: {}; Loss: {}\n".format(i, running_loss / j))

testx=X_loader(test_x)
testy=Y_loader(test_y)
running_loss = 0.0

preds=[]
targets=[]

scores=[]
for j,X_batch in enumerate(testx):
    #Parse loaded batch

    model.eval()
    output = model(X_batch)

    target=next(testy)
    loss = SMAPE(target,output)
    scores.append(loss.item())
    out=output.detach().cpu().numpy()
    target=target.detach().cpu().numpy()
    if j ==0:
        preds=out
        targets=target
    else:
        preds=np.concatenate((preds,out),axis=0)
        targets=np.concatenate((targets,target),axis=0)
testy=Y_loader(test_y)
b=next(testy)
b.shape
# b
testx=X_loader(test_x)
a=next(testx)
model(a*0.1)
a.shape
np.mean(scores)
len(preds)
# def recover(start,change):
#     series=[]
#     last=start
#     for c in change:
#         last=last*(1+c)
#         series.append(last)
#     return series
# def recover(ID,series,bin_size):
#     std,biased_mean=discrete_dic[ID]
#     series=series*(std*bin_size)+biased_mean
#     return series
preds
ID=200
source=sample_series[ID][encode_start:encode_end]
pred=preds[ID]
truth=targets[ID]
print(pred)
# for i in range(10):
plt.figure()
plt.plot(np.arange(encode_start,encode_end),source,color='g',label='history')
plt.plot(np.arange(encode_end,encode_end+decode_len),pred,color='r',label='prediction')
plt.plot(np.arange(encode_end,encode_end+decode_len),truth,color='b',label='truth')
plt.legend()
a=next(trainx)
a[:,2,:].shape
a.shape
from util.evaluation import SMAPE
mean_scores=[]
for ID in range(4000):
    source=sample_series[ID][encode_start:encode_end]
    pred=preds[ID]
    truth=targets[ID]
    scores=[]
    for (t,p) in zip(truth,pred):
        scores.append(SMAPE(t,p))

    mean_scores.append(np.mean(scores))
np.mean(mean_scores)
from torch import optim
epoch=30
indicator=20

# optimizer = optim.RMSprop(model.parameters(), lr=0.001, alpha=0.99, \
#     eps=1e-08, weight_decay=0, momentum=0, centered=False)
# optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0)
hidden_size=10
lr=0.01


encoder=EncoderRNN(1,hidden_size).cuda() if use_cuda else EncoderRNN(1,hidden_size)
decoder=DecoderRNN(1,hidden_size).cuda() if use_cuda else DecoderRNN(1,hidden_size)
teacher_forcing_ratio = 1
encoder_optimizer = optim.SGD(encoder.parameters(), lr=lr)
decoder_optimizer = optim.SGD(decoder.parameters(), lr=lr)

for i in range(epoch):
    trainx=X_loader(train_x)
    trainy=Y_loader(train_y)
    running_loss = 0.0
#     validation(test_x,test_y)
    for j,X_batch in enumerate(trainx):
        #Parse loaded batch
        
        x=next(trainx)
        y=next(trainy)
        loss=train(x, y, encoder, decoder, encoder_optimizer, \
                  decoder_optimizer, SMAPE)
        running_loss += loss

        if (j>0) and (j % indicator == 0):
            print("Epoch: {}; iterations: {}; Loss: {}\n".format(i, j, running_loss / indicator))
            running_loss = 0.0
trainx=X_loader(train_x)
trainy=Y_loader(train_y)

from numpy import random as random
use_cuda=True
class EncoderRNN(nn.Module):
    def __init__(self, input_size, hidden_size):
        super(EncoderRNN, self).__init__()

        self.hidden_size = hidden_size
        self.gru = nn.GRU(1, hidden_size)

    def forward(self, input, hidden):

        output, hidden = self.gru(input, hidden)
        return output, hidden

    def initHidden(self):
        result = torch.zeros(1, 1, self.hidden_size)
        if use_cuda:
            return result.cuda()
        else:
            return result

class DecoderRNN(nn.Module):
    def __init__(self, hidden_size, output_size):
        super(DecoderRNN, self).__init__()
        self.hidden_size = hidden_size
        self.gru = nn.GRU(1, hidden_size)
#         self.out = nn.Linear(hidden_size, output_size)


    def forward(self, input, hidden):

        output, hidden = self.gru(input, hidden)

        return output, hidden

    def initHidden(self):
        result = torch.zeros(1, 1, self.hidden_size)
        if use_cuda:
            return result.cuda()
        else:
            return result




def train(input_variable, target_variable, encoder, decoder, encoder_optimizer, \
          decoder_optimizer, criterion):
    encoder_hidden = encoder.initHidden()

    encoder_optimizer.zero_grad()
    decoder_optimizer.zero_grad()

    input_length = input_variable.size()[1]
    target_length = target_variable.size()[1]
    
    encoder_outputs = torch.zeros(input_length, encoder.hidden_size)
    encoder_outputs = encoder_outputs.cuda() if use_cuda else encoder_outputs
   
    loss = 0

    for ei in range(input_length):

        encoder_output, encoder_hidden = encoder(
            input_variable[:,ei:(ei+1),:], encoder_hidden)
        encoder_outputs[ei] = encoder_output[0][0]

    decoder_input = input_variable[:,-1:,:]
    decoder_input = decoder_input.cuda() if use_cuda else decoder_input
    
    decoder_hidden = encoder_hidden

    use_teacher_forcing = True if random.random() < teacher_forcing_ratio else False

    if use_teacher_forcing:
        # Teacher forcing: Feed the target as the next input
        for di in range(target_length):
            decoder_output, decoder_hidden = decoder(
                decoder_input, decoder_hidden)
            loss += criterion(decoder_output, target_variable[:,di:di+1,:])
            decoder_input = target_variable[:,di:di+1,:]  # Teacher forcing

    else:
        # Without teacher forcing: use its own predictions as the next input
        for di in range(target_length):

            decoder_output, decoder_hidden = decoder(
                decoder_input, decoder_hidden)
            
            decoder_input = decoder_output
            decoder_input = decoder_input.cuda() if use_cuda else decoder_input
            
            loss += criterion(decoder_output, target_variable[:,di:di+1,:])

    loss.backward()

    encoder_optimizer.step()
    decoder_optimizer.step()

    return loss.item() / target_length<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13121189342687714, 'fraction_numerical': 0.019878058966006848, 'mean_word_length': 3.3637026239067054, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '408392', 'license': 'MIT', 'n_tokens_mistral': 4389, 'n_tokens_neox': 4141, 'n_words': 698, 'path': 'analysis/rnn.ipynb', 'repo_name': 'YichengPu/web_traffic', 'size': '11973'}