- 博客(18)
- 收藏
- 关注
原创 PyTorch深度学习实战——多分类问题
import torchfrom torchvision import transformsfrom torchvision import datasetsfrom torch.utils.data import DataLoaderimport torch.nn.functional as Fimport torch.optim as optimbatch_size = 64transforms = transforms.Compose([ transforms.ToTensor.
2022-04-10 10:16:03
581
原创 PyTorch深度学习实践——加载数据集
import numpy as npimport torchfrom torch.utils.data import Dataset # Dataset是一个抽象类,不能实例化,只能被其他的子类继承from torch.utils.data import DataLoaderclass DiabetesDataset(Dataset): # 这个类继承自Dataset def __init__(self, filepath): # filepath是文件路径 xy.
2022-04-09 17:04:34
789
原创 PyTorch深度学习实践——处理多维特征的输入
import torchimport numpy as npxy = np.loadtxt('diabetes.csv', delimiter=',', dtype=np.float32) # 括号内第一个为文件名,第二个为分隔符,第三个为指定数据类型x_data = torch.from_numpy(xy[:, :-1]) # 最后一列不要(-1表示最后一列),取前面八列(最后一列是y)y_data = torch.from_numpy(xy[:, [-1]]) # 中括号表示仅取-1这.
2022-04-04 17:57:23
2163
原创 PyTorch深度学习实践——Logistic回归
import torchimport torch.nn.functional as Fx_data = torch.Tensor([[1.0], [2.0], [3.0]])y_data = torch.Tensor([[0], [0], [1]]) # 现在的输入y为分类,故只为0或1# ----------------------------------------------------------------准备数据集class LogisticRegressionModel.
2022-04-01 20:30:38
1613
原创 PyTorch深度学习实践——用PyTorch实现线性回归
总体分为四个步骤:准备数据集、设计模型(通过前馈和反馈函数计算y^,直接从nn.module中继承)、构造损失函数和优化器(使用PyTorch应用接口)、训练周期(一个周期包含前馈、反馈、更新)执行代码如下:import torchx_data = torch.Tensor([[1.0], [2.0], [3.0]]) # x_data为3*1的矩阵y_data = torch.Tensor([[2.0], [4.0], [6.0]])# 使用mini-batch封装数据,x和y的值
2022-04-01 16:12:06
952
原创 PyThorch深度学习实践——反向传播算法
例题代码如下:(反向传播可以使计算更为简便)import torchx_data = [1.0, 2.0, 3.0]y_data = [2.0, 4.0, 6.0]w = torch.Tensor([1.0]) # 创建一个Tensor变量w.requires_grad = True # 指需要计算梯度(默认的Tensor不计算梯度)def forward(x): return x * w # x与Tensor的数乘,x会被自动转换为Tensor类型def los
2022-03-31 20:58:34
494
原创 PyTorch深度学习实践——梯度下降算法
梯度下降算法代码如下:(增加绘图代码)import matplotlib.pyplot as pltx_data = [1.0, 2.0, 3.0]y_data = [2.0, 4.0, 6.0]w = 1.0 # 设置初始权重def forward(x): # 定义前馈函数 return x * wdef cost(xs, ys): # 定义损失函数 cost = 0 for x, y in zip(xs, ys): y_pred
2022-03-30 16:17:53
1758
原创 PyTorch深度学习实践——线性模型
线性模型:随机选取权重w后评估该模型的效果,通过穷举法选出最佳的w评估模型:损失函数:计算损失函数的平均值,最终使平均损失最小平均平方误差(MSE):例题:,使用线性模型做出预测。代码如下:# 穷举法import numpy as npimport matplotlib.pyplot as plt # 绘图包x_data = [1.0, 2.0, 3.0] # 数据集保存,x和y需分开,x为输入,y为输出y_data = [2.0, 4.0, 6.0]d
2022-03-23 17:53:49
1927
原创 pytorch深度学习实战——预训练网络
from torchvision import modelsfrom torchvision import transformsfrom PIL import Imageimport torchdir(models)alexnet = models.AlexNet()resnet = models.resnet101(pretrained=True)resnetpreprocess = transforms.Compose([ transforms.Resize(256), .
2022-03-20 19:08:45
3063
原创 【终于看懂了】笨办法学python——习题41
import randomfrom urllib.request import urlopenimport sysWORD_URL = "http://learncodethehardway.org/words.txt"WORDS = []PHRASES = { "class %%%(%%%):": "Make a class named %%% that is-a %%%.", "class %%%(object): \n\t def __init__(se.
2022-03-19 17:02:19
603
原创 笨办法学python3——习题40
class Song(object): def __init__(self, lyrics): self.lyrics = lyrics def sing_me_a_song(self): for line in self.lyrics: print(line)happy_bday = Song(["Happy Birthday to you", "I don't want to get.
2022-03-19 11:31:27
295
原创 笨办法学python3——习题39
# create a mapping of state to abbreviationstates = { 'Oregon': 'OR', 'Florida': 'FL', 'California': 'CA', 'New York': 'NY', 'Michigan': 'MI'}# create a basic set of states and some cities in themcities = { 'CA': 'San Francisc.
2022-03-18 15:26:59
272
原创 笨办法学python3——习题38
ten_things = "Apples Oranges Crows Telephone Light Sugar"print("Wait there are not 10 things in that list. Let's fix that.")stuff = ten_things.split(' ')more_stuff = ["Day", "Night", "Song", "Frisbee", "Corn", "Banana", "Girl", "Boy"].
2022-03-17 16:37:40
665
原创 笨办法学python——习题35
from sys import exitdef gold_room(): print("This room is full of gold. How much do you take?") choice = input(">") if "0" in choice or "1" in choice: how_much = int(choice) else: dead("Man, learn to type a number.").
2022-03-17 13:13:08
200
原创 笨办法学python3——习题33
def whileloop(top, stop, step): i = 0 numbers = [] for i in range(top, stop, step): print(f"At the top of i is {i}") numbers.append(i) i += 1 print("Numbers now: ", numbers) print(f"At the bottom i is .
2022-03-16 11:11:38
491
原创 笨办法学python3——习题32
the_count = [1, 2, 3, 4, 5]fruits = ['apples', 'oranges', 'pears', 'apricots']change = [1, 'pennies', 2, 'dimes', 3, 'quarters']# this first kind of for-loop goes through a listfor number in the_count: print(f"This is count {number}")# same as .
2022-03-15 12:13:55
386
原创 笨办法学python3——习题25
def break_words(stuff): """ This function will break up words for us.""" words = stuff.split('') return wordsdef sort_words(words): """Sorts the words.""" return sorted(words)def print_first_word(words): """Prints the first wor.
2022-03-15 10:04:26
154
原创 笨办法学python3——习题23注释
import sysscript, encoding, error = sys.argv # argv用于输入参数def main(language_file, encoding, errors): line = language_file.readline() # 从给它的语言文件中读取一行,用readline处理文本文件 # 以下if结构用来检查文件是否结束,运行到文件结尾会返回空字符串,则此处为真,if后面的代码可以执行 if line: # 防止一直循环下去.
2022-03-13 15:17:31
414
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人