【问题标题】:using built in tensor functions with R使用带有 R 的内置张量函数
【发布时间】:2021-04-03 02:46:46
【问题描述】:

在 R 中,我一直在尝试使用 Tensorflow 数学函数。我的问题是我不知道如何像使用 Python 一样将参数输入到 tensorflow 函数中。

比如tensorflowdot函数怎么做?

x = tf$constant(c(1, 2, 3))
y = tf$constant(c(9, 8, 7))
tf$tensordot(x, y, axes = 1)
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  TypeError: Cannot convert 1.0 to EagerTensor of dtype int32 

我可以手动重现tensordot的输出,但我想使用内置

tf$reduce_sum(x * y)

【问题讨论】:

    标签: r tensorflow


    【解决方案1】:

    axes 值被解析为浮点数,而不是整数。将其修复为整数可以解决错误:

    > tf$tensordot(x, y, axes = 1L) 
    tf.Tensor(46.0, shape=(), dtype=float32)
    

    这与how R defines numeric data types有关。

    is.integer(1) # FALSE
    is.double(1) # TRUE
    
    is.integer(1L) # TRUE
    is.double(1L) # FALSE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      相关资源
      最近更新 更多