我在 pytorch 1.3.1 中使用tensorboard,并且我在pytorch 文档中为tensorboard做了完全相同的事情。运行后tensorboard --logdir=runs
,我得到了这个:
。
$ tensorboard --logdir=runs
TensorFlow installation not found - running with reduced feature set.
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.1.0 at http://localhost:6006/ (Press CTRL+C to quit)
Run Code Online (Sandbox Code Playgroud)
打开 http://localhost:6006/ 后,我得到这样的空白页面
我也尝试了tensorboardX,并得到了相同的结果。您能告诉我如何解决这个问题吗?谢谢。
假设我有一个以下形式的张量
[[-5, 0, -1],
[3, 100, 87],
[17, -34, 2],
[45, 1, 25]]
Run Code Online (Sandbox Code Playgroud)
我想找到每一行中的最大值并返回一个 1 级张量,如下所示:
[0,
100,
17,
45]
Run Code Online (Sandbox Code Playgroud)
我该如何在 PyTorch 中做到这一点?
我正在关注 Pytorch 的Tensorboard文档。
我有以下代码:
model = torchvision.models.resnet50(False)
writer.add_graph(model)
Run Code Online (Sandbox Code Playgroud)
它抛出以下错误:
_ = model(*args) # 不捕获,只打印错误信息
TypeError:* 之后的 ResNet 对象参数必须是可迭代的,而不是 NoneType
我不知道我在这里做错了什么!
有人可以告诉我方法中多个参数背后的概念吗forward()
?一般来说,方法的实现forward()
有两个参数
如果前向方法的参数多于这些参数,PyTorch 如何使用前向方法。
让我们考虑这个代码库: https://github.com/bamps53/kaggle-autonomous-driven2019/blob/master/models/centernet.py 这里在线 236 位作者使用了带有两个参数的前向方法:
我找不到一篇文章可以回答我关于第 254( return_embeddings:
) 行和第 257( if centers is not None:
) 行将在什么条件下执行的查询。据我所知,该方法由 nn 模块内部调用。有人可以在这上面放一些灯吗?
我需要在 PyTorch 中实现多标签图像分类模型。然而我的数据不平衡,所以我使用WeightedRandomSampler
PyTorch 中的创建自定义数据加载器。但是当我迭代自定义数据加载器时,我收到错误:IndexError: list index out of range
def make_weights_for_balanced_classes(images, nclasses):
count = [0] * nclasses
for item in images:
count[item[1]] += 1
weight_per_class = [0.] * nclasses
N = float(sum(count))
for i in range(nclasses):
weight_per_class[i] = N/float(count[i])
weight = [0] * len(images)
for idx, val in enumerate(images):
weight[idx] = weight_per_class[val[1]]
return weight
Run Code Online (Sandbox Code Playgroud)
weights = make_weights_for_balanced_classes(train_dataset.imgs, len(full_dataset.classes))
weights = torch.DoubleTensor(weights)
sampler = WeightedRandomSampler(weights, len(weights))
train_loader = DataLoader(train_dataset, batch_size=4,sampler = sampler, pin_memory=True)
Run Code Online (Sandbox Code Playgroud)
在Pytorch中,是否有类似于Scipy的三次样条插值?给定一维输入张量x
和y
,我想对这些点进行插值并在 处求值xs
以获得ys
。另外,我想要一个积分器函数来查找,即从到 的Ys
样条插值的积分。x[0]
xs
我正在使用 PyTorch 训练 CNN 架构来解决回归问题,其中我的输出是 20 个值的张量。我计划使用 RMSE 作为模型的损失函数,并尝试使用 PyTorchnn.MSELoss()
并对其进行平方根,torch.sqrt()
但在获得结果后感到困惑。我会尽力解释原因。很明显,对于批量大小,bs
我的输出张量的尺寸将为[bs , 20]
。我尝试实现我自己的 RMSE 函数:
def loss_function (predicted_x , target ):
loss = torch.sum(torch.square(predicted_x - target) , axis= 1)/(predicted_x.size()[1]) #Taking the mean of all the squares by dividing it with the number of outputs i.e 20 in my case
loss = torch.sqrt(loss)
loss = torch.sum(loss)/predicted_x.size()[0] #averaging out by batch-size
return loss
Run Code Online (Sandbox Code Playgroud)
但是我的输出loss_function()
和 PyTorch 如何实现它是nn.MSELoss()
不同的。我不确定我的实现是否错误或者我是否nn.MSELoss()
以错误的方式使用。
python artificial-intelligence deep-learning pytorch loss-function
在 CPU 上,与 numpy 数组torch.as_tensor(a)
相同, ?如果没有,那为什么不呢?torch.from_numpy(a)
a
从文档中torch.as_tensor
如果数据是
ndarray
对应的dtype
并且device
是cpu,则不会执行任何复制。
从文档中torch.from_numpy
:
返回的张量和
ndarray
共享相同的内存。对张量的修改将反映在 中,ndarray
反之亦然。
在这两种情况下,结果张量的任何更改都会更改原始 numpy 数组。
a = np.array([[1., 2], [3, 4]])
t1 = torch.as_tensor(a)
t2 = torch.from_numpy(a)
t1[0, 0] = 42.
print(a)
# prints [[42., 2.], [3., 4.]]
t2[1, 1] = 55.
print(a)
# prints [[42., 2.], [3., 55.]]
Run Code Online (Sandbox Code Playgroud)
此外,在这两种情况下,尝试调整张量的大小都会导致错误。
我是 PyTorch 的新手,最近我一直在尝试使用 Transformers。我正在使用 HuggingFace 提供的预训练分词器。
我成功下载并运行它们。但如果我尝试保存它们并再次加载,则会发生一些错误。
如果我用来 AutoTokenizer.from_pretrained
下载分词器,那么它就可以工作。
[1]: tokenizer = AutoTokenizer.from_pretrained('distilroberta-base')
text = "Hello there"
enc = tokenizer.encode_plus(text)
enc.keys()
Out[1]: dict_keys(['input_ids', 'attention_mask'])
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用保存它tokenizer.save_pretrained("distilroberta-tokenizer")
并尝试在本地加载它,则会失败。
[2]: tmp = AutoTokenizer.from_pretrained('distilroberta-tokenizer')
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/transformers/configuration_utils.py in get_config_dict(cls, pretrained_model_name_or_path, **kwargs)
238 resume_download=resume_download,
--> 239 local_files_only=local_files_only,
240 )
/opt/conda/lib/python3.7/site-packages/transformers/file_utils.py in cached_path(url_or_filename, cache_dir, force_download, proxies, resume_download, user_agent, extract_compressed_file, force_extract, local_files_only)
266 # File, but it doesn't exist.
--> 267 raise EnvironmentError("file {} not found".format(url_or_filename))
268 else:
OSError: file …
Run Code Online (Sandbox Code Playgroud) python deep-learning pytorch huggingface-transformers huggingface-tokenizers
我是 PyTorch 的新手,我从 cnn 层收到以下错误:“RuntimeError:预期标量类型 Double 但发现 Float”。我将每个元素转换为.astype(np.double)
但错误消息仍然存在。然后在转换后Tensor
尝试使用.double()
,但错误消息仍然存在。这是我的代码,以便更好地理解:
import torch.nn as nn
class CNN(nn.Module):
# Contructor
def __init__(self, shape):
super(CNN, self).__init__()
self.cnn1 = nn.Conv1d(in_channels=shape, out_channels=32, kernel_size=3)
self.act1 = torch.nn.ReLU()
# Prediction
def forward(self, x):
x = self.cnn1(x)
x = self.act1(x)
return x
X_train_reshaped = np.zeros([X_train.shape[0],int(X_train.shape[1]/depth),depth])
for i in range(X_train.shape[0]):
for j in range(X_train.shape[1]):
X_train_reshaped[i][int(j/3)][j%3] = X_train[i][j].astype(np.double)
X_train = torch.tensor(X_train_reshaped)
y_train = torch.tensor(y_train)
# Dataset w/o any tranformations
train_dataset_normal = CustomTensorDataset(tensors=(X_train, y_train), transform=None)
train_loader = …
Run Code Online (Sandbox Code Playgroud)