简体   繁体   中英

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

I'm using Tensorflow 1.4.0 & Python 3.6 on Windows 10. I looked at other posts about the ordering of the values, but found nothing that worked so far.

Thanks.

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.

tf.constant accepts a constant value or list in it's value parameter. What you are doing is supplying it with a tensor which is not possible.

Consider the following example and you will get similar error:

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

Error:

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

To overcome this problem, check why you really want to convert the tensor into a constant ? Maybe you may not even require this operation in the first place.

One thing you can do is use an untrainable variable instead of a constant:

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

Setting trainable=False means that TensorFlow won't try to change it to minimize your cost function. Note that I needed to change the type to float64 ; you may not need to.

However, it would probably be cleaner to normalize the values while they're still a Numpy array, and then use that to create the tf.constant .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM