AttributeError: type object 'torch._C._VariableFunctions' has no attribute 'threshold_'

username: ohcurrent
cooked: <p>Hello, I am trying to run PyTorch examples.<br>
I installed ‘PyTorch’ and ‘examples’ files and did the ‘python setup.py install --user’.<br>
However, when I try to run ‘examples/word_language_model’ by typing ‘python main.py --cuda --epochs 6’, it prints out error message like this.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/9/97645130112b787cb4b1583fcd1e130e1e21d2e1.png" data-download-href="https://discuss.pytorch.org/uploads/default/97645130112b787cb4b1583fcd1e130e1e21d2e1" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/9/97645130112b787cb4b1583fcd1e130e1e21d2e1.png" alt="image" data-base62-sha1="lBh1DW4gsgTQ4TGDPtpgqV4mgb7" width="690" height="254" data-dominant-color="111111"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1050×387 20.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Do you know how to solve this?</p>
<p>Thank you.</p>

username: Kushaj
cooked: <p>Which pytorch version are you using. Cause the example is working for me. <code>torch.__version__</code></p>

username: ohcurrent
cooked: <p>I am using python 2.7.12. Thanks.</p>

username: Kushaj
cooked: <p>No your pytorch version</p>

username: ohcurrent
cooked: <p>My Pytorch version is “release v0.4.1”. Thank you.</p>

username: Kushaj
cooked: <p>You will have to upgrade to v1.0</p>

username: lianzhang132
cooked: <p>The 1.0.1.post2 version I used also had this problem when I called the model who can explain why in detail?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b13c366320ae25482b1887929330774b44a42f9.png" data-download-href="https://discuss.pytorch.org/uploads/default/4b13c366320ae25482b1887929330774b44a42f9" title="微信图片_20200703174648"><img src="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b13c366320ae25482b1887929330774b44a42f9.png" alt="微信图片_20200703174648" data-base62-sha1="aIa9tN9Y6e3BS1ibVJF0bN8wdjb" width="689" height="310" data-dominant-color="151515"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信图片_20200703174648</span><span class="informations">1293×582 36.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: ptrblck
cooked: <p>I would recommend to update to the latest stable release (<code>1.5.1</code>) and rerun the code.<br>
If you still encounter this issue, could you post a code snippet to reproduce it, please?</p>

username: lianzhang132
cooked: <p>Thank you for your reply. I used the prediction service Clipper to make the prediction. At present, the default installed version of PyTorch is 1.0.1.Post2</p>
<p>So I want to know the root cause of this error and find a solution that doesn’t change the version<br>
This is my call and deployment code</p>
<p>import os<br>
import sys<br>
import time<br>
import torch<br>
from torch import nn, optim<br>
import torch.nn.functional as F<br>
import torchvision<br>
from torchvision import transforms</p>
<p>class GlobalAvgPool2d(nn.Module):<br>
“”"<br>
全局平均池化层<br>
可通过将普通的平均池化的窗口形状设置成输入的高和宽实现<br>
“”"</p>
<pre><code>def __init__(self):
    super(GlobalAvgPool2d, self).__init__()

def forward(self, x):
    return F.avg_pool2d(x, kernel_size=x.size()[2:])
</code></pre>
<p>class FlattenLayer(torch.nn.Module):<br>
def <strong>init</strong>(self):<br>
super(FlattenLayer, self).<strong>init</strong>()</p>
<pre><code>def forward(self, x):  # x shape: (batch, *, *, ...)
    return x.view(x.shape[0], -1)
</code></pre>
<p>class Residual(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels, use_1x1conv=False, stride=1):<br>
“”"<br>
use_1×1conv: 是否使用额外的1x1卷积层来修改通道数<br>
stride: 卷积层的步幅, resnet使用步长为2的卷积来替代pooling的作用，是个很赞的idea<br>
“”"<br>
super(Residual, self).<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1, stride=stride)<br>
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=1)<br>
if use_1x1conv:<br>
self.conv3 = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride)<br>
else:<br>
self.conv3 = None<br>
self.bn1 = nn.BatchNorm2d(out_channels)<br>
self.bn2 = nn.BatchNorm2d(out_channels)</p>
<pre><code>def forward(self, X):
    Y = F.relu(self.bn1(self.conv1(X)))
    Y = self.bn2(self.conv2(Y))
    if self.conv3:
        X = self.conv3(X)
    return F.relu(Y + X)
