在Python 3.5.2中使用TensorFlow 1.3.0版。我正在尝试建立一个卷积神经网络,它接收69行和44个独立值的数据来执行二进制分类(即跳转或跌落),但不断收到错误:
ValueError: Shapes (69, 44, 2) and (69, 2, 2) are incompatible
我不确定代码在何处将大小从44列更改为2.我构建的网络代码主要来自MNIST数据库的tensorflow教程,该教程已经过修改以解决我的数据/问题。主要区别在于,我使用的是conv1d()而不是conv2d(),而且我没有使用任何池化层。代码如下:
def cnn_model_fn(features, labels, mode):
"""Model function for CNN."""
# Input Layer
#-1 will be filled by a 69 later on
#44 used for independent vars, 1 must be used for input in tf.layers.conv1d()
input_layer = tf.reshape(features["x"], [-1, 44, 1])
# Convolutional Layer #1
conv1 = tf.layers.conv1d(
inputs=input_layer,
filters=32,
kernel_size=1,
padding="same",
activation=tf.nn.relu,
name = "Alan")
# Convolutional Layer #2
conv2 = tf.layers.conv1d(
inputs=conv1,
filters=64,
kernel_size=1,
padding="same",
activation=tf.nn.relu,
name = "Borris")
# Dense Layer
dense = tf.layers.dense(inputs=conv2, units=1024, activation=tf.nn.relu)
dropout = tf.layers.dropout(
inputs=dense, rate=0.4, training=mode == tf.estimator.ModeKeys.TRAIN)
# Logits Layer
logits = tf.layers.dense(inputs=dropout, units=2)
print(logits)
predictions = {
# Generate predictions (for PREDICT and EVAL mode)
"classes": tf.argmax(input=logits, axis=1),
# Add `softmax_tensor` to the graph. It is used for PREDICT and by the
# `logging_hook`.
"probabilities": tf.nn.softmax(logits, name="softmax_tensor")
}
if mode == tf.estimator.ModeKeys.PREDICT:
return tf.estimator.EstimatorSpec(mode=mode, predictions=predictions)
# Calculate Loss (for both TRAIN and EVAL modes)
onehot_labels = tf.one_hot(indices=tf.cast(labels, tf.int32), depth=2)
loss = tf.losses.softmax_cross_entropy(
onehot_labels=onehot_labels, logits=logits)
# Configure the Training Op (for TRAIN mode)
if mode == tf.estimator.ModeKeys.TRAIN:
optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
train_op = optimizer.minimize(
loss=loss,
global_step=tf.train.get_global_step())
return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
# Add evaluation metrics (for EVAL mode)
eval_metric_ops = {
"accuracy": tf.metrics.accuracy(
labels=labels, predictions=predictions["classes"])}
return tf.estimator.EstimatorSpec(
mode=mode, loss=loss, eval_metric_ops=eval_metric_ops)
只要我运行eval_input_fn:
,就会出现问题jumpupdown_classifier = tf.estimator.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/jumpupdown_convnet_model")
tensors_to_log = {"probabilities": "softmax_tensor"}
logging_hook = tf.train.LoggingTensorHook(tensors=tensors_to_log, every_n_iter=50)
# Train the model
train_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": mTrainDat},
y=mTrainLab,
batch_size=69,
num_epochs=None,
shuffle=True)
jumpupdown_classifier.train(
input_fn=train_input_fn,
steps=20000,
hooks=[logging_hook])
# Evaluate the model and print results
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": mTestDat},
y=mTestLab,
num_epochs=1,
shuffle=False)
eval_results = jumpupdown_classifier.evaluate(input_fn=eval_input_fn)
print(eval_results)
为了澄清一点,mTrainDat如下所示:
array([[ 0.23971076, 0.24687247, -0.22665831, ..., 0.73542702,
-0.57439029, -0.22714323],
[ 0.32290912, 0.61977148, -0.4258523 , ..., 0.73542702,
0.74438596, -0.57915074],
[ 0.41255337, 0.2795991 , -0.56257713, ..., -0.30046052,
0.74438596, 0.74211949],
...,
[ 0.56027043, 1.63016009, 1.36453617, ..., 0.57338732,
0.74438596, 0.56973094],
[-0.1470048 , 1.23301661, 0.63612264, ..., 0.73542702,
0.58223492, 0.74211949],
[-0.46544313, 1.02278185, 0.30256116, ..., -1.38631892,
0.74438596, 0.57966185]], dtype=float32)
mTrainLab看起来像这样:
array([[ 1., 0.],
[ 1., 0.],
[ 0., 1.],
[ 1., 0.],
[ 1., 0.],
........
[ 0., 1.],
[ 0., 1.],
[ 1., 0.]], dtype=float32)
mTestDat和mTestLab看起来相似,但只包含6行。
我试图改变价值观以试图诊断问题,但一直没有成功。任何帮助表示赞赏。