我正在构建一个卷积神经网络(使用Tensorflow),该网络应该对一维输入进行分类。
到目前为止,这是我的代码:
import tensorflow as tf
n_outputs = 1
batch_size = 32
x = tf.placeholder(tf.float32, [batch_size, 10, 1])
filt = tf.zeros([3, 1, 1])
output = tf.nn.conv1d(x, filt, stride=2, padding="VALID")
y = tf.placeholder(tf.int32)
logits = tf.layers.dense(output, n_outputs)
xentropy = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y, logits=logits)
correct = tf.nn.in_top_k(logits, y, 1)
当我运行上面的代码时,出现以下错误:
追溯(最近一次通话):文件“ minex.py”,第16行,正确= tf.nn.in_top_k(logits,y,1)文件“ /home/jk/tensorflow/local/lib/python2.7/ in-top_k中的site-packages / tensorflow / python / ops / gen_nn_ops.py“行1449中target = targets,k = k,name = name)文件” /home/jk/tensorflow/local/lib/python2.7/site -packages / tensorflow / python / framework / op_def_library.py“,第763行,位于apply_op op_def = op_def)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/在create_op set_shapes_for_outputs(ret)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”的行1717中,ops.py”,第2329行,在set_shapes_for_outputs中,shapes = shape_func(op)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py”,行1667,在call_with_requireing return call_cpp_shape_fn(op, require_shape_fn = True)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行610,在call_cpp_shape_fn中debug_python_shape_fn,require_shape_fn)文件“ / home / jk / tensorflow / local / lib / python2.7 / site-packages / tensorflow / python / framework / common_shapes.py“,行676,在_call_cpp_shape_fn_impl中提高ValueError(err.message)ValueError:形状必须为2级,但为3级输入形状为[32,4,1] 、?的'InTopK'(op:'InTopK')。/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py“,行1667,在call_with_requiring返回中call_cpp_shape_fn(op,require_shape_fn = True)文件” / home / jk /tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py“,行610,在call_cpp_shape_fn中debug_python_shape_fn,require_shape_fn)文件“ / home / jk / tensorflow / local / lib / python2。 7 / site-packages / tensorflow / python / framework / common_shapes.py“,行676,在_call_cpp_shape_fn_impl中,引发ValueError(err.message)ValueError:形状必须为2,但对于'InTopK'为3级(op:'InTopK' ),输入形状为[32,4,1],?。/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py“,行1667,在call_with_requiring返回中call_cpp_shape_fn(op,require_shape_fn = True)文件” / home / jk /tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py“,行610,在call_cpp_shape_fn中debug_python_shape_fn,require_shape_fn)文件“ / home / jk / tensorflow / local / lib / python2。 7 / site-packages / tensorflow / python / framework / common_shapes.py“,行676,在_call_cpp_shape_fn_impl中,引发ValueError(err.message)ValueError:形状必须为2,但对于'InTopK'为3级(op:'InTopK' ),输入形状为[32,4,1],?。在call_with_requiring中返回call_cpp_shape_fn(op,require_shape_fn = True)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行610,在call_cpp_shape_fn中debug_python_shape_fn, require_shape_fn)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行676,在_call_cpp_shape_fn_impl中引发ValueError(err.message)ValueError:形状必须为是输入形状为[32,4,1] 、?的'InTopK'(op:'InTopK')的等级2,但排名3。在call_with_requiring中返回call_cpp_shape_fn(op,require_shape_fn = True)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行610,在call_cpp_shape_fn中debug_python_shape_fn, require_shape_fn)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行676,在_call_cpp_shape_fn_impl中引发ValueError(err.message)ValueError:形状必须为是输入形状为[32,4,1] 、?的'InTopK'(op:'InTopK')的等级2,但排名3。require_shape_fn)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行676,在_call_cpp_shape_fn_impl中引发ValueError(err.message)ValueError:形状必须为是输入形状为[32,4,1] 、?的'InTopK'(op:'InTopK')的等级2,但排名3。require_shape_fn)文件“ /home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py”,行676,在_call_cpp_shape_fn_impl中引发ValueError(err.message)ValueError:形状必须为是输入形状为[32,4,1] 、?的'InTopK'(op:'InTopK')的等级2,但排名3。
基于该错误,似乎我的问题出在形状上,但是我不确定为什么会发生或如何纠正它。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。