</code></pre>
<p>def resnet_block(in_channels, out_channels, num_residuals, first_block=False):<br>
‘’’<br>
resnet block<br>
num_residuals: 当前block包含多少个残差块<br>
first_block: 是否为第一个block<br>
一个resnet block由num_residuals个残差块组成<br>
其中第一个残差块起到了通道数的转换和pooling的作用<br>
后面的若干残差块就是完成正常的特征提取<br>
‘’’<br>
if first_block:<br>
assert in_channels == out_channels  # 第一个模块的输出通道数同输入通道数一致<br>
blk = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
for i in range(num_residuals):<br>
if i == 0 and not first_block:<br>
blk.append(Residual(in_channels, out_channels, use_1x1conv=True, stride=2))<br>
else:<br>
blk.append(Residual(out_channels, out_channels))<br>
return nn.Sequential(*blk)</p>
<h1><a name="resnet-1" class="anchor" href="#resnet-1"></a>定义resnet模型结构</h1>
<p>net = nn.Sequential(<br>
nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),  # TODO: 缩小感受野, 缩channel<br>
nn.BatchNorm2d(32),<br>
nn.ReLU())</p>
<h1><a name="nnrelu-2" class="anchor" href="#nnrelu-2"></a>nn.ReLU(),</h1>
<h1><a name="nnmaxpool2dkernel_size2-stride2-todomaxpool-3" class="anchor" href="#nnmaxpool2dkernel_size2-stride2-todomaxpool-3"></a>nn.MaxPool2d(kernel_size=2, stride=2))   # TODO：去掉maxpool缩小感受野</h1>
<h1><a name="h-4block-4" class="anchor" href="#h-4block-4"></a>然后是连续4个block</h1>
<p>net.add_module(“resnet_block1”, resnet_block(32, 32, 2, first_block=True))  # TODO: channel统一减半<br>
net.add_module(“resnet_block2”, resnet_block(32, 64, 2))<br>
net.add_module(“resnet_block3”, resnet_block(64, 128, 2))<br>
net.add_module(“resnet_block4”, resnet_block(128, 256, 2))</p>
<h1><a name="global-average-pooling-5" class="anchor" href="#global-average-pooling-5"></a>global average pooling</h1>
<p>net.add_module(“global_avg_pool”, GlobalAvgPool2d())</p>
<h1><a name="fc-layer-6" class="anchor" href="#fc-layer-6"></a>fc layer</h1>
<p>net.add_module(“fc”, nn.Sequential(FlattenLayer(), nn.Linear(256, 10)))</p>
<p>def load_data_fashion_mnist(batch_size, root=‘…/data’):<br>
“”“Download the fashion mnist dataset and then load into memory.”“”</p>
<pre><code>normalize = transforms.Normalize(mean=[0.28], std=[0.35])
train_augs = transforms.Compose([
    transforms.RandomCrop(28, padding=2),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    normalize
])

test_augs = transforms.Compose([
    transforms.ToTensor(),
    normalize
])

mnist_train = torchvision.datasets.FashionMNIST(root=root, train=True, download=True, transform=train_augs)
mnist_test = torchvision.datasets.FashionMNIST(root=root, train=False, download=True, transform=test_augs)
if sys.platform.startswith('win'):
    num_workers = 0  # 0表示不用额外的进程来加速读取数据
else:
    num_workers = 4
train_iter = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=True, num_workers=num_workers)
test_iter = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=False, num_workers=num_workers)

return train_iter, test_iter
</code></pre>
<p>batch_size = 1<br>
train_iter, test_iter = load_data_fashion_mnist(batch_size, root=‘/root/.pytorch/F_MNIST_data’)<br>
lr, num_epochs, lr_period, lr_decay = 0.01, 50, 5, 0.1<br>
<span class="hashtag-raw">#optimizer</span> = optim.Adam(net.parameters(), lr=lr)<br>
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=0.9, weight_decay=5e-4)<br>
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)</p>
<p>print(‘加载最优模型’)<br>
net.load_state_dict(torch.load(‘model/best.pth’))<br>
net = net.to(device)</p>
<p>print(‘inference测试集’)<br>
net.eval()<br>
id = 0<br>
preds_list = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
with torch.no_grad():<br>
for X, y in test_iter:<br>
batch_pred = list(net(X.to(device)).argmax(dim=1).cpu().numpy())<br>
print(batch_pred)<br>
for y_pred in batch_pred:<br>
print(y_pred)<br>
preds_list.append((id, y_pred))<br>
id += 1</p>
<p>print(‘生成测试集评估文件’)<br>
with open(‘result.csv’, ‘w’) as f:<br>
f.write(‘ID,Prediction\n’)<br>
for id, pred in preds_list:<br>
f.write(‘{},{}\n’.format(id, pred))</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/6/56be17303fd4ee09ae25622b24917ab308c5f01d.png" data-download-href="https://discuss.pytorch.org/uploads/default/56be17303fd4ee09ae25622b24917ab308c5f01d" title="微信截图_20200706142424"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/6/56be17303fd4ee09ae25622b24917ab308c5f01d.png" alt="微信截图_20200706142424" data-base62-sha1="cnmkraksvaajcF0WZMLsdh8anXD" width="690" height="53" data-dominant-color="F4F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信截图_20200706142424</span><span class="informations">1152×89 4 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/3/73da7221b8648d7157d16f569ed2ce264d6c915f.png" data-download-href="https://discuss.pytorch.org/uploads/default/73da7221b8648d7157d16f569ed2ce264d6c915f" title="微信截图_20200706142506"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/3/73da7221b8648d7157d16f569ed2ce264d6c915f_2_690x145.png" alt="微信截图_20200706142506" data-base62-sha1="gwSVtSsC1cczEPHCoiSY7FzgEH5" width="690" height="145" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/3/73da7221b8648d7157d16f569ed2ce264d6c915f_2_690x145.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/3/73da7221b8648d7157d16f569ed2ce264d6c915f_2_1035x217.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/7/3/73da7221b8648d7157d16f569ed2ce264d6c915f.png 2x" data-dominant-color="F4F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信截图_20200706142506</span><span class="informations">1311×276 14 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dccb52da26e26ddbd9945275f50896eaa9e57147.png" data-download-href="https://discuss.pytorch.org/uploads/default/dccb52da26e26ddbd9945275f50896eaa9e57147" title="微信截图_20200706142528"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dccb52da26e26ddbd9945275f50896eaa9e57147.png" alt="微信截图_20200706142528" data-base62-sha1="vveF4elbVesBnCqJxsOtmUaQHHN" width="690" height="286" data-dominant-color="F1DBDB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">微信截图_20200706142528</span><span class="informations">1284×534 21.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>