t1 = tf.linalg.diag([1.0, 2.0, 3.0])
t2 = tf.constant([1.0, 2.0, 3.0])
tf.concat(t1, t2)
我要做的就是將額外的一行[1.0, 2.0, 3.0]連接到矩陣[[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]。
它們都是浮點型別。但它失敗了
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, dtype_hint, ctx, accepted_result_types)
1588 raise ValueError(
1589 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r" %
-> 1590 (dtype.name, value.dtype.name, value))
1591 return value
1592
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: <tf.Tensor: shape=(1, 3), dtype=float32, numpy=array([[1., 2., 3.]], dtype=float32)>
編輯:即使沒有強制轉換,診斷輸出實際上也是一個浮點數:
<tf.Tensor: shape=(3, 3), dtype=float32, numpy=
array([[1., 0., 0.],
[0., 2., 0.],
[0., 0., 3.]], dtype=float32)>
即使在轉換為浮動之后它也不起作用并給出相同的錯誤
t0 = tf.linalg.diag([1.0, 2.0, 3.0])
t1 = tf.cast(t0, tf.float32)
t2 = tf.constant([[1.0, 2.0, 3.0]])
tf.concat(t1, t2)
uj5u.com熱心網友回復:
該錯誤具有誤導性,因為它與張量的資料型別沒有任何關系。嘗試這樣的事情,不要忘記使用方括號并設定axis引數tf.concat:
import tensorflow as tf
t1 = tf.linalg.diag([1.0, 2.0, 3.0])
t2 = tf.constant([1.0, 2.0, 3.0])
t2 = tf.expand_dims(t2, axis=0)
print(tf.concat([t1, t2], axis=0))
[[1. 0. 0.]
[0. 2. 0.]
[0. 0. 3.]
[1. 2. 3.]], shape=(4, 3), dtype=float32)
uj5u.com熱心網友回復:
看起來你的第一個張量是 type int32。如果要進行浮點運算,只需將其轉換為浮點數:
t1 = tf.cast(tf.linalg.diag([1.0, 2.0, 3.0]), tf.float32)
否則,您可以使用以下int32型別定義常量:
t2 = tf.constant([1.0, 2.0, 3.0], dtype=tf.int32)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/408852.html
標籤:
