Python: ArgumentError - this function requires 6 arguments, but you've provided 8

During my attempt to implement a gradient descent algorithm, I encountered an intriguing issue related to the ineffective use of **kwargs. The function in question is as follows:

def gradient_descent(g,x,y,alpha,max_its,w,**kwargs):    
    # switch for verbose
    verbose = True
    if 'verbose' in kwargs:
        verbose = kwargs['verbose']

    # determine num train and batch size
    num_train = y.size()[1]
    batch_size = num_train
    if 'batch_size' in kwargs:
        batch_size = kwargs['batch_size']
    ........

This resulted in the following error:

TypeError                                 Traceback (most recent call last)
<ipython-input-12-f71adb8a241b> in <module>()
  3 w_train = Variable(torch.Tensor(w_init), requires_grad=True)
  4 g = softmax; alpha_choice = 10**(-1); max_its = 100; num_pts = y.size; 
batch_size = 10;
----> 5 weight_hist_2,train_hist_2 = gradient_descent(g,x_train,y_train,alpha_choice,max_its,w_train,num_pts,batch_size,verbose = False)

TypeError: gradient_descent() takes 6 positional arguments but 8 were given.

Are there any oversights in my approach to developing this function?

Answer №1

It seems like your function signature does not match the number of parameters you are using it with:

gradient_descent(g,x,y,alpha,max_its,w,**kwargs)

The function expects 6 positional arguments g, x, y, alpha, max_its, w, but in your function call:

gradient_descent(g, x_train, y_train, alpha_choice, max_its, w_train, num_pts, batch_size, verbose=False)

You are providing it with 8 arguments

g, x_train, y_train, alpha_choice, max_its, w_train, num_pts, batch_size

I assumed that you meant to use num_pts as the batch_size argument, so the corrected call would be:

weight_hist_2, train_hist_2 = gradient_descent(
    g,
    x_train,
    y_train,
    alpha_choice,
    max_its,
    w_train,
    batch_size=num_pts,
    verbose=False)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adding quotation marks to the string form of a two-dimensional array in Python

My code takes user input from a text box and needs to convert it into a 2D list of strings. The user input comes from a Jupyter Dash text input I am working on creating a design where the user does not have to quote the elements themselves and can simply ...

Google's Selenium Colab

I recently encountered this issue while using Colab after following the installation steps below: # !pip install selenium # !apt-get update # to update ubuntu to correctly run apt install # !apt install chromium-chromedriver # !cp /usr/lib/chromium-browser ...

Using Q objects in Django Filters across multiple models

I have been working on a Django Application where I am trying to implement a search function across two models, Profile (fields: surname and othernames) and Account (field: account_number), using Q objects. However, my current implementation is only search ...

Combine dataframes while excluding any overlapping values from the second dataframe

I have two distinct dataframes, A and B. A = pd.DataFrame({'a'=[1,2,3,4,5], 'b'=[11,22,33,44,55]}) B = pd.DataFrame({'a'=[7,2,3,4,9], 'b'=[123,234,456,789,1122]}) My objective is to merge B with A while excluding an ...

Exploring the cleaned_data attribute in a django Form

Recently, I encountered a scenario with my Django form setup that involves using a simple form structure like this: class ContactForm(forms.Form): subject = forms.CharField(max_length=100) message = forms.CharField(widget=forms.Textarea) In my vi ...

What is the best way to incorporate a function within a $.click (jquery) event that utilizes an id directly from the clicked element?

It seems that my title may not be very clear, but I have a jQuery code snippet that is causing some issues. The problem stems from the fact that when I click on an image with the class 'slideimg', I expect a function named slideDo to be executed, ...

Do all images need to be uniform in size when utilizing the tensorflow object detection api?

I need to train a set of images that have already been labeled for object detection. The images have different resolutions, so I am wondering if it is possible to train them using the object detection API. Would specifying the min_dimension as the greate ...

Creating a Docker container to execute Python3 from a Node.js child_process on Google App Engine

I have been working on deploying a node.js web application using a custom runtime with flex environment. In my code, I am calling child_process in Node.js to open python3 like so: const spawn = require("child_process").spawn; pythonProcess = spawn('p ...

What is the process for obtaining the most recent stock price through Fidelity's screening tool?

Trying to extract the latest stock price using Fidelity's screener. For instance, the current market value of AAPL stands at $165.02, accessible via this link. Upon inspecting the webpage, the price is displayed within this tag: <div _ngcontent-cx ...

Utilizing SelenuimBase to fetch a Tableau dashboard for downloading

I have been working on a script to automate the download of my Tableau Public visualization for a project. However, I am facing issues after clicking on the download button. from seleniumbase import BaseCase import pyautogui class Test(BaseCase): def test ...

Changing a subelement in an XML with Python depending on another subelement

I came across this XML file: <document> <file> <format>pdf</format> <location>example1</location> </file> <file> <format>docx</format> <location>example2</location> < ...

What is the best way to switch proxies multiple times during a single webdriver session?

I have been developing a bot that requires changing the proxy of the webdriver every 50 searches. I am currently using an API to request proxies and sockets, storing these variables for each session. However, my current method of setting up proxies through ...

A guide to verifying the data type of a CLI argument in Python's Click module

Currently, I am working on a click command that can accept arguments of both integer (int) and string (str) types. The challenge I'm facing is determining the type of argument passed within the function. Below is an example: import click @click.comm ...

Generating a fresh GeoJSON LineString by utilizing the information from JSON items and incorporating a certain property

I need to combine multiple JSON objects into a GeoJSON feature collection with LineStrings Here are some example poorly formatted JSON objects: {"lat":16.0269337,"lon":40.073042,"score":1,"ID":"13800006252028","TYPES":"Regional","N2C":"2","NAME":"Strada ...

When "if choice" is activated, it triggers additional code execution in the program instead of halting as intended

When the user enters "1" in my code, it prints more code than I want it to. I can't use the break statement because I still want the user to be able to enter the correct password. Before this part of the code runs, there's another section that di ...

Selenium in Python is facing difficulties establishing a connection with the Webdriver Firefox extension

Currently, I am working with Python 2.7, Selenium 2.35, and Firefox 22.0 in my project. When I execute the following code: from selenium import webdriver d = webdriver.Firefox() The Firefox browser opens successfully and remains open. However, an error ...

Is multiprocessing the answer?

Imagine having a function that retrieves a large amount of data from a device each time it is called. The goal is to store this data in a memory buffer and when the buffer reaches a certain size, another function should come into play to process it. Howeve ...

Filter numbers within an Array using a Function Parameter

I'm currently experimenting with Arrays and the .filter() function. My goal is to filter between specified parameters in a function to achieve the desired output. However, I'm encountering an issue where my NPM test is failing. Although the outpu ...

Create a custom Python graphical user interface that integrates a button for executing a command in the terminal

I currently have a project that involves two separate files. File number one handles eye-tracking and blinking calculations. This file runs whenever I input a command in the terminal using this line: python3 blink.py --shape-predictor shape_predictor_68_f ...

Exploring web content using BeautifulSoup and Selenium

Seeking to extract average temperatures and actual temperatures from a specific website: Although I am able to retrieve the source code of the webpage, I am encountering difficulties in filtering out only the data for high temperatures, low temperatures, ...