Reputation: 3046
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.
Upvotes: 1
Views: 8711
Reputation: 3046
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)
Upvotes: 3
Reputation: 6499
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()
.
Upvotes: 0