New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linear or non linear Regression #127

Closed
kareem1925 opened this Issue Nov 14, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@kareem1925
Copy link

kareem1925 commented Nov 14, 2018

can someone please suggest a way to do regression using this great library? thanks

@co9olguy

This comment has been minimized.

Copy link
Member

co9olguy commented Nov 14, 2018

Hi @kareem1925,
Thanks for your interest in the library.
Typically the github issues page isn't the best place to ask this sort of question (it's more for issues with the code/bugs, etc.), but we haven't yet set up a public forum for pennylane questions (should be coming soon).

I attach a brief toy example below for how to do linear regression in PennyLane. Hopefully it can be modified to suit your needs 😄

import pennylane as qml
from pennylane import numpy as np

x = np.linspace(-1,1,10)

np.random.seed(0)
m,b = 0.5, 1.2
y_data = m * x + b + 0.1 * np.random.randn(10)
def y_pred(weights):
    return weights[0] * x + weights[1]

def cost(weights):
    y_pred_ = y_pred(weights)
    mse = np.mean((y_data - y_pred_) ** 2)
    return mse

opt = qml.GradientDescentOptimizer(0.5)

init_weights = [0.0, 0.0]
weights = init_weights

for step in range(20):
    weights = opt.step(cost, weights)
    print(cost(weights))

import matplotlib.pyplot as plt
plt.scatter(x,y_data)
plt.plot(x, y_pred(weights),'r')

@co9olguy co9olguy closed this Nov 14, 2018

@josh146

This comment has been minimized.

Copy link
Member

josh146 commented Nov 16, 2018

Hi @kareem1925, just letting you know we now have a PennyLane discussion forum: https://discuss.pennylane.ai.

Feel free to post your PennyLane regression questions there, and we will aim to answer as soon as possible :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment