python - 在 tensorflow 中创建一个 float64 变量

标签 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

最佳答案

要实现此功能,您应该使用 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))

关于python - 在 tensorflow 中创建一个 float64 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884045/

相关文章:

c++ - 将指针数组转换到其他对象?

tensorflow - 日志消息分类/分组并为每个组找到人类可读的模式

python - 如何正确分割代码串

python - tf.Estimator 训练后检索张量 (Numpy) 值

python - lambda x : lambda: x 的用途

使用 numpy 数组输入的 Python 函数给出意外结果

machine-learning - 共现图(TCG)的用途(优点)是什么?

opencv - SVM Predict 返回一个不是 1 或 -1 的大值

python - 哪种数据结构和/或算法适合这个问题?

java - ANTLR 是否提供语义