免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4014 | 回复: 6
打印 上一主题 下一主题

[C++] 矩阵乘 [复制链接]

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-02-01 00:32 |只看该作者 |倒序浏览
矩阵乘      

  1. #include <iostream>
  2. #include <glog/logging.h>

  3. #include "ATen/ATen.h"

  4. #include "torch/csrc/autograd/variable.h"
  5. #include "torch/csrc/assertions.h"
  6. #include "torch/csrc/autograd/generated/VariableType.h"
  7. #include "torch/csrc/autograd/generated/Functions.h"
  8. #include "torch/csrc/autograd/functions/accumulate_grad.h"
  9. #include "torch/csrc/autograd/functions/tensor.h"

  10. using namespace at;

  11. using namespace torch::autograd;

  12. //void test_tensor(Type & type);

  13. int main(int argc, char** argv)
  14. {
  15.     //Initialize Google's logging library.
  16.     //google::InitGoogleLogging(argv[0]);
  17.     //FLAGS_log_dir = "./log";

  18.     google::InitGoogleLogging(argv[0]);
  19.     google::LogToStderr();

  20.     LOG(INFO) << argv[0];

  21.     //test_tensor(CPU(kFloat));
  22.     //test_tensor(CPU(kDouble));

  23.     at::Type  &mv_type = at::CPU(at::kDouble);

  24.     //at::Tensor   ta = mv_type.rand({3,5});
  25.     //at::Tensor   tb = mv_type.rand({3,5});

  26.     double data01[] = { 1.0, 2.0, 3.0,
  27.                         4.0, 5.0, 6.3};

  28.     double data02[] = { 2.0, 3.0, 5.0,
  29.                         4.0, 2.5, 1.2};

  30.     at::Tensor  ta = mv_type.tensorFromBlob(data01, {2,3});
  31.     at::Tensor  tb = mv_type.tensorFromBlob(data02, {3,2});

  32.     at::Tensor  tc = mv_type.zeros({2,3});

  33.     std::cout << std::endl;

  34.     std::cout << "Tensor ta :  \n" << ta << "\n" << std::endl;
  35.     std::cout << "Tensor tb :  \n" << tb << "\n" << std::endl;

  36.     Variable  va(ta);
  37.     Variable  vb(tb);
  38.     //Variable  vc(tc);

  39.     //vc = va * vb;
  40.     //vc = va.mul(vb);
  41.     //vc = va.div(vb);
  42.     auto  vc = va.matmul(vb);

  43.     //at::Type  &va_type = at::CPU(at::kDouble);
  44.    
  45.     std::cout << "Variable vc :  \n" << vc << "\n" << std::endl;


  46.     return 0;
  47. }
复制代码





论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
2 [报告]
发表于 2018-02-01 17:41 |只看该作者
十分 的 佩服

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
3 [报告]
发表于 2018-03-02 15:27 |只看该作者
其实这个就是 pytorch 底层的 C 模块,最近这一个月主要是在移植和调试 win10 mingw 环境下的 pytorch。
今天终于调通了。

我终于把 C 模块和 python 连上了。
  1. import time
  2. import torch
  3. from torch.autograd import Variable

  4. torch.set_num_threads(2)
  5. num_threads = torch.get_num_threads()
  6. print("num_threads {}".format(num_threads))

  7. #dtype = torch.FloatTensor
  8. dtype = torch.DoubleTensor
  9. # dtype = torch.cuda.FloatTensor # Uncomment this to run on GPU

  10. # N is batch size; D_in is input dimension;
  11. # H is hidden dimension; D_out is output dimension.
  12. N, D_in, H, D_out = 64, 1000, 100, 10

  13. # Create random Tensors to hold input and outputs, and wrap them in Variables.
  14. # Setting requires_grad=False indicates that we do not need to compute gradients
  15. # with respect to these Variables during the backward pass.
  16. x = Variable(torch.randn(N, D_in).type(dtype), requires_grad=False)
  17. y = Variable(torch.randn(N, D_out).type(dtype), requires_grad=False)

  18. # Create random Tensors for weights, and wrap them in Variables.
  19. # Setting requires_grad=True indicates that we want to compute gradients with
  20. # respect to these Variables during the backward pass.
  21. w1 = Variable(torch.randn(D_in, H).type(dtype), requires_grad=True)
  22. w2 = Variable(torch.randn(H, D_out).type(dtype), requires_grad=True)

  23. timer_up = time.time()

  24. learning_rate = 1e-6
  25. for t in range(1000):
  26.     # Forward pass: compute predicted y using operations on Variables; these
  27.     # are exactly the same operations we used to compute the forward pass using
  28.     # Tensors, but we do not need to keep references to intermediate values since
  29.     # we are not implementing the backward pass by hand.
  30.     y_pred = x.mm(w1).clamp(min=0).mm(w2)

  31.     # Compute and print loss using operations on Variables.
  32.     # Now loss is a Variable of shape (1,) and loss.data is a Tensor of shape
  33.     # (1,); loss.data[0] is a scalar value holding the loss.
  34.     loss = (y_pred - y).pow(2).sum()
  35.     #print(t, loss.data[0])
  36.     print(t, loss.item())

  37.     # Use autograd to compute the backward pass. This call will compute the
  38.     # gradient of loss with respect to all Variables with requires_grad=True.
  39.     # After this call w1.grad and w2.grad will be Variables holding the gradient
  40.     # of the loss with respect to w1 and w2 respectively.
  41.     loss.backward()

  42.     # Update weights using gradient descent; w1.data and w2.data are Tensors,
  43.     # w1.grad and w2.grad are Variables and w1.grad.data and w2.grad.data are
  44.     # Tensors.
  45.     w1.data -= learning_rate * w1.grad.data
  46.     w2.data -= learning_rate * w2.grad.data

  47.     # Manually zero the gradients after updating weights
  48.     w1.grad.data.zero_()
  49.     w2.grad.data.zero_()

  50. timer = time.time() - timer_up
  51. print("timer : {0:.3}s".format(timer))
复制代码

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
4 [报告]
发表于 2018-03-02 19:25 |只看该作者
铁甲将军夜移植,
日上三竿还调试,
看来终于调通了。
就是十分的佩服

评分

参与人数 1信誉积分 +9 收起 理由
zhujiang73 + 9 神马都是浮云

查看全部评分

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
5 [报告]
发表于 2018-03-02 22:20 |只看该作者
dorodaloo 发表于 2018-03-02 19:25
铁甲将军夜移植,
日上三竿还调试,
看来终于调通了。

      惭愧,其实就是想“辛苦干几天,舒服大半年。”   

      下一步还要解决 OpenCL 计算加速,舒服大半年计划又得推迟了。

论坛徽章:
24
狮子座
日期:2013-12-31 10:48:0015-16赛季CBA联赛之吉林
日期:2016-04-18 14:43:1015-16赛季CBA联赛之北控
日期:2016-05-18 15:01:4415-16赛季CBA联赛之上海
日期:2016-06-22 18:00:1315-16赛季CBA联赛之八一
日期:2016-06-25 11:02:2215-16赛季CBA联赛之佛山
日期:2016-08-17 22:48:2615-16赛季CBA联赛之福建
日期:2016-12-27 22:39:272016科比退役纪念章
日期:2017-02-08 23:49:4315-16赛季CBA联赛之八一
日期:2017-02-16 01:05:3415-16赛季CBA联赛之山东
日期:2017-02-22 15:34:5615-16赛季CBA联赛之上海
日期:2017-11-25 16:17:5015-16赛季CBA联赛之四川
日期:2016-01-17 18:38:37
6 [报告]
发表于 2018-03-04 00:41 |只看该作者
   忙了一个月 pytorch_mingw  终于调通了,代码放在 GitHub :[url]https://github.com/zhujiang73/pytorch_mingw[/url]

    pytorch_mingw : pytorch cpu_only on windows mingw (msys2/win10)  

    Based on pytorch Tensors and Dynamic neural networks in Python  
pytorch_mingw

  pytorch cpu_only on windows mingw (msys2/win10)

  Based on pytorch :  https://github.com/pytorch/pytorch

  msys2 :  http://www.msys2.org


  pytorch_mingw  install :

      win10  cmd  console :
            
        cd pytorch_mingw
        
        md build

        cd build

        cmake -G "MinGW Makefiles"  .. -DCMAKE_INSTALL_PREFIX="c:\mingw"

        mingw32-make  install

  
  examples  run :
      
     win10  cmd  console :

       cd  pytorch_mingw\examples

       python3  var_back.py


论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
7 [报告]
发表于 2018-03-04 17:00 |只看该作者
精华
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP