开发者社区> 问答> 正文

一维CNN用于分类

我正在构建一个卷积神经网络(使用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。

基于该错误,似乎我的问题出在形状上,但是我不确定为什么会发生或如何纠正它。

展开
收起
祖安文状元 2020-02-23 15:34:39 1581 0
2 条回答
写回答
取消 提交回答
  • 外部尺寸不对。

    2020-03-14 14:22:54
    赞同 展开评论 打赏
  • 您可以使用tf.squeeze从logit中删除外部尺寸。

    您的最后一行可能变为:

    correct = tf.nn.in_top_k(tf.squeeze(logits), y, 1)
    
    

    这将使对数张量的形状从[32,4,1]变为[32,4]。

    2020-02-23 15:34:51
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
利⽤CNN实现⽆需联⽹的图像识别 立即下载
神经网络结构设计 立即下载
机器能理解上下文吗 RNN和LSTM神经网络的原理及应用 立即下载