BSV 上的机器学习-基于SVD的图像售卖合约

受到这节 Bitcoin Class 的启发,我们将展示如何将机器学习技术应用于 BSV 。具体来说,我们将演示如何将奇异值分解(SVD)应用于低分辨率图像预览,并以此为基础实现对原始图像的无需信任购买。

奇异值分解 (SVD)

SVD 是一种矩阵分解类型,可将单个矩阵分解为矩阵U,∑和V*。

在这里插入图片描述

  • U和V*是正交矩阵。
  • ∑是奇异值的对角矩阵。

直觉上,它可以看作是将一个复杂的矩阵转换为3个更简单的矩阵(旋转,缩放和旋转),其中

  • 矩阵U和V*引起旋转
  • 对角矩阵∑引起缩放

数据压缩

一个典型的机器学习问题可能面对数百个或更多的变量,同时有很多机器学习算法能够接收和处理的变量不超过几十个,这使得在机器学习中奇异值分解对于减少变量是必不可少的。

在这里插入图片描述

出售更高分辨率的图像

假设一位摄影师想出售他的作品。他在网上发布了模糊的版本,例如上面的第三张图片。在看到预览后,一些感兴趣的买家可以在下面的合约中锁定一些资金(其中A是预览图像)。只有高分辨率原图的SVD可以解锁合约并赎回资金。卖方解锁后,买方可以通过计算U * ∑ * V来重建更清晰的图像。这项购买行为是由 BSV 网络强制执行的,因此是原子的。

type Matrix = int[N][N];

// a contract using Singular Value Decomposition
// One use case could be: given a low-resolution picture, a higher-resolution picture is needed to unlock it.
// After its SVD is shown, the needed picture can be reconstructed by calculating U*Sigma*V
// A picture is represented as a matrix
contract SVD {
    // lower quality approximation of the original matrix
    Matrix A;

    static const int N = 4;
    static const Matrix Identity = [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]];

    public function main(Matrix U, Matrix Sigma, Matrix V, int k) {
        // check SVD
        require(validate(Sigma, k));
        require(orthogonal(U));
        require(orthogonal(V));

        // we keep the first k values in S as is and set the subsequent singular values to 0
        loop (N) : i {
            if (i >= k) {
                Sigma[i][i] = 0;
            }
        }

        Matrix product = multiply(multiply(U, Sigma), V);
        require(product == this.A);
    }

    // a matrix is valid if and only if all following conditions are met
    // 1) it is diagonal: all elements are 0, except on the main diagonal
    // 2) contain more than k sigular values, i.e., closer to the original matrix
    // 3) sigular values are ordered non-increasingly
    static function validate(Matrix mat, int k) : bool {
        bool result = true;

        loop (N) : i {
            loop (N) : j {
                if (j == i) {
                    if (i > 1) {
                        // sigular values are ordered in non-increasing order
                        result = result && (mat[i][i] <= mat[i - 1][i - 1]);
                    }
                    if (i <= k) {
                        // there are over k sigular values
                        result = result && (mat[i][j] > 0);
                    }
                }
                else {
                    // all elements not on the main diagonal are 0
                    result = result && (mat[i][j] == 0);
                }
            }
        }

        return result;
    }

    // A * A^ = I
    static function orthogonal(Matrix mat) : bool {
        return multiply(mat, transpose(mat)) == Identity;
    }

    static function transpose(Matrix mat) : Matrix {
        Matrix mat1 = repeat(repeat(0, N), N);

        loop (N) : i {
            loop (N) : j {
                mat1[i][j] = mat[j][i];
            }
        }
        return mat1;
    }

    static function multiply(Matrix mat0, Matrix mat1) : Matrix {
        Matrix mat2 = repeat(repeat(0, N), N);

        loop (N) : i {
            loop (N) : j {
                loop (N) : k {
                    mat2[i][j] += mat0[i][k] * mat1[k][j];
                }
            }
        }

        return mat2;
    }
}

我们也可以轻松修改合约,比如图像质量越高,访问该图像所需要的价格就越高。数据也可以是其他类型的数字项目,例如视频,气候或财务数据。

扩展

SVD 除了数据压缩之外,还有许多其他应用。上述合约可以扩展至很多场景,例如:

总结

我们这里仅演示了一个在机器学习中应用智能合约的场景,相同的原理可以轻松地扩展到许多其他机器学习相关应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sCrypt Web3应用开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值