Biu
Biu

Reputation: 39

ValueError: Tensor("cnn/conv2d/kernel:0", shape=(), dtype=resource) must be from the same graph as Tensor("Placeholder:0", shape=(), dtype=variant)

I am a newer in Deep Learning and TFF. I need to use a CNN to classify images from EMNIST. And I see the tutorials on GitHub named Federated Learning for Image Classification. I create a Network named CNN, and then I use forward_pass function to instance a cnn model to calculate the predictions. But TFF need to pass the model variables as trainable variables to the tff.learning.Model. I print the CNN model.variables. I don't know how to named them so I use cnn_conv2d_kernel to represents cnn/conv2d/kernel. Here is my code:

the model.variables printed:

variables: [<tf.Variable 'cnn/conv2d/kernel:0' shape=(5, 5, 1, 32) dtype=float32>, <tf.Variable 'cnn/conv2d/bias:0' shape=(32,) dtype=float32>, <tf.Variable 'cnn/conv2d_1/kernel:0' shape=(5, 5, 32, 64) dtype=float32>, <tf.Variable 'cnn/conv2d_1/bias:0' shape=(64,) dtype=float32>, <tf.Variable 'cnn/dense/kernel:0' shape=(3136, 1024) dtype=float32>, <tf.Variable 'cnn/dense/bias:0' shape=(1024,) dtype=float32>, <tf.Variable 'cnn/dense_1/kernel:0' shape=(1024, 10) dtype=float32>, <tf.Variable 'cnn/dense_1/bias:0' shape=(10,) dtype=float32>]

My variables created to pass trainable and non_trainable variables to tff.learning.Model:

MnistVariables = collections.namedtuple(
'MnistVariables','cnn_conv2d_kernel cnn_conv2d_bias cnn_conv2d_1_kernel cnn_conv2d_1_bias cnn_dense_kernel cnn_dense_bias cnn_dense_1_kernel cnn_dense_1_bias num_examples loss_sum accuracy_sum'

)

def create_mnist_variables():
  return MnistVariables(
      # weights=tf.Variable(
      #     # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
      #     lambda: tf.zeros(dtype=tf.float32, shape=(28,28,10)),
      #     name='weights',
      #     trainable=True),
      # bias=tf.Variable(
      #     lambda: tf.zeros(dtype=tf.float32, shape=(10)),
      #     name='bias',
      #     trainable=True),

      cnn_conv2d_kernel=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(5,5,1,32)),
          name='cnn_conv2d_kernel',
          trainable=True),
      cnn_conv2d_bias=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(32,)),
          name='cnn_conv2d_bias',
          trainable=True),
      cnn_conv2d_1_kernel=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(5,5,32,64)),
          name='cnn_conv2d_1_kernel',
          trainable=True),
      cnn_conv2d_1_bias=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(64,)),
          name='cnn_conv2d_1_bias',
          trainable=True),
      cnn_dense_kernel=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(3136,1024)),
          name='cnn_dense_kernel',
          trainable=True),
      cnn_dense_bias=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(1024,)),
          name='cnn_dense_bias',
          trainable=True),
      cnn_dense_1_kernel=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(1024,10)),
          name='cnn_dense_1_kernel',
          trainable=True),
      cnn_dense_1_bias=tf.Variable(
          # lambda: tf.zeros(dtype=tf.float32, shape=(784,10)),
          lambda: tf.zeros(dtype=tf.float32, shape=(10,)),
          name='cnn_dense_1_bias',
          trainable=True),
      num_examples=tf.Variable(0.0, name='num_examples', trainable=False),
      loss_sum=tf.Variable(0.0, name='loss_sum', trainable=False),
      accuracy_sum=tf.Variable(0.0, name='accuracy_sum', trainable=False)
  )

my partial tff.learning.Model code:

class MnistModel(tff.learning.Model):

  def __init__(self):
    self._variables = create_mnist_variables()

  #所有的“tf.Variables”都应该在“__init__”中引入
  @property
  def trainable_variables(self):
    #return [self._variables.weights, self._variables.bias]
    return [self._variables.cnn_conv2d_kernel,
        self._variables.cnn_conv2d_bias,
        self._variables.cnn_conv2d_1_kernel,
        self._variables.cnn_conv2d_1_bias,
        self._variables.cnn_dense_kernel,
        self._variables.cnn_dense_bias,
        self._variables.cnn_dense_1_kernel,
        self._variables.cnn_dense_1_bias
        ]

please forgive my poor English and help me please.(Please)

Now ,I have a new problem:

ValueError: No gradients provided for any variable: ['cnn_conv2d_kernel:0', 'cnn_conv2d_bias:0', 'cnn_conv2d_1_kernel:0', 'cnn_conv2d_1_bias:0', 'cnn_dense_kernel:0', 'cnn_dense_bias:0', 'cnn_dense_1_kernel:0', 'cnn_dense_1_bias:0'].

Upvotes: 1

Views: 1035

Answers (1)

Keith Rush
Keith Rush

Reputation: 1405

For this use case it might be easier, rather than subclassing a tff.learning.Model directly, to write a tf.keras.Model and use TFF's utilities to convert this to a tff.learning.Model.

There are some examples of doing exactly this in research code hosted in TFF; one such pointer is here. This pointer is to a pure Keras model; to use this in TFF, we must use the tff.learning.from_keras_model function linked above. If you have a tf.data.Dataset ds which contains your images and labels, and a loss function loss_fn, you can get your hands on a tff.learning.model by calling:

keras_model = create_keras_model()
tff_model = tff.learning.from_keras_model(
    keras_model=keras_model, loss=loss_fn, input_spec=ds.element_spec)

Directly subclassing a tff.learning.Model is a bit of a power-user feature; you will want to write native TensorFlow to define the forward pass, for example. For getting started with deep learning in general and TFF in particular, I would recommend using a higher-level API like tf.keras and TFF's keras utilities in the manner outlined above.

Upvotes: 1

Related Questions