首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow Quantum: PQC未优化

Tensorflow Quantum: PQC未优化
EN

Stack Overflow用户
提问于 2020-06-29 01:07:23
回答 1查看 97关注 0票数 0

我遵循了https://www.tensorflow.org/quantum/tutorials/mnist上提供的教程。我已经将本教程修改为我能想到的最简单的示例:输入集,其中x从0线性增加到1,y=x< 0.3。然后,我使用带有符号的单个Rx门的PQC,以及使用Z门的读取器。

当检索优化的符号并手动调整它时,我可以很容易地找到一个提供100%准确性的值,但是当我让Adam优化器运行时,它要么收敛到always predict 1,要么总是predict -1。有人发现我做错了什么吗?(我为不能将代码分解为一个较小的示例而道歉)

代码语言:javascript
复制
import tensorflow as tf
import tensorflow_quantum as tfq

import cirq
import sympy
import numpy as np

# used to embed classical data in quantum circuits
def convert_to_circuit_cont(image):
    """Encode truncated classical image into quantum datapoint."""
    values = np.ndarray.flatten(image)
    qubits = cirq.GridQubit.rect(4, 1)
    circuit = cirq.Circuit()
    for i, value in enumerate(values):
        if value:
            circuit.append(cirq.rx(value).on(qubits[i]))
    return circuit

# define classical dataset
length = 1000
np.random.seed(42)

# create a linearly increasing set for x from 0 to 1 in 1/length steps
x_train_sorted = np.asarray([[x/length] for x in range(0,length)], dtype=np.float32)
# p is used to shuffle x and y similarly
p = np.random.permutation(len(x_train_sorted))
x_train = x_train_sorted[p]
# y = x < 0.3 in {-1, 1} for Hinge loss
y_train_sorted = np.asarray([1 if (x/length)<0.30 else -1 for x in range(0,length)])
y_train = y_train_sorted[p]
# test == train for this example
x_test = x_train_sorted[:]
y_test = y_train_sorted[:]

# convert classical data into quantum circuits
x_train_circ = [convert_to_circuit_cont(x) for x in x_train]
x_test_circ = [convert_to_circuit_cont(x) for x in x_test]
x_train_tfcirc = tfq.convert_to_tensor(x_train_circ)
x_test_tfcirc = tfq.convert_to_tensor(x_test_circ)

# define the PQC circuit, consisting out of 1 qubit with 1 gate (Rx) and 1 parameter
def create_quantum_model():
    data_qubits = cirq.GridQubit.rect(1, 1)  
    circuit = cirq.Circuit()
    a = sympy.Symbol("a")
    circuit.append(cirq.rx(a).on(data_qubits[0])),
    return circuit, cirq.Z(data_qubits[0])
model_circuit, model_readout = create_quantum_model()

# Build the Keras model.
model = tf.keras.Sequential([
    # The input is the data-circuit, encoded as a tf.string
    tf.keras.layers.Input(shape=(), dtype=tf.string),
    # The PQC layer returns the expected value of the readout gate, range [-1,1].
    tfq.layers.PQC(model_circuit, model_readout),
])

# used for logging progress during optimization
def hinge_accuracy(y_true, y_pred):
    y_true = tf.squeeze(y_true) > 0.0
    y_pred = tf.squeeze(y_pred) > 0.0
    result = tf.cast(y_true == y_pred, tf.float32)
return tf.reduce_mean(result)

# compile the model with Hinge loss and Adam, as done in the example. Have tried with various learning_rates
model.compile(
    loss = tf.keras.losses.Hinge(),
    optimizer=tf.keras.optimizers.Adam(learning_rate=0.1),
    metrics=[hinge_accuracy])

EPOCHS = 20
BATCH_SIZE = 32
NUM_EXAMPLES = 1000

# fit the model
qnn_history = model.fit(
      x_train_tfcirc, y_train, 
      batch_size=32,
      epochs=EPOCHS,
      verbose=1,
      validation_data=(x_test_tfcirc, y_test),
      use_multiprocessing=False)

results = model.predict(x_test_tfcirc)
results_mapped = [-1 if x<=0 else 1 for x in results[:,0]]
print(np.sum(np.equal(results_mapped, y_test)))

经过20个时期的优化后,我得到了以下结果:

代码语言:javascript
复制
1000/1000 [==============================] - 0s 410us/sample - loss: 0.5589 - hinge_accuracy: 0.6982 - val_loss: 0.5530 - val_hinge_accuracy: 0.7070

这导致1000个样本中有700个样本被正确预测。查看映射结果时,这是因为所有结果都被预测为-1。当查看原始结果时,它们从-0.5484014线性增加到-0.99996257。

当使用w= model.layers.get_weights()检索权重,减去0.8,并使用model.layers.set_weights(w)再次设置它时,我得到920/1000正确。对这个过程进行微调可以让我达到1000/1000。

更新1:我还打印了各个时期的权重更新:

代码语言:javascript
复制
4.916246, 4.242602, 3.3765688, 2.6855211, 2.3405066, 2.206207, 2.1734586, 2.1656137, 2.1510274, 2.1634471, 2.1683235, 2.188944, 2.1510284, 2.1591303, 2.1632445, 2.1542525, 2.1677444, 2.1702878, 2.163104, 2.1635907

我将权重设置为1.36,该值为908/1000 (而不是700/100)。优化器会离开它:

代码语言:javascript
复制
1.7992111, 2.0727847, 2.1370323, 2.15711, 2.1686404, 2.1603785, 2.183334, 2.1563332, 2.156857, 2.169908, 2.1658351, 2.170673, 2.1575692, 2.1505954, 2.1561477, 2.1754034, 2.1545155, 2.1635509, 2.1464484, 2.1707492

我注意到的一件事是,铰链精度的值为0.75,重量为1.36,高于2.17的0.7。如果是这种情况,我要么处于优化景观的不幸部分,其中全局最小值与损失景观的最小值不对应,要么损失值被错误地确定。这就是我接下来要调查的。

EN

回答 1

Stack Overflow用户

发布于 2020-06-29 18:47:34

这个例子的铰链损失函数的最小值与正确分类的例子的数量的最大值不对应。请参阅这些w.r.t的图表。参数的值。假设优化器的工作目标是损失的最小值,而不是分类示例数量的最大值,那么代码(和框架/优化器)就会做它们应该做的事情。或者,可以使用不同的损失函数来尝试找到更好的拟合。例如,二值化的l1损失。此函数将具有相同的全局最优值,但可能具有非常平坦的景观。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62625744

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档