from tensorflow.python.framework import tensor_shape
from tensorflow.python.framework import tensor_util
from tensorflow.python.ops import array_ops
+from tensorflow.python.ops import check_ops
from tensorflow.python.ops import gen_nn_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import random_ops
# Store label shape for result later.
labels_static_shape = labels.get_shape()
labels_shape = array_ops.shape(labels)
+ static_shapes_fully_defined = (
+ labels_static_shape.is_fully_defined() and
+ logits.get_shape()[:-1].is_fully_defined())
if logits.get_shape().ndims is not None and logits.get_shape().ndims == 0:
raise ValueError(
"Logits cannot be scalars - received shape %s." % logits.get_shape())
raise ValueError("Rank mismatch: Rank of labels (received %s) should "
"equal rank of logits minus 1 (received %s)." %
(labels_static_shape.ndims, logits.get_shape().ndims))
+ if (static_shapes_fully_defined and
+ labels_static_shape != logits.get_shape()[:-1]):
+ raise ValueError("Shape mismatch: The shape of labels (received %s) "
+ "should equal the shape of logits except for the last "
+ "dimension (received %s)." % (labels_static_shape,
+ logits.get_shape()))
# Check if no reshapes are required.
if logits.get_shape().ndims == 2:
cost, _ = gen_nn_ops.sparse_softmax_cross_entropy_with_logits(
else:
return cost
- # Reshape logits to 2 dim, labels to 1 dim.
- num_classes = array_ops.shape(logits)[array_ops.rank(logits) - 1]
- precise_logits = array_ops.reshape(precise_logits, [-1, num_classes])
- labels = array_ops.reshape(labels, [-1])
- # The second output tensor contains the gradients. We use it in
- # _CrossEntropyGrad() in nn_grad but not here.
- cost, _ = gen_nn_ops.sparse_softmax_cross_entropy_with_logits(
- precise_logits, labels, name=name)
- cost = array_ops.reshape(cost, labels_shape)
- cost.set_shape(labels_static_shape)
- if logits.dtype == dtypes.float16:
- return math_ops.cast(cost, dtypes.float16)
- else:
- return cost
+ # Perform a check of the dynamic shapes if the static shapes are not fully
+ # defined.
+ shape_checks = []
+ if not static_shapes_fully_defined:
+ shape_checks.append(
+ check_ops.assert_equal(
+ array_ops.shape(labels),
+ array_ops.shape(logits)[:-1]))
+ with ops.control_dependencies(shape_checks):
+ # Reshape logits to 2 dim, labels to 1 dim.
+ num_classes = array_ops.shape(logits)[array_ops.rank(logits) - 1]
+ precise_logits = array_ops.reshape(precise_logits, [-1, num_classes])
+ labels = array_ops.reshape(labels, [-1])
+ # The second output tensor contains the gradients. We use it in
+ # _CrossEntropyGrad() in nn_grad but not here.
+ cost, _ = gen_nn_ops.sparse_softmax_cross_entropy_with_logits(
+ precise_logits, labels, name=name)
+ cost = array_ops.reshape(cost, labels_shape)
+ cost.set_shape(labels_static_shape)
+ if logits.dtype == dtypes.float16:
+ return math_ops.cast(cost, dtypes.float16)
+ else:
+ return cost
@tf_export("nn.avg_pool")