为了学习如何编写自定义TensorFlow操作,我按照添加新操作教程并制作了一个"add_b"操作,b
为每个输入值添加一个标量.
add_b_op.cc
:
#define EIGEN_USE_THREADS
#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/core/framework/common_shape_fns.h"
#include "tensorflow/core/framework/op.h"
#include "tensorflow/core/framework/op_kernel.h"
#include "tensorflow/core/framework/shape_inference.h"
using namespace tensorflow;
REGISTER_OP("AddB")
.Attr("T: {float, double}")
.Input("input: T")
.Input("b: T")
.Output("output: T")
.SetShapeFn([] (shape_inference::InferenceContext* c) -> Status {
shape_inference::ShapeHandle out;
TF_RETURN_IF_ERROR(c->WithRank(c->input(1), 0, &out));
return shape_inference::UnchangedShape(c);
})
//----------------------------------------------------------------------
.Doc(R"doc(
Adds `b` to each input.
input: The input values.
b: A number to add to each input value.
)doc");
template <typename T>
class AddBCpuOp : public OpKernel {
public:
explicit AddBCpuOp(OpKernelConstruction* context) …
Run Code Online (Sandbox Code Playgroud) 我正在大学学习化学,想尝试在Perl6或Perl中编写教科书示例,比如平衡化学式或其他过程!
然后我遇到的问题是关于perl6自定义运算符.当我使用该功能时,我觉得我一直在重复我的代码和我自己.它很难读写.我该如何简化这个?
#!/usr/bin/env perl6
use v6;
#basic SI(International System of Units) type
role MetricPrefix {
method baseOn ( Str $base , Numeric $input ) {
given $base {
when 'pico' { return $input * 10**-12 }
when 'namo' { return $input * 10**-9 }
when 'micro' { return $input * 10**-6}
when 'milli' { return $input * 10**-3 }
when 'centi' { return $input * 10**-2 }
when 'hecto' { return $input * 10**2 }
when 'kilo' { return $input …
Run Code Online (Sandbox Code Playgroud) 它不应该是左联想吗?
我觉得
let a = b ?? c ?? d
被分组像
let a = (b ?? c) ?? d
不
let a = b ?? (c ?? d)
但它被宣布为右联盟.我误解或错过了什么吗?
operators null-coalescing-operator associativity custom-operator swift
let (**) ls1 ls2 = List.intersect ls1 ls2
Run Code Online (Sandbox Code Playgroud)
不起作用,因为 (**) 被视为注释。还有逃脱的可能吗?
我用 swift 编写了一个简单的队列类。它是通过数组实现的。现在我希望它的执行更像内置数组。所以我需要实现该[]
运算符但失败了。有人帮忙吗?
public class SimpleQueue<T : Any>
{
private var frontCur = 0
private var reuseCur = -1
private var capacity = 0
private var impl = [T]()
public var count : Int
{
get
{
return impl.count - frontCur
}
}
public func empty() -> Bool
{
return self.count == 0
}
public func size() -> Int
{
return impl.count
}
public func append(o : T)
{
if(frontCur > reuseCur && reuseCur >= 0)
{
impl[reuseCur] …
Run Code Online (Sandbox Code Playgroud) 我想在我的计算表达式上定义一些自定义运算符,但无法使其工作
type ZipSeq() =
[<CustomOperation("<*>")>]
member this.Apply f s =
f |> Seq.zip s |> Seq.map (fun (y, x) -> x(y))
member this.Return x =
Seq.initInfinite (fun _ -> x)
// (a -> b) -> seq<a> -> seq<b>
[<CustomOperation("<!>")>]
member this.Map f s =
this.Apply (this.Return f) s
let zipSeq = new ZipSeq()
let f (a : float) = a * a
let s = seq { yield 1. }
// seq<b>
let h1 = zipSeq.Map f s
//thinking h1 …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为数字创建一个运算符.例如,将数字递增10的运算符.
这是我写的代码:
prefix operator +++{}
prefix operator +++<T>(inout operand: T) -> T{
operand += 10
return operand
}
Run Code Online (Sandbox Code Playgroud)
我的+=
运营商出错了.它需要数字操作数.所以我这样做了:
protocol Numeric {}
extension Int: Numeric {}
extension Float: Numeric {}
extension Double: Numeric {}
prefix operator +++ {}
prefix operator +++<T: Numeric>(inout operand: T) -> T {
operand += 10
return operand
}
Run Code Online (Sandbox Code Playgroud)
但它无法编译.有人有什么想法吗?