1

I am trying to use a Keras network (A) within another Keras network (B). I train network A first. Then I'm using it in network B to perform some regularization. Inside network B I would like to use evaluate or predict to get the output from network A. Unfortunately I haven't been able to get this to work since those functions expect a numpy array, instead it is receiving a Tensorflow variable as input.

Here is how I'm using network A inside a custom regularizer:

class CustomRegularizer(Regularizer):
    def __init__(self, model):
        """model is a keras network"""
        self.model = model

    def __call__(self, x):
        """Need to fix this part"""
        return self.model.evaluate(x, x)

How can I compute a forward pass with a Keras network with a Tensorflow variable as input?

As an example, here's what I get with numpy:

x = np.ones((1, 64), dtype=np.float32)
model.predict(x)[:, :10]

Output:

array([[-0.0244251 ,  3.31579041,  0.11801113,  0.02281714, -0.11048832,
         0.13053198,  0.14661783, -0.08456061, -0.0247585 ,  
0.02538805]], dtype=float32)

With Tensorflow

x = tf.Variable(np.ones((1, 64), dtype=np.float32))
model.predict_function([x])

Output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-92-4ed9d86cd79d> in <module>()
      1 x = tf.Variable(np.ones((1, 64), dtype=np.float32))
----> 2 model.predict_function([x])

~/miniconda/envs/bolt/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2266         updated = session.run(self.outputs + [self.updates_op],
   2267                               feed_dict=feed_dict,
-> 2268                               **self.session_kwargs)
   2269         return updated[:len(self.outputs)]
   2270 

~/miniconda/envs/bolt/lib/python3.6/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    776     try:
    777       result = self._run(None, fetches, feed_dict, options_ptr,
--> 778                          run_metadata_ptr)
    779       if run_metadata:
    780         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/miniconda/envs/bolt/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    952             np_val = subfeed_val.to_numpy_array()
    953           else:
--> 954             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
    955 
    956           if (not is_tensor_handle_feed and

~/miniconda/envs/bolt/lib/python3.6/site-packages/numpy/core/numeric.py in asarray(a, dtype, order)
    529 
    530     """
--> 531     return array(a, dtype, copy=False, order=order)
    532 
    533 

ValueError: setting an array element with a sequence.
Jacques Kvam
  • 2,856
  • 1
  • 26
  • 31

2 Answers2

3

I found my answer in a keras blog post. https://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html

from keras.models import Sequential

model = Sequential()
model.add(Dense(32, activation='relu', input_dim=784))
model.add(Dense(10, activation='softmax'))

# this works! 
x = tf.placeholder(tf.float32, shape=(None, 784))
y = model(x)
Jacques Kvam
  • 2,856
  • 1
  • 26
  • 31
  • 1
    I don't think y = model(x) is the answer. How is it same as your original intention to predict the output of x data ? model function is not even a complete code in keras. Please fix it. – exteral Jul 22 '18 at 00:57
  • I wanted to take a tensorflow variable and pass it through a network defined in keras. To that end this answers my question. `y` is the output of the keras network, I'm not sure what more you're looking for. – Jacques Kvam Jul 22 '18 at 02:26
  • My point is you did not explain why your original code would have `setting an array element with a sequence` error and why use keras model would fix it. – exteral Jul 22 '18 at 03:56
  • That error happened when I ran `x = tf.Variable(np.ones((1, 64), dtype=np.float32)); model.predict_function([x])` – Jacques Kvam Jul 22 '18 at 05:37
0

I am not sure where the tensorflow variable is coming in, but if it is there, you can do this:

model.predict([sess.run(x)])

where sess is the tensorflow session, i.e. sess = tf.Session().

Gerges
  • 6,269
  • 2
  • 22
  • 44
  • I added context to how the network is being used to my question. I haven't been able to adapt your answer to solve my problem yet. – Jacques Kvam Oct 11 '17 at 17:45
  • Sorry, but I think more details are still needed to help you debug. The only thing I can think of is you can try doing `cr([sess.run(x)])` and where `cr = CustomRegularizer(model)`. – Gerges Oct 11 '17 at 18:11