Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a float64 Variable in tensorflow

I am trying to do a logistic regression and my training dataset is derived from a a numpy float64 array. My code looks like,

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

This throws an exception

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

This probably because W and b are float32 and not float64. Is there a way to convert W and b OR create it as a float64

like image 737
nitin Avatar asked Mar 09 '16 05:03

nitin


People also ask

How do I assign a value in TensorFlow?

Tensorflow variables represent the tensors whose values can be changed by running operations on them. The assign() is the method available in the Variable class which is used to assign the new tf. Tensor to the variable. The new value must have the same shape and dtype as the old Variable value.

What are trainable variables in TensorFlow?

From my understanding, trainable means that the value could be changed during sess.run() That is not the definition of a trainable variable. Any variable can be modified during a sess. run() (That's why they are variables and not constants).

What is the difference between tf constant and tf variable?

In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation). Nevertheless, when you declare a Variable, you can change its value in the future with tf.


1 Answers

To make this work, you should define the W and b variables with tf.float64 initial values. The tf.truncated_normal() and tf.zeros() ops each take an optional dtype argument that can be set to tf.float64 as follows:

    W = tf.Variable(tf.truncated_normal([115713, 2], dtype=tf.float64))
    b = tf.Variable(tf.zeros([2], dtype=tf.float64))
like image 63
mrry Avatar answered Oct 01 '22 01:10

mrry