【问题标题】:Why model.fit() method of keras do not accept any tensor as feature or label argument, on the other hand it accepts numpy arrays为什么 keras 的 model.fit() 方法不接受任何张量作为特征或标签参数,另一方面它接受 numpy 数组
【发布时间】:2022-11-01 21:09:07
【问题描述】:

上次当我训练 dnn 模型时,我注意到当我尝试使用张量(dtype = float64)训练我的模型时,它总是会出错,但是当我使用具有相同规格(形状、值、dtype)的 numpy 数组训练模型时张量它没有显示错误。为什么会这样

Code

对于作为张量的特征和标签,将第二个脚本中的 numpy.arrays 替换为:

celsius_q    = tf.Variable([-40, -10,  0,  8, 15, 22,  38],  tf.float64)
fahrenheit_a = tf.Variable([-40,  14, 32, 46, 59, 72, 100],  tf.float64)

当使用特征和标签作为张量时,它会显示这个错误:

Error: ValueError: Failed to find data adapter that can handle input:
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>,
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>

【问题讨论】:

  • 请查看如何创建minimal reproducible example。您应该发布导致错误的最小可执行代码
  • 不要使用tf.Variable 作为输入。使用tf.constant 创建张量,例如celsius_q = tf.constant([-40, -10, 0, 8, 15, 22, 38], tf.float64)

标签: numpy tensorflow machine-learning deep-learning data-science


【解决方案1】:

使用 tf.constant 在 Tensorflow 中创建输入张量。 tf.Variable 可以稍后更改,因此这种类型的张量不适合模型输入。请参考这个答案https://stackoverflow.com/a/44746203/20388268

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2015-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多