pytorch入门:读取CSV文件并自己搭建一个回归神经网络

这篇博客介绍了如何使用PyTorch从CSV文件中读取数据,进行数据类型转换,然后搭建一个简单的回归神经网络。作者通过解释数据集的含义,演示了将DataFrame数据转换为张量的过程,并展示了从系列到张量的转换步骤。最后,提到了网络搭建的代码来源于B站的莫烦教程。
摘要由CSDN通过智能技术生成

原始数据

In this part of this exercise, you will implement linear regression with one
variable to predict profits for a food truck. Suppose you are the CEO of a
restaurant franchise and are considering different cities for opening a new
outlet. The chain already has trucks in various cities and you have data for
profits and populations from the cities.
You would like to use this data to help you select which city to expand
to next.
The file ex1data1.txt contains the dataset for our linear regression prob-
lem. The first column is the population of a city and the second column is
the profit of a food truck in that city. A negative value for profit indicates a
loss.
截图看一下:
在这里插入图片描述

数据类型转换

torch中默认的数据类型都是tensor,但读取CSV文件的时候读取的格式为dataframe(单列为series)
抽取第一列作为x(population),第二列作为y(profit),转化顺序为
series -> np.array -> list ->tensor
转化用到的函数:
series转np.array

np.array(x)

np.array转list

x.tolist()

list转tensor

torch.FloatTensor(x)

注意这个时候如果输出x.shape的话,结果为torch.Size([97])
我们要把它转化为97x1的数据:

x = torch.unsqueeze(torch.FloatTensor(x), dim=1)
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值