Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8213551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:09:26+00:00 2026-06-07T11:09:26+00:00

I wrote a neural network object in python that has a cost function and

  • 0

I wrote a neural network object in python that has a cost function and determines its gradients via back-propogation. I see a bunch of optimization functions here, but I have no idea how to implement them. I’m also having a hard time finding any example code to learn from.

Clearly I need to somehow tell it what parameters I’m trying to change, the cost function I’m trying to minimize, and then the gradient calculated by backprop. How do I tell, say, fmin_cg what’s what?

bonus question: where can I learn about the differences in uses of the various algorithms?

===== OK, update =====

This is what I have atm:

def train(self, x, y_vals, iters = 400):
    t0 = concatenate((self.hid_t.reshape(-1), self.out_t.reshape(-1)), 1)
    self.forward_prop(x, t0)

    c = lambda v: self.cost(x, y_vals, v)
    g = lambda v: self.back_prop(y_vals, v)

    t_best = fmin_cg(c, t0, g, disp=True, maxiter=iters)
    self.hid_t = reshape(t_best[:,:(hid_n * (in_n+1))], (hid_n, in_n+1))
    self.out_t = reshape(t_best[:,(hid_n * (in_n+1)):], (out_n, hid_n+1))

And, this is the error it’s throwing:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "netset.py", line 27, in <module>
    net.train(x,y)
  File "neuralnet.py", line 60, in train
    t_best = fmin_cg(c, t0, g, disp=True, maxiter=iters)
  File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 952, in fmin_cg
    res = _minimize_cg(f, x0, args, fprime, callback=callback, **opts)
  File "/usr/local/lib/python2.7/dist-packages/scipy/optimize/optimize.py", line 1017, in _minimize_cg
    deltak = numpy.dot(gfk, gfk)
ValueError: matrices are not aligned

…Halp!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T11:09:28+00:00Added an answer on June 7, 2026 at 11:09 am

    I never used fmin_cg. I guess v is your weight vector. I did not find an error in your code, when I read the documentation. But I searched for your error and I found this: matrices are not aligned Error: Python SciPy fmin_bfgs

    In addition, I think it is not garantueed that g(v) is always calculated after c(v). Thus, your backpropagation function should forward propagate x once again:

    c = lambda v: self.cost(x, y_vals, v)
    g = lambda v: self.cost(x, y_vals, v); self.back_prop(y_vals, v)
    #             ------------------------
    

    Or you can just pass one function that returns the cost function and the gradient as a tuple to avoid two forward propagations as Issam Laradji mentioned.

    Good articles about optimization algorithms for artificial neural networks are:

    • Efficient Backprop
    • Neural Network FAQ, Part 2

    I can recommend Levenberg-Marquardt. This algorithms works really well. Unfortunately every iteration step has cubic complexity O(n^3).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a text file to store the weight of a neural network that
I tried to write a Neural Network system, but even running through simple AND/OR/NOR
I wrote a C++ DLL and it has been compiled. It requires Visual C++
I wrote a PHP script that retrieves values from a MySQL Query. I used
I wrote a Flickr search engine that makes a call to either a public
I wrote a php code that display error in red color. But somehow, this
I wrote a webpage where a user can enter a log entry that is
I wrote my class with its private variables and then I wrote the accessor
I wrote this: protected void btnup_Click(object sender, EventArgs e) { if (ASPxUploadControl1.HasFile) { try
I wrote an objective-C class that needs to notify another class, so i defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.