{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Example2\n", "This is an example2. In this example, a transfer learning Gaussian process regression surrogate model is constructed.\n", "\n", "Here, the subject is a beam bending problem. The displacements of a cantilevered beam under horizontal and vertical loads are to be determined. The formula is as follows. \n", "$$\n", "D(\\mathbf{x})=\\frac{4 L^3}{E w t} \\sqrt{\\left(\\frac{Y}{t^2}\\right)^2+\\left(\\frac{X}{w^2}\\right)^2}\n", "$$ \n", "where $D$ is a displacement, $L$ is a length, $E$ is Young's modulus, $w$ is a width, $t$ is a height, $X$ is a horizontal load, $Y$ is a vertical load.\n", "\n", "First this function is defined." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "def beam_function(length, width, height, yang_modulus, load_horizontal, load_vertical):\n", " displacement = (4.0*length*length*length/yang_modulus/height/width) * np.sqrt(np.square(load_vertical/height/height)+np.square(load_horizontal/width/width))\n", " return displacement" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Create training data\n", "Here, we assume that the source data is an analysis on members with different Young's modulus. For other parameters, dimensions are assumed to be fixed and loads are assumed to be indeterminate." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(10, 3) (10, 1) (50, 3) (50, 1)\n" ] } ], "source": [ "import TL_GPRSM.utils.sampling as sampling\n", "\n", "length = 3.0\n", "width = 0.2\n", "height = 0.1\n", "\n", "target_x = sampling.latin_hypercube_sampling(10, 3, False)\n", "target_x = sampling.uniform_scaling(target_x, np.array([7.0e10*0.9, 5000.0*0.8, 10000.0*0.8]), np.array([7.0e10*1.1, 5000.0*1.2, 10000.0*1.2]))\n", "target_y = np.array([beam_function(length, width, height, target_x[i,0], target_x[i,1], target_x[i,2]) for i in range(target_x.shape[0])])[:,np.newaxis]\n", "source_x = sampling.latin_hypercube_sampling(50, 3, False)\n", "source_x = sampling.uniform_scaling(source_x, np.array([2.06e11*0.9, 5000.0*0.8, 10000.0*0.8]), np.array([2.06e11*1.1, 5000.0*1.2, 10000.0*1.2]))\n", "source_y = np.array([beam_function(length, width, height, source_x[i,0], source_x[i,1], source_x[i,2]) for i in range(source_x.shape[0])])[:,np.newaxis]\n", "print(target_x.shape, target_y.shape, source_x.shape, source_y.shape)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Construct TL-GPRSM" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " c:\\Users\\saida\\Downloads\\temp_0330\\venv\\lib\\site-packages\\paramz\\transformations.py:111: RuntimeWarning:overflow encountered in expm1\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Optimization restart 1/10, f = -337.7729887430097\n", "Optimization restart 2/10, f = -337.4309989442386\n", "Optimization restart 3/10, f = -337.91045566764296\n", "Optimization restart 4/10, f = -337.4601961548478\n", "Optimization restart 5/10, f = -335.74369807361484\n", "Optimization restart 6/10, f = -335.7868282723364\n", "Optimization restart 7/10, f = -337.7697880453027\n", "Optimization restart 8/10, f = -337.90583014656556\n", "Optimization restart 9/10, f = -337.9105955093783\n", "Optimization restart 10/10, f = -337.4114574484434\n" ] } ], "source": [ "import TL_GPRSM.models.GPRSM as GPRSM\n", "\n", "gprsm = GPRSM(target_x, target_y, kernel_name=\"Matern52\")\n", "gprsm.set_transfer_learning(source_x, source_y)\n", "gprsm.optimize(max_iter=1e4)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluation\n", "Firest, get the ARD contribution." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2.11999180e+01 1.44326074e-01 2.73857023e+00 4.17810230e-01\n", " 1.91054986e+00 3.49176740e-02 5.60770130e+01 3.88342730e-02\n", " 1.74380606e+01]\n" ] } ], "source": [ "contributions = gprsm.get_ard_contribution()\n", "print(contributions)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Second, get a effect of transfer learning." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.3274171965641457\n" ] } ], "source": [ "tl_effect = gprsm.get_transfer_learning_effect()\n", "print(tl_effect)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Finally, evaluate with r2 index." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.9999885656886551\n" ] } ], "source": [ "import TL_GPRSM.utils.metrics as metrics\n", "\n", "test_x = sampling.latin_hypercube_sampling(10000, 3, False)\n", "test_x = sampling.uniform_scaling(test_x, np.array([7.0e10*0.9, 5000.0*0.8, 10000.0*0.8]), np.array([7.0e10*1.1, 5000.0*1.2, 10000.0*1.2]))\n", "test_y = np.array([beam_function(length, width, height, test_x[i,0], test_x[i,1], test_x[i,2]) for i in range(test_x.shape[0])])[:,np.newaxis]\n", "predict_y_mean, predict_y_std = gprsm.predict(test_x)\n", "r2 = metrics.r2_index(test_y, predict_y_mean)\n", "print(r2)" ] } ], "metadata": { "kernelspec": { "display_name": "surrogate_model", "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.8.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }