Exemplo n.º 1
0
def objective(x_train, y_train, W1, b1, z1, a1, W2, b2, z2, a2, W3, b3, z3, u, v1, v2, rho):
    r1 = tf.reduce_sum((z1 - tf.matmul(W1, x_train) - b1) * (z1 - tf.matmul(W1, x_train) - b1))
    r2 = tf.reduce_sum((z2 - tf.matmul(W2, a1) - b2) * (z2 - tf.matmul(W2, a1) - b2))
    r3 = tf.reduce_sum((z3 - tf.matmul(W3, a2) - b3) * (z3 - tf.matmul(W3, a2) - b3))
    loss = common.cross_entropy_with_softmax(y_train, z3)
    obj = loss + tf.linalg.trace(tf.matmul(z3 - tf.matmul(W3, a2) - b3, tf.transpose(u)))
    obj = obj + rho / 2 * r1 + rho / 2 * r2 + rho / 2 * r3
    obj = obj + rho / 2 * tf.reduce_sum((a1 - common.relu(z1) + v1) * (a1 - common.relu(z1) + v1)) + rho / 2 * tf.reduce_sum(
        (a2 - common.relu(z2) + v2) * (a2 - common.relu(z2) + v2))
    return obj
Exemplo n.º 2
0
def objective(x_train, y_train, W1, b1, z1, a1, W2, b2, z2, a2, W3, b3, z3, u, v1, v2, rho):
    r1 = np.sum((z1 - mul(W1, x_train) - b1) * (z1 - mul(W1, x_train) - b1))
    r2 = np.sum((z2 - mul(W2, a1) - b2) * (z2 - mul(W2, a1) - b2))
    r3 = np.sum((z3 - mul(W3, a2) - b3) * (z3 - mul(W3, a2) - b3))
    loss = common.cross_entropy_with_softmax(y_train, z3)
    obj = loss + np.trace(mul(z3 - mul(W3, a2) - b3, np.transpose(u)))
    obj = obj + rho / 2 * r1 + rho / 2 * r2 + rho / 2 * r3
    obj = obj + rho / 2 * np.sum((a1 - common.relu(z1) + v1) * (a1 - common.relu(z1) + v1)) + rho / 2 * np.sum(
        (a2 - common.relu(z2) + v2) * (a2 - common.relu(z2) + v2))
    return obj
Exemplo n.º 3
0
def test_accuracy(W1, b1, W2, b2, W3, b3, images, labels):
    nums = labels.shape[1]
    z1 = np.matmul(W1, images) + b1
    a1 = common.relu(z1)
    z2 = np.matmul(W2, a1) + b2
    a2 = common.relu(z2)
    z3 = np.matmul(W3, a2) + b3
    cost = common.cross_entropy_with_softmax(labels, z3) / nums
    pred = np.argmax(labels, axis=0)
    label = np.argmax(z3, axis=0)
    return (np.sum(np.equal(pred, label)) / nums, cost)
Exemplo n.º 4
0
def test_accuracy(W1, b1, W2, b2, W3, b3, images, labels):
    nums = labels.shape[1]
    z1 = tf.matmul(W1, images) + b1
    a1 = common.relu(z1)
    z2 = tf.matmul(W2, a1) + b2
    a2 = common.relu(z2)
    z3 = tf.matmul(W3, a2) + b3
    cost = common.cross_entropy_with_softmax(labels, z3) / nums
    actual = tf.argmax(labels, axis=0)
    pred = tf.argmax(z3, axis=0)
    return (tf.reduce_sum(tf.cast(tf.equal(pred, actual),tf.float32)) / nums,cost)