fn

How to return a string (or similar) from Rust in WebAssembly?

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I created a small WASM file from this Rust code: #[no_mangle] pub fn hello() -> &'static str { "hello from rust" } It builds and the hello function can be called from JS: My problem is that the alert displays "undefined". If I return a i32 , it works and displays the i32 . I also tried to return a String but it does not work (it still displays "undefined"). Is there a way to return a string from Rust in WebAssembly? What type should I use? 回答1: WebAssembly only supports a few numeric types , which is all that can be returned via an exported

How can you make a safe static singleton in Rust?

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This is something of a controversial topic, so let me start by explaining my use case, and then talk about the actual problem. I find that for a bunch of unsafe things, it's important to make sure that you don't leak memory; this is actually quite easy to do if you start using transmute() and forget() . For example, passing a boxed instance to C code for an arbitrary amount of time, then fetching it back out and 'resurrecting it' by using transmute . Imagine I have a safe wrapper for this sort of API: trait Foo {} struct CBox; impl CBox { //

Training and Predicting with instance keys

匿名 (未验证) 提交于 2019-12-03 01:27:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am able to train my model and use ML Engine for prediction but my results don't include any identifying information. This works fine when submitting one row at a time for prediction but when submitting multiple rows I have no way of connecting the prediction back to the original input data. The GCP documentation discusses using instance keys but I can't find any example code that trains and predicts using an instance key. Taking the GCP census example how would I update the input functions to pass a unique ID through the graph and ignore

Estimator.predict() has Shape Issues?

匿名 (未验证) 提交于 2019-12-03 01:25:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I can train and evalaute a Tensorflow Estimator model without any problems. When I do prediction, this error arises: InvalidArgumentError (see above for traceback): output_shape has incorrect number of elements: 68 should be: 2 [[Node: output = SparseToDense[T=DT_INT32, Tindices=DT_INT32, validate_indices=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](ToInt32, ToInt32_1, ToInt32_2, bidirectional_rnn/bidirectional_rnn/fw/fw/time)]] All of the model functions use the same architecture: def _train_model_fn(features, labels, mode,

How do I pass Rc<RefCell<Box<MyStruct>>> to a function accepting Rc<RefCell<Box<MyTrait>>>?

匿名 (未验证) 提交于 2019-12-03 01:21:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have originally asked this question here , but it was marked as duplicate, although it duplicates only a part of it in my opinion, so I have created a more specific one: Consider the following code: use std :: rc :: Rc ; trait MyTrait { fn trait_func (& self ); } struct MyStruct1 ; impl MyStruct1 { fn my_fn (& self ) { // do something } } impl MyTrait for MyStruct1 { fn trait_func (& self ) { // do something } } fn my_trait_fn ( t : Rc < MyTrait >) { t . trait_func (); } fn main () { let my_str : Rc < MyStruct1 > = Rc :: new (

How to call a method that consumes self on a boxed trait object?

匿名 (未验证) 提交于 2019-12-03 01:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have the following sketch of an implementation: trait Listener { fn some_action (& mut self ); fn commit ( self ); } struct FooListener {} impl Listener for FooListener { fn some_action (& mut self ) { println !( "{:?}" , "Action!!" ); } fn commit ( self ) { println !( "{:?}" , "Commit" ); } } struct Transaction { listeners : Vec < Box < Listener >>, } impl Transaction { fn commit ( self ) { // How would I consume the listeners and call commit() on each of them? } } fn listener () { let transaction = Transaction { listeners : vec

How can I store function pointers in an array? [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:18:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Accepting named functions vs. unboxed closures : distinct types issues 1 answer How do you stick functions (or function pointers) into an array for testing purposes? fn foo() -> isize { 1 } fn bar() -> isize { 2 } fn main() { let functions = vec![foo, bar]; println!("foo() = {}, bar() = {}", functions[0](), functions[1]()); } This code in the Rust playground This is the error code I get: error: mismatched types: expected `fn() -> isize {foo}`, found `fn() -> isize {bar}` (expected fn item, found a

Constructor, Copy Constructor and Stack Creation : C++

匿名 (未验证) 提交于 2019-12-03 01:09:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question is regarding Function Stack Creation. Suppose we create a function fn(int a,char b) and call from main fn(A,B) , in this case when the function is called a fn. stack is created with return address, Stack pointer (etc) where local variables and parameters are created and on return is destroyed. I have a few questions: 1) For our parameterized constructor suppose myClass{ int a; char c; public: myClass(int a,char c) { this->a=a; this->c=c; } }; does the constructor myClass(int a,char c) also create its function stack and create

How to use a decaying learning rate with an estimator in tensorflow?

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to use a LinearClassifier with a GradientDescentOptimizer with a decaying learning rate. My code: def main(): # load data features = np.load('data/feature_data.npz') tx = features['arr_0'] y = features['arr_1'] ## Prepare logistic regression n_point, n_feat = tx.shape # Input functions def get_input_fn_from_numpy(tx, y, num_epochs=None, shuffle=True): # Preprocess data return tf.estimator.inputs.numpy_input_fn( x={"x":tx}, y=y, num_epochs=num_epochs, shuffle=shuffle, batch_size=128 ) cols_label = "x" feature_cols = [tf.contrib

Error message: Error in fn(x, …) : Downdated VtV is not positive definite

匿名 (未验证) 提交于 2019-12-03 00:56:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Thanks in advance for anyone who gives this a look over. I've seen this problem once on the archives but I'm a bit new to R and had a lot of trouble understanding both the problem and the solution... I'm trying to use the lmer function to create a minimum adequate model. My model is Mated ~ Size * Attempts * Status + (random factor). as.logical(Mated) as.numeric(Size) as.factor(Attempts) as.factor(Status) (These have all worked on previous models) So after all that I try running my model: Model1<-lmer(Mated ~ Size*Status*Attempts + (1