用我自己的值初始化 pytorch 卷积层 [英] Initialise pytorch convolution layer with my own values

查看:26
本文介绍了用我自己的值初始化 pytorch 卷积层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法用我自己的值初始化 pytorch 卷积过滤器.

I want to know if there's a way to initialize pytorch convolution filter with my own values.

例如,我有一个元组 [-0.8423, 0.3778][-3.1070, -2.6518] 并且我想用这些值初始化一个 2X2 过滤器,我该怎么做?我查了一些答案,但他们大多使用火炬正态分布和其他.没有什么我不能指定的.

eg, I have a tuple [-0.8423, 0.3778][-3.1070, -2.6518] and I want to initialize a 2X2 filter with these values, how do I do that? I looked up some answers but they were mostly using torch normal distribution and others. Nothing that I can't specify.

告诉我.

推荐答案

你可以直接给权重赋值:

You can directly assign values to weigts:

conv = nn.Conv2d(1, 1, kernel_size=2)
with torch.no_grad():
  conv.weight.data = torch.tensor([[-0.8423,  0.3778],[-3.1070, -2.6518]])  # you might need to play a bit with the dimensionality of this tensor

这篇关于用我自己的值初始化 pytorch 卷积层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