我通过安装pytorch
conda install pytorch-cpu torchvision-cpu -c pytorch
我也试过
pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl
pip3 install torchvision
Run Code Online (Sandbox Code Playgroud)
两个都安装成功!
但是,它只适用于木星笔记本。每当我尝试从控制台执行脚本时,都会收到错误消息:没有名为“torch”的模块
我怎样才能解决这个问题?
我正在阅读神经转移pytorch教程,并对使用retain_variable
(弃用,现在称为retain_graph
)感到困惑.代码示例显示:
class ContentLoss(nn.Module):
def __init__(self, target, weight):
super(ContentLoss, self).__init__()
self.target = target.detach() * weight
self.weight = weight
self.criterion = nn.MSELoss()
def forward(self, input):
self.loss = self.criterion(input * self.weight, self.target)
self.output = input
return self.output
def backward(self, retain_variables=True):
#Why is retain_variables True??
self.loss.backward(retain_variables=retain_variables)
return self.loss
Run Code Online (Sandbox Code Playgroud)
从文档中
retain_graph(bool,optional) - 如果为False,将释放用于计算grad的图形.请注意,几乎在所有情况下都不需要将此选项设置为True,并且通常可以以更有效的方式解决此问题.默认为create_graph的值.
因此,通过设置retain_graph= True
,我们不会释放在向后传递上为图形分配的内存.保持这种记忆的优势是什么,我们为什么需要它?
automatic-differentiation backpropagation neural-network conv-neural-network pytorch
我想将带有gensim的预训练word2vec嵌入到PyTorch嵌入层中.
所以我的问题是,如何将gensim加载的嵌入权重加到PyTorch嵌入层中.
提前致谢!
通过使用pyTorch有两种方式滤除
torch.nn.Dropout
和torch.nn.functional.Dropout
.
我很难看到使用它们之间的区别
- 何时使用什么?
- 这有什么不同吗?
当我换掉它时,我没有看到任何性能差异.
我最近在尝试实现 AlexNet 时在 Pytorch 中遇到了一种方法。我不明白它是如何工作的。请用一些例子解释它背后的想法。以及它在神经网络功能方面与 Maxpooling 或 Average poling 的区别
nn.AdaptiveAvgPool2d((6, 6))
我正在尝试使用 roberta 变压器和预训练模型,但我不断收到此错误:
ImportError:
AutoModelForSequenceClassification requires the PyTorch library but it was not found in your environment. Checkout the instructions on the
installation page: https://pytorch.org/get-started/locally/ and follow the ones that match your environment.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
# Tasks:
# emoji, emotion, hate, irony, offensive, sentiment
# stance/abortion, stance/atheism, stance/climate, stance/feminist, stance/hillary
task='sentiment'
MODEL = f"cardiffnlp/twitter-roberta-base-{task}"
tokenizer = AutoTokenizer.from_pretrained(MODEL)
# download label mapping
labels=[]
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
model.save_pretrained(MODEL)
labels=[]
mapping_link = f"https://raw.githubusercontent.com/cardiffnlp/tweeteval/main/datasets/{task}/mapping.txt"
with urllib.request.urlopen(mapping_link) as f:
html = f.read().decode('utf-8').split("\n")
csvreader …
Run Code Online (Sandbox Code Playgroud) 我尝试使用 加载数据集Torch Dataset and DataLoader
,但出现以下错误:
AttributeError: '_MultiProcessingDataLoaderIter' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)
我使用的代码是:
class WineDataset(Dataset):
def __init__(self):
# Initialize data, download, etc.
# read with numpy or pandas
xy = np.loadtxt('./data/wine.csv', delimiter=',', dtype=np.float32, skiprows=1)
self.n_samples = xy.shape[0]
# here the first column is the class label, the rest are the features
self.x_data = torch.from_numpy(xy[:, 1:]) # size [n_samples, n_features]
self.y_data = torch.from_numpy(xy[:, [0]]) # size [n_samples, 1]
# support indexing such that dataset[i] can be used to get …
Run Code Online (Sandbox Code Playgroud) 我正在尝试深入了解PyTorch Tensor内存模型的工作原理.
# input numpy array
In [91]: arr = np.arange(10, dtype=float32).reshape(5, 2)
# input tensors in two different ways
In [92]: t1, t2 = torch.Tensor(arr), torch.from_numpy(arr)
# their types
In [93]: type(arr), type(t1), type(t2)
Out[93]: (numpy.ndarray, torch.FloatTensor, torch.FloatTensor)
# ndarray
In [94]: arr
Out[94]:
array([[ 0., 1.],
[ 2., 3.],
[ 4., 5.],
[ 6., 7.],
[ 8., 9.]], dtype=float32)
Run Code Online (Sandbox Code Playgroud)
我知道PyTorch张量器共享 NumPy ndarrays 的内存缓冲区.因此,改变一个将反映在另一个.所以,在这里我正在切片并更新Tensor中的一些值t2
In [98]: t2[:, 1] = 23.0
Run Code Online (Sandbox Code Playgroud)
正如预期的那样,它已经更新t2
,arr
因为它们共享相同的内存缓冲区.
In …
Run Code Online (Sandbox Code Playgroud)