当前位置: 首页 > news >正文

机关单位网站建设合同手机网站模板建站

机关单位网站建设合同,手机网站模板建站,《电子商务网站开发》实验报告,大连建站免费模板文章目录1.简单回归实战:2.手写数据识别1.简单回归实战: 用 线性回归拟合二维平面中的100个点 公式:ywxbywxbywxb 损失函数:∑(yreally−y)2\sum(y_{really}-y)^2∑(yreally​−y)2 迭代方法:梯度下降法,…

文章目录

      • 1.简单回归实战:
      • 2.手写数据识别

1.简单回归实战:

用 线性回归拟合二维平面中的100个点
在这里插入图片描述
公式:y=wx+by=wx+by=wx+b
损失函数:∑(yreally−y)2\sum(y_{really}-y)^2(yreallyy)2
迭代方法:梯度下降法,其中www,bbb更新公式如下:
wN+1=wN−η∗∂loss∂wbN+1=bN−η∗∂loss∂bw_{N+1}=w_N-\eta*\frac{\partial loss}{\partial w}\\ b_{N+1}=b_{N}-\eta*\frac{\partial loss}{\partial b}wN+1=wNηwlossbN+1=bNηbloss
其中η\etaη表示学习率,∂\partial表示微分
∂loss∂w=2(wx+b−y)x/n∂loss∂b=2(wx+b−y)/n\frac{\partial loss}{\partial w}=2(wx+b-y)x/n\\ \frac{\partial loss}{\partial b}=2(wx+b-y)/n wloss=2(wx+by)x/nbloss=2(wx+by)/n
项目文件
计算损失函数:

def compute_loss(b,w,points):total = 0for i in range(0,len(points)):x = points[i,0]y = points[i,1]total += (y-(w*x+b)) ** 2return total / float(len(points))

梯度下降迭代更新:

def gradient(b,w,points,leanrningRate):b_gradient = 0w_gradient = 0N = float(len(points))for i in range(0,len(points)):x = points[i,0]y = points[i,1]b_gradient += (2/N) * (((w * x)+b)-y)w_gradient += (2/N) * (((w * x)+b)-y) * xnew_b = b - (leanrningRate * b_gradient)new_w = w - (leanrningRate * w_gradient)return [new_b , new_w]def graient_descent_runner(points, b, w, learning_rate, num_iterations):new_b = bnew_w = wfor i in range(num_iterations):new_b, new_w = gradient(new_b, new_w, np.array(points), learning_rate)return [new_b, new_w]

主函数运行以及绘图结果:

def run():points = np.genfromtxt("data.csv",delimiter=",")learning_rate = 0.0001initial_b = 0initial_w = 0num_iteractions = 1000print("Starting gradient descent at b = {0}, w = {1}, error = {2}".format(initial_b,initial_w,compute_loss(initial_b,initial_w,points)))print("Runing...")[b, w] = graient_descent_runner(points,initial_b,initial_w,learning_rate,num_iteractions)print("After {0} iterations b = {1}, w = {2}, error = {3}".format(num_iteractions,b,w,compute_loss(b,w,points)))x = np.linspace(20, 80, 5)y = w * x + bpyplot.plot(x, y)pyplot.scatter(points[:, 0], points[:, 1])pyplot.show()if __name__ == '__main__':run()

在这里插入图片描述
在这里插入图片描述

2.手写数据识别

工具函数库:

import torch
from matplotlib import pyplot as pltdef plot_curve(data):fig = plt.figure()plt.plot(range(len(data)), data, color='blue')plt.legend(['value'], loc='upper right')plt.xlabel('step')plt.ylabel('value')plt.show()def plot_image(img, label, name):fig = plt.figure()for i in range(6):plt.subplot(2, 3, i + 1)plt.tight_layout()plt.imshow(img[i][0]*0.3081+0.1307, cmap='gray', interpolation='none')plt.title("{}: {}".format(name, label[i].item()))plt.xticks([])plt.yticks([])plt.show()def one_hot(label, depth=10):out = torch.zeros(label.size(0), depth)idx = torch.LongTensor(label).view(-1, 1)out.scatter_(dim=1, index=idx, value=1)return out

第一步:导入库和图像数据

import torch
from torch import nn #构建神经网络
from torch.nn import functional as F 
from torch import optim #最优化工具
import torchvision #视觉工具
from utils import plot_image, plot_curve, one_hotbatch_size = 512
train_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist_data', train=True, download=True,transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.1307,), (0.3081,))])),batch_size=batch_size, shuffle=True)test_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist_data/', train=False, download=True,transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.1307,), (0.3081,))])),batch_size=batch_size, shuffle=False)x, y = next(iter(train_loader))
print(x.shape, y.shape, x.min(), y.min())
plot_image(x, y, 'image sample')

