1、安装anaconda
Index of /anaconda/archive/ | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
2、安装pycharm,安装Community版本的
Download PyCharm: Python IDE for Professional Developers by JetBrains
3、在anaconda中创建python环境
1】创建一个masr_env(名字可改)环境: conda create -n masr_env python=3.6
2】激活该环境:conda activate masr_env
3】安装其他的库:
conda install pandas
conda install numpy
conda install matplotlib
有时候网速太慢,可替换国内的下载源:
两个可用的镜像,下载时加上即可
https://pypi.tuna.tsinghua.edu.cn/simple
https://pypi.douban.com/simple
安装
pip install keras==2.2.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy==1.16.4 -i https://pypi.tuna.tsinghua.edu.cn/simple (tf1.x时)
pip install pandas==1.1.5 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install scikit-learn -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install matplotlib==3.3.4 -i https://pypi.tuna.tsinghua.edu.cn/simple
注:安装方式有两种,一种使用,conda install pandas,或者,pip install pandas。另外版本对应关系tf2.3时安装对应的numpy==1.18.5和pandas==1.1.5和matplotlib==3.3.4。
6】查看当前有哪些环境: conda env list
7】删除环境:conda remove -n masr_env --all
8】查看:conda search cudnn 和 conda search cuda
3.1、安装pytorch
pytorch的whl的链接:
https://download.pytorch.org/whl/torch_stable.html
【1】gpu安装参考下面链接
python=3.6,pytorch=1.7,
conda install cudatoolkit=11.0
conda install cudnn=8.1
(带你避坑)win10安装带CUDA的Pytorch看这篇就够了_aquapisces的博客-CSDN博客_pytorch自带cuda
GPU版本pyTorch安装教程(实测,特别详细)_楊龘龘的博客-CSDN博客
GPU版本PyTorch详细安装教程_吉果果、的博客-CSDN博客_pytorch gpu安装
【2】安装CPU版本
1、查看pytorch,torchvision,python 版本对应:
PyTorch深度学习(13)PyTorch Torch Vision python 版本对应_JYliangliang的博客-CSDN博客_pytorch与python版本对应
2、pip install torch===1.8.0 torchvision===0.9.0 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.douban.com/simple
3.2、安装tensorflow
tensorflow的whl的链接:
【1】gpu版本参考如下链接:
python=3.6 tf=1.14
conda install cudatoolkit=10.0
conda install cudnn=7.6
tensorflow1.4安装记录_fatfatfred的博客-CSDN博客_tensorflow1.4安装
Tensorflow-gpu安装教程_江江想做程序员的博客-CSDN博客_tensorflow gpu安装
【2】安装cpu版本的tensorflow
在命令行输入 conda install tensorflow=版本号,即可安装CPU版本。使用pip安装需要输入 pip install tensorflow==版本号
安装太慢的时候,使用:pip install tensorflow-cpu==2.3.0 -i https://pypi.douban.com/simple/
windows下完全离线安装Anaconda+Tensorflow_09踏雪飞鸿的博客-CSDN博客_anaconda 离线安装
4、打开pycharm写代码
1】导入环境
5、神经网络代码
import torch
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from torch.autograd import Variable
x = torch.unsqueeze(torch.linspace(-1,1,100),dim=1)
y = x.pow(3)+0.1*torch.randn(x.size())
x , y =(Variable(x),Variable(y))
plt.scatter(x.data,y.data)
# 或者采用如下的方式也可以输出x,y
# plt.scatter(x.data.numpy(),y.data.numpy())
plt.show()
class Net(nn.Module):
def __init__(self,n_input,n_hidden,n_output):
super(Net,self).__init__()
self.hidden1 = nn.Linear(n_input,n_hidden)
self.hidden2 = nn.Linear(n_hidden,n_hidden)
self.predict = nn.Linear(n_hidden,n_output)
def forward(self,input):
out = self.hidden1(input)
out = F.relu(out)
out = self.hidden2(out)
out = F.sigmoid(out)
out =self.predict(out)
return out
net = Net(1,20,1)
print(net)
optimizer = torch.optim.SGD(net.parameters(),lr = 0.1)
loss_func = torch.nn.MSELoss()
for t in range(5000):
prediction = net(x)
loss = loss_func(prediction,y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if t%5 ==0:
plt.cla()
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), prediction.data.numpy(), 'r-', lw=5)
plt.text(0.5, 0, 'Loss = %.4f' % loss.data, fontdict={'size': 20, 'color':'red'})
plt.pause(0.05)
参考