用typecho做的网站提供网站建设哪家好
web/
2025/9/30 16:08:41/
文章来源:
用typecho做的网站,提供网站建设哪家好,营销型网站四大功能,没有做网站能备案吗目录 1.对真实值类别编码#xff1a;2.预测值#xff1a;3.目标函数要求#xff1a;4.使用Softmax模型将输出置信度Oi计算转换为输出匹配概率y^i#xff1a;5.使用交叉熵作为损失函数#xff1a;6.代码实现#xff1a; 1.对真实值类别编码#xff1a; y为真实值#xf… 目录 1.对真实值类别编码2.预测值3.目标函数要求4.使用Softmax模型将输出置信度Oi计算转换为输出匹配概率y^i5.使用交叉熵作为损失函数6.代码实现 1.对真实值类别编码 y为真实值有且仅有一个位置值为1该位置即为该元素真实类别
2.预测值 Oi为该元素与类别i匹配的置信度
3.目标函数要求 对于正确类y的置信度Oy要远远大于其他非正确类的置信度Oi才能使识别到的正确类与错误类具有更明显的差距
4.使用Softmax模型将输出置信度Oi计算转换为输出匹配概率y^i y^为n维向量每个元素非负且和为1y^i为元素与类别i匹配的概率
5.使用交叉熵作为损失函数 L为真实概率y与预测概率y^的差距分类问题不关心非正确类的预测值只关心正确类的预测值有多大
6.代码实现
import sys
import os
import matplotlib.pyplot as plt
import torch
import torchvision
from torchvision import transforms
from torch.utils import data
from d2l import torch as d2los.environ[KMP_DUPLICATE_LIB_OK] TRUE## 读取小批量数据
batch_size 256
trans transforms.ToTensor()
#train_iter, test_iter common.load_fashion_mnist(batch_size) #无法翻墙的可以参考这种方法取下载数据集
mnist_train torchvision.datasets.FashionMNIST(root../data, trainTrue, transformtrans, downloadTrue) # 需要网络翻墙这里数据集会自动下载到项目跟目录的/data目录下
mnist_test torchvision.datasets.FashionMNIST(root../data, trainFalse, transformtrans, downloadTrue) # 需要网络翻墙这里数据集会自动下载到项目跟目录的/data目录下
print(len(mnist_train)) # train_iter的长度是235说明数据被分成了234组大小为256的数据加上最后一组大小不足256的数据
print(11111111)## 展示部分数据
def get_fashion_mnist_labels(labels): # save返回Fashion-MNIST数据集的文本标签。text_labels [t-shirt, trouser, pullover, dress, coat,sandal, shirt, sneaker, bag, ankle boot]return [text_labels[int(i)] for i in labels]def show_fashion_mnist(images, labels):d2l.use_svg_display()# 这里的_表示我们忽略不使用的变量_, figs plt.subplots(1, len(images), figsize(12, 12))for f, img, lbl in zip(figs, images, labels):f.imshow(img.view((28, 28)).numpy())f.set_title(lbl)f.axes.get_xaxis().set_visible(False)f.axes.get_yaxis().set_visible(False)plt.show()train_data, train_targets next(iter(data.DataLoader(mnist_train, batch_size18)))
#展示部分训练数据
show_fashion_mnist(train_data[0:10], train_targets[0:10])# 初始化模型参数
num_inputs 784
num_outputs 10W torch.normal(0, 0.01, size(num_inputs, num_outputs), requires_gradTrue)
b torch.zeros(num_outputs, requires_gradTrue)# 定义模型
def softmax(X):X_exp X.exp()partition X_exp.sum(dim1, keepdimTrue)return X_exp / partition # 这里应用了广播机制def net(X):return softmax(torch.matmul(X.reshape(-1, num_inputs), W) b)# 定义损失函数
y_hat torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
y torch.LongTensor([0, 2])
y_hat.gather(1, y.view(-1, 1))def cross_entropy(y_hat, y):return - torch.log(y_hat.gather(1, y.view(-1, 1)))# 计算分类准确率
def accuracy(y_hat, y):return (y_hat.argmax(dim1) y).float().mean().item()# 计算这个训练集的准确率
def evaluate_accuracy(data_iter, net):acc_sum, n 0.0, 0for X, y in data_iter:acc_sum (net(X).argmax(dim1) y).float().sum().item()n y.shape[0]return acc_sum / nnum_epochs, lr 10, 0.1# 本函数已保存在d2lzh包中方便以后使用
def train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,paramsNone, lrNone, optimizerNone):for epoch in range(num_epochs):train_l_sum, train_acc_sum, n 0.0, 0.0, 0for X, y in train_iter:y_hat net(X)l loss(y_hat, y).sum()# 梯度清零if params is not None and params[0].grad is not None:for param in params:param.grad.data.zero_()l.backward()# 执行优化方法if optimizer is not None:optimizer.step()else:d2l.sgd(params, lr, batch_size)train_l_sum l.item()train_acc_sum (y_hat.argmax(dim1) y).sum().item()n y.shape[0]test_acc evaluate_accuracy(test_iter, net)print(epoch %d, loss %.4f, train acc %.3f, test acc %.3f% (epoch 1, train_l_sum / n, train_acc_sum / n, test_acc))# 训练模型
train_iter, test_iter d2l.load_data_fashion_mnist(batch_size)
train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, [W, b], lr)# 预测模型
for X, y in test_iter:break
true_labels get_fashion_mnist_labels(y.numpy())
pred_labels get_fashion_mnist_labels(net(X).argmax(dim1).numpy())
titles [true \n pred for true, pred in zip(true_labels, pred_labels)]
show_fashion_mnist(X[0:9], titles[0:9])
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/84537.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!