{ "cells": [ { "cell_type": "markdown", "id": "2f04eee0-5928-4e74-a754-6dc2e528810c", "metadata": {}, "source": [ "# CpGPTPCGrimAge3" ] }, { "cell_type": "markdown", "id": "a3f514a3-772c-4a14-afdf-5a8376851ff4", "metadata": {}, "source": [ "## Index\n", "1. [Instantiate model class](#Instantiate-model-class)\n", "2. [Define clock metadata](#Define-clock-metadata)\n", "3. [Download clock dependencies](#Download-clock-dependencies)\n", "5. [Load features](#Load-features)\n", "6. [Load weights into base model](#Load-weights-into-base-model)\n", "7. [Load reference values](#Load-reference-values)\n", "8. [Load preprocess and postprocess objects](#Load-preprocess-and-postprocess-objects)\n", "10. [Check all clock parameters](#Check-all-clock-parameters)\n", "10. [Basic test](#Basic-test)\n", "11. [Save torch model](#Save-torch-model)\n", "12. [Clear directory](#Clear-directory)" ] }, { "cell_type": "markdown", "id": "d95fafdc-643a-40ea-a689-200bd132e90c", "metadata": {}, "source": [ "Let's first import some packages:" ] }, { "cell_type": "code", "execution_count": 1, "id": "4adfb4de-cd79-4913-a1af-9e23e9e236c9", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:41.316681Z", "iopub.status.busy": "2025-04-07T17:51:41.316440Z", "iopub.status.idle": "2025-04-07T17:51:42.738147Z", "shell.execute_reply": "2025-04-07T17:51:42.737780Z" } }, "outputs": [], "source": [ "import os\n", "import inspect\n", "import shutil\n", "import json\n", "import torch\n", "import pandas as pd\n", "import pyaging as pya\n", "import numpy as np" ] }, { "cell_type": "markdown", "id": "145082e5-ced4-47ae-88c0-cb69773e3c5a", "metadata": {}, "source": [ "## Instantiate model class" ] }, { "cell_type": "code", "execution_count": 2, "id": "8aa77372-7ed3-4da7-abc9-d30372106139", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.740018Z", "iopub.status.busy": "2025-04-07T17:51:42.739761Z", "iopub.status.idle": "2025-04-07T17:51:42.750935Z", "shell.execute_reply": "2025-04-07T17:51:42.750574Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class CpGPTPCGrimAge3(pyagingModel):\n", " def __init__(self):\n", " super().__init__()\n", "\n", " self.rotation = nn.Parameter(torch.empty((30, 29)), requires_grad=False)\n", "\n", " def preprocess(self, x):\n", " \"\"\"\n", " Scales an array based on the mean and standard deviation.\n", " \"\"\"\n", " mean = torch.tensor(self.preprocess_dependencies[0], device=x.device, dtype=x.dtype)\n", " std = torch.tensor(self.preprocess_dependencies[1], device=x.device, dtype=x.dtype)\n", " x = (x - mean) / std\n", " return x\n", " \n", " def forward(self, x):\n", "\n", " x = self.preprocess(x)\n", "\n", " age = x[:, 0].unsqueeze(1)\n", " proxies = x[:, 1:]\n", "\n", " PCs = torch.mm(proxies, self.rotation) # Apply PCA rotation\n", "\n", " x = torch.concat([age, PCs], dim=1)\n", "\n", " # Scale\n", " mean = torch.tensor(self.preprocess_dependencies[2], device=x.device, dtype=x.dtype)\n", " std = torch.tensor(self.preprocess_dependencies[3], device=x.device, dtype=x.dtype)\n", " x[:, 1:] = (x[:, 1:] - mean) / std\n", "\n", " x = self.base_model(x)\n", "\n", " x = self.postprocess(x)\n", "\n", " return x\n", "\n", " def postprocess(self, x):\n", " \"\"\"\n", " Converts from a Cox parameter to age in units of years.\n", " \"\"\"\n", " cox_mean = self.postprocess_dependencies[0]\n", " cox_std = self.postprocess_dependencies[1]\n", " age_mean = self.postprocess_dependencies[2]\n", " age_std = self.postprocess_dependencies[3]\n", "\n", " # Normalize\n", " x = (x - cox_mean) / cox_std\n", "\n", " # Scale\n", " x = (x * age_std) + age_mean\n", "\n", " return x\n", "\n" ] } ], "source": [ "def print_entire_class(cls):\n", " source = inspect.getsource(cls)\n", " print(source)\n", "\n", "print_entire_class(pya.models.CpGPTPCGrimAge3)" ] }, { "cell_type": "code", "execution_count": 3, "id": "78536494-f1d9-44de-8583-c89a310d2307", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.753015Z", "iopub.status.busy": "2025-04-07T17:51:42.752877Z", "iopub.status.idle": "2025-04-07T17:51:42.754599Z", "shell.execute_reply": "2025-04-07T17:51:42.754314Z" } }, "outputs": [], "source": [ "model = pya.models.CpGPTPCGrimAge3()" ] }, { "cell_type": "markdown", "id": "51f8615e-01fa-4aa5-b196-3ee2b35d261c", "metadata": {}, "source": [ "## Define clock metadata" ] }, { "cell_type": "code", "execution_count": 4, "id": "135ce001-03f7-4025-bceb-01a3e2e2b0ef", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:42.755964Z", "iopub.status.busy": "2025-04-07T17:51:42.755873Z", "iopub.status.idle": "2025-04-07T17:51:42.758035Z", "shell.execute_reply": "2025-04-07T17:51:42.757775Z" } }, "outputs": [], "source": [ "model.metadata[\"clock_name\"] = \"cpgptpcgrimage3\"\n", "model.metadata[\"data_type\"] = \"DNA methylation\" # Paper: CpGPT models DNA-methylation profiles and derives methylation-based aging and mortality predictors.\n", "model.metadata[\"species\"] = \"Homo sapiens\" # Paper: The assigned mortality task uses human DNA-methylation cohorts.\n", "model.metadata[\"year\"] = 2024\n", "model.metadata[\"approved_by_author\"] = \"✅\"\n", "model.metadata[\"citation\"] = \"de Lima Camillo, L. P., Sehgal, R., Armstrong, J., Higgins-Chen, A. T., Horvath, S., & Wang, B. CpGPT: a foundation model for DNA methylation. bioRxiv 2024.10.24.619766 (2024).\"\n", "model.metadata[\"doi\"] = \"https://doi.org/10.1101/2024.10.24.619766\"\n", "model.metadata[\"notes\"] = \"Principal-component CpGPTGrimAge3 implementation combining chronological age, GrimAge2 DNAm proxies, and CpGPT-predicted plasma-protein proxies; 30 proxy inputs are projected to 29 PCs, entered with age into a Cox linear predictor, and calibrated to years.\"\n", "model.metadata[\"research_only\"] = True\n", "model.metadata[\"tissue\"] = [\"whole blood\"] # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"predicts\"] = [\"biological age\", \"mortality risk\"] # Paper: The implementation converts the Cox parameter to an age-scaled output.\n", "model.metadata[\"training_target\"] = [\"mortality\"] # Paper: The mortality model was trained with a modified Cox proportional-hazards loss against time-to-mortality data.\n", "model.metadata[\"unit\"] = [\"years\"] # Paper: The implementation normalizes a Cox score and rescales it with an age mean and standard deviation.\n", "model.metadata[\"model_type\"] = \"PCA + Cox regression\" # Paper: The model rotates 30 standardized proxies into 29 PCs, concatenates chronological age, applies a linear Cox score, and calibrates it to years.\n", "model.metadata[\"platform\"] = [\"Illumina 450K\"] # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"population\"] = \"adults\" # Paper: cpgptgrimage3 and cpgptpcgrimage3 were trained in blood with the 450k array to predict mortality. it was trained in the FHS cohort with methylation data.\n", "model.metadata[\"journal\"] = \"bioRxiv\"\n", "model.metadata[\"last_author\"] = \"Bo Wang\"\n", "model.metadata[\"n_features\"] = 31\n", "model.metadata[\"citations\"] = 30\n", "model.metadata[\"citations_date\"] = \"2026-07-05\"\n" ] }, { "cell_type": "markdown", "id": "74492239-5aae-4026-9d90-6bc9c574c110", "metadata": {}, "source": [ "## Download clock dependencies" ] }, { "cell_type": "code", "execution_count": 5, "id": "95f6ba57", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|-----------> Downloading data to ./cpgpt_grimage3_weights.csv\n", "|-----------> in progress: 100.0000%\n", "|-----------> Downloading data to ./cpgpt_grimage3_weights_all_datasets.csv\n", "|-----------> in progress: 100.0000%\n", "|-----------> Downloading data to ./cpgpt_pcgrimage3_weights.csv\n", "|-----------> in progress: 100.0000%\n", "|-----------> Downloading data to ./input_scaler_mean.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./input_scaler_scale.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./input_scaler_mean_all_datasets.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./input_scaler_scale_all_datasets.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./pca_scaler_mean.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./pca_scaler_scale.npy\n", "|-----------> in progress: 100.0000%%\n", "|-----------> Downloading data to ./cpgpt_pcgrimage3_pca_components.npy\n", "|-----------> in progress: 100.0000%\n" ] } ], "source": [ "logger = pya.logger.Logger()\n", "urls = [\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/cpgpt_grimage3_weights.csv\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/cpgpt_grimage3_weights_all_datasets.csv\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/cpgpt_pcgrimage3_weights.csv\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/input_scaler_mean.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/input_scaler_scale.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/input_scaler_mean_all_datasets.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/input_scaler_scale_all_datasets.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/pca_scaler_mean.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/pca_scaler_scale.npy\",\n", " \"https://huggingface.co/lucascamillomd/pyaging-data/resolve/main/supporting_files/cpgpt_grimage3_dependencies/cpgpt_pcgrimage3_pca_components.npy\",\n", "]\n", "dir = \".\"\n", "for url in urls:\n", " pya.utils.download(url, dir, logger, indent_level=1)" ] }, { "cell_type": "markdown", "id": "a14c7fc1-abe5-42a3-8bc9-0987521ddf33", "metadata": {}, "source": [ "## Load features" ] }, { "cell_type": "markdown", "id": "3e737582-3a28-4f55-8da9-3e34125362cc", "metadata": {}, "source": [ "#### From CSV" ] }, { "cell_type": "code", "execution_count": 6, "id": "f1486db4", "metadata": {}, "outputs": [], "source": [ "df = pd.read_csv('cpgpt_pcgrimage3_weights.csv')\n", "model.features = pd.read_csv('cpgpt_grimage3_weights.csv')['feature'].tolist()" ] }, { "cell_type": "code", "execution_count": 7, "id": "8c43149c", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
featurecoefficient
0age0.672689
1pca_component_10.759243
2pca_component_2-0.686806
3pca_component_30.233157
4pca_component_4-0.138548
\n", "
" ], "text/plain": [ " feature coefficient\n", "0 age 0.672689\n", "1 pca_component_1 0.759243\n", "2 pca_component_2 -0.686806\n", "3 pca_component_3 0.233157\n", "4 pca_component_4 -0.138548" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "markdown", "id": "ee6d8fa0-4767-4c45-9717-eb1c95e2ddc0", "metadata": {}, "source": [ "## Load weights into base model" ] }, { "cell_type": "code", "execution_count": 8, "id": "f8408f8d", "metadata": {}, "outputs": [], "source": [ "pc_matrix = np.load('cpgpt_pcgrimage3_pca_components.npy').T" ] }, { "cell_type": "markdown", "id": "6b8c6e29", "metadata": {}, "source": [ "#### PC Components" ] }, { "cell_type": "code", "execution_count": 9, "id": "d7336137", "metadata": {}, "outputs": [], "source": [ "model.rotation.data = torch.tensor(pc_matrix).float()" ] }, { "cell_type": "markdown", "id": "3958ba73-42e8-40a5-94a1-4f4b8ae05dca", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 10, "id": "321a437c-8888-4e10-96e9-5ed2826a8f74", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.647294Z", "iopub.status.busy": "2025-04-07T17:51:44.647116Z", "iopub.status.idle": "2025-04-07T17:51:44.688112Z", "shell.execute_reply": "2025-04-07T17:51:44.687757Z" } }, "outputs": [], "source": [ "weights = torch.tensor(df['coefficient'].tolist()).unsqueeze(0)\n", "intercept = torch.tensor([0.0])" ] }, { "cell_type": "markdown", "id": "5742dc16-e063-414f-a38e-9721beb11351", "metadata": {}, "source": [ "#### Linear model" ] }, { "cell_type": "code", "execution_count": 11, "id": "c2e54115-c17b-48ce-88f1-de546c90d2b3", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.689783Z", "iopub.status.busy": "2025-04-07T17:51:44.689683Z", "iopub.status.idle": "2025-04-07T17:51:44.692010Z", "shell.execute_reply": "2025-04-07T17:51:44.691738Z" } }, "outputs": [], "source": [ "base_model = pya.models.LinearModel(input_dim=len(model.features))\n", "\n", "base_model.linear.weight.data = weights.float()\n", "base_model.linear.bias.data = intercept.float()\n", "\n", "model.base_model = base_model" ] }, { "cell_type": "markdown", "id": "ad8b4c1d-9d57-48b7-9a30-bcfea7b747b1", "metadata": {}, "source": [ "## Load reference values" ] }, { "cell_type": "code", "execution_count": 12, "id": "2089b66f-9cc4-4528-9bdc-5e45efc6d06b", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.693409Z", "iopub.status.busy": "2025-04-07T17:51:44.693309Z", "iopub.status.idle": "2025-04-07T17:51:44.708180Z", "shell.execute_reply": "2025-04-07T17:51:44.707878Z" } }, "outputs": [], "source": [ "scale_mean = np.load('input_scaler_mean.npy')\n", "scale_std = np.load('input_scaler_scale.npy')\n", "\n", "pca_scale_mean = np.load('pca_scaler_mean.npy')\n", "pca_scale_std = np.load('pca_scaler_scale.npy')\n", "\n", "model.reference_values = None" ] }, { "cell_type": "markdown", "id": "af3bcf7b-74a8-4d21-9ccb-4de0c2b0516b", "metadata": {}, "source": [ "## Load preprocess and postprocess objects" ] }, { "cell_type": "code", "execution_count": 13, "id": "7a22fb20-c605-424d-8efb-7620c2c0755c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.709686Z", "iopub.status.busy": "2025-04-07T17:51:44.709594Z", "iopub.status.idle": "2025-04-07T17:51:44.711225Z", "shell.execute_reply": "2025-04-07T17:51:44.710973Z" } }, "outputs": [], "source": [ "model.preprocess_name = 'scale'\n", "model.preprocess_dependencies = [scale_mean, scale_std, pca_scale_mean, pca_scale_std]" ] }, { "cell_type": "code", "execution_count": 14, "id": "ff4a21cb-cf41-44dc-9ed1-95cf8aa15772", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.712430Z", "iopub.status.busy": "2025-04-07T17:51:44.712349Z", "iopub.status.idle": "2025-04-07T17:51:44.713842Z", "shell.execute_reply": "2025-04-07T17:51:44.713585Z" } }, "outputs": [], "source": [ "model.postprocess_name = 'cox_to_years'\n", "model.postprocess_dependencies = [\n", " 4.66184408e-17,\n", " 1.70884158624939,\n", " 58.8234007654456,\n", " 13.091231557630831\n", "]" ] }, { "cell_type": "markdown", "id": "86e3d6b1-e67e-4f3d-bd39-0ebec5726c3c", "metadata": {}, "source": [ "## Check all clock parameters" ] }, { "cell_type": "code", "execution_count": 15, "id": "2168355c-47d9-475d-b816-49f65e74887c", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.715112Z", "iopub.status.busy": "2025-04-07T17:51:44.715032Z", "iopub.status.idle": "2025-04-07T17:51:44.726874Z", "shell.execute_reply": "2025-04-07T17:51:44.726577Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "%==================================== Model Details ====================================%\n", "Model Attributes:\n", "\n", "training: True\n", "metadata: {'approved_by_author': '✅',\n", " 'citation': 'de Lima Camillo, Lucas Paulo, et al. \"CpGPT: a foundation model '\n", " 'for DNA methylation.\" bioRxiv (2024): 2024-10.',\n", " 'clock_name': 'cpgptpcgrimage3',\n", " 'data_type': 'methylation',\n", " 'doi': 'https://doi.org/10.1101/2024.10.24.619766',\n", " 'notes': None,\n", " 'research_only': True,\n", " 'species': 'Homo sapiens',\n", " 'version': None,\n", " 'year': 2025}\n", "reference_values: None\n", "preprocess_name: 'scale'\n", "preprocess_dependencies: [array([ 5.88234008e+01, 3.37892988e+04, 2.13030677e+01, 6.29543290e-02,\n", " 1.71182803e+06, 3.48032879e+02, 8.07863072e+03, 8.40183045e+02,\n", " -4.62783972e-01, -2.81553566e-01, 2.08716775e-02, -2.03755490e-01,\n", " -3.31462601e-01, -1.99447326e-01, -2.86641682e-01, 9.69636186e-02,\n", " 9.23084436e-02, -1.77190600e-01, -5.19144939e-01, 2.77395512e-01,\n", " -5.05034895e-02, -1.69724841e-01, -2.09474468e-01, -1.81162437e-01,\n", " 2.91470602e-01, -2.46817951e-01, -1.38063048e-01, -2.31378318e-01,\n", " -1.60070027e-01, -4.51811847e-01, -2.54749226e-01]),\n", " array([1.30894420e+01, 1.96256186e+03, 1.18901350e+01, 4.18591628e-01,\n", " 1.73166548e+05, 2.79560288e+01, 3.95321057e+03, 1.97280471e+02,\n", " 7.76691367e-02, 1.88703875e-01, 8.32203637e-02, 9.29975592e-02,\n", " 9.75873526e-02, 2.24521829e-01, 2.24054312e-01, 2.35572764e-01,\n", " 1.57862263e-01, 9.37500577e-02, 1.60579409e-01, 6.03601420e-02,\n", " 1.31061777e-01, 2.68979974e-01, 2.70291051e-01, 3.07832239e-01,\n", " 1.89457709e-01, 1.12294499e-01, 1.04148286e-01, 1.73197185e-01,\n", " 2.55819219e-01, 2.33352986e-01, 2.00723138e-01]),\n", " array([ 3.84844941e-17, 1.28382816e-17, -1.76943691e-17, -2.32485193e-17,\n", " -2.96524848e-17, -1.89690921e-18, -3.21715803e-18, -1.40376024e-17,\n", " 1.26106525e-17, -1.44544482e-18, -2.46598198e-19, -1.61237283e-18,\n", " -7.10961573e-18, 4.55637593e-18, 3.66672551e-18, -1.15948576e-18,\n", " 7.37660570e-18, 5.32557262e-19, -7.01856409e-20, 1.61711510e-19,\n", " -3.30726121e-18, -5.22432509e-18, -5.37341030e-18, 4.27515914e-18,\n", " -9.05537036e-19, 6.74588339e-19, -4.72330394e-19, 1.02907325e-18,\n", " -7.48804912e-19]),\n", " array([4.21748493, 2.11495255, 1.91135404, 1.29849805, 0.89811783,\n", " 0.68972216, 0.68116282, 0.44284398, 0.40801214, 0.35556359,\n", " 0.30007137, 0.17650593, 0.1483157 , 0.08898671, 0.06836764,\n", " 0.06232975, 0.0343495 , 0.03288555, 0.02752429, 0.02667721,\n", " 0.02145187, 0.01956034, 0.01423029, 0.01370304, 0.01111563,\n", " 0.01072517, 0.00881539, 0.0079158 , 0.00748125])]\n", "postprocess_name: 'cox_to_years'\n", "postprocess_dependencies: [4.66184408e-17, 1.70884158624939, 58.8234007654456, 13.091231557630831]\n", "features: ['age', 'grimage2timp1', 'grimage2packyrs', 'grimage2logcrp', 'grimage2b2m', 'grimage2adm', 'grimage2leptin', 'grimage2gdf15', 'cpgpt_s100a9', 'cpgpt_il17ra', 'cpgpt_nampt', 'cpgpt_tnfrsf13c', 'cpgpt_faslg', 'cpgpt_tgfb1', 'cpgpt_ccl19', 'cpgpt_cst3', 'cpgpt_il6r', 'cpgpt_snap25', 'cpgpt_sdc1', 'cpgpt_cd200', 'cpgpt_tek', 'cpgpt_ccl14', 'cpgpt_il5', 'cpgpt_timp1', 'cpgpt_tnfsf15', 'cpgpt_ctf1', 'cpgpt_il20', 'cpgpt_pdgfa', 'cpgpt_calb2', 'cpgpt_il1rn']... [Total elements: 31]\n", "base_model_features: None\n", "\n", "%==================================== Model Details ====================================%\n", "Model Structure:\n", "\n", "base_model: LinearModel(\n", " (linear): Linear(in_features=31, out_features=1, bias=True)\n", ")\n", "\n", "%==================================== Model Details ====================================%\n", "Model Parameters and Weights:\n", "\n", "rotation: [0.14989051222801208, -0.2575840353965759, 0.2105623334646225, -0.11764489859342575, -0.07999813556671143, 0.2998059093952179, 0.03677944839000702, 0.06442702561616898, -0.21868155896663666, -0.0400826632976532, 0.8137347102165222, 0.1822977364063263, 0.06282850354909897, 0.007936620153486729, 0.02314574271440506, -0.030949102714657784, 0.019456684589385986, -0.012623480521142483, 0.0012334324419498444, -0.017306583002209663, 0.002008328679949045, -0.0023207056801766157, 0.0028594015166163445, -0.004331233445554972, -0.001271342160180211, 0.0009904390899464488, -0.0020116211380809546, -0.0018152670236304402, -7.565721170976758e-05, 0.08682457357645035]... [Tensor of shape torch.Size([30, 29])]\n", "base_model.linear.weight: tensor([[ 0.6727, 0.7592, -0.6868, 0.2332, -0.1385, 0.3705, 0.0353, 0.0611,\n", " -0.0175, -0.0420, -0.0281, 0.0249, 0.0433, 0.0515, 0.0116, -0.0187,\n", " 0.0360, -0.0367, -0.0120, -0.0274, 0.0219, -0.0447, 0.0598, -0.0433,\n", " -0.0298, -0.0062, -0.0176, -0.0209, 0.0035, 0.0329]])\n", "base_model.linear.bias: tensor([0.])\n", "\n", "%==================================== Model Details ====================================%\n", "\n" ] } ], "source": [ "pya.utils.print_model_details(model)" ] }, { "cell_type": "markdown", "id": "986d0262-e0c7-4036-b687-dee53ba392fb", "metadata": {}, "source": [ "## Basic test" ] }, { "cell_type": "code", "execution_count": 16, "id": "936b9877-d076-4ced-99aa-e8d4c58c5caf", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.728239Z", "iopub.status.busy": "2025-04-07T17:51:44.728153Z", "iopub.status.idle": "2025-04-07T17:51:44.733560Z", "shell.execute_reply": "2025-04-07T17:51:44.733262Z" } }, "outputs": [ { "data": { "text/plain": [ "tensor([[-365.0296],\n", " [-418.9304],\n", " [-114.4988],\n", " [ -79.3617],\n", " [ 576.5590],\n", " [ 419.1323],\n", " [-391.7414],\n", " [ 237.9895],\n", " [-340.7663],\n", " [-172.7751]], dtype=torch.float64, grad_fn=)" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "torch.manual_seed(42)\n", "input = torch.randn(10, len(model.features), dtype=float).double()\n", "model.eval()\n", "model.to(float)\n", "pred = model(input)\n", "pred" ] }, { "cell_type": "markdown", "id": "fe8299d7-9285-4e22-82fd-b664434b4369", "metadata": {}, "source": [ "## Save torch model" ] }, { "cell_type": "code", "execution_count": 17, "id": "5ef2fa8d-c80b-4fdd-8555-79c0d541788e", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.734916Z", "iopub.status.busy": "2025-04-07T17:51:44.734814Z", "iopub.status.idle": "2025-04-07T17:51:44.739278Z", "shell.execute_reply": "2025-04-07T17:51:44.738936Z" } }, "outputs": [], "source": [ "torch.save(model, f\"../weights/{model.metadata['clock_name']}.pt\")" ] }, { "cell_type": "markdown", "id": "bac6257b-8d08-4a90-8d0b-7f745dc11ac1", "metadata": {}, "source": [ "## Clear directory\n", "" ] }, { "cell_type": "code", "execution_count": 18, "id": "11aeaa70-44c0-42f9-86d7-740e3849a7a6", "metadata": { "execution": { "iopub.execute_input": "2025-04-07T17:51:44.740582Z", "iopub.status.busy": "2025-04-07T17:51:44.740494Z", "iopub.status.idle": "2025-04-07T17:51:44.743819Z", "shell.execute_reply": "2025-04-07T17:51:44.743572Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Deleted file: input_scaler_mean_all_datasets.npy\n", "Deleted file: cpgpt_pcgrimage3_pca_components.npy\n", "Deleted file: cpgpt_pcgrimage3_weights.csv\n", "Deleted file: input_scaler_scale_all_datasets.npy\n", "Deleted file: input_scaler_scale.npy\n", "Deleted file: pca_scaler_mean.npy\n", "Deleted file: cpgpt_grimage3_weights_all_datasets.csv\n", "Deleted file: pca_scaler_scale.npy\n", "Deleted file: cpgpt_grimage3_weights.csv\n", "Deleted file: input_scaler_mean.npy\n" ] } ], "source": [ "# Function to remove a folder and all its contents\n", "def remove_folder(path):\n", " try:\n", " shutil.rmtree(path)\n", " print(f\"Deleted folder: {path}\")\n", " except Exception as e:\n", " print(f\"Error deleting folder {path}: {e}\")\n", "\n", "# Get a list of all files and folders in the current directory\n", "all_items = os.listdir('.')\n", "\n", "# Loop through the items\n", "for item in all_items:\n", " # Check if it's a file and does not end with .ipynb\n", " if os.path.isfile(item) and not item.endswith('.ipynb'):\n", " os.remove(item)\n", " print(f\"Deleted file: {item}\")\n", " # Check if it's a folder\n", " elif os.path.isdir(item):\n", " remove_folder(item)" ] } ], "metadata": { "kernelspec": { "display_name": "research", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }