天道酬勤,学无止境

callback

Exception with Callback in Keras - Tensorflow 2.0 - Python

The following code runs a Sequential Keras model, pretty straight forward , on the MNIST data that are packaged with Keras. In running the following piece of code I get an exception. The code is readily reproducible. import tensorflow as tf class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): if(logs.get('acc')>0.99): print("\nReached 99% accuracy so cancelling training!") self.model.stop_training = True mnist = tf.keras.datasets.mnist (x_train, y_train),(x_test, y_test) = mnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0 callbacks =

2022-01-17 08:42:59    分类:问答    python   python-3.x   keras   callback

Gulp: call an async function which provides its own callback from within a transform function

I want to create a function for use in a pipe() call in Gulp that'll enable conversion of xlsx files to json. I had this working with NPM package 'excel-as-json' for gulp 3, however Gulp 4 has forced me to actually understand what it is doing ;-) Six hours in, and my incapacity of getting this to work due to a lack of js/async/streaming knowledge is punching my curiosity. Code is as follows: paths = {excel_sourcefiles: "./sourcefiles/*.xls*", excel_targetdir_local_csvjson: "./targetfiles_local/*.*"} var replaceExt = require('replace-ext'); var PluginError = require('plugin-error') var

2022-01-17 08:17:41    分类:问答    javascript   node.js   asynchronous   callback   gulp

Avoiding a static member function in c++ when using a callback interface from C

I would like to access the data within this member function that is static. Right now the member function is static so that I can use it with a third party API written in C that has typdef function pointer for callback purposes. Based on the info below, what is the best way to get around the need to create a static function in order to use the data from the following function member within other member functions of my class that are non-static. Maybe there is a way to still use this static function but still overcome the inability to mix static with non-static variables. My code does works as

2022-01-17 06:32:50    分类:问答    c++   callback   function-pointers

使函数调用等待 Web SQL 查询(Make function call wait for web SQL query)

问题 无论传递的数字是否为素数,Primecheck 函数都应该返回 true 或 false。 如果数字是素数,函数将其添加到 PRIMES 表中。 这是一个埃拉托色尼筛算法,但还没有完成。 function primecheck (number) { var isprime = true; if (number%10 == 1 || number%10 == 3 || number%10 == 7 || number%10 == 9) { db.transaction(function (tx) { tx.executeSql('SELECT * from Primes', [], function (tx, result) { for (var i = 1; i < result.rows.length; i++) { if (number%result.rows.item(i)['prime'] == 0) { isprime = false; break; } } if (isprime) { tx.executeSql('INSERT INTO PRIMES (prime) values (?)', [number]); } return isprime; }, null); }, null, null); } else { isprime = false

2022-01-16 16:53:19    分类:技术分享    javascript   callback   web-sql

AttributeError:'function'对象没有属性'func_name'和python 3(AttributeError: 'function' object has no attribute 'func_name' and python 3)

问题 我下载了以下代码: from __future__ import print_function from time import sleep def callback_a(i, result): print("Items processed: {}. Running result: {}.".format(i, result)) def square(i): return i * i def processor(process, times, report_interval, callback): print("Entered processor(): times = {}, report_interval = {}, callback = {}".format( times, report_interval, callback.func_name)) # Can also use callback.__name__ instead of callback.func_name in line above. result = 0 print("Processing data ...") for i in range(1, times + 1): result += process(i) sleep(1) if i % report_interval == 0: # This

2022-01-16 16:33:40    分类:技术分享    python   python-3.x   function   callback   python-2.x

为什么回调在 Ruby on Rails 中使用符号(Why do callbacks use symbols in Ruby on Rails)

问题 我很难理解何时以及何时不在 Rails 中使用符号。 我知道如果没有许多方法,符号与字符串并没有太大的不同。 我也理解符号是好的键,因为同名的符号在内存中占据一个地址。 我难以理解的是为什么 Rails 决定在某些情况下使用符号。 如果我有回调 before_action :ask_stack_overflow_question def ask_stack_overflow_question puts "why did I just use a symbol?" end 我不太明白为什么将方法视为符号? 为什么我需要保持方法不变? 我在文档中找不到任何答案。 谢谢 回答1 这并不像看起来那么棘手。 调用before_action时,您不希望该方法立即执行,但您必须描述您希望以某种方式执行的操作。 这里使用一个符号来表示要调用的方法的名称。 因此,如果这被捕获为name ,那么稍后,在代码中的某处,它会在适当的时间转换为send(name) 。 这两者有很大的不同。 即时版本是: before_action method_name 这具有立即执行method_name的效果,因为 Ruby 将被迫运行method_name以找出它返回的内容,然后将其作为参数传递给before_action 。 延期约定是: before_action :method_name

2022-01-16 14:54:45    分类:技术分享    ruby-on-rails   ruby   callback   symbols

while循环中的异步函数(Asynchronous function in a while-loop)

问题 我有一个关于如何在 while 循环中执行异步任务直到满足某些条件的问题。 这更像是一个理论问题,但我可以看到在某些情况下这可能是一个问题。 我将尝试在一个示例中演示该问题(我在这里使用 JavaScript,但您可以使用任何语言): 我可以拥有一个设备,并且我想保留我的应用程序,直到该设备达到特定状态。 如果我可以获取设备状态的方法是同步的,则代码可能如下所示: // Hold until the desired state is reached var state = false; while (!state) { state = device.getStateSync(); } // [...] continue the program 我现在的问题是:当我从设备获得的只是一个异步 getState 函数时,如何转换这段代码? 无论调用的执行时间有多长,代码都应该工作,并且应该记住我正在处理有限的内存和堆栈大小。 // [...] This would call the async function in a loop and crash the program while (!state) { // [...] something device.getStateAsync(function(newState) { state = newState; }); // [

2022-01-16 14:51:37    分类:技术分享    javascript   asynchronous   callback   theory   synchronous

Make function call wait for web SQL query

Primecheck function is supposed to return true or false whether passed number is prime or not. If the number is prime, function adds it to PRIMES table. This is a Sieve of Eratosthenes algorithm, but it's not finished yet. function primecheck (number) { var isprime = true; if (number%10 == 1 || number%10 == 3 || number%10 == 7 || number%10 == 9) { db.transaction(function (tx) { tx.executeSql('SELECT * from Primes', [], function (tx, result) { for (var i = 1; i < result.rows.length; i++) { if (number%result.rows.item(i)['prime'] == 0) { isprime = false; break; } } if (isprime) { tx.executeSql(

2022-01-16 13:08:25    分类:问答    javascript   callback   web-sql

AttributeError: 'function' object has no attribute 'func_name' and python 3

I downloaded the following code : from __future__ import print_function from time import sleep def callback_a(i, result): print("Items processed: {}. Running result: {}.".format(i, result)) def square(i): return i * i def processor(process, times, report_interval, callback): print("Entered processor(): times = {}, report_interval = {}, callback = {}".format( times, report_interval, callback.func_name)) # Can also use callback.__name__ instead of callback.func_name in line above. result = 0 print("Processing data ...") for i in range(1, times + 1): result += process(i) sleep(1) if i % report

2022-01-16 12:12:25    分类:问答    python   python-3.x   function   callback   python-2.x

通过 Tornado 中附加到 IOLoop 的回调写回(Write back through the callback attached to IOLoop in Tornado)

问题 有一个棘手的后处理程序,有时它可能需要很多时间(取决于输入值),有时不是。 我想要的是每当 1 秒过去时写回,动态分配响应。 def post(): def callback(): self.write('too-late') self.finish() timeout_obj = IOLoop.current().add_timeout( dt.timedelta(seconds=1), callback, ) # some asynchronous operations if not self.request.connection.stream.closed(): self.write('here is your response') self.finish() IOLoop.current().remove_timeout(timeout_obj) 事实证明,我在callback内无能为力。 即使引发异常也会被内部上下文抑制,并且不会通过post方法传递。 还有其他实现目标的方法吗? 谢谢你。 UPD 2020-05-15 :我发现了类似的问题 感谢@ionut-ticus,使用with_timeout()更方便。 经过一些尝试,我想我真的很接近我正在寻找的东西: def wait(fn): @gen.coroutine @wraps(fn) def wrap(

2022-01-16 12:07:58    分类:技术分享    python   asynchronous   callback   tornado   python-asyncio