鱼C论坛

 找回密码
 立即注册
查看: 1361|回复: 9

[已解决]Python实现灰色预测过程中的错误

[复制链接]
发表于 2020-5-21 22:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
感谢热心人士回答。
代码如下,运行时出现错误:
  1. Traceback (most recent call last):
  2.   File "C:/Users/lenovo/Desktop/shishi.py", line 2, in <module>
  3.     import torch as th
  4. ModuleNotFoundError: No module named 'torch'
复制代码


然后用cmd执行以下操作:

cmd运行中出错及错误信息

cmd运行中出错及错误信息


程序代码:

  1. # condig:utf-8
  2. import torch as th
  3. import numpy as np
  4. class GM():

  5.     def __init__(self):
  6.         # 判断是否可用 gpu 编程 , 大量级计算使用GPU
  7.         self._is_gpu = False  # th.cuda.is_available()

  8.     def fit(self,dt:list or np.ndarray):
  9.         self._df :th.Tensor = th.from_numpy(np.array(dt,dtype=np.float32))

  10.         if self._is_gpu:
  11.             self._df.cuda()

  12.         self._n:int = len(self._df)

  13.         self._x,self._max_value = self._sigmod(self._df)

  14.         z:th.Tensor = self._next_to_mean(th.cumsum(self._x,dim=0))

  15.         self.coef:th.Tensor = self._coefficient(self._x, z)

  16.         del z

  17.         self._x0:th.Tensor = self._x[0]

  18.         self._pre:th.Tensor = self._pred()

  19.     # 归一化
  20.     def _sigmod(self,x:th.Tensor):
  21.         _maxv:th.Tensor = th.max(x)
  22.         return th.div(x,_maxv),_maxv

  23.     # 计算紧邻均值数列
  24.     def _next_to_mean(self, x_1:th.Tensor):

  25.         z:th.Tensor = th.zeros(self._n-1)
  26.         if self._is_gpu:
  27.             z.cuda()

  28.         for i in range(1,self._n):  # 下标从0开始,取不到最大值
  29.             z[i - 1] = 0.5 * x_1[i] + 0.5 * x_1[i - 1]

  30.         return z

  31.     # 计算系数 a,b
  32.     def _coefficient(self,x:th.Tensor,z:th.Tensor):

  33.         B:th.Tensor = th.stack((-1*z, th.ones(self._n-1)),dim=1)

  34.         Y:th.Tensor = th.tensor(x[1:],dtype=th.float32).reshape((-1,1))

  35.         if self._is_gpu:
  36.             B.cuda()
  37.             Y.cuda()

  38.         # 返回的是a和b的向量转置,第一个是a 第二个是b;
  39.         return th.matmul(th.matmul(th.inverse(th.matmul(B.t(), B)), B.t()),Y)


  40.     def _pred(self,start:int=1,end:int=0):

  41.         les:int = self._n+end

  42.         resut:th.Tensor = th.zeros(les)

  43.         if self._is_gpu:
  44.             resut.cuda()

  45.         resut[0] = self._x0

  46.         for i in range(start,les):
  47.             resut[i] = (self._x0 - (self.coef[1] / self.coef[0])) * \
  48.                             (1 - th.exp(self.coef[0])) * th.exp(-1 * self.coef[0] * (i))
  49.         del les
  50.         return resut

  51.     # 计算绝对误差
  52.     def confidence(self):
  53.         return round((th.sum(th.abs(th.div((self._x-self._pre),self._x)))/self._n).item(),4)

  54.     # 预测个数,默认个数大于等于0,
  55.     def predict(self,m:int=1,decimals:int=4):

  56.         y_pred:th.Tensor = th.mul(self._pre,self._max_value)

  57.         y_pred_ = th.zeros(1)

  58.         if m<0:
  59.             return "预测个数需大于等于0"
  60.         elif m>0:
  61.             y_pred_:th.Tensor = self._pred(self._n,m)[-m:].mul(self._max_value)
  62.         else:
  63.             if self._is_gpu:
  64.                 return list(map(lambda _: round(_, decimals), y_pred.cpu().numpy().tolist()))
  65.             else:
  66.                 return list(map(lambda _:round(_,decimals),y_pred.numpy().tolist()))

  67.         # cat 拼接 0 x水平拼接,1y垂直拼接
  68.         result:th.Tensor = th.cat((y_pred,y_pred_),dim=0)

  69.         del y_pred,y_pred_

  70.         if self._is_gpu:
  71.             return list(map(lambda _: round(_, decimals), result.cpu().numpy().tolist()))

  72.         return list(map(lambda _:round(_,decimals),result.numpy().tolist()))



  73. if __name__=="__main__":
  74.     ls = np.arange(91,100,2)
  75.     print(type(ls))
  76.     # ls = list(range(91, 100, 2))
  77.     gm = GM()
  78.     gm.fit(ls)
  79.     print(gm.confidence())
  80.     print(ls)
  81.     print(gm.predict(m=2))
复制代码
最佳答案
2020-5-21 23:17:16
https://pypi.org/project/torch/#files
官网下载 whl 文件 然后存放到桌面,文件名不用动
打开CMD 输入
  1. cd Desktop
复制代码
回车 然后再输入:
  1. pip install 文件名
复制代码

把文件名替换成 你刚刚下载的 whl 完整文件名(注意要带上后缀)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-21 22:16:56 | 显示全部楼层
以管理员身份运行cmd
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-21 22:17:14 | 显示全部楼层
命令行:
  1. pip install -i http://pypi.douban.com/simple --trusted-host pypi.douban.com torch
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-21 22:18:40 | 显示全部楼层
  1. python -m pip install torch -i https://pypi.tuna.tsinghua.edu.cn/simple
复制代码

试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-21 22:21:28 | 显示全部楼层
国内镜像(第二条)
pip装不上模块?快来这看看!(非常全!!!)
https://fishc.com.cn/thread-167155-1-1.html
(出处: 鱼C论坛)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-21 22:39:54 | 显示全部楼层

你好,感谢回复,但是仍然是红色报错。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-21 22:40:31 | 显示全部楼层
liuzhengyuan 发表于 2020-5-21 22:21
国内镜像(第二条)
pip装不上模块?快来这看看!(非常全!!!)
https://fishc.com.cn/thread-167155- ...

你好,我看了一下,但是仍然没解决。

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-21 23:17:16 | 显示全部楼层    本楼为最佳答案   
https://pypi.org/project/torch/#files
官网下载 whl 文件 然后存放到桌面,文件名不用动
打开CMD 输入
  1. cd Desktop
复制代码
回车 然后再输入:
  1. pip install 文件名
复制代码

把文件名替换成 你刚刚下载的 whl 完整文件名(注意要带上后缀)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-21 23:25:49 | 显示全部楼层
Twilight6 发表于 2020-5-21 23:17
https://pypi.org/project/torch/#files
官网下载 whl 文件 然后存放到桌面,文件名不用动
打开CMD 输入 ...

感谢,下载过程中进度条完全不动正常吗?几分钟了仍然是700K
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-21 23:30:05 | 显示全部楼层
明月小仙 发表于 2020-5-21 23:25
感谢,下载过程中进度条完全不动正常吗?几分钟了仍然是700K

https://pypi.tuna.tsinghua.edu.cn/simple/torch/
那去这里下载
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-20 00:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表