pytorch学习笔记2

首先如果遇到conda找不到包,pip老是超时的情况建议添加一下镜像源

conda的

python 复制代码
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
# reference
# https://mirror.tuna.tsinghua.edu.cn/help/anaconda/

cuda最好还是像下面链接里的用10.0

https://blog.csdn.net/zzq060143/article/details/88042075

安装pytorch的时候,主要是把官网指令后面的-c pytorch删掉,-c pytorch的意思是,安装下载的channel强制为pytorch官网的channel。所以需要删除才能走清华镜像的channel。

pip的话用这个命令

python 复制代码
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

添加清华镜像

写python可以使用这个python console,是ipython内核,可以交互式运行Python语句(jupyter也是这个内核)

写代码时

上边部分写代码

下面部分进行实时调试

一个简单的Dataset类

python 复制代码
import os
from PIL import Image
from torch.utils.data import Dataset

class MyDateSet(Dataset):
    def __init__(self,root_dir,lable_dir):#提供全局变量,root_dir通常为train文件的地址,lable_dir提供标签
        self.root_dir=root_dir
        self.lable_dir=lable_dir
        self.path=os.path.join(root_dir,lable_dir)
        self.img_path=os.listdir(self.path)


    def __getitem__(self, idx):
        img_name=self.img_path[idx]
        img_item_name=os.path.join(self.root_dir,self.lable_dir,img_name)
        img=Image.open(img_item_name)
        lable=self.lable_dir
        return img,lable

    def __len__(self):
        return len(self.img_path)

root_dir=r'hymenoptera_data/train'
ants_lable_dir=r'ants'
bees_lable_dir=r'bees'
ants_dataset=MyDateSet(root_dir,ants_lable_dir)
bees_dataset=MyDateSet(root_dir,bees_lable_dir)

train_dataset=ants_dataset+bees_dataset#对两个数据集进行拼接
相关推荐
路溪非溪5 分钟前
嵌入式wifi专家成长学习路线
学习
mxwin14 分钟前
Unity Shader中如何学习阴影技术 产生阴影,接受阴影,联级阴影,软阴影
学习·unity·游戏引擎·shader
qeen8719 分钟前
【算法笔记】模拟与高精度加减乘除
c++·笔记·算法·高精度·模拟
配奇25 分钟前
PyTorch 核心使用
人工智能·pytorch·python
鱼很腾apoc38 分钟前
【学习篇】第17期 C++入门必看——类和对象全站最详篇
c语言·开发语言·学习·算法·青少年编程
土豆~1 小时前
Claude Code源码学习—— Agent Prompt 设计
学习·prompt·claude code
U盘失踪了1 小时前
什么是B/S 架构和 C/S 架构
笔记
星幻元宇VR1 小时前
VR流动行走平台|让虚拟体验真正“走起来”
科技·学习·安全·vr·虚拟现实
赵侃侃爱分享1 小时前
学习网络安全后首先应该做这些工作
学习·安全·web安全
像一只黄油飞1 小时前
第二章-02-注释
笔记·python·学习·零基础