我第一次使用 Tsai(版本 0.3.5)进行时间序列分类。
我有以下代码,但使用此代码时出现错误。
我的数据集的形状是: X_train.shape=[-1,20,4] , y_train.shape=[-1]
我在 tsai 中生成数据集时出错。下面是我的代码。
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from tsai.all import *
from fastai.tabular.all import *
from tsai.data.core import TSTensor
import numpy as np
import pandas as pd
import traceback
import numpy as np
from imblearn.under_sampling import RandomUnderSampler
from sklearn.preprocessing import StandardScaler
from pickle import load, dump
import torch
# Define the ResNet architecture
def resnet_custom(c_out, block, layers, seq_len, n_features, hidden_size, dropout=0.5):
return nn.Sequential(
nn.Conv1d(in_channels=n_features, out_channels=hidden_size, kernel_size=1),
nn.BatchNorm1d(hidden_size),
nn.ReLU(),
nn.Dropout(dropout),
nn.Sequential(*[
block(hidden_size, hidden_size, dropout) for i in range(layers)
]),
nn.AdaptiveAvgPool1d(1),
Flatten(),
nn.Linear(hidden_size, c_out),
SigmoidRange(0, 1)
)
# Define the ResNet block
def resnet_block(ni, nf, dropout=0.5):
return nn.Sequential(
nn.Conv1d(in_channels=ni, out_channels=nf, kernel_size=3, padding=1),
nn.BatchNorm1d(nf),
nn.ReLU(),
nn.Dropout(dropout),
nn.Conv1d(in_channels=nf, out_channels=nf, kernel_size=3, padding=1),
nn.BatchNorm1d(nf),
nn.ReLU(),
nn.Dropout(dropout),
nn.Conv1d(in_channels=nf, out_channels=ni, kernel_size=1),
nn.BatchNorm1d(ni)
)
def get_ts_dls(X_train, y_train, X_valid, y_valid, verbose=False, num_workers=0, **kwargs):
train_ds, valid_ds = TSDatasets(X_train, y_train), TSDatasets(X_valid, y_valid)
train_dl, valid_dl = TSDataLoaders(train_ds, valid_ds)
dls = DataLoaders(train_dl, valid_dl)
if verbose: print(f'X summary:\n{dls.one_batch()[0].shape}\n\ny summary:\n{dls.one_batch()[1].shape}')
return dls
# Define the dataset
X_train = load(open(r"X_train_resnet.pkl", 'rb'))
y_train = load(open(r"y_train_resnet.pkl", 'rb'))
X_test = load(open(r"X_test_resnet.pkl", 'rb'))
y_test = load(open(r"y_test_resnet.pkl", 'rb'))
print("dataset loaded")
X_train = TSTensor(X_train)
y_train = TSTensor(y_train)
X_test = TSTensor(X_test)
y_test = TSTensor(y_test)
dls = get_ts_dls(X_train, y_train, X_test, y_test)
# Define the ResNet model and train the network
learn = Learner(dls, resnet_custom(1, resnet_block, 2, seq_len=20, n_features=4, hidden_size=32, dropout=0.5), metrics=accuracy)
learn.fine_tune(epochs=10, base_lr=3e-3)
# Save the model
learn.save('resnet_model')
# Load the model and test it on the test set
learn = load_learner('resnet_model')
test_dl = learn.dls.test_dl(X_test)
preds, targs = learn.get_preds(dl=test_dl)
acc = accuracy(preds, targs)
print(f'Test accuracy: {acc}')
下面是错误:-
Traceback (most recent call last):
File "D:\Scripts\resnet.py", line 64, in <module>
dls = get_ts_dls(X_train, y_train, X_test, y_test)
File "D:\Scripts\resnet.py", line 48, in get_ts_dls
train_dl, valid_dl = TSDataLoaders(train_ds, valid_ds)
File "C:\Users\grant\anaconda3\lib\site-packages\tsai\data\core.py", line 820, in __init__
self.device = ifnone(device, default_device())
File "C:\Users\grant\anaconda3\lib\site-packages\fastai\data\core.py", line 228, in device
for dl in self.loaders: dl.to(d)
File "C:\Users\grant\anaconda3\lib\site-packages\fastai\data\core.py", line 461, in __getattr__
def __getattr__(self,k): return gather_attrs(self, k, 'tls')
File "C:\Users\grant\anaconda3\lib\site-packages\fastcore\transform.py", line 173, in gather_attrs
if not res: raise AttributeError(k)
AttributeError: to
有人可以帮我指出我做错了什么吗。