这些天看了一些关于采样矩阵(大概是这么翻译的)的论文,简单做个总结。

  • FAST MONTE CARLO ALGORITHMS FOR MATRICES I: APPROXIMATING MATRIX MULTIPLICATION

算法如下:

目的是为了毕竟矩阵的乘积AB, 以CR来替代。

其中右上角带有i_t的A表示A的第i_t列,右下角带有i_t的B表示B的第i_t行。

关于 c 的选择,以及误差的估计,请回看论文。

下面是一个小小的测试:

代码:

import numpy as np

def Generate_P(A, B): #生成概率P
try:
n1 = len(A[1,:])
n2 = len(B[:,1])
if n1 == n2:
n = n1
else:
print('Bad matrices')
return 0
except:
print('The matrices are not fit...')
A_New = np.square(A)
B_New = np.square(B)
P_A = np.array([np.sqrt(np.sum(A_New[:,i])) for i in range(n)])
P_B = np.array([np.sqrt(np.sum(B_New[i,:])) for i in range(n)])
P = P_A * P_B / (np.sum(P_A * P_B))
return P def Generate_S(n, c, P): #生成采样矩阵S 简化了一下算法
S = np.zeros((n, c))
T = np.random.choice(np.array([i for i in range(n)]), size = c, replace = True, p = P)
for i in range(c):
S[T[i], i] = 1 / np.sqrt(c * P[T[i]]) return S def Summary(times, n, c, P, A_F, B_F, AB): #总结和分析
print('{0:^15} {1:^15} {2:^15} {3:^15} {4:^15} {5:^15} {6:^15}'.format('A_F', 'B_F', 'NEW_F', 'A_F * B_F', 'AB_F', 'RATIO', 'RATIO2'))
print('{0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15}'.format(''))
A_F_B_F = A_F * B_F
AB_F = np.sqrt(np.sum(np.square(AB)))
Max = -1
Min = 99999999999
Max2 = -1
Min2 = 99999999999
Max_NEW_F = 0
Min_NEW_F = 0
Mean_NEW_F = 0
Mean_ratio = 0
Mean_ratio2 = 0
for i in range(times):
S = Generate_S(n, c, P)
CR = np.dot(A.dot(S), (S.T).dot(B))
NEW = AB - CR
NEW_F = np.sqrt(np.sum(np.square(NEW)))
ratio = NEW_F / A_F_B_F
ratio2 = NEW_F / AB_F
Mean_NEW_F += NEW_F
Mean_ratio += ratio
Mean_ratio2 += ratio2
if ratio > Max:
Max = ratio
Max2 = ratio2
Max_NEW_F = NEW_F
if ratio < Min:
Min = ratio
Min2 = ratio2
Min_NEW_F = NEW_F
print('{0:^15.5f} {1:^15.5f} {2:^15.5f} {3:^15.5f} {4:^15.5f} {5:^15.3%} {6:^15.3%}'.format(A_F, B_F, NEW_F, A_F_B_F, AB_F, ratio, ratio2))
Mean_NEW_F = Mean_NEW_F / times
Mean_ratio = Mean_ratio / times
Mean_ratio2 = Mean_ratio2 / times
print('{0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15}'.format(''))
print('{0:^15.5f} {1:^15.5f} {2:^15.5f} {3:^15.5f} {4:^15.5f} {5:^15.3%} {6:^15.3%}'.format(A_F, B_F, Mean_NEW_F, A_F_B_F, AB_F, Mean_ratio, Mean_ratio2))
print('{0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15} {0:-<15}'.format(''))
print('Count: {0} times'.format(times))
print('Max_ratio: {0:<15.3%} Min_ratio: {1:<15.3%}'.format(Max, Min))
print('Max_ratio2: {0:<15.3%} Min_ratio2: {1:<15.3%}'.format(Max2, Min2))
print('Max_NEW_F: {0:<15.5f} Min_NEW_F: {1:<15.5f}'.format(Max_NEW_F, Min_NEW_F)) #下面是关于矩阵行列的一些参数,我是采用均匀分布产生的矩阵
m = 47
n = 120
p = 55
A = np.array([[np.random.rand() * 100 for j in range(n)] for i in range(m)])
B = np.array([[np.random.rand() * 100 for j in range(p)] for i in range(n)]) #构建c的一些参数 这个得参考论文
Thelta = 1/4
Belta = 1
Yita = 1 + np.sqrt((8/Belta * np.log(1/Thelta)))
e = 1/5
c = int(1 / (Belta * e ** 2)) + 1
P = Generate_P(A, B) #结果分析
AB = A.dot(B)
A_F = np.sqrt(np.sum(np.square(A)))
B_F = np.sqrt(np.sum(np.square(B)))
times = 1000
Summary(times, n, c, P, A_F, B_F, AB)

粗略的结果:

用了原矩阵的一半的维度,代价是约17%的误差。

用正态分布生成矩阵的时候,发现,如果是标准正态分布,效果很差,我猜是由计算机舍入误差引起的,这样的采样的性能不好。当均值增加的时候,和”均匀分布“差不多,甚至更优(F范数的意义上)。

补充:

















Sampling Matrix的更多相关文章

  1. 【NLP】Conditional Language Modeling with Attention

    Review: Conditional LMs Note that, in the Encoder part, we reverse the input to the ‘RNN’ and it per ...

  2. Sampling Distributions and Central Limit Theorem in R(转)

    The Central Limit Theorem (CLT), and the concept of the sampling distribution, are critical for unde ...

  3. [LeetCode] Random Flip Matrix 随机翻转矩阵

    You are given the number of rows n_rows and number of columns n_cols of a 2D binary matrix where all ...

  4. 【RS】Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering - 基于拉普拉斯分布的稀疏概率矩阵分解协同过滤

    [论文标题]Sparse Probabilistic Matrix Factorization by Laplace Distribution for Collaborative Filtering  ...

  5. 470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)

    1. 问题 已提供一个Rand7()的API可以随机生成1到7的数字,使用Rand7实现Rand10,Rand10可以随机生成1到10的数字. 2. 思路 简单说: (1)通过(Rand N - 1) ...

  6. [Python] 01 - Number and Matrix

    故事背景 一.大纲 如下,chapter4 是个概览,之后才是具体讲解. 二. 编译过程 Ref: http://www.dsf.unica.it/~fiore/LearningPython.pdf

  7. 目录:Matrix Differential Calculus with Applications in Statistics and Econometrics,3rd_[Magnus2019]

    目录:Matrix Differential Calculus with Applications in Statistics and Econometrics,3rd_[Magnus2019] Ti ...

  8. 【论文笔记】SamWalker: Social Recommendation with Informative Sampling Strategy

    SamWalker: Social Recommendation with Informative Sampling Strategy Authors: Jiawei Chen, Can Wang, ...

  9. angular2系列教程(十一)路由嵌套、路由生命周期、matrix URL notation

    今天我们要讲的是ng2的路由的第二部分,包括路由嵌套.路由生命周期等知识点. 例子 例子仍然是上节课的例子:

随机推荐

  1. [20190213]学习bbed-恢复删除的数据.txt

    [20190213]学习bbed-恢复删除的数据.txt --//以前也做过类似测试,当时在用bbed做verify时错误都不处理,当时的想法就是能读出就ok了.--//而且当时也做成功,纯粹是依葫芦 ...

  2. Linux进程优先级的处理--Linux进程的管理与调度(二十二)

    1. linux优先级的表示 1.1 优先级的内核表示 linux优先级概述 在用户空间通过nice命令设置进程的静态优先级, 这在内部会调用nice系统调用, 进程的nice值在-20~+19之间. ...

  3. 文件服务器HFS

    用途:用于文件共享(类似FTP) 官网:http://www.rejetto.com/hfs/?f=ss 优点:比起apache,它有界面方便查看当前正连接的主机 如果在内网环境下进行文件共享的话,可 ...

  4. 详解PHP中的过滤器(Filter)

    PHP 过滤器用于验证和过滤来自非安全来源的数据,比如用户的输入. 什么是 PHP 过滤器? PHP 过滤器用于验证和过滤来自非安全来源的数据. 验证和过滤用户输入或自定义数据是任何 Web 应用程序 ...

  5. php学习----文件系统

    PHP文件系统之读取文件内容 PHP具有丰富的文件操作函数,最简单的读取文件的函数为file_get_contents,可以将整个文件全部读取到一个字符串中. $content = file_get_ ...

  6. vue开发常见命令

    1.安装脚手架 安装脚手架命令:npm install -global vue-cli 2.升级脚手架 有时候需要把整个脚手架升级一下,这个用到命令npm install --global vue-c ...

  7. java用JDBC连接MySQL数据库的详细知识点

    想实现java用JDBC连接MySQL数据库.需要有几个准备工作: 1.下载Connector/J的库文件,下载Connector/J的官网地址:http://www.mysql.com/downlo ...

  8. 【CQOI2011】放棋子

    [CQOI2011]放棋子 在一个n行m列的棋盘里放一些彩色的棋子,使得每个格子最多放一个棋子,且不同颜色的棋子不能在同一行或者同一列.有多少种方法? 例如\(,n=m=3\),有两个白棋子和一个灰棋 ...

  9. Jenkins+Ansible+Gitlab自动化部署三剑客-Jenkins本地搭建

    后面需要shell基础,目前没有,等有了,再更

  10. ElasticSearch(五):Java操作ElasticSearch执行查询

    package com.gxy.ESChap01; import java.net.InetAddress; import org.elasticsearch.action.search.Search ...