Getting error even after enabling eager execution

Viewed 45

I am working on tensorflow federated. I have the following imports

!pip install --quiet tensorflow-federated==0.20.0 # The latest version of tensorflow-federated is not working with the colab python version
!pip install --quiet --upgrade nest-asyncio
import nest_asyncio
nest_asyncio.apply()
%load_ext tensorboard
tf.compat.v1.enable_eager_execution()
import tensorflow as tf
import tensorflow_federated as tff
import collections
import os
import random
import math
import time
import numpy as np
from numpy import sqrt
from numpy.fft import fft, ifft
from numpy.random import rand
import inspect
import tensorflow_probability as tfp
from matplotlib import pyplot as plt

from tensorflow.keras.models import Model
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import AveragePooling2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Dropout
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Input
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import concatenate
from tensorflow.keras import initializers
from keras import layers, initializers
from tensorflow.python.eager import backprop
from tensorflow.python.eager import context
from tensorflow.python.eager import function
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import indexed_slices
from tensorflow.python.framework import ops
from tensorflow.python.ops import embedding_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import resource_variable_ops
from tensorflow.python.ops import resources
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
from tensorflow.python.training import gradient_descent



Consider the following model

def create_model():
  x_1=tf.Variable(24)
  bias_initializer = tf.keras.initializers.HeNormal()
  model = Sequential()
  model.add(Conv2D(2, (5, 5),  input_shape=(28,28,1),activation="relu", name='conv2d_1', use_bias=True,bias_initializer=bias_initializer))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Conv2D(1, (5, 5), activation="relu",name='conv2d_2',  use_bias=True,bias_initializer=bias_initializer))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Flatten())
  model.add(Dense(2, name='dense_1',activation="relu", use_bias=True,bias_initializer=bias_initializer),)
  model.add(Dense(10, name='dense_2', activation="softmax", use_bias=True,bias_initializer=bias_initializer),)

  a=model.weights[0]
  b=model.weights[1]
  c=model.weights[2]
  d=model.weights[3]
  e=model.weights[4]
  f=model.weights[5]
  g=model.weights[6]
  h=model.weights[7]
  print(h)
  print(type(a))
  L1,B1,L2,B2,L3,B3,L4,B4=processing_work(a,b,c,d,e,f,g,h,x_1)
  print('L1 is',L1)
  print(type(L1))
  print(type(h))
  kk=resource_variable_ops.ResourceVariable(L1)
  print(type(kk))
  KB=resource_variable_ops.ResourceVariable(B1)
  print(type(KB))
  L1=tf.Variable(L1, dtype='float32')#, name='conv2d_1/kernel:0')
  B1=tf.Variable(B1, dtype='float32')#, name='conv2d_1/bias:0')
  L2=tf.Variable(L2, dtype='float32')#, name='conv2d_2/kernel:0')
  B2=tf.Variable(B2, dtype='float32')#, name='conv2d_2/bias:0')
  L3=tf.Variable(L3, dtype='float32')#, name='dense_1/kernel:0')
  B3=tf.Variable(B3, dtype='float32')#, name='dense_1/bias:0')
  L4=tf.Variable(L4, dtype='float32')#, name='dense_2/kernel:0')
  B4=tf.Variable(B4, dtype='float32')#, name='dense_2/bias:0')
  model.get_layer('conv2d_1').set_weights([L1,B1])
  model.get_layer('conv2d_2').set_weights([L2,B2])
  model.get_layer('dense_1').set_weights([L3,B3])
  model.get_layer('dense_2').set_weights([L4,B4])
  return model 

What I am doing in this model is; extracting the weights and biases of all the layers, performing various operations on them and re-assigning the processed/modified weights and biases to their respective layers. I make an instance of the model here:

def model_fn():
  # We _must_ create a new model here, and _not_ capture it from an external
  # scope. TFF will call this within different graph contexts.
  local_model = create_model()
  return tff.learning.from_keras_model(
      local_model,
      input_spec=preprocessed_example_dataset.element_spec,
      loss=tf.keras.losses.SparseCategoricalCrossentropy(),
      metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])

I then call the following algo:


iterative_process = tff.learning.algorithms.build_weighted_fed_avg(
    model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),
    server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.00))

However, I get the following error.

<tf.Variable 'dense_2/bias:0' shape=(10,) dtype=float32>
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
<function reshape at 0x7efde367c830>
L1 is Tensor("Reshape_41:0", shape=(5, 5, 1, 2), dtype=float32)
<class 'tensorflow.python.framework.ops.Tensor'>
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
<class 'tensorflow.python.ops.resource_variable_ops.ResourceVariable'>
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-33-e5ea47468ee2> in <module>
      1 iterative_process = tff.learning.algorithms.build_weighted_fed_avg(
      2     model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=0.02),
----> 3     server_optimizer_fn=lambda: tf.keras.optimizers.SGD(learning_rate=1.00))

8 frames
/usr/local/lib/python3.7/dist-packages/keras/backend.py in batch_set_value(tuples)
   4024         feed_dict = {}
   4025         for x, value in tuples:
-> 4026           value = np.asarray(value, dtype=dtype_numpy(x))
   4027           tf_dtype = tf.as_dtype(x.dtype.name.split('_')[0])
   4028           if hasattr(x, '_assign_placeholder'):

NotImplementedError: numpy() is only available when eager execution is enabled.

I have tried both types , i.e., L1 and B1 and kk and KB in

model.get_layer('conv2d_1').set_weights([L1,B1])

But I am getting the same error. Also at the start of the notebook, I added the following

tf.compat.v1.enable_eager_execution()

What might be causing this error?

0 Answers
Related