类型错误:预期为 float32,得到的列表包含类型为“_Message”的张量

Log*_*ase 3 python normalization tensorflow

我在 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.
Run Code Online (Sandbox Code Playgroud)

use*_*882 5

tf.constant在其value参数中接受一个常量值或列表。你正在做的是为它提供一个tensor不可能的。

考虑下面的例子,你会得到类似的错误:

y = tf.ones((2,2))
x_c = tf.constant(y, dtype = tf.float32)
Run Code Online (Sandbox Code Playgroud)

错误:

TypeError: Expected float32, got list containing Tensors of type '_Message' instead.
Run Code Online (Sandbox Code Playgroud)

要解决这个问题,请检查您为什么真的要将张量转换为constant? 也许您一开始甚至可能不需要此操作。