我有以下图表networkx
:
import matplotlib.pyplot as plt
import networkx as nx
G = nx.Graph()
G.add_edge(1, 2, weight = 1)
G.add_edge(2, 3, weight = 3)
G.add_edge(4, 5, weight = 2)
G.add_edge(6, 3, weight = 4)
G.add_edge(6, 4, weight = 6)
plt.figure(figsize=(12,12))
edges = G.edges()
pos = nx.spring_layout(G, k = 0.5) # k regulates the distance between nodes
weights = [G[u][v]['weight'] for u,v in edges]
nx.draw(G, with_labels=True, node_color='skyblue', font_weight='bold', width=weights, pos=pos)
Run Code Online (Sandbox Code Playgroud)
有没有办法将这个 2D 图形转换为 3D 图形?坐标并不重要。我只对绘图的交互能力感兴趣(即我能够旋转图表)。我知道plotly
有能力做这样的图,但我不确定如何结合我的二维networkx
图并 …
我正在尝试按照本指南设置 GitHub 页面网站。我按照jekyllrb.com 教程安装了 Homebrew、chruby 和 Jekyll 。
当我跑步时,ruby -v
我得到了ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin18]
。当我跑步时,chruby -V
我得到了chruby: 0.3.9
。安装 Jekyll 后,gem install jekyll
我得到了Successfully installed jekyll-4.3.1 Parsing documentation for jekyll-4.3.1 Done installing documentation for Jekyll after 0 seconds 1 gem installed
.
但是当我运行jekyll -v
(或任何Jekyll
命令)时,我收到一条很长的消息,其中包含以下错误:
<internal:/Users/my_username/.rubies/ruby-3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require': dlopen(/Users/my_username/.gem/ruby/3.1.2/gems/google-protobuf-3.21.12-x86_64-darwin/lib/google/3.1/protobuf_c.bundle, 9): no suitable image found. Did find: (LoadError)
/Users/my_username/.gem/ruby/3.1.2/gems/google-protobuf-3.21.12-x86_64-darwin/lib/google/3.1/protobuf_c.bundle: cannot load 'protobuf_c.bundle' (load command 0x80000034 is unknown) …
Run Code Online (Sandbox Code Playgroud) 假设我有以下模型(来自此脚本):
\nfrom transformers import AutoTokenizer, GPT2LMHeadModel, AutoConfig\n\nconfig = AutoConfig.from_pretrained(\n "gpt2",\n vocab_size=len(tokenizer),\n n_ctx=context_length,\n bos_token_id=tokenizer.bos_token_id,\n eos_token_id=tokenizer.eos_token_id,\n)\nmodel = GPT2LMHeadModel(config)\n
Run Code Online (Sandbox Code Playgroud)\n我目前正在为 Trainer 使用以下训练参数:
\nfrom transformers import Trainer, TrainingArguments\n\nargs = TrainingArguments(\n output_dir="codeparrot-ds",\n per_device_train_batch_size=32,\n per_device_eval_batch_size=32,\n evaluation_strategy="steps",\n eval_steps=5_000,\n logging_steps=5_000,\n gradient_accumulation_steps=8,\n num_train_epochs=1,\n weight_decay=0.1,\n warmup_steps=1_000,\n lr_scheduler_type="cosine",\n learning_rate=5e-4,\n save_steps=5_000,\n fp16=True,\n push_to_hub=True,\n)\n\ntrainer = Trainer(\n model=model,\n tokenizer=tokenizer,\n args=args,\n data_collator=data_collator,\n train_dataset=tokenized_datasets["train"],\n eval_dataset=tokenized_datasets["valid"],\n)\ntrainer.train()\n
Run Code Online (Sandbox Code Playgroud)\n我如何对此进行调整,以便训练器将使用多个 GPU(例如 8 个)?
\n我发现了这个问题,但他们没有使用 Trainer,只是使用 PyTorchDataParallel
model = torch.nn.DataParallel(model, device_ids=[0,1])\n
Run Code Online (Sandbox Code Playgroud)\n关于使用多个 GPU 进行训练的Huggingface文档对我来说并不是很清楚,并且没有使用 Trainer 的示例。相反,我在这里发现他们使用 …
machine-learning pytorch huggingface-transformers huggingface
我正在遵循有关从维基数据查询的指南。
我可以使用以下命令获取某个实体(如果我知道其代码):
from wikidata.client import Client
client = Client()
entity = client.get('Q20145', load=True)
entity
>>><wikidata.entity.Entity Q20145 'IU'>
entity.description
>>>m'South Korean singer-songwriter, record producer, and actress'
Run Code Online (Sandbox Code Playgroud)
但是我怎样才能获得该实体的 RDF 三元组呢?即所有出边和入边的形式为(主语、谓语、宾语)
我有一个矩阵:
\nt = torch.rand(2,3)\nprint(t)\n>>>tensor([[0.5164, 0.3651, 0.0882],\n [0.4488, 0.9824, 0.4067]])\n
Run Code Online (Sandbox Code Playgroud)\n我正在关注这个规范简介,并想在 PyTorch 中尝试一下。
\n看起来像:
\nnorm
向量的大小或长度是一个非负数,描述向量在空间中的范围,有时称为向量\xe2\x80\x99s 大小或范数”1-Norm
是“绝对向量值的总和,其中标量的绝对值使用符号 |a1|。实际上,范数是距向量空间原点的曼哈顿距离的计算。”2-Norm
是“向量坐标距向量空间原点的距离。L2 范数计算为向量值平方和的平方根。”我目前只知道这个:
\nprint(torch.linalg.norm(t, dim=1))\n>>>tensor([0.6385, 1.1541])\n
Run Code Online (Sandbox Code Playgroud)\n但我无法从这里弄清楚三个(范数,1-范数,2-范数)中的哪一个计算出这三个(范数、1-范数、2-范数)中的哪一个,以及如何计算其余的
\n我正在尝试readme.md
在我的 git repo上的文件中显示图像。我之前在我的其他 repos 中做过这个,但由于某种原因,这一次不起作用。
目前我的代码在readme
文件中生成以下内容:
我试过:

Run Code Online (Sandbox Code Playgroud)
这只是使链接打开图像所在的文件夹并显示它
我也试过

Run Code Online (Sandbox Code Playgroud)
它做同样的事情。
我也试过

Run Code Online (Sandbox Code Playgroud)
这使得链接下载图像
我有一个简单的神经网络:
import torch
import torch.nn as nn
import torch.optim as optim
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.fc1 = nn.Linear(1, 5)
self.fc2 = nn.Linear(5, 10)
self.fc3 = nn.Linear(10, 1)
def forward(self, x):
x = self.fc1(x)
x = torch.relu(x)
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Model()
opt = optim.Adam(net.parameters())
Run Code Online (Sandbox Code Playgroud)
我还有一些输入功能:
features = torch.rand((3,1))
Run Code Online (Sandbox Code Playgroud)
我可以使用一个简单的损失函数来正常训练它,该函数将被最小化:
for i in range(10):
opt.zero_grad()
out = net(features)
loss = torch.mean(torch.square(torch.tensor(5) - torch.sum(out)))
print('loss:', loss)
loss.backward()
opt.step()
Run Code Online (Sandbox Code Playgroud)
但是,如果我添加另一个我想要最大化的损失分量loss2
——:
loss2s = [] …
Run Code Online (Sandbox Code Playgroud) python machine-learning neural-network pytorch loss-function
我有一个非常大(~100k)的字典列表:
[{'sequence': 'read the rest of this note', 'score': 0.22612378001213074, 'token': 3805, 'token_str': 'note'}, {'sequence': 'read the rest of this page', 'score': 0.11293990164995193, 'token': 3674, 'token_str': 'page'}, {'sequence': 'read the rest of this week', 'score': 0.06504543870687485, 'token': 1989, 'token_str': 'week'}]
Run Code Online (Sandbox Code Playgroud)
给定一个token
ID(例如1989
),我如何才能score
有效地找到对应的ID?我必须为每个列表多次执行此操作(我有几个这样的大列表,并且对于每个列表我都有几个令牌 ID)。
我目前正在迭代列表中的每个字典并检查它们是否与ID
我的输入 ID 匹配,如果匹配,我将获得score
. 但速度相当慢。
考虑以下网络:
%%time
import torch
from torch.autograd import grad
import torch.nn as nn
import torch.optim as optim
class net_x(nn.Module):
def __init__(self):
super(net_x, self).__init__()
self.fc1=nn.Linear(1, 20)
self.fc2=nn.Linear(20, 20)
self.out=nn.Linear(20, 400) #a,b,c,d
def forward(self, x):
x=torch.tanh(self.fc1(x))
x=torch.tanh(self.fc2(x))
x=self.out(x)
return x
nx = net_x()
#input
val = 100
t = torch.rand(val, requires_grad = True) #input vector
t = torch.reshape(t, (val,1)) #reshape for batch
#method
dx = torch.autograd.functional.jacobian(lambda t_: nx(t_), t)
Run Code Online (Sandbox Code Playgroud)
这输出
CPU times: user 11.1 s, sys: 3.52 ms, total: 11.1 s
Wall time: …
Run Code Online (Sandbox Code Playgroud) python ×7
pytorch ×4
database ×1
dictionary ×1
github ×1
github-pages ×1
gpu ×1
graph ×1
huggingface ×1
jekyll ×1
matplotlib ×1
networkx ×1
plotly ×1
rdf ×1
ruby ×1
vector ×1
wikidata ×1