2017-06-07 17 views
0

TensorFlowのドキュメントに従って、Tensorflowをマスターしてください。Tensorflowプログラムでタイプ変換エラーが発生する

File "v-prog3-variables.py", line 7, in linear_model = W * x + b .. .. .. ValueError: Incompatible type conversion requested to type 'float32' for variable of type 'int32_ref'

import tensorflow as tf 

W = tf.Variable([.3], tf.float32) 
b = tf.Variable([-3], tf.float32) 
x = tf.placeholder(tf.float32) 
linear_model = 1.0 
linear_model = W * x + b 
#tf.to_float(linear_model, name='ToFloat') 

# Global initialization is must 
init = tf.global_variables_initializer() 
sess.run(init) 

print(sess.run(linear_model, {x:[1,2,3,4]})) 

このエラーでプログラムの結果の上の '互換性のない型変換エラー' におけるプログラム結果の下

は、私が「linear_modelを定義することで問題を解決しようとしました'float(linear_model = 1.0)またはtf.to_float(linear_model = W * x + b)の変数

しかし何も動作しません

私はTensorFlow初心者、私を助けてください。 ありがとうございます。

答えて

3

float32にリキャストして実行することができました。あなたはライブラリのソースコードを読んだことがありますか?そうですね...質問はしません。

import tensorflow as tf 

W = tf.Variable([.3], tf.float32) 
b = tf.Variable([-3], tf.float32) 
x = tf.placeholder(tf.float32) 

linear_model = W * x + tf.cast(b, tf.float32) 

init = tf.global_variables_initializer() 
sess = tf.Session() 
sess.run(init) 

print(sess.run(linear_model, {x:[1,2,3,4]})) 
+0

Thx Alex、それは働いた。 – Vikram

1

タイプには名前付き引数を使用する必要があります。 tf.Variable([.3], dtype=tf.float32)

tf.Variableの署名は、一般的なエラーにつながる__init__(self, initial_value=None, trainable=True,...です。

  • ​​がtf.float32を与えるだろう、と
  • [-3]は、あなたがそれらを掛けたときにあなたが得たエラーにつながるtf.int32 を与える:

    __init__方法は、あなたの入力からタイプを推測します。

あなたはtf.float32タイプに固執したい場合は、bの初期値として[-3.]を使用することができます。

関連する問題