在张量流中创建float64变量

时间:2016-03-09 05:58:20

标签: python numpy machine-learning 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

1 个答案:

答案 0 :(得分:9)

要使其发挥作用,您应该使用W初始值定义btf.float64变量。 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))