Pytorch 7多维

多维读取

numpy

python 复制代码
xy = np.loadtxt('diabetes.csv', delimiter=',', dtype=np.float32)

print("input data.shape", x_data.shape)
x_data = torch.from_numpy(xy[:, :-1])# 选择所有行不包括最后一列
y_data = torch.from_numpy(xy[:, [-1]])# 选择所有行,只有最后一列
np.loadtxt(fname, dtype=float, delimiter=' ', comments='#', skiprows=0, usecols=None, unpack=False, ndmin=0, encoding='bytes', max_rows=None)
# 参数分别是路径,dtype: 数据类型,默认为 float。delimiter: 分隔符,默认为空格。用于分隔文件中的数据列skiprows: 要跳过的行数,默认为 0。用于跳过文件开头的标题行或其他不需要的行。

多维多层数类

python 复制代码
class Model(torch.nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.linear1 = torch.nn.Linear(8, 6)
        self.linear2 = torch.nn.Linear(6, 4)
        self.linear3 = torch.nn.Linear(4, 2)
        self.linear4 = torch.nn.Linear(2, 1)
        self.sigmoid = torch.nn.Sigmoid()
 
    def forward(self, x):
        x = self.sigmoid(self.linear1(x))
        x = self.sigmoid(self.linear2(x))
        x = self.sigmoid(self.linear3(x)) # y hat
        x = self.sigmoid(self.linear4(x))  # y hat
        return x

这里维度由输入数据决定

后面基本上一样

python 复制代码
torch.eq(a,b).sum().item()# 把a和b相等的返回1否则为0,求和,item是转化为py的数据0维
相关推荐
MATLAB代码顾问5 分钟前
Python Matplotlib数据可视化实战指南
python·信息可视化·matplotlib
xhtdj12 分钟前
智源大会圆桌大模型没有终局具身智能可能是中国的 AlphaGo 时刻
人工智能·clickhouse·安全·动态规划
HavenlonLabs14 分钟前
区块链解决信任分布,AI 需要解决能力控制
人工智能·安全·区块链
良枫17 分钟前
01 “自进化 Agent”是什么
人工智能
LaughingZhu17 分钟前
Product Hunt 每日热榜 | 2026-06-12
人工智能·经验分享·深度学习·神经网络·产品运营
数据门徒18 分钟前
神经网络原理 第十一章:植根于统计力学的随机机器和它们的逼近
人工智能·深度学习·神经网络
AI 编程助手GPT20 分钟前
用 Python 做一个世界杯赛前分析脚本:以巴西 vs 摩洛哥为例
开发语言·网络·人工智能·python·chatgpt
Data-Miner21 分钟前
大模型赋能金融贷款评估,颠覆传统风控方案全解析
人工智能·金融
键盘歌唱家21 分钟前
Spring AI 入门分享:它和“直接调 API“到底差在哪
java·人工智能·spring
品牌测评21 分钟前
2026年AI声音克隆工具深度实测:声线APP领衔,解锁声音创作全场景新范式
人工智能