gpt4 book ai didi

c++ - 如何为 Kronecker-Produkt 实现张量类

转载 作者:行者123 更新时间:2023-11-27 22:41:55 27 4
gpt4 key购买 nike

<分区>

目前我看到一篇有趣的文章,叫做 Kronecker-Produkt .与此同时,我正在研究我的神经网络库。为了让我的算法起作用,我需要一个张量类,我可以在其中获得两个张量与重载 * 运算符的乘积。

考虑以下示例/问题:

  1. 如何高效地构造/存储嵌套矩阵?
  2. 如何计算两个张量的乘积?
  3. 如何尽可能简单地可视化张量 c?

我的第 3 类张量目前仅支持 3 个维度:

#pragma once

#include <iostream>
#include <sstream>
#include <random>
#include <cmath>
#include <iomanip>

template<typename T>
class tensor {
public:
const unsigned int x, y, z, s;

tensor(unsigned int x, unsigned int y, unsigned int z, T val) : x(x), y(y), z(z), s(x * y * z) {
p_data = new T[s];
for (unsigned int i = 0; i < s; i++) p_data[i] = val;
}

tensor(const tensor<T> & other) : x(other.x), y(other.y), z(other.z), s(other.s) {
p_data = new T[s];
memcpy(p_data, other.get_data(), s * sizeof(T));
}

~tensor() {
delete[] p_data;
p_data = nullptr;
}

T * get_data() {
return p_data;
}

static tensor<T> * random(unsigned int x, unsigned int y, unsigned int z, T val, T min, T max) {
tensor<T> * p_tensor = new tensor<T>(x, y, z, val);

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<T> dist(min, max);

for (unsigned int i = 0; i < p_tensor->s; i++) {
T rnd = dist(mt);
while (abs(rnd) < 0.001) rnd = dist(mt);
p_tensor->get_data()[i] = rnd;
}

return p_tensor;
}

static tensor<T> * from(std::vector<T> * p_data, T val) {
tensor<T> * p_tensor = new tensor<T>(p_data->size(), 1, 1, val);

for (unsigned int i = 0; i < p_tensor->get_x(); i++) p_tensor->set_data(i + 0 * p_tensor->get_x() * + 0 * p_tensor->get_x() * p_tensor->get_y(), p_data->at(i));

return p_tensor;
}

friend std::ostream & operator <<(std::ostream & stream, tensor<T> & tensor) {
stream << "(" << tensor.x << "," << tensor.y << "," << tensor.z << ") Tensor\n";

for (unsigned int i = 0; i < tensor.x; i++) {
for (unsigned int k = 0; k < tensor.z; k++) {
stream << "[";

for (unsigned int j = 0; j < tensor.y; j++) {
stream << std::setw(5) << roundf(tensor(i, j, k) * 1000) / 1000;
if (j + 1 < tensor.y) stream << ",";
}

stream << "]";

}

stream << std::endl;
}

return stream;
}

tensor<T> & operator +(tensor<T> & other) {
tensor<T> result(*this);

return result;
}

tensor<T> & operator -(tensor<T> & other) {
tensor<T> result(*this);

return result;
}

tensor<T> & operator *(tensor<T> & other) {
tensor<T> result(*this);

return result;
}

T & operator ()(unsigned int i, unsigned int j, unsigned int k) {
return p_data[i + (j * x) + (k * x * y)];
}

T & operator ()(unsigned int i) {
return p_data[i];
}

private:
T * p_data = nullptr;
};

int main() {
tensor<double> * p_tensor_input = tensor<double>::random(6, 2, 3, 0.0, 0.0, 1.0);
tensor<double> * p_tensor_weight = tensor<double>::random(2, 6, 3, 0.0, 0.0, 1.0);

std::cout << *p_tensor_input << std::endl;
std::cout << *p_tensor_weight << std::endl;

tensor<double> p_tensor_output = *p_tensor_input + *p_tensor_weight;

return 0;
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com