我在尝试进行逻辑回归,我的训练数据集来源于一个numpy的float64数组。我的代码如下所示,
import tensorflow as tfgraph = 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,或者直接创建为float64吗?
回答:
为了解决这个问题,你应该使用tf.float64
作为初始值来定义W
和b
变量。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))