在 tensorflow 中创建一个 float64 变量

Creating a float64 Variable in tensorflow

我正在尝试进行逻辑回归,我的训练数据集来自一个 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

要完成这项工作,您应该使用 tf.float64 初始值定义 Wb 变量。 tf.truncated_normal() and tf.zeros() ops 每个都有一个可选的 dtype 参数,可以设置为 tf.float64 如下:

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