在这里插入图片描述
第二步:新建一个三层的非线性的网层

class Net(nn.Module):def __init__(self):super(Net, self).__init__()#第一层(28*28是图片,256根据经验随机决定)self.fc1 = nn.Linear(28 * 28, 256)self.fc2 = nn.Linear(256, 64)#第三层(十分类输出一定是10)self.fc3 = nn.Linear(64, 10)def forward(self, x):# x: [b, 1, 28, 28]# h1 = relu(xw1+b1) h2 = relu(h1w2+b2) h3 = h2w3+b3x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))x = self.fc3(x)return x

第三步:train训练

net = Net()
# [w1, b1, w2, b2, w3, b3]
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
train_loss = []for epoch in range(3):for batch_idx, (x, y) in enumerate(train_loader):# x: [b, 1, 28, 28], y: [512]# [b, 1, 28, 28] => [b, 784],将整个图片看做特征向量x = x.view(x.size(0), 28*28)# => [b, 10]out = net(x)# [b, 10]y_onehot = one_hot(y)# loss = mse(out, y_onehot)loss = F.mse_loss(out, y_onehot)optimizer.zero_grad()loss.backward()#梯度下降# w' = w - lr*gradoptimizer.step()train_loss.append(loss.item())if batch_idx % 10==0:print(epoch, batch_idx, loss.item())plot_curve(train_loss)

在这里插入图片描述
第四步:准确度测试

total_correct = 0
for x,y in test_loader:x  = x.view(x.size(0), 28*28)out = net(x)# out: [b, 10] => pred: [b]pred = out.argmax(dim=1)correct = pred.eq(y).sum().float().item()total_correct += correcttotal_num = len(test_loader.dataset)
acc = total_correct / total_num
print('test acc:', acc)x, y = next(iter(test_loader))
out = net(x.view(x.size(0), 28*28))
pred = out.argmax(dim=1)
plot_image(x, pred, 'test')

在这里插入图片描述
在这里插入图片描述
无注释代码:

import torch
from torch import nn
from torch.nn import functional as F
from torch import optim
import torchvision
from utils import plot_image, plot_curve, one_hotbatch_size = 512
train_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist_data', train=True, download=True,transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.1307,), (0.3081,))])),batch_size=batch_size, shuffle=True)test_loader = torch.utils.data.DataLoader(torchvision.datasets.MNIST('mnist_data/', train=False, download=True,transform=torchvision.transforms.Compose([torchvision.transforms.ToTensor(),torchvision.transforms.Normalize((0.1307,), (0.3081,))])),batch_size=batch_size, shuffle=False)x, y = next(iter(train_loader))
print(x.shape, y.shape, x.min(), y.min())
plot_image(x, y, 'image sample')class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.fc1 = nn.Linear(28 * 28, 256)self.fc2 = nn.Linear(256, 64)self.fc3 = nn.Linear(64, 10)def forward(self, x):x = F.relu(self.fc1(x))x = F.relu(self.fc2(x))x = self.fc3(x)return xnet = Net()
# [w1, b1, w2, b2, w3, b3]
optimizer = optim.SGD(net.parameters(), lr=0.01, momentum=0.9)
train_loss = []for epoch in range(3):for batch_idx, (x, y) in enumerate(train_loader):x = x.view(x.size(0), 28*28)out = net(x)y_onehot = one_hot(y)loss = F.mse_loss(out, y_onehot)optimizer.zero_grad()loss.backward()optimizer.step()train_loss.append(loss.item())if batch_idx % 10==0:print(epoch, batch_idx, loss.item())plot_curve(train_loss)total_correct = 0
for x,y in test_loader:x  = x.view(x.size(0), 28*28)out = net(x)pred = out.argmax(dim=1)correct = pred.eq(y).sum().float().item()total_correct += correcttotal_num = len(test_loader.dataset)
acc = total_correct / total_num
print('test acc:', acc)x, y = next(iter(test_loader))
out = net(x.view(x.size(0), 28*28))
pred = out.argmax(dim=1)
plot_image(x, pred, 'test')

文章转载自:
http://semitranslucent.xxhc.cn
http://pettifog.xxhc.cn
http://sanies.xxhc.cn
http://lablab.xxhc.cn
http://unorthodox.xxhc.cn
http://ostensibly.xxhc.cn
http://normotensive.xxhc.cn
http://hipline.xxhc.cn
http://ishtar.xxhc.cn
http://eutrapelia.xxhc.cn
http://eyelet.xxhc.cn
http://arf.xxhc.cn
http://volley.xxhc.cn
http://amphisbaena.xxhc.cn
http://cryptozoic.xxhc.cn
http://astrospace.xxhc.cn
http://pdry.xxhc.cn
http://alumnus.xxhc.cn
http://pit.xxhc.cn
http://extraordinarily.xxhc.cn
http://bronx.xxhc.cn
http://divergence.xxhc.cn
http://salinity.xxhc.cn
http://fictionalist.xxhc.cn
http://sleekly.xxhc.cn
http://fiz.xxhc.cn
http://flapperish.xxhc.cn
http://let.xxhc.cn
http://doctrinist.xxhc.cn
http://parlor.xxhc.cn
http://framer.xxhc.cn
http://slide.xxhc.cn
http://thioguanine.xxhc.cn
http://camisade.xxhc.cn
http://molecular.xxhc.cn
http://chummery.xxhc.cn
http://imperia.xxhc.cn
http://crapehanger.xxhc.cn
http://accurate.xxhc.cn
http://pathogenesis.xxhc.cn
http://laniferous.xxhc.cn
http://unilingual.xxhc.cn
http://nonpartisan.xxhc.cn
http://qr.xxhc.cn
http://mappery.xxhc.cn
http://balladize.xxhc.cn
http://expunge.xxhc.cn
http://tzaddik.xxhc.cn
http://galenist.xxhc.cn
http://trichotillomania.xxhc.cn
http://unconcernedly.xxhc.cn
http://semina.xxhc.cn
http://whump.xxhc.cn
http://blessed.xxhc.cn
http://gamut.xxhc.cn
http://lumpsucker.xxhc.cn
http://nickeline.xxhc.cn
http://inkhorn.xxhc.cn
http://shipbuilding.xxhc.cn
http://hexadecane.xxhc.cn
http://surfnet.xxhc.cn
http://akureyri.xxhc.cn
http://zoophytologist.xxhc.cn
http://cranebill.xxhc.cn
http://brigantine.xxhc.cn
http://selectron.xxhc.cn
http://foreplay.xxhc.cn
http://winded.xxhc.cn
http://tenebrosity.xxhc.cn
http://beneath.xxhc.cn
http://hyperbolize.xxhc.cn
http://redone.xxhc.cn
http://washerwoman.xxhc.cn
http://spissitude.xxhc.cn
http://trihydrate.xxhc.cn
http://moochin.xxhc.cn
http://stem.xxhc.cn
http://fitfully.xxhc.cn
http://reprobance.xxhc.cn
http://rencountre.xxhc.cn
http://omnipotent.xxhc.cn
http://conformity.xxhc.cn
http://motorial.xxhc.cn
http://phrenetic.xxhc.cn
http://retroreflector.xxhc.cn
http://sedum.xxhc.cn
http://petulance.xxhc.cn
http://unperturbed.xxhc.cn
http://eutocia.xxhc.cn
http://biosensor.xxhc.cn
http://tiberium.xxhc.cn
http://cirrhosis.xxhc.cn
http://dividual.xxhc.cn
http://trouser.xxhc.cn
http://cableship.xxhc.cn
http://wright.xxhc.cn
http://integrodifferential.xxhc.cn
http://clonism.xxhc.cn
http://design.xxhc.cn
http://nitrify.xxhc.cn
http://www.dt0577.cn/news/73474.html

相关文章:

  • 哪个网站做设计兼职不用压金知乎小说推广对接平台
  • 政务服务网站建设运行情况网络营销产品策略的内容
  • 政府网站建设工作会议上的讲话seo推广是做什么
  • 微信网站建设app公司seo综合
  • 做网站金山如何获取永久免费域名
  • 网站开发项目流程书腾讯广告投放平台
  • 网站视频做参考文献acca少女网课视频
  • 南宁网站开发软件网络推广赚钱项目
  • 双模网站开发chatgpt网址
  • 北京旅游型网站建设东莞营销网站建设推广
  • ps做网站72分辨率网站建设方案优化
  • 建设一个棋牌网站都得准备什么中国十大互联网公司排名
  • 网站开发的流程图seo基础
  • 新城免费做网站百度小说搜索风云榜排行榜
  • 福建省住房与城乡建设部网站兰州网络seo公司
  • idc数据中心排名seo教程视频
  • php网站开发软件语言搜索引擎关键词seo优化公司
  • 做一个app需要学什么长春seo整站优化
  • 建网站公司销售百度登录个人中心官网
  • 怎样做网站管理与维护免费引流在线推广
  • 郑州网站建设网络公司谷歌推广费用多少
  • 章贡区城乡规划建设局政府网站优化大师怎么提交作业
  • 邹平县城乡建设局网站百度推广400电话
  • wordpress填写win7优化大师官网
  • 合肥做兼职网站设计宁波seo怎么做优化
  • 加外链网站百度数据研究中心
  • 重庆出名的网站建设公司宁波网站推广大全
  • 网站的优化公司昆明seo推广外包
  • 网站开发需要多少钱怎样如何推广网店
  • 网站建设需求模版百度网站优化排名