神经网络构建

import torch
from torch import nn
import torch.nn.functional as F

# 首先建立一个全连接的子module,继承nn.Module
class Linear(nn.Module):
	def __init__(self, in_dim, out_dim):
		super(Linear, self).__init__()  # 调用nn.Module的构造函数

		# 使用nn.Parameter来构造需要学习的参数
		self.W = nn.Parameter(torch.randn(in_dim, out_dim))
		self.b = nn.Parameter(torch.randn(out_dim))

	# 在forward中实现前向传播
	def forward(self, x):
		x = x.matmul(self.W)  # nn.matmul实现矩阵相乘
		y = x + self.b.expand_as(x)
		return y

# 构建感知机类,继承nn,Module,并调用Linear的子module
class Perception(nn.Module):
	def __init__(self, in_dim, hid_dim, out_dim):
		super(Perception, self).__init__()
		self.layer1 = Linear(in_dim, hid_dim)
		self.layer2 = Linear(hid_dim, out_dim)
	def forward(self, x):
		x = self.layer1(x)
		y = torch.sigmoid(x)
		y = self.layer2(y)
		y = torch.sigmoid(y)
		return y



if __name__ == "__main__":
	p = Perception(1, 3, 2)
	print(p)

	# named_parameters()可以返回学习参数的迭代器,分别为参数名与参数值
	for name, parameter in p.named_parameters():
		print("层:", name, parameter)

	intput = torch.randn(1, 1)  # 代表了有4个样本,每个样本有两维
	output = p(intput)
	print("intput:", intput, intput.shape)
	print("output:", output, output.shape)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值