小编Mr.*_*eer的帖子

如何使用 Web Crypto API 创建哈希?

我正在尝试在客户端创建 SHA-1 哈希。我正在尝试使用 Web Crypto API 来做到这一点,但是当我将输出与各种在线工具给我的输出进行比较时,结果完全不同。我认为问题在于 ArrayBuffer 到 Hex 的转换。这是我的代码:

function generateHash() {
            var value = "mypassword";
            var crypto = window.crypto;
            var buffer = new ArrayBuffer(value);
            var hash_bytes = crypto.subtle.digest("SHA-1", buffer);
            hash_bytes.then(value => document.write([...new Uint8Array(value)].map(x => x.toString(16).padStart(2, '0')).join('')));
        }
Run Code Online (Sandbox Code Playgroud)

的输出document.write应该是:

91dfd9ddb4198affc5c194cd8ce6d338fde470e2
Run Code Online (Sandbox Code Playgroud)

但事实并非如此,我得到了不同长度的完全不同的哈希值(应该是 40)。我可以就这个问题提供一些建议吗?谢谢。

javascript cryptography ecmascript-6 webcrypto-api

6
推荐指数
1
解决办法
4489
查看次数

React:异步和等待不适用于 fetch

我在 Node 服务器上有 API,在调用时返回如下 JSON:

{"result":[{"ProductID":1,"ProductName":"iPhone10","ProductDescription":"Latest smartphone from Apple","ProductQuantity":100}]}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 fetch API 和 React 向用户显示所有这些信息,但无论我的调用返回什么undefined。这是我的反应代码:

const [products, setProducts] = useState({})

async function getProducts() {
    await fetch(`http://127.0.0.1:5000/listProducts`)
    .then(response => response.json())
    .then(response=>{
      setProducts({products:response.result})
      console.log(response.result);
      products.map(products =>
        <h1>{products.ProductName}</h1>
        <h1>{products.ProductDescription}</h1>
        )
    })
    .catch(err=>console.error(err))
  }
Run Code Online (Sandbox Code Playgroud)

页面加载时,函数 getProducts() 被调用一次。我做错了什么?提前致谢。

javascript json reactjs fetch-api

5
推荐指数
2
解决办法
3万
查看次数

Pandas: check if column value is unique

I have a DataFrame like:

         value
0          1
1          2
2          2
3          3
4          4
5          4
Run Code Online (Sandbox Code Playgroud)

I need to check if each value is unique or not, and mark that boolean value to new column. Expected result would be:

         value        unique
0          1           True
1          2           False
2          2           False
3          3           True
4          4           False
5          4           False
Run Code Online (Sandbox Code Playgroud)

I have tried:

df['unique'] = ""
df.loc[df["value"].is_unique, 'unique'] = True
Run Code Online (Sandbox Code Playgroud)

But this throws exception:

cannot use a single bool …
Run Code Online (Sandbox Code Playgroud)

python pandas

4
推荐指数
1
解决办法
89
查看次数

Transformers:如何使用 CUDA 进行推理?

我已经使用 GPU 微调了我的模型,但推理过程非常慢,我认为这是因为推理默认使用 CPU。这是我的推理代码:

txt = "This was nice place"
model = transformers.BertForSequenceClassification.from_pretrained(model_path, num_labels=24)
tokenizer = transformers.BertTokenizer.from_pretrained('TurkuNLP/bert-base-finnish-cased-v1')
encoding = tokenizer.encode_plus(txt, add_special_tokens = True, truncation = True, padding = "max_length", return_attention_mask = True, return_tensors = "pt")
output = model(**encoding)
output = output.logits.softmax(dim=-1).detach().cpu().flatten().numpy().tolist()
Run Code Online (Sandbox Code Playgroud)

这是我的第二个推理代码,它使用管道(针对不同的模型):

classifier = transformers.pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
result = classifier(txt)
Run Code Online (Sandbox Code Playgroud)

如何强制 Transformer 库在 GPU 上进行更快的推理?我尝试添加model.to(torch.device("cuda"))但会引发错误:

Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu
Run Code Online (Sandbox Code Playgroud)

我认为问题与未发送到 GPU 的数据有关。这里有一个类似的问题:pytorch summarise Failures …

python inference pytorch huggingface-transformers

4
推荐指数
1
解决办法
1万
查看次数