Skip to content

Instantly share code, notes, and snippets.

@luowanqian
Last active May 3, 2019
Embed
What would you like to do?
文章:快速生成Kernel Matrix,文章链接:http://www.scutmath.com/fast_kernel_matrix_generation.html
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:39:43.429138Z",
"start_time": "2019-04-19T11:39:43.347068Z"
}
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__实验说明__\n",
"\n",
"多种方法生成核矩阵,本实验采用的核函数为高斯核函数(Gauss Kernel)\n",
"$$\n",
"K(x,y) = e^{-\\frac{\\Vert x - y \\Vert^2}{2\\sigma^2}}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__实验参数__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:39:43.437691Z",
"start_time": "2019-04-19T11:39:43.433124Z"
}
},
"outputs": [],
"source": [
"num_samples1 = 1000\n",
"num_samples2 = 5000\n",
"dims = 100\n",
"\n",
"sigma = 10"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__生成随机数据__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:39:43.472345Z",
"start_time": "2019-04-19T11:39:43.440679Z"
}
},
"outputs": [],
"source": [
"X1 = np.random.randn(num_samples1, dims)\n",
"X2 = np.random.randn(num_samples2, dims)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__循环遍历生成__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:39:43.482777Z",
"start_time": "2019-04-19T11:39:43.476416Z"
}
},
"outputs": [],
"source": [
"def method1(X1, X2, sigma=1.0):\n",
" num_samples1, dims = X1.shape\n",
" num_samples2, dims = X2.shape\n",
" mat = np.empty((num_samples1, num_samples2))\n",
" \n",
" for i in range(num_samples1):\n",
" for j in range(num_samples2):\n",
" vec = X1[i, :] - X2[j, :]\n",
" mat[i, j] = np.exp(-0.5 / sigma**2 * np.dot(vec, vec))\n",
" \n",
" return mat"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:41:24.343261Z",
"start_time": "2019-04-19T11:39:43.486186Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"25.1 s ± 66.8 ms per loop (mean ± std. dev. of 3 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -r 3\n",
"mat1 = method1(X1, X2, sigma)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__矩阵向量运算生成1__"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:41:24.351647Z",
"start_time": "2019-04-19T11:41:24.345308Z"
}
},
"outputs": [],
"source": [
"def method2(X1, X2, sigma=1.0):\n",
" X1_sum = np.sum(X1**2, axis=1)\n",
" X2_sum = np.sum(X2**2, axis=1)\n",
" mat1 = np.dot(np.reshape(X1_sum, (X1_sum.shape[0], 1)),\n",
" np.ones((1, X2_sum.shape[0])))\n",
" mat2 = np.dot(np.ones((X1_sum.shape[0], 1)),\n",
" np.reshape(X2_sum, (1, X2_sum.shape[0])))\n",
" mat = mat1 + mat2 - 2 * np.dot(X1, X2.T)\n",
" mat = np.exp(-0.5 / sigma**2 * mat)\n",
" \n",
" return mat"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:41:24.998373Z",
"start_time": "2019-04-19T11:41:24.353801Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"132 ms ± 2.01 ms per loop (mean ± std. dev. of 3 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit -r 3\n",
"mat2 = method2(X1, X2, sigma)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__矩阵向量运算生成2__"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:41:25.005161Z",
"start_time": "2019-04-19T11:41:25.000510Z"
}
},
"outputs": [],
"source": [
"def method3(X1, X2, sigma=1.0):\n",
" mat = np.sum(X1**2, 1).reshape(-1, 1) + np.sum(X2**2, 1) - 2 * np.dot(X1, X2.T)\n",
" mat = np.exp(-0.5 / sigma**2 * mat)\n",
" \n",
" return mat"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2019-04-19T11:41:29.405329Z",
"start_time": "2019-04-19T11:41:25.007547Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"106 ms ± 330 µs per loop (mean ± std. dev. of 3 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit -r 3\n",
"mat3 = method3(X1, X2, sigma)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.0"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment