简体   繁体   English

使用 TensorFlow Quantum 进行多类分类

[英]Multiclass classification using TensorFlow Quantum

I am running some examples and tests on TensorFlow Quantum (TFQ) and I am struggling to perform a multi-class classification.我正在 TensorFlow Quantum (TFQ) 上运行一些示例和测试,并且正在努力执行多类分类。 I will used the MNIST classification example as base ( https://www.tensorflow.org/quantum/tutorials/mnist ), since this is where I am starting from too.我将使用 MNIST 分类示例作为基础( https://www.tensorflow.org/quantum/tutorials/mnist ),因为这也是我的起点。

For binary classification I played with the different examples of classes and different gates combination, and the classification result is obtained by measuring a single readout qubit (qR)result, thus if qR=0 we classify with class 0 and if qR=1 then we have class 1.对于二元分类,我玩了不同类别的例子和不同的门组合,分类结果是通过测量单个读出量子位(qR)结果获得的,因此如果 qR=0 我们分类为 0 类,如果 qR=1 那么我们有1级。

I extended it to a multi-class problems, so we have a 4 classes (0,1,2,3).我将其扩展为多类问题,因此我们有 4 个类(0、1、2、3)。 To do this I change the labels of the classes with tf.keras.utils.to_categorical(y_train) , such that the labels get converted from single values to vectors (0 -> (1,0,0,0); 1-> (0,1,0,0); etc..), use tf.keras.losses.CategoricalHinge() as loss of the model and create 4 readouts qubits, one for each class (M(qR0, qR1, qR2, qR3) = (0,0,1,0) -> class 2), and this works.为此,我使用tf.keras.utils.to_categorical(y_train)更改类的标签,以便将标签从单个值转换为向量 (0 -> (1,0,0,0); 1-> (0,1,0,0); 等..),使用tf.keras.losses.CategoricalHinge()作为模型的损失并创建 4 个读出量子位,每个类一个 (M(qR0, qR1, qR2, qR3 ) = (0,0,1,0) -> class 2),这是有效的。

However, this method increases massively the size of the circuit.然而,这种方法极大地增加了电路的尺寸。 So what I want to do is to pass to TFQ only 2 readout qubits and use the combined measurement for the 4 classes classification (|00> = 0, |10> = 1, |01> = 2, |11> = 3).所以我想要做的是仅将 2 个读出量子位传递给 TFQ,并使用 4 类分类的组合测量 (|00> = 0, |10> = 1, |01> = 2, |11> = 3) . Ideally this would allow a 2^n multi-class classification, where n is the number of qubits.理想情况下,这将允许 2^n 多类分类,其中 n 是量子位的数量。 In Cirq I can achieved this output by performing a cirq.measure(qR0, qR1, key='measure') on the two readout qubits.在 Cirq 中,我可以通过对两个读出量子位执行cirq.measure(qR0, qR1, key='measure')来实现此输出。 However I am struggling in passing such command to TFQ, since from what I understand it measures only the qubits that end with a single qubit Pauli gate.然而,我正在努力将这样的命令传递给 TFQ,因为据我所知,它只测量以单个量子比特泡利门结尾的量子比特。

So, is there something that I am missing in the functionalities of TFQ that allows such kind of measurements in the training process?那么,我在 TFQ 的功能中是否缺少某些允许在训练过程中进行此类测量的功能?

Starting with this snippet:从这个片段开始:

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

# !This is important!
ops = [-1.0 * cirq.Z(bit), cirq.X(bit) + 2.0 * cirq.Z(bit)]
# !This is important!

circuit_list = [
    _gen_single_bit_rotation_problem(bit, symbols),
    cirq.Circuit(
        cirq.Z(bit) ** symbols[0],
        cirq.X(bit) ** symbols[1],
        cirq.Z(bit) ** symbols[2]
    ),
    cirq.Circuit(
        cirq.X(bit) ** symbols[0],
        cirq.Z(bit) ** symbols[1],
        cirq.X(bit) ** symbols[2]
    )
]
expectation_layer = tfq.layers.Expectation()
output = expectation_layer(
    circuit_list, symbol_names=symbols, operators = ops)
# Here output[i][j] corresponds to the expectation of all the ops
# in ops w.r.t circuits[i] where keras managed variables are
# placed in the symbols 'x', 'y', 'z'.
tf.shape(output)

Which I took from here: https://www.tensorflow.org/quantum/api_docs/python/tfq/layers/Expectation .我从这里获取的: https : //www.tensorflow.org/quantum/api_docs/python/tfq/layers/Expectation

The shape of the output tensor is [3, 2] Where I have 3 different circuits and I took two expectation values over each circuit. output张量的形状是[3, 2]其中我有 3 个不同的电路,我对每个电路取了两个期望值。 The value at [1, 0] of output would be: output [1, 0]处的值将是:

U 值 0

在此处输入图片说明

Then the value at [2, 1] of output would be:那么output [2, 1]处的值将是:

U值

在此处输入图片说明

The shape and contents of output 's values are partly dictated by the shape and contents of ops . output值的形状和内容部分由ops的形状和内容决定。 If I wanted to make the output shape [3, 3] I could just add another valid cirq.PauliSum object to the ops list.如果我想制作输出形状[3, 3]我可以将另一个有效的cirq.PauliSum对象添加到ops列表中。 In your case if you want the probability of getting 00, 01, 10, 11, on two particular cirq.GridQubit s q0 and q1 you can do something like this:在您的情况下,如果您希望在两个特定的cirq.GridQubit s q0q1上获得 00、01、10、11 的概率,您可以执行以下操作:

def zero_proj(qubit):
  return (1 + cirq.Z(qubit)) / 2

def one_proj(qubit):
  return (1 - cirq.Z(qubit)) / 2

# ! This is important
ops = [
  zero_proj(q0) * zero_proj(q1),
  zero_proj(q0) * one_proj(q1),
  one_proj(q0) * zero_proj(q1),
  one_proj(q0)* one_proj(q1)
]
# ! This is important

Making the output shape of any layer that ingests ops : [whatever_your_batch_size_is, 4] .制作任何摄取ops层的输出形状: [whatever_your_batch_size_is, 4] Does this help clear things up ?这有助于解决问题吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于多类分类的Tensorflow混淆矩阵 - Tensorflow confusion matrix for multiclass classification 为多类分类python tensorflow keras设置偏差 - setting bias for multiclass classification python tensorflow keras 使用 tensorflow 进行多类分类时遇到问题 - Having problems while doing multiclass classification with tensorflow Tensorflow/Keras/BERT 多类文本分类准确度 - Tensorflow/Keras/BERT MultiClass Text Classification Accuracy 使用Keras进行多类分类时数组形状错误 - Wrong array shape in multiclass classification using Keras 使用 TPU 进行二元与多类分类 - Binary vs Multiclass Classification using TPU 使用 TensoFlow 的多类分类标签错误 - Multiclass classification label error using TensoFlow 使用 LSTM 和 Glove、Keras 和 Tensorflow 改进多类文本分类 model - Improve multiclass text classification model with LSTM and Glove, Keras and Tensorflow 用于多类和二元分类的 Tensorflow Macro F1 分数 - Tensorflow Macro F1 Score for multiclass and also for binary classification 如何将数据馈入TensorFlow中的LSTM单元以进行多类分类? - How to feed data to LSTM cells in TensorFlow for multiclass classification?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM