【问题标题】:Creating a float64 Variable in tensorflow在张量流中创建一个 float64 变量
【发布时间】:2016-06-23 09:10:37
【问题描述】:

我正在尝试进行逻辑回归,我的训练数据集来自一个 numpy float64 数组。我的代码看起来像,

import tensorflow as tf
graph = tf.Graph()
with graph.as_default():
    examples =tf.constant(mat6)  # mat6 is a numpy float64 array
    t_labels = tf.constant(labels) # labels is an a numpy float64 array
    W = tf.Variable(tf.truncated_normal([115713, 2]))
    b = tf.Variable(tf.zeros([2]))
    logits = tf.matmul(examples, W)+b

这会引发异常

TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type float64 of argument 'a'.

这可能是因为 W 和 b 是 float32 而不是 float64。有没有办法转换 W 和 b 或将其创建为 float64

【问题讨论】:

    标签: python numpy machine-learning tensorflow


    【解决方案1】:

    要完成这项工作,您应该使用tf.float64 初始值定义Wb 变量。 tf.truncated_normal()tf.zeros() 操作均采用可选的 dtype 参数,该参数可以设置为 tf.float64,如下所示:

        W = tf.Variable(tf.truncated_normal([115713, 2], dtype=tf.float64))
        b = tf.Variable(tf.zeros([2], dtype=tf.float64))
    

    【讨论】:

    • 我发现的另一个解决方法是使用 tf.cast.. 像这样,W = tf.cast(tf.Variable(tf.truncated_normal([115713, 2])), tf.float64)
    • 考虑另一种方式并向下转换为 float32。这应该在 GPU 等上运行得更快,并且在大多数情况下你很少需要 NN 的额外精度。
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 2017-10-12
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    相关资源
    最近更新 更多