我对Tensorflow完全不熟悉。我一直试图重新设计Deep MNIST教程,以预测MovieLens数据集上的电影评级。我稍微简化了模型,因此它不是使用5分制,而是简单的二进制Y / N评级(类似于Netflix上最新的评级系统)。我试图仅使用零件评级来预测新项目的偏好。在训练模型时,我在堆栈跟踪中得到以下错误:
Traceback (most recent call last):
File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 223, in <module>
train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1550, in run
_run_using_default_session(self, feed_dict, self.graph, session)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 3764, in >_run_using_default_session
session.run(operation, feed_dict)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 767, in run
run_metadata_ptr)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 965, in _run
feed_dict_string, options, run_metadata)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 1015, in _do_run
target_list, options, run_metadata)
File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 1035, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and >labels must be same size: logits_size=[1,2] labels_size=[50,2]
[[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]
Caused by op u'SoftmaxCrossEntropyWithLogits', defined at:
File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 209, in <module>
cross_entropy = >tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, >logits=y_conv))
File "/Library/Python/2.7/site->packages/tensorflow/python/ops/nn_ops.py", line 1617, in >softmax_cross_entropy_with_logits
precise_logits, labels, name=name)
File "/Library/Python/2.7/site->packages/tensorflow/python/ops/gen_nn_ops.py", line 2265, in >_softmax_cross_entropy_with_logits
features=features, labels=labels, name=name)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/op_def_library.py", line 763, in >apply_op
op_def=op_def)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 2327, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1226, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): logits and labels must >be same size: logits_size=[1,2] labels_size=[50,2]
[[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]
可以查看导致错误的代码here
模型中使用的变量大小:
x(?,1682)
y_(?,2)
答案 0 :(得分:2)
问题在于您将输入批次(形状50个训练实例x 1682特征)重塑为此行中的[-1,290,290,1]:
x_history = tf.reshape(x, [-1, 290, 290, 1])
您可以通过运行以下命令查看x_history的形状:
x_history.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0}).shape
=> (1, 290, 290, 1)
这有效地为您的50个实例批处理所有功能并将其放在单个实例中(第一个维度为1,需要为50),然后通过网络的其余部分运行。 因此,您的cross_entropy评估失败,因为它无法将50个目标标签的批次与网络的单个输出对齐。
您需要选择图层形状,以便通过网络保留批量尺寸(形状打印输出中的?)。一种方法是将你的特征填充到1764并重塑为[-1,42,42,1],因为42 * 42 = 1764。
值得注意的是,2d卷积最常用于图像数据,其自然是二维的。鉴于您的功能不是二维的,您可能最好从一个更简单的完全连接的层网络开始?
答案 1 :(得分:0)
为了解决这个问题,我最终将批量大小从50减少到了
batch = create_batches(train_data_pairs, 1)
并大大增加了训练迭代次数。另外,为了准确性,我根据大量小片测试数据测试模型,然后采用这种评估的平均值。
这不仅仅是解决问题的方法,而是让我继续探索TensorFlow的其他方面,并以不同的方式使用数据。