介绍
FloatTensor与变量怎么在Pytorch中使用?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
Pytorch中基本的变量类型当属FloatTensor(以下都用FloatTensor),而变量(以下都用变量)是FloatTensor的封装,除了包含FloatTensor还包含有梯度信息
Pytorch中的dochi给出一些对于FloatTensor的基本的操作,比如四则运算以及平方等(链接),这些操作对于FloatTensor是十分的不友好,有时候需要写一个正则化的项需要写很长的一串,比如两个FloatTensor之间的相加需要用torch.add()来实现
for step 拷贝范围(config.total_step): ,,,, ,,,#,Extract 多个(5),conv feature 向量 ,,,target_features =, vgg(目标),,#,每一次输入到网络中的是同样一张图片,反传优化的目标是输入的目标 ,,,content_features =, vgg(变量(内容) ,,,style_features =, vgg(变量(风格) ,,,style_loss =0 ,,,content_loss =0 ,,,for f1, f2,, f3 拷贝zip (target_features, content_features,, style_features): ,,,,,#,Compute content loss (target 以及content 图像) ,,,,,content_loss +=, torch.mean ((f1 安康;f2) * * 2), #, square 可以进行直接加——操作?可以,并且意味着对所有的元素进行均值化造作 ,,,,,#,Reshape conv 特性 ,,,,,,_,c, h, w =, f1.size (), #, channel height 宽度 ,,,,,f1 =, f1.view (c, h *, w), #, reshape a 向量 ,,,,,f3 =, f3.view (c, h *, w), #, reshape a 向量 ,,,,,#,Compute gram matrix ,,,,,f1 =, torch.mm (f1, f1.t ()) ,,,,,f3 =, torch.mm (f3, f3.t ()) ,,,,,#,Compute style loss (target 以及style 图像) ,,,,,style_loss +=, torch.mean ((f1 安康;f3) * * 2),/, (c *, h *, w),, #,总共元素的数目?
其中f1与f2、f3的变量类型是变量,作者对其直接用四则运算符进行加减,并且用python内置的* *进行平方操作,然后
#, - *编码:utf-8 - * - import 火炬 得到torch.autograd import 变量 #,dtype =torch.FloatTensor #=dtype torch.cuda.FloatTensor Uncomment 却;能够用run 提醒GPU #,N is  batch 规模;,D_in is input 维度; #,H is  hidden 维度;D_out is output 维度。 N, D_in, H, D_out =, 64年,1000年,100年,10 #,Randomly initialize 权重 时间=w1 torch.randn (D_in, H) .type (dtype), #,两个权重矩阵 时间=w2 torch.randn (D_in, H) .type (dtype) #,operate with  + - */,以及* * w3 =w1-2 * w2 w4 =, w3 * * 2 时间=w5 w4/w1 #,operate 从而,Variable with + - */,以及* * 时间=w6 变量(torch.randn (N, D_in) .type (dtype)) 时间=w7 变量(torch.randn (N, D_in) .type (dtype)) +=w8 w6 支w7 时间=w9 将*支w7 时间=w10 w9 * * 2 打印(1)
基本上调试的结果与预期相符
看完上述内容,你们掌握FloatTensor与变量怎么在Pytorch中使用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!