下面的代码去年还可以使用,但 keras/tensorflow/numpy 中的更新破坏了它。它现在输出下面的异常。有谁知道如何让它再次工作?
我正在使用:
import numpy as np
from keras.layers import LSTM, Embedding, Input, Bidirectional
dim = 30
max_seq_length = 40
vecs = np.random.rand(45,dim)
input_layer = Input(shape=(max_seq_length,))
embedding_layer = Embedding(len(vecs), dim, weights=[vecs], input_length=max_seq_length, trainable=False, name="layerA")(input_layer)
lstm_nobi = LSTM(max_seq_length, return_sequences=True, activation="linear", name="layerB")
lstm = Bidirectional(lstm_nobi, name="layerC")(embedding_layer)
Run Code Online (Sandbox Code Playgroud)
上面脚本的完整输出:https : //pastebin.com/DsQNWVwz
缩短输出:
2021-02-10 17:51:13.037468: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2021-02-10 17:51:13.037899: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI …Run Code Online (Sandbox Code Playgroud) 正如这篇文章所说,你可以像这样创建一个闭包向量:
let mut xs: Vec<Box<dyn Fn((i32, i32)) -> (i32, i32)>> = vec![
Box::new(move |(x, y)| (y, x)),
Box::new(move |(x, y)| (1 - y, 1 - x)),
];
Run Code Online (Sandbox Code Playgroud)
但是为什么不能使用以下方法附加到它:
xs.append(Box::new(move |(x, y)| (2 - y, 2 - x)));
Run Code Online (Sandbox Code Playgroud)
这引发了错误:
|
162 | xs.append(Box::new(move |(x, y)| (2 - y, 2 - x)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected mutable reference, found struct `std::boxed::Box`
|
= note: expected mutable reference `&mut std::vec::Vec<std::boxed::Box<dyn std::ops::Fn((i32, i32)) -> (i32, i32)>>`
found struct `std::boxed::Box<[closure@src/lib.rs:162:22: 162:50]>`
Run Code Online (Sandbox Code Playgroud) 是否有更好,更短,更容易阅读的以下代码版本:
char ar[100];
int main() {
//ar = "hello"; doesn't compile
ar[0] = 'h';
ar[1] = 'e';
ar[2] = 'l';
ar[3] = 'l';
ar[4] = 'o';
ar[5] = '\x00';
return 0;
}
Run Code Online (Sandbox Code Playgroud)
注意:类型ar必须是char[100]."真正的"计划是:
#include <string>
#include <cstdarg>
#define TO_STRING_BUF_SIZE 100
char toStringBuf[TO_STRING_BUF_SIZE];
std::string toCptr_(const char * format, ...) {
va_list argzeiger;
va_start(argzeiger, format);
int16_t ret = vsnprintf(toStringBuf, TO_STRING_BUF_SIZE, format, argzeiger);
if(ret >= TO_STRING_BUF_SIZE - 1) {
//toStringBuf = "buffer too small";
} else if(ret < 0) …Run Code Online (Sandbox Code Playgroud) 长话短说:为什么该程序输出“ 0”而不是“ 5”,如何使它输出“ 5”:main.py:
from mod import *
setvar(5)
printvar()
Run Code Online (Sandbox Code Playgroud)
mod.py:
var = 0
def setvar(x):
var = x
def printvar():
print("var =", var)
Run Code Online (Sandbox Code Playgroud)