Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 468, in make_tensor_proto
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 468, in <listcomp>
str_values = [compat.as_bytes(x) for x in proto_values]
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/util/compat.py", line 65, inas_bytes
(bytes_or_text,))
TypeError: Expected binary or unicode string, got tf.float32
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "cnn.py", line 56, in <module>
W_fc2 = weight_variable(tf.float32)
File "cnn.py", line 14, in weight_variable
initial = tf.truncated_normal(shape, stddev=0.1)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/random_ops.py", line 167, in truncated_normal
shape_tensor = _ShapeTensor(shape)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/random_ops.py", line 42,in _ShapeTensor
return ops.convert_to_tensor(shape, dtype=dtype, name="shape")
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 836,in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 926,in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 229, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 208, in constant
value, dtype=dtype, shape=shape, verify_shape=verify_shape))
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py", line 472, in make_tensor_proto
"supported type." % (type(values), values))
TypeError: Failed to convert object of type <class 'tensorflow.python.framework.dtypes.DType'> to Tensor. Contents: <dtype: 'float32'>. Consider casting elements to a supported type.
错误原因:
在喂数据的过程中,数据不匹配
解决方法:
修改定义的数据的shape大小。W_fc2 = weight_variable([1024, 10])
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 686, in _call_cpp_shape_fn_impl
input_tensors_as_shapes, status)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 473, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Invalid reduction dimension 1 for input with 0dimensions. for 'Mean' (op: 'Mean') with input shapes: [], [1] and with computed input tensors: input[1] = <1>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "cnn.py", line 64, in <module>
cross_entropy = tf.reduce_mean( - tf.reduce_sum(y_ * tf.log(y_conv)), reduction_indices=[1])
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 1411, in reduce_mean
name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 2568, in _mean
keep_dims=keep_dims, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2958, in create_op
set_shapes_for_outputs(ret)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2209, in set_shapes_for_outputs
shapes = shape_func(op)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2159, in call_with_requiring
return call_cpp_shape_fn(op, require_shape_fn=True)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 627, in call_cpp_shape_fn
require_shape_fn)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/common_shapes.py", line 691, in _call_cpp_shape_fn_impl
raise ValueError(err.message)
ValueError: Invalid reduction dimension 1 for input with 0 dimensions. for 'Mean' (op: 'Mean') with input shapes: [], [1] and with computed input tensors: input[1] = <1>.
错误原因:
tf.reduce_mean()函数的作用时用于求tensor的平均值,输入参数1为待求值的tensor,输入参数2为reduction_indices,指定在哪一维上求解。 如果不指定第二个参数,那么就在所有元素中取平均值。
tf.reduce_sum()类似
解决方法:
先对axis=1的tensor进行sum统计,然后求mean。
cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv), reduction_indices=[1]))
Addition:
在阅读tensorflow实战实现CNN网络时,遇到了以上错误,在此记录。