繁体   English   中英

TensorFlow:试图将“输入”转换为张量,但失败。 错误:不支持任何值

[英]TensorFlow : Tried to convert 'input' to a tensor and failed. Error: None values not supported

我正在尝试使用Tensorflow和python(3.6)制作我的第一个卷积神经网络。 这是我的代码的相关部分:

def init_weights(shape):
  init_random_dist = tf.truncated_normal(shape , stddev = 0.1)
  return tf.Variable(init_random_dist)
def init_bias(shape):
  init_bias_vals = tf.constant(0.1 , shape = shape)
  return tf.Variable(init_bias_vals)
def conv2d(x,W):
  return tf.nn.conv2d(x , W, strides = [1,1,1,1] , padding = 'SAME')
def max_pool_2by2(x):
  tf.nn.max_pool(x , ksize = [1,2,2,1] , strides = [ 1,2,2,1 ]  , padding ='SAME')
def convolutional_layer(input_x,shape):
  W = init_weights(shape)
  b = init_bias([shape[3]])
  return tf.nn.relu(conv2d(input_x,W)+b)
x = tf.placeholder(tf.float32 , shape = [ None , 784 ])
y_true = tf.placeholder(tf.float32 , shape = [ None , 10 ])
x_image = tf.reshape(x , [-1 , 28 , 28 , 1])
convo_1 = convolutional_layer(x_image , shape = [5,5,1,32])#height-width-inputchannels-no.ofkernels
convo_1_pooling = max_pool_2by2(convo_1)

convo_2 = convolutional_layer(convo_1_pooling,shape = [5,5,32,64])
convo_2_pooling = max_pool_2by2(convo_2)

我正在为此网络使用MNIST数据集,因此使用784和28x28。 这是我得到的错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    509                 as_ref=input_arg.is_ref,
--> 510                 preferred_dtype=default_dtype)
    511           except TypeError as err:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
   1035     if ret is None:
-> 1036       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1037 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    234   _ = as_ref
--> 235   return constant(v, dtype=dtype, name=name)
    236 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
    213       tensor_util.make_tensor_proto(
--> 214           value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    215   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
    420     if values is None:
--> 421       raise ValueError("None values not supported.")
    422     # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    523               observed = ops.internal_convert_to_tensor(
--> 524                   values, as_ref=input_arg.is_ref).dtype.name
    525             except ValueError as err:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
   1035     if ret is None:
-> 1036       ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
   1037 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in _constant_tensor_conversion_function(v, dtype, name, as_ref)
    234   _ = as_ref
--> 235   return constant(v, dtype=dtype, name=name)
    236 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
    213       tensor_util.make_tensor_proto(
--> 214           value, dtype=dtype, shape=shape, verify_shape=verify_shape))
    215   dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
    420     if values is None:
--> 421       raise ValueError("None values not supported.")
    422     # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-17-d2abcb4a99e1> in <module>()
      3 convo_1_pooling = max_pool_2by2(convo_1)
      4 
----> 5 convo_2 = convolutional_layer(convo_1_pooling,shape = [5,5,32,64])
      6 convo_2_pooling = max_pool_2by2(convo_2)

<ipython-input-14-aed4eef361c3> in convolutional_layer(input_x, shape)
      2   W = init_weights(shape)
      3   b = init_bias([shape[3]])
----> 4   return tf.nn.relu(conv2d(input_x,W)+b)

<ipython-input-6-52ed03971cc3> in conv2d(x, W)
      1 def conv2d(x,W):
----> 2   return tf.nn.conv2d(x , W, strides = [1,1,1,1] , padding = 'SAME')

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_nn_ops.py in conv2d(input, filter, strides, padding, use_cudnn_on_gpu, data_format, dilations, name)
    629         "Conv2D", input=input, filter=filter, strides=strides,
    630         padding=padding, use_cudnn_on_gpu=use_cudnn_on_gpu,
--> 631         data_format=data_format, dilations=dilations, name=name)
    632     _result = _op.outputs[:]
    633     _inputs_flat = _op.inputs

/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
    526               raise ValueError(
    527                   "Tried to convert '%s' to a tensor and failed. Error: %s" %
--> 528                   (input_name, err))
    529             prefix = ("Input '%s' of '%s' Op has type %s that does not match" %
    530                       (input_name, op_type_name, observed))

ValueError: Tried to convert 'input' to a tensor and failed. Error: None values not supported.

请询问可能需要的更多详细信息。 我是TensorFlow的新手。

函数max_pool_2by2不返回任何值,因此convo_1_poolingNone

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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