我意识到在我的代码中某处我的一个张量的某个维度是错误的主要错误消息
ValueError: Incompatible shapes between op input and calculated input gradient. Forward operation: SegmentMean. Input index: 0. Original input shape: (126, 80). Calculated input gradient shape: (126, 80)
我还想知道如何解释整个错误消息,因为我觉得它可以让我更清楚地知道到底出了什么问题。
以下是完整的错误消息。
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in set_shape(self, shape)
534 dim_list,
--> 535 unknown_shape)
536 except errors.InvalidArgumentError as e:
InvalidArgumentError: Dimension 0 in both shapes must be equal, but are 504 and 126. Shapes are [504,80] and [126,80].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_impl.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
661 try:
--> 662 in_grad.set_shape(t_in.get_shape())
663 except ValueError:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in set_shape(self, shape)
537 # Convert to ValueError for backwards compatibility.
--> 538 raise ValueError(str(e))
539
ValueError: Dimension 0 in both shapes must be equal, but are 504 and 126. Shapes are [504,80] and [126,80].
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-11-e8feccf3b5a2> in <module>()
57 # that contribute to the tensor it is passed.
58 # See docs on `tf.train.Optimizer.minimize()` for more details.
---> 59 optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss)
60
61 # Compute the similarity between minibatch examples and all embeddings.
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/optimizer.py in minimize(self, loss, global_step, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, name, grad_loss)
412 aggregation_method=aggregation_method,
413 colocate_gradients_with_ops=colocate_gradients_with_ops,
--> 414 grad_loss=grad_loss)
415
416 vars_with_grad = [v for g, v in grads_and_vars if g is not None]
/usr/local/lib/python3.6/dist-packages/tensorflow/python/training/optimizer.py in compute_gradients(self, loss, var_list, gate_gradients, aggregation_method, colocate_gradients_with_ops, grad_loss)
524 gate_gradients=(gate_gradients == Optimizer.GATE_OP),
525 aggregation_method=aggregation_method,
--> 526 colocate_gradients_with_ops=colocate_gradients_with_ops)
527 if gate_gradients == Optimizer.GATE_GRAPH:
528 grads = control_flow_ops.tuple(grads)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
492 with ops.get_default_graph()._lock: # pylint: disable=protected-access
493 return _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops,
--> 494 gate_gradients, aggregation_method, stop_gradients)
495
496
/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gradients_impl.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
667 "Original input shape: %s. "
668 "Calculated input gradient shape: %s" %
--> 669 (op.name, i, t_in.shape, in_grad.shape))
670 _SetGrad(grads, t_in, in_grad)
671 if loop_state:
ValueError: Incompatible shapes between op input and calculated input gradient. Forward operation: SegmentMean. Input index: 0. Original input shape: (126, 80). Calculated input gradient shape: (126, 80)
以下是产生错误的代码部分
batch_size = 126
embedding_size = 80 # Dimension of the embedding vector.
cbow_window =2
valid_size = 16 # Random set of words to evaluate similarity on.
valid_window = 100 # Only pick dev samples in the head of the distribution.
valid_examples = np.array(random.sample(range(valid_window), valid_size))
# Random set of words to evaluate similarity on.
num_sampled = 64 # Number of negative examples to sample.
graph = tf.Graph()
with graph.as_default(): #took out " , tf.device('/cpu:0')"
# Input data.
train_dataset = tf.placeholder(tf.int32, shape=[batch_size])
train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
valid_dataset = tf.constant(valid_examples, dtype=tf.int32)
# Variables.
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
softmax_weights = tf.Variable(
tf.truncated_normal([vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)))
softmax_biases = tf.Variable(tf.zeros([vocabulary_size]))
embed = tf.nn.embedding_lookup(embeddings, train_dataset) #train data set is
segments= np.arange(batch_size).repeat(cbow_window*2)
averaged_embeds = tf.segment_mean( embed, segments, name=None)
loss = tf.reduce_mean(
tf.nn.sampled_softmax_loss(weights=softmax_weights, biases=softmax_biases, inputs=averaged_embeds,
labels=train_labels, num_sampled=num_sampled, num_classes=vocabulary_size))
# Optimizer.
# Note: The optimizer will optimize the softmax_weights AND the embeddings.
# This is because the embeddings are defined as a variable quantity and the
# optimizer's `minimize` method will by default modify all variable quantities
# that contribute to the tensor it is passed.
# See docs on `tf.train.Optimizer.minimize()` for more details.
optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss)
# Compute the similarity between minibatch examples and all embeddings.
# We use the cosine distance:
norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keepdims=True))
normalized_embeddings = embeddings / norm
valid_embeddings = tf.nn.embedding_lookup(
normalized_embeddings, valid_dataset)
similarity = tf.matmul(valid_embeddings, tf.transpose(normalized_embeddings))
链接到整个代码
https://drive.google.com/open?id=1x87Mj35J47vPFclby4qLlBHc77d2XtKc