Incompatible shapes using `sample_weight` in Graph execution

Viewed 185

Works fine in Eager. Code + error below; pasted outputs are from a Colab instance with tf.__version__ == 2.3.0-dev20200526 (tf-nightly), also reproduced in 2.2.0 and on Windows 10. No error in TF 1.14.0 Graph.

Any clues?


Attempted debug: Placing below here (in local install)

print(sample_weight)
try: print("sample_weight =", K.eval(sample_weight))
except: pass
print(loss, '\n')

yields:

# EAGER
Tensor("ExpandDims:0", shape=(32, 1), dtype=float32)
Tensor("mean_squared_error/weighted_loss/value:0", shape=(), dtype=float32)

Tensor("ExpandDims:0", shape=(32, 1), dtype=float32)
Tensor("mean_squared_error/weighted_loss/value:0", shape=(), dtype=float32)

# GRAPH
1.0
sample_weight = 1.0
Tensor("loss/conv2d_loss/weighted_loss/Mul:0", shape=(32, 28, 28), dtype=float32)

Tensor("conv2d_sample_weights:0", shape=(None,), dtype=float32)
sample_weight = [1.]
Tensor("loss_1/conv2d_loss/weighted_loss/Mul:0", shape=(32, 28, 28), dtype=float32)

Graph handles sample_weight tensor differently. Both Eager and Graph work fine with 2D output shape and loss='categorical_crossentropy', though possibly incorrectly; code at bottom here.


Reproducible code + Error:

import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D
from tensorflow.keras.models import Model
tf.compat.v1.disable_eager_execution()

batch_shape = (32, 28, 28, 1)

ipt = Input(batch_shape=batch_shape)
out = Conv2D(filters=1, kernel_size=(1, 1))(ipt)
model = Model(ipt, out)
model.compile('adam', 'mse')

x = y = np.random.randn(*batch_shape)
sw = np.ones(len(x))

model.train_on_batch(x, y, sw)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1470         ret = tf_session.TF_SessionRunCallable(self._session._session,
   1471                                                self._handle, args,
-> 1472                                                run_metadata_ptr)
   1473         if run_metadata:
   1474           proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Incompatible shapes: [32] vs. [32,28,28]
     [[{{node training/Adam/gradients/gradients/loss_1/conv2d_loss/weighted_loss/Mul_grad/Mul}}]]
0 Answers
Related