我建立應該一維輸入分類卷積神經網絡(與Tensorflow)。1D CNN分類
這是到目前爲止我的代碼:
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)
當我運行上面的代碼,我收到以下錯誤:
Traceback (most recent call last): File "minex.py", line 16, in correct = tf.nn.in_top_k(logits, y, 1) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1449, in in_top_k targets=targets, k=k, name=name) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op op_def=op_def) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2329, in create_op set_shapes_for_outputs(ret) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1717, in set_shapes_for_outputs shapes = shape_func(op) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1667, in call_with_requiring return call_cpp_shape_fn(op, require_shape_fn=True) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn debug_python_shape_fn, require_shape_fn) File "/home/jk/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 676, in _call_cpp_shape_fn_impl raise ValueError(err.message) ValueError: Shape must be rank 2 but is rank 3 for 'InTopK' (op: 'InTopK') with input shapes: [32,4,1], ?.
基於錯誤,看來我的問題是形狀,但我不知道它爲什麼會發生或如何糾正它。
其他一些資源可能會提到[tf.reshape(https://www.tensorflow.org/api_docs/python/tf/reshape),這是一個更通用的解決方案,但在這種情況下tf.squeeze更簡單易懂。在這個[tf.expand_dims](https://www.tensorflow.org/api_docs/python/tf/expand_dims)旁邊,它比tf.squeeze做了更多或更少的反作用。 –