<jupyter_start><jupyter_text>Prepare dataset as PyTorch dataset<jupyter_code>fn = os.path.join(data_path, 'drifted_data_y_2023_reduced_more_cos_phi.h5')
df_data = pd.read_hdf(fn, key='df')
print('Shape of drifted data: {}'.format(df_data.shape))
data_fn = os.path.join(data_path, 'simulation_data_y_2020_2021_reduced.h5')
df_data_sim = pd.read_hdf(data_fn, key='df')
print('Shape of simulation data: {}'.format(df_data_sim.shape))
s_drift_labels = df_data['drift_labels']
s_drift_labels.reset_index(drop=True, inplace=True)
df_data.drop('drift_labels', axis=1, inplace=True)
print('Shape of drifted data: {}'.format(df_data.shape))
print('Drift Labels: {}'.format(sorted(s_drift_labels.unique())))
### Scale data
print('Scale data..')
scaler_train = MinMaxScaler((-1,1))
scaler_train = scaler_train.fit(df_data_sim)
scaled_drifted = scaler_train.transform(df_data.to_numpy())
del(df_data_sim)
df_data_scaled = pd.DataFrame(scaled_drifted)
# build tensor from numpy
drifted_torch_tensor_X = torch.from_numpy(df_data_scaled.to_numpy()).type(torch.FloatTensor)
drifted_torch_tensor_y = torch.from_numpy(s_drift_labels.to_numpy().reshape(len(s_drift_labels),1)).type(torch.FloatTensor)
# build pytorch dataset from tensor
drifted_dataset = TensorDataset(drifted_torch_tensor_X, drifted_torch_tensor_y)<jupyter_output><empty_output><jupyter_text>Load PyTorch Model<jupyter_code>%run -i ./scripts/ReadSimpleAE.py<jupyter_output>Load UNtrained simple AE..
/Users/torge/Development/master/masterthesis_code/02_Experimente/MetaLearning/models/model_bib/20200302_firstAE_model.pt
SimpleAutoEncoder(
  (encoder): Sequential(
    (0): Linear(in_features=17, out_features=12, bias=True)
    (1): ReLU(inplace=True)
    (2): Linear(in_features=12, out_features=8, bias=True)
    (3): Tanh()
  )
  (decoder): Sequential(
    (0): Linear(in_features=8, out_features=12, bias=True)
    (1): ReLU(inplace=True)
    (2): Linear(in_features=12, out_features=17, bias=True)
    (3): Tanh()
  )
)<jupyter_text>Prepare Data for meta learning<jupyter_code>data = l2l.data.MetaDataset(drifted_dataset)
# build l2l MetaDataset and TaskDataset for sampling
train_tasks = l2l.data.TaskDataset(data,
                                   task_transforms=[
                                        l2l.data.transforms.NWays(data, n=1),
                                        l2l.data.transforms.KShots(data, k=5),
                                        l2l.data.transforms.LoadData(data)],
                                   num_tasks=32)
eval_task = l2l.data.TaskDataset(data,
                                   task_transforms=[
                                        l2l.data.transforms.NWays(data, n=1),
                                        l2l.data.transforms.KShots(data, k=1),
                                        l2l.data.transforms.LoadData(data),
                                   ],
                                   num_tasks=1)<jupyter_output><empty_output><jupyter_text>Meta Learning<jupyter_code>from MetaLearner.Learner import Learner
learner = Learner(model=model, train_task_dataset=train_tasks, eval_task_dataset=eval_task, num_iterations=400, optimizer='SGD')
learner.do_meta_learning()<jupyter_output>Adaption Error: 86913.421875, Validation Error: 9259.553711: 100%|██████████| 400/400 [05:22<00:00,  1.24it/s]<jupyter_text>Training results<jupyter_code>tmp_eval = learner.evaluation_loss
tmp_adapt = learner.adaption_loss
s_eval_loss = pd.Series(learner.evaluation_loss)
s_adapt_loss = pd.Series(learner.adaption_loss)
s_eval_loss.describe()
s_eval_loss.hist()
s_adapt_loss.describe()
s_adapt_loss.hist()
save_plot = False
fig, ax = plt.subplots()
fig.set_size_inches(20, 10)
ax.set_title('Adaption Loss während des Meta Learnings', fontsize=22)

ax.plot(s_adapt_loss)
plt.tight_layout()

if save_plot:
    fn = os.path.join(fig_path, 'adaption_loss_meta_learning.pdf')
    fig.savefig(fn, bbox_inches = 'tight', pad_inches = 0 )
save_plot = False
fig, ax = plt.subplots()
fig.set_size_inches(20, 10)
ax.set_title('Evaluation Loss während des Meta Learnings', fontsize=22)

ax.plot(s_eval_loss)
plt.tight_layout()

if save_plot:
    fn = os.path.join(fig_path, 'adaption_loss_meta_learning.pdf')
    fig.savefig(fn, bbox_inches = 'tight', pad_inches = 0 )<jupyter_output><empty_output><jupyter_text>Save adapted Meta Model<jupyter_code>meta_model = learner.meta_model.module
print(type(meta_model))
model_fn = '{}_first_meta_model.pt'.format(arrow.now().format('YYYYMMDD'))
print('Model Name: {}'.format(model_fn))
torch.save(meta_model.state_dict(), os.path.join(model_bib_path, model_fn))<jupyter_output>Model Name: 20200316_first_meta_model.pt<jupyter_text>Save Learner Classs as Pickle<jupyter_code># Delte data and models from Class for pickle
learner.meta_model = None
learner.model = None
learner.train_task_data = None
learner.eval_task_data = None
meta_model_fn = '{}_learner_class.pkl'.format(arrow.now().format('YYYYMMDD'))
print('Model Name: {}'.format(meta_model_fn))
fn = os.path.join(os.getcwd(), 'MetaLearner', 'save', meta_model_fn)

with open(fn, 'wb') as file:
    pickle.dump(learner, file)<jupyter_output>Model Name: 20200316_learner_class.pkl
{'fraction_non_alphanumeric': 0.12587141750580946, 'fraction_numerical': 0.027304415182029435, 'mean_word_length': 4.488841657810839, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '570518', 'license': 'MIT', 'n_tokens_mistral': 1990, 'n_tokens_neox': 1881, 'n_words': 277, 'path': '02_MetaLearning/99_l2l_beginnings.ipynb', 'repo_name': 'yellingmonkees/MetaLearningForAD', 'size': '5164'}