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

Torch.onnx.export error #43

Closed
shushan2017 opened this issue May 14, 2023 · 4 comments
Closed

Torch.onnx.export error #43

shushan2017 opened this issue May 14, 2023 · 4 comments

Comments

@shushan2017
Copy link

shushan2017 commented May 14, 2023

from tqdm import tqdm
import torch
import pandas as pd
import numpy as np
import math
from torch import nn
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()

data = np.genfromtxt('c:/data.csv', delimiter=",")
data=scaler.fit_transform(data.reshape(-1,1)).flatten()
# In[ ]:
iw = 96
ow = 15
train=data

from torch.utils.data import DataLoader, Dataset

class windowDataset(Dataset):
    def __init__(self, y, input_window=80, output_window=20, stride=3):
        L = y.shape[0]
        num_samples = (L - input_window - output_window) // stride + 1
        X = np.zeros([input_window, num_samples])
        Y = np.zeros([output_window, num_samples])
        print(X.shape,y.shape)
        for i in np.arange(num_samples):
            start_x = stride*i
            end_x = start_x + input_window
            X[:,i] = y[start_x:end_x]

            start_y = stride*i + input_window
            end_y = start_y + output_window
            Y[:,i] = y[start_y:end_y]

        # size: [num_samples, input_window, 1]
        X = X.reshape(X.shape[1], X.shape[0], 1) 
        Y = Y.reshape(Y.shape[1], Y.shape[0], 1)
        self.x = X
        self.y = Y
        self.len = len(X)

    def __getitem__(self, i):
        return self.x[i], self.y[i]
    def __len__(self):
        return self.len


# In[ ]:
train_dataset = windowDataset(train, input_window=iw, output_window=ow, stride=2)
train_loader = DataLoader(train_dataset, batch_size=512)
# In[ ]:
from PatchTST import PatchTST
class PT_config():
    def __init__(self):
        self.seq_len=iw
        self.pred_len=ow
        self.individual=0
        self.enc_in=1
        self.e_layers=3
        self.n_heads= 16 
        self.d_model= 128 
        self.d_ff= 256 
        self.dropout =0.2
        self.fc_dropout= 0.2
        self.head_dropout= 0
        self.patch_len =16
        self.stride =8
        self.padding_patch='end'
        self.revin=1
        self.affine=0
        self.subtract_last=0
        self.decomposition=1
        self.kernel_size=25

# In[ ]:
# # 3. Train
#device = torch.device("cuda")
device = torch.device("cpu")
lr = 1e-4

class modelParam():
    def __init__(self,label):
        self.model=PatchTST(configs=PT_config()).to(device)
        self.optimizer=torch.optim.Adam(self.model.parameters(), lr=lr)

    def epoch(self,epoch):
        self.epoch=epoch
        return self.epoch

# In[ ]:
criterion = nn.MSELoss() #0.58
# In[ ]:
PT=modelParam('PT')
PTmodel=PT.model.to(device)
optimizer=PT.optimizer
epoch=PT.epoch(2) #训练次数
progress = tqdm(range(epoch))
PTmodel.train()
losses=[]
for i in progress:
    batchloss = 0.0
    for (inputs, outputs) in train_loader:
        optimizer.zero_grad()
        #'''
        result = PTmodel(inputs.float().to(device))
        loss = criterion(result, outputs.float().to(device))
        loss.backward()
        optimizer.step()
        batchloss += loss
    losses.append(batchloss.cpu().item())
    progress.set_description("PT- loss: {:0.6f}".format(batchloss.cpu().item() / len(train_loader)))

torch.save(PTmodel.to('cpu'), 'PTmodel.pth')

# In[ ]:
input_shape = (1, 96, 1)  
input_names = ["input"]  
output_names = ["output"]  


x = torch.randn(input_shape)

 
onnx_model_path = "PTmodel.onnx"  
dynamic_axes = {'input': {0: 'batch_size', 1: 'sequence_length', 2: 'input_dim'}, # 输入张量的动态维度axis
                'output': {0: 'batch_size', 1: 'sequence_length', 2: 'output_dim'}} # 输出张量的动态维度axis
torch.onnx.export(PTmodel, x, onnx_model_path, input_names=input_names, output_names=output_names, dynamic_axes=dynamic_axes)


data.csv

@yuqinie98
Copy link
Owner

please provide more details

@shushan2017
Copy link
Author

shushan2017 commented May 15, 2023

please provide more details

Thank you for your prompt reply! I have modified my question and uploaded my code and test data. Could you please take a look at any bugs in my code? thank you

@yuqinie98
Copy link
Owner

Hi, somehow you delete the error hint, so sorry I cannot figure out a specific error here. If you want to get a more intuitive code to start with, maybe you could tried with this code: https://github.com/timeseriesAI/tsai/blob/main/tutorial_nbs/15_PatchTST_a_new_transformer_for_LTSF.ipynb.

@shushan2017
Copy link
Author

thank you! tsai is ok!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants