ValueError Traceback (most recent call last)
<ipython-input-30-33821ccddf5f> in <module>
23 output = model(data)
24 # calculate the batch loss
---> 25 loss = criterion(output, target)
26 # backward pass: compute gradient of the loss with respect to model parameters
27 loss.backward()
C:\Users\mnauf\Anaconda3\envs\federated_learning\lib\site-packages\torch\nn\modules\module.py in __call__(self, *input, **kwargs)
487 result = self._slow_forward(*input, **kwargs)
488 else:
--> 489 result = self.forward(*input, **kwargs)
490 for hook in self._forward_hooks.values():
491 hook_result = hook(self, input, result)
C:\Users\mnauf\Anaconda3\envs\federated_learning\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
593 self.weight,
594 pos_weight=self.pos_weight,
--> …Run Code Online (Sandbox Code Playgroud) &代码中的目的是什么&i in list?如果我删除&,则会在 中产生错误largest = i,因为它们的类型不匹配(其中iis&32和iis i32)。但是如何&i转换i为i32?
fn largest(list: &[i32]) -> i32 {
println!("{:?}", list);
let mut largest = list[0];
for &i in list {
if i > largest {
largest = i;
}
}
largest
}
fn main() {
let hey = vec![1, 3, 2, 6, 90, 67, 788, 12, 34, 54, 32];
println!("The largest number is: {}", largest(&hey)); …Run Code Online (Sandbox Code Playgroud) 我知道这setTimeout是一个异步运行的 API,我想同步运行它。使用async/await如下所示应该bla bla首先打印,但我bla先打印。
async function testing(){
console.log("testing function has been triggered");
await setTimeout(function () {
console.log("bla bla")
}, 4200)
console.log("bla");
}
Run Code Online (Sandbox Code Playgroud) 我按照 Diesel 的官方文档开始使用。我还安装了 PostgreSQL。我的数据库用户名是postgres,密码是1schoollife@。
我开始于
$ echo DATABASE_URL=postgres://postgres:1schoollife@@localhost/diesel_demo > .env
$ diesel setup
Run Code Online (Sandbox Code Playgroud)
结果:
Creating migrations directory at: /home/naufil/Desktop/rust/3june/testing/migrations
Creating database: diesel_demo
database "diesel_demo" already exists
Run Code Online (Sandbox Code Playgroud)
我创建了一个迁移:
$ diesel migration generate create_posts
Creating migrations/2019-06-03-182531_create_posts/up.sql
Creating migrations/2019-06-03-182531_create_posts/down.sql
Run Code Online (Sandbox Code Playgroud)
迁移数据库时出现以下错误:
$ diesel migration run
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ConnectionError(BadConnection("could not translate host name \"@localhost\" to address: Name or service not known\n"))', src/libcore/result.rs:997:5
note: Run with `RUST_BACKTRACE=1` environment variable to display a …Run Code Online (Sandbox Code Playgroud) 当我搜索“ 什么是2 + 2 ” 时,我试图抓取Google结果,但是下面的代码正在返回'NoneType' object has no attribute 'text'。请帮助我实现所需的目标。
text="What is 2+2"
search=text.replace(" ","+")
link="https://www.google.com/search?q="+search
headers={'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'}
source=requests.get(link,headers=headers).text
soup=BeautifulSoup(source,"html.parser")
answer=soup.find('span',id="cwos")
self.respond(answer.text)
Run Code Online (Sandbox Code Playgroud)
唯一的问题是idin soup.find,但是我非常仔细地选择了此id。我不要误会 我也尝试过answer=soup.find('span',class_="cwcot gsrt"),但是都没有用。
Rust文档告诉我们cargo build在编译后创建一个二进制文件,可以使用执行该文件cargo run。cargo run如果在cargo build执行命令后发现任何更改,它将再次编译代码。它还说该cargo build --release命令创建了最终程序,它将运行得更快。
我的问题是,为什么当我这样做时cargo build --release,它会编译代码,这很好。但是,当我执行时cargo run,即使此后我没有更改任何代码,它也会再次编译代码。它与一起正常工作cargo build,然后cargo run与前一个命令一起编译一次。
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo build
Compiling variables v0.1.0 (/home/naufil/Desktop/rust/20April/variables)
Finished dev [unoptimized + debuginfo] target(s) in 0.35s
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.02s
Running `target/debug/variables`
Hello, world! 6
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo build --release
Compiling variables v0.1.0 (/home/naufil/Desktop/rust/20April/variables)
Finished release [optimized] target(s) in 0.34s
naufil@naufil-Inspiron-7559:~/Desktop/rust/20April/variables$ cargo run …Run Code Online (Sandbox Code Playgroud) Epoch: 1 Training Loss: 0.816370 Validation Loss: 0.696534
Validation loss decreased (inf --> 0.696534). Saving model ...
Epoch: 2 Training Loss: 0.507756 Validation Loss: 0.594713
Validation loss decreased (0.696534 --> 0.594713). Saving model ...
Epoch: 3 Training Loss: 0.216438 Validation Loss: 1.119294
Epoch: 4 Training Loss: 0.191799 Validation Loss: 0.801231
Epoch: 5 Training Loss: 0.111334 Validation Loss: 1.753786
Epoch: 6 Training Loss: 0.064309 Validation Loss: 1.348847
Epoch: 7 Training Loss: 0.058158 Validation Loss: 1.839139
Epoch: 8 Training Loss: 0.015489 Validation …Run Code Online (Sandbox Code Playgroud) Rust文档说默认的整数类型是i32,这意味着变量默认可以保存的最大数字是2147483647ie 2e31 - 1。事实证明,这是千真万确的:如果我试图挽救数量大于2e31 - 1在x变,我得到的错误literal out of range。
码
fn main() {
let x = 2147483647;
println!("Maximum signed integer: {}", x);
let x = 2e100;
println!("x evalues to: {}", x);
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我2e100在x变量中保存值,为什么我不会出错?它的计算结果肯定大于2e31 - 1。
输出量
fn main() {
let x = 2147483647;
println!("Maximum signed integer: {}", x);
let x = 2e100;
println!("x evalues to: {}", x);
}
Run Code Online (Sandbox Code Playgroud)
码
fn main() {
let …Run Code Online (Sandbox Code Playgroud) 为什么我需要使这个闭包变量可变?闭包不返回任何内容,因此闭包变量中没有存储任何内容。这个闭包只是从环境中捕获一个值并递增它。
fn main() {
let mut x = 1;
let mut y = || x = x + 1;
y();
println!("{}", x);
}
Run Code Online (Sandbox Code Playgroud) 我正在制作十进制到二进制的转换程序。为什么我无法将字符串解析为数字?binary_vec[i]是一个character。我应用to_string()了将其转换为字符串的方法,因为parse()它不适用于字符,但仍然给我一个错误。
use std::io;
fn main() {
let mut binary = String::new();
println!("Enter a decimal: ");
io::stdin().read_line(&mut binary)
.ok()
.expect("Couldn't read line");
println!("{}",to_decimal(binary));
}
fn to_decimal(mut binary_str:String) -> String {
let mut binary_no: u32 = binary_str.trim().parse().expect("invalid input");
if binary_no == 0 {
format!("{}",binary_no)
}
else {
let mut bits = String::new();
let mut binary_vec: Vec<char> = binary_str.chars().collect();
let mut result = 0;
let mut i = 0;
while i <=binary_str.len()-2{
let mut result …Run Code Online (Sandbox Code Playgroud) 如何替换Node.js中的单词,例如,如果我想将is字符串下面的单词替换为done:
He is a Pakistani and is a good person
Run Code Online (Sandbox Code Playgroud)
输出仅给出:
He done a pakdonetani and done a good person代替He done a Pakistani and done a good person。我正在使用此代码:
var str = "He is a Pakistani and is a good person";
var res = str.replace(/is/g, "done");
console.log(res);
Run Code Online (Sandbox Code Playgroud)
可以这样说,改为:
var res = str.replace(/ is /g, "done");
Run Code Online (Sandbox Code Playgroud)
但它对于以下字符串将失败:
var str = "He is a Pakistani and a good person he is.";
Run Code Online (Sandbox Code Playgroud)
为了应对这种情况,我们可以:
var res = str.replace(/ …Run Code Online (Sandbox Code Playgroud) rust ×6
python ×2
pytorch ×2
types ×2
ampersand ×1
async-await ×1
asynchronous ×1
closures ×1
compilation ×1
javascript ×1
literals ×1
node.js ×1
parsing ×1
promise ×1
reference ×1
replace ×1
rust-cargo ×1
rust-diesel ×1
size ×1
string ×1
web-scraping ×1