Skip to content

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

@frustea

Description

@frustea

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'

Metadata

Metadata

Labels

kind/bug-reportSomething doesn't seem to work

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions