我正在尝试编写一个在SWI-Prolog中在运行时生成新约束的程序.is_true([A,means,B])旨在在运行时生成另一个约束:
:- use_module(library(chr)).
:- chr_constraint is_true/1.
is_true([A,means,B]) ==> (is_true(A) ==> is_true(B),writeln('asserted')).
is_true([[A,is,true],means,[A,is,not,false]]).
is_true([something,is,true]).
Run Code Online (Sandbox Code Playgroud)
但是当我输入这些查询时,is_true约束似乎没有效果.is_true([something, is, not, false])不归还true:
?- is_true([something,is,true]).
true .
?- is_true([something,is,not,false]).
is_true([something, is, not, false]).
Run Code Online (Sandbox Code Playgroud)
在控制台中断言约束似乎没有任何影响:
?- asserta(is_true(A>B)==>(is_true(B<A),writeln("asserted"))).
true.
?- is_true(4>3).
is_true(4>3).
Run Code Online (Sandbox Code Playgroud)
还有另一种在运行时定义新CHR约束的方法吗?
据我所知,Prolog 没有任何内置的泛型编程机制。可以使用统一来模拟泛型,但这需要在运行时进行类型检查:
:- initialization(main).
:- set_prolog_flag(double_quotes, chars).
% this is a "generic" predicate, where A and B have the same type
add(A,B,C) :-
generic_types([A:Type,B:Type]),
(Type = number,
C is A + B;Type=var,C = A+B).
main :-
add(A,B,C),
add(3,4,D),
writeln(C),
writeln(D).
generic_types([]).
generic_types([A:B|C]) :-
member(B,[var,nonvar,float,rational,number,atom,atomic,compound,callable,ground,acyclic_term]),
call(B,A),
generic_types(C).
has_type(Type,A) :-
call(Type,A).
Run Code Online (Sandbox Code Playgroud)
是否可以在运行时不检查每个变量的类型的情况下编写“通用”谓词?
我正在尝试在 TPU 上微调 Huggingface Transformers BERT 模型。它在 Colab 中工作,但当我切换到 GCP 上的付费 TPU 时失败。Jupyter笔记本代码如下:
[1] model = transformers.TFBertModel.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
# works
[2] cluster_resolver = tf.distribute.cluster_resolver.TPUClusterResolver(
tpu='[My TPU]',
zone='us-central1-a',
project='[My Project]'
)
tf.config.experimental_connect_to_cluster(cluster_resolver)
tf.tpu.experimental.initialize_tpu_system(cluster_resolver)
tpu_strategy = tf.distribute.experimental.TPUStrategy(cluster_resolver)
#Also works. Got a bunch of startup messages from the TPU - all good.
[3] with tpu_strategy.scope():
model = TFBertModel.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')
#Generates the error below (long). Same line works in Colab.
Run Code Online (Sandbox Code Playgroud)
这是错误消息:
NotFoundError Traceback (most recent call last)
<ipython-input-14-2cfc1a238903> in <module>
1 with tpu_strategy.scope():
----> 2 model …Run Code Online (Sandbox Code Playgroud) google-cloud-platform google-colaboratory google-cloud-tpu bert-language-model huggingface-transformers
我有使用预训练的 bert 模型构建的语义搜索引擎的代码。我想将此模型转换为 tflite,以便将其部署到 google mlkit。我想知道如何转换它。我想知道是否有可能将其转换为 tflite。这可能是因为它在官方tensorflow网站上提到: https: //www.tensorflow.org/lite/convert。但我不知道从哪里开始
代码:
from sentence_transformers import SentenceTransformer
# Load the BERT model. Various models trained on Natural Language Inference (NLI) https://github.com/UKPLab/sentence-transformers/blob/master/docs/pretrained-models/nli-models.md and
# Semantic Textual Similarity are available https://github.com/UKPLab/sentence-transformers/blob/master/docs/pretrained-models/sts-models.md
model = SentenceTransformer('bert-base-nli-mean-tokens')
# A corpus is a list with documents split by sentences.
sentences = ['Absence of sanity',
'Lack of saneness',
'A man is eating food.',
'A man is eating a piece of bread.',
'The girl is carrying a baby.',
'A man …Run Code Online (Sandbox Code Playgroud) 我正在寻找有关使用 Bert 和 Bert 的屏蔽语言模型来预测多个标记的建议。
我的数据看起来像:
语境: some very long context paragraph
问题:rainy days lead to @placeholder这个问题的答案@placeholder是wet weather。在模型中,wet environment是要预测的答案。
那么在预处理阶段,我应该将文本更改为rainy days lead to [MASK]或类似的内容rainy days lead to [MASK] [MASK]吗?我知道 masked LM 在单个 token 预测上效果很好,你认为 masked LM 可以在多个 token 预测上很好地工作吗?如果没有,您对如何预处理和训练此类数据有什么建议吗?
非常感谢!
在许多函数式编程语言中,可以使用let表达式“重新定义”局部变量:
let example =
let a = 1 in
let a = a+1 in
a + 1
Run Code Online (Sandbox Code Playgroud)
为此我找不到内置的 Prolog 谓词,因此我尝试以let这种方式定义表达式:
:- initialization(main).
:- set_prolog_flag(double_quotes, chars).
replace(Subterm0, Subterm, Term0, Term) :-
( Term0 == Subterm0 -> Term = Subterm
; var(Term0) -> Term = Term0
; Term0 =.. [F|Args0],
maplist(replace(Subterm0,Subterm), Args0, Args),
Term =.. [F|Args]
).
let(A,B) :-
((D,D1) = (A1 is B1,C is B1);
(D,D1) = (A1=B1,C=B1)),
subsumes_term(D,A),
D=A,
replace(A1,C,B,B2),
call((D1,B2)).
main :- let(A = 1,( …Run Code Online (Sandbox Code Playgroud) 我正在玩一些javascript,并发现(至少对我来说)通过foreach循环处理多维数组时的奇怪行为.所以我有这段代码:
<script type="text/javascript">
var arr = [['a', 'b'], ['c','d']];
var first='';
for (var singleArray in arr) {
first += ' ' + singleArray[0] + ' ' + singleArray[1];
}
var second = '';
for (var i=0;i<arr.length; i++) {
second += ' ' + arr[i][0] + ' ' + arr[i][1];
}
console.log('First: ', first);
console.log('Second: ', second);
</script>
Run Code Online (Sandbox Code Playgroud)
输出是:
First: 0 undefined 1 undefined
Second: a b c d
Run Code Online (Sandbox Code Playgroud)
我希望第一和第二将是相同的.你能解释一下我错过了什么吗?
注意:请不要建议通过jQuery或其他方式迭代数组.我没有编码麻烦,我只是对这种特殊情况感到好奇.谢谢!
在有人向我射击之前,我知道Java到二进制编译器,我不会追随其中一个.
我也知道没有完美的工具可以毫无问题地转换所有东西.我知道缺少Java库是一个主要问题; 但是我的源代码不会使用许多Java库,除了像String和print之类的东西.我只想使用该工具来创建Java源引用的类.在字符串的情况下,我很乐意填补空白或在稍后阶段修复.我只是想让工具做无聊的位,所以我不必手动进行翻译.
在需要的类等的情况下,我将在稍后阶段手动修复它们,但是会欣赏指向某些东西的指针,这些指针至少可以完成无聊的东西.
我再次想要翻译源代码而不是编译器来生成二进制文件.基本上我想要一些Java东西并将其转换为C++,以便以后在其他项目中使用.
编辑附加说明
对不起,如果我在这个问题的前面部分不清楚.我知道Java与C++有很大的不同.我有一些Java代码,主要是处理数组和位,几乎没有对象创建.从某种意义上说,它是非常独立的,几乎没有其他类的调用.这些类似乎是转换的主要候选者; 其他的东西将不得不重写,但至少有些部分是杠杆化的.
使用caolan的node.js异步库,我一直在尝试调用一个async.series在另一个使用async.series的函数内部使用的函数,但我仍然无法使函数以正确的顺序运行,详情如下:
终端输出显示第一个函数在第一个函数之前被调用,原因不明:
The "sys" module is now called "util". It should have a similar interface.
Starting the second step in the series
Value of b: undefined
Invoking the function firstStep
the value of toObtain is: [object Object]
Run Code Online (Sandbox Code Playgroud)
这是相应的源代码:
var im = require('imagemagick');
var async = require('async');
var toObtain;
var b;
async.series([
function (callback) {
//It appears that this function is being invoked after the second function.
//Why is this happening?
firstStep();
callback();
},
function (callback) {
//Why is …Run Code Online (Sandbox Code Playgroud) prolog ×2
python ×2
arrays ×1
c ×1
c++ ×1
generics ×1
html ×1
java ×1
javascript ×1
let ×1
node.js ×1
polymorphism ×1
swi-prolog ×1
tensorflow ×1