forked from yuanxiao7/yolov5-pytorch-main
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
114 lines (68 loc) · 2.43 KB
/
board.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import torch
from torch.autograd import Variable
import torch.nn.functional as functional
from tensorboardX import SummaryWriter
# from torch.utils.tensorboard import SummaryWriter
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
# x的shape为(100,1)
x = torch.from_numpy(np.linspace(-1, 1, 100).reshape([100, 1])).type(torch.FloatTensor)
# y的shape为(100,1)
y = torch.sin(x) + 0.2 * torch.rand(x.size())
class Net(torch.nn.Module):
def __init__(self, n_feature, n_hidden, n_output):
super(Net, self).__init__()
# Applies a linear transformation to the incoming data: :math:y = xA^T + b
# 全连接层,公式为y = xA^T + b
self.hidden = torch.nn.Linear(n_feature, n_hidden)
self.predict = torch.nn.Linear(n_hidden, n_output)
def forward(self, x):
# 隐含层的输出
hidden_layer = functional.relu(self.hidden(x))
output_layer = self.predict(hidden_layer)
return output_layer
# 类的建立
net = Net(n_feature=1, n_hidden=10, n_output=1)
writer = SummaryWriter('logs')
graph_inputs = torch.from_numpy(np.random.rand(2, 1)).type(torch.FloatTensor)
writer.add_graph(net, (graph_inputs,))
# torch.optim是优化器模块
optimizer = torch.optim.Adam(net.parameters(), lr=1e-3)
# 均方差loss
loss_func = torch.nn.MSELoss()
for t in range(1000):
prediction = net(x)
loss = loss_func(prediction, y)
# 反向传递步骤
# 1、初始化梯度
optimizer.zero_grad()
# 2、计算梯度
loss.backward()
# 3、进行optimizer优化
optimizer.step()
writer.add_scalar('loss', loss, t)
writer.close()
'''
from tensorboardX import SummaryWriter
import random
writer = SummaryWriter('./runs/examples')
for i in range(100):
writer.add_scalar('example1', i ** 2, global_step=i)
writer.add_scalar('example2', random.random(), global_step=i)
'''
'''
from torch.utils.tensorboard import SummaryWriter
if __name__ == '__main__':
writer = SummaryWriter(log_dir="runs/result_1", flush_secs=120)
for n_iter in range(100):
writer.add_scalar(tag='Loss/train',
scalar_value=np.random.random(),
global_step=n_iter)
writer.add_scalar('Loss/test', np.random.random(), n_iter)
writer.close()
'''
# import tensorflow as tf
# hello =tf.constant('hello, tensorflow')
# print('Hello python')
# sess = tf.compat.v1.Session()