# Anaconda-pytorch-pycharm安装配置

## Anaconda Python3.6 version安装

from future import print_function
#import os
#os.environ[“KMP_DUPLICATE_LIB_OK”]=“TRUE”
from itertools import count
import numpy as np
import torch

import torch.nn.functional as F
import matplotlib.pyplot as plt

random_state = 5000
torch.manual_seed(random_state)
poly_degree = 4
W_target = torch.randn(poly_degree, 1) * 5
b_target = torch.randn(1) * 5

def make_features(x):
“”"

“”"
x = x.unsqueeze(1)
return torch.cat([x ** i for i in range(1, poly_degree + 1)], 1)

def f(x):
“”“近似函数”""
return x.mm(W_target) + b_target[0]

def poly_desc(W, b):
“”“生成多项式描述内容”""
result = 'y = ’
for i, w in enumerate(W):
result += '{:+.2f} x^{} '.format(w, len(W) - i)
result += ‘{:+.2f}’.format(b[0])
return result

def get_batch(batch_size=32):
“”“创建类似(x, f(x))批数据”""
random = torch.from_numpy(np.sort(torch.randn(batch_size)))
x = make_features(random)
y = f(x)
return Variable(x), Variable(y)

#声明模型
fc = torch.nn.Linear(W_target.size(0), 1)

for batch_idx in count(1):
# 获取数据
batch_x, batch_y = get_batch()
# 重制求导

# 前向传播
output = F.smooth_l1_loss(fc(batch_x), batch_y)
loss = output.item()

# 后向传播
output.backward()

# 应用导数
for param in fc.parameters():

# 停止条件
if loss < 1e-3:
plt.cla()
plt.scatter(batch_x.data.numpy()[:, 0], batch_y.data.numpy()[:, 0], label='real curve', color='b')
plt.plot(batch_x.data.numpy()[:, 0], fc(batch_x).data.numpy()[:, 0], label='fitting curve', color='r')
plt.title('$Y=W^T*X+b$')
plt.legend()
plt.savefig('1.png')
plt.show()
break

print(‘Loss: {:.6f} after {} batches’.format(loss, batch_idx))
print(’> Learned function:\t’ + poly_desc(fc.weight.data.view(-1), fc.bias.data))
print(’
> Actual function:\t’ + poly_desc(W_target.view(-1), b_target))
`

## 嵌入开发工具Pycharm

• 0
点赞
• 4
收藏
• 打赏
• 0
评论
04-26 5万+
03-06 2万+
09-03 2619
10-01 1万+
12-04 483
07-01 1674
10-04 6360
08-15 3112
03-29 19
09-29 5919

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

YY吖~

¥2 ¥4 ¥6 ¥10 ¥20

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