类型错误:期望的是 float32,但实际传入的是包含 '_Message' 类型张量的列表。

3
我正在使用Windows 10上的Tensorflow 1.4.0和Python 3.6。我查看了其他有关值排序的帖子,但到目前为止没有发现有效的解决方法。
谢谢。
import tensorflow as tf
import numpy as np
from sklearn.datasets import fetch_california_housing

housing = fetch_california_housing()
m, n = housing.data.shape
housing_data_plus_bias = np.c_[np.ones((m, 1)), housing.data]

#normalization
scaled_housing_data_plus_bias = tf.nn.l2_normalize(housing_data_plus_bias, 1, epsilon=1e-12,name="Normalized")


n_epochs = 1000
learning_rate = 0.01

#error occurs here
X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")




Traceback (most recent call last):
  File "C:/Users/tony/PycharmProjects/NNCourse/Hands-On_Book_5.py", line 14, in <module>
    X = tf.constant(scaled_housing_data_plus_bias, dtype=tf.float32, name="X")
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 383, in make_tensor_proto
_AssertCompatible(values, dtype)
  File "C:\Users\tony\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\framework\tensor_util.py", line 303, in _AssertCompatible
(dtype.name, repr(mismatch), type(mismatch).__name__))
TypeError: Expected float32, got list containing Tensors of type '_Message' instead.
2个回答

5

tf.constant函数的value参数接受常量值或列表。但你传递了一个不可能被接受的tensor

考虑以下示例,您会得到类似的错误:

y = tf.ones((2,2))
x_c = tf.constant(y, dtype = tf.float32)

错误:

TypeError: Expected float32, got list containing Tensors of type '_Message' instead.

为了解决这个问题,请检查您为什么要将张量转换为常数?也许你根本不需要这个操作。

0

你可以使用一个不可训练的变量代替常量:

X = tf.Variable(scaled_housing_data_plus_bias, dtype=tf.float64, name="X", trainable=False)

设置trainable=False意味着TensorFlow不会尝试更改它以最小化您的成本函数。请注意,我需要将类型更改为float64;您可能不需要。

然而,当它们仍然是Numpy数组时规范化值可能会更清晰,并且然后使用它来创建tf.constant


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接