quantum icon indicating copy to clipboard operation
quantum copied to clipboard

Feed Keras model with output of tfq.layers.Sample()

Open frustea opened this issue 4 years ago • 4 comments

I wonder how can I feed the Keras model with output=tfq.layers.Sample(). I got the following error. I also tried to convert ragged tensor to normal tensor, but I didn't work

def _gen_single_bit_rotation_problem(bit, symbols):
    """Generate a toy problem on 1 qubit."""
    starting_state = [0.123, 0.456, 0.789]
    circuit = cirq.Circuit(
        cirq.rx(starting_state[0])(bit),
        cirq.ry(starting_state[1])(bit),
        cirq.rz(starting_state[2])(bit),
        cirq.rz(symbols[2])(bit),
        cirq.ry(symbols[1])(bit),
        cirq.rx(symbols[0])(bit)
    )
    return circuit

bit = cirq.GridQubit(0, 0)
symbols = sympy.symbols('x, y, z')
circuit = _gen_single_bit_rotation_problem(bit, symbols)

sample_layer = tfq.layers.Sample()
output = sample_layer(circuit,
    symbol_names=symbols, symbol_values=values, repetitions=4)

data_in = np.array([[1], [0]], dtype=np.float32)
data_out = np.array([[1], [-1]], dtype=np.float32)

model = tf.keras.Model(
    inputs=[circuit_inputs, control_input], outputs=output)


#model = tf.keras.Model(
#    inputs=[circuit_inputs, control_input], outputs=output.to_tesnor())

` AttributeError Traceback (most recent call last) in () 24 25 model = tf.keras.Model( ---> 26 inputs=[circuit_inputs, control_input], outputs=output) 27 28

5 frames /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in init(self, *args, **kwargs) 144 145 def init(self, *args, **kwargs): --> 146 super(Model, self).init(*args, **kwargs) 147 _keras_api_gauge.get_cell('model').set(True) 148 # initializing _distribution_strategy here since it is possible to call

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in init(self, *args, **kwargs) 167 'inputs' in kwargs and 'outputs' in kwargs): 168 # Graph network --> 169 self._init_graph_network(*args, **kwargs) 170 else: 171 # Subclassed network

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs) 270 271 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs): --> 272 base_layer_utils.create_keras_history(self._nested_outputs) 273 274 self._base_init(name=name, **kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors) 185 keras_tensors: The Tensors found that came from a Keras Layer. 186 """ --> 187 _, created_layers = _create_keras_history_helper(tensors, set(), []) 188 return created_layers 189

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers) 211 if getattr(tensor, '_keras_history', None) is not None: 212 continue --> 213 op = tensor.op # The Op that created this Tensor. 214 if op not in processed_ops: 215 if op.type.startswith('Sparse'):

AttributeError: 'RaggedTensor' object has no attribute 'op'

frustea avatar Aug 07 '20 12:08 frustea

I also tried to convert ragged tensor to normal tensor, but I didn't work

How did you go about doing this ? it looks like the error you posted still has to do with the fact that you are operating on ragged tensors in the model and not dense. Did something like:

output_dense = tf.keras.layers.Lambda(lambda x: x.to_tensor())(output)

Not work ?

MichaelBroughton avatar Aug 07 '20 16:08 MichaelBroughton

I tried to make the output as a dense tensor, but I get another error. for example, by replacing the output with your's output_dense I get( which is similar to what I get)

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () 1 two_axis_control_model = tf.keras.Model( 2 inputs=[circuits_input, commands_input], ----> 3 outputs=output_dense)

6 frames /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in init(self, *args, **kwargs) 144 145 def init(self, *args, **kwargs): --> 146 super(Model, self).init(*args, **kwargs) 147 _keras_api_gauge.get_cell('model').set(True) 148 # initializing _distribution_strategy here since it is possible to call

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in init(self, *args, **kwargs) 167 'inputs' in kwargs and 'outputs' in kwargs): 168 # Graph network --> 169 self._init_graph_network(*args, **kwargs) 170 else: 171 # Subclassed network

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs) 455 self._self_setattr_tracking = False # pylint: disable=protected-access 456 try: --> 457 result = method(self, *args, **kwargs) 458 finally: 459 self._self_setattr_tracking = previous_value # pylint: disable=protected-access

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name, **kwargs) 270 271 if any(not hasattr(tensor, '_keras_history') for tensor in self.outputs): --> 272 base_layer_utils.create_keras_history(self._nested_outputs) 273 274 self._base_init(name=name, **kwargs)

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in create_keras_history(tensors) 185 keras_tensors: The Tensors found that came from a Keras Layer. 186 """ --> 187 _, created_layers = _create_keras_history_helper(tensors, set(), []) 188 return created_layers 189

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/base_layer_utils.py in _create_keras_history_helper(tensors, processed_ops, created_layers) 211 if getattr(tensor, '_keras_history', None) is not None: 212 continue --> 213 op = tensor.op # The Op that created this Tensor. 214 if op not in processed_ops: 215 if op.type.startswith('Sparse'):

/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py in op(self) 1092 def op(self): 1093 raise AttributeError( -> 1094 "Tensor.op is meaningless when eager execution is enabled.") 1095 1096 @property

AttributeError: Tensor.op is meaningless when eager execution is enabled.`

frustea avatar Aug 07 '20 19:08 frustea

This could be a bug. Would you mind posting what version of TFQ you are using, your OS and a full snippet that produces this error (imports and everything) ?

MichaelBroughton avatar Aug 10 '20 18:08 MichaelBroughton

Any updates on this issue?

lockwo avatar Aug 24 '22 18:08 lockwo