question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hello Sir, I'm a total beginner trying this code out and I'm getting an error can you help me out?

See original GitHub issue

CODE

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from tensorflow.contrib import learn
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn import datasets, linear_model
from sklearn import model_selection
from sklearn import preprocessing

import tensorflow as tf
import pyswarms as ps
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import math
import calendar
import time





from IPython import get_ipython
ipython = get_ipython()

if '__IPYTHON__' in globals():
    ipython.magic('load_ext autoreload')
    ipython.magic('autoreload 2')
    
# Load the iris dataset




# Load the dataset
region = "iris"
filename = region + ".csv"

x = np.array(pd.read_csv(filename, usecols=[1], header=None))
y = np.array(pd.read_csv(filename, usecols=[3], header=None))

normalized_X = preprocessing.scale(x)

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(
normalized_X, y, test_size=0.2222222222222222, shuffle=False, stratify=None)
print(Y_train)
import numpy as np
Y_train_new = Y_train.ravel()
le = preprocessing.LabelEncoder()
le.fit(Y_train_new)
le.classes_
Y_train_transformed = le.transform(Y_train_new)
le.inverse_transform(le.transform(Y_train_new))
print(Y_train_new)
print(Y_train_transformed)


# Forward propagation
def forward_prop(params):
    # Neural network architecture
    n_inputs = X_train.shape[1]
    n_hidden = 20
    n_classes = 1

    # Roll-back the weights and biases
    W1 = params[0:80].reshape((n_inputs,n_hidden))
    b1 = params[80:100].reshape((n_hidden,))
    W2 = params[100:120].reshape((n_hidden,n_classes))
    b2 = params[120:121].reshape((n_classes,))

    # Perform forward propagation
    z1 = X_train.dot(W1) + b1  # Pre-activation in Layer 1
    a1 = np.tanh(z1)     # Activation in Layer 1
    z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
    logits = z2          # Logits for Layer 2

    # Compute for the softmax of the logits
    exp_scores = np.exp(logits)
    probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)

    # Compute for the negative log likelihood
    N = 84 # Number of samples
    correct_logprobs = -np.log(probs[range(N), Y_train])
    loss = np.sum(correct_logprobs) / N
    return loss

def f(X_train):
    n_particles = 100
    j = [forward_prop(X_train[i]) for i in range(n_particles)]
    return np.array(j)

# Initialize swarm
options = {'c1': 0.5, 'c2': 0.3, 'w':0.9}

# Call instance of PSO
dimensions = (4 * 20) + (20 * 1) + 20 + 1
optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options)

# Perform optimization
cost, pos = optimizer.optimize(f, print_step=100, iters=1000, verbose=3)

def predict(X_train, pos):
    # Neural network architecture
    n_inputs = X_train.shape[1]
    n_hidden = 20
    n_classes = 1

    # Roll-back the weights and biases
    W1 = pos[0:80].reshape((n_inputs,n_hidden))
    b1 = pos[80:100].reshape((n_hidden,))
    W2 = pos[100:120].reshape((n_hidden,n_classes))
    b2 = pos[120:121].reshape((n_classes,))

    # Perform forward propagation
    z1 = X_train.dot(W1) + b1  # Pre-activation in Layer 1
    a1 = np.tanh(z1)     # Activation in Layer 1
    z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
    logits = z2          # Logits for Layer 2

    y_pred = np.argmax(logits, axis=1)
    print(y_pred)
    return y_pred

(predict(X_train, pos) == Y_train).mean()
================================
Error:
File "/anaconda3/lib/python3.6/site-packages/pyswarms-0.3.1-py3.6.egg/pyswarms/single/global_best.py", line 167, in optimize
    self.swarm.position, *kwargs

TypeError: f() takes 1 positional argument but 3 were given

_Originally posted by @Sanjeeth8733 in https://github.com/ljvmiranda921/pyswarms/issues/41#issuecomment-433631657_

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
whzupcommented, Oct 30, 2018

@ljvmiranda921 that looks weird to me. @Sanjeeth8733 Maybe try to change the print_step and iters keywords around. From this:

# Perform optimization
cost, pos = optimizer.optimize(f, print_step=100, iters=1000, verbose=3)

To that:

# Perform optimization
cost, pos = optimizer.optimize(f, iters=1000, print_step=100, verbose=3)

I don’t think this will change a lot but frankly, it’s the only thing I can see an error in πŸ˜• I feel like Anaconda might misinterpret the keywords for kwargs and feed it into the objective function instead of using them as function β€œkeys”.

0reactions
Sanjeeth8733commented, Nov 5, 2018

@whzup the error still persists after I randomly added some variables into f() as you can see below.

Import modules

from future import absolute_import from future import division from future import print_function from sklearn import model_selection from sklearn import preprocessing

import pyswarms as ps import numpy as np import pandas as pd

import matplotlib.pyplot as plt

from pyswarms.utils.plotters import plot_cost_history from pyswarms.utils.plotters.formatters import Designer from matplotlib import animation from matplotlib import rc from IPython.display import HTML

Import PySwarms

from pyswarms.utils.plotters import (plot_cost_history, plot_contour, plot_surface)

Import PySwarms

Import modules

from IPython import get_ipython ipython = get_ipython()

if β€˜IPYTHON’ in globals(): ipython.magic(β€˜load_ext autoreload’) ipython.magic(β€˜autoreload 2’)

Load the dataset

region = β€œtesting” filename = region + β€œ.csv”

x = np.array(pd.read_csv(filename, usecols=[0,1,2,3,4,5,6,7,8,9])) y = np.array(pd.read_csv(filename, usecols=[10]))

β€œβ€" x = np.array(pd.read_csv(filename, usecols=[0,1,2,3,4,5,6,7,8,9], header=None)) y = np.array(pd.read_csv(filename, usecols=[10], header=None)) β€œβ€" print(β€œ------------------”)

normalized_X = preprocessing.scale(x)

X_train, X_test, Y_train, Y_test = model_selection.train_test_split( normalized_X, y, test_size=0.32, shuffle=False, stratify=None)

β€˜β€™β€™ print(β€œ------------------”) print(Y_train.shape[0]) print(β€œ------------------”) β€˜β€™β€™ Y_train_new = Y_train.ravel()

le = preprocessing.LabelEncoder() le.fit(Y_train_new) le.classes_ Y_train_transformed = le.transform(Y_train_new) le.inverse_transform(le.transform(Y_train_new)) β€˜β€™β€™ print(Y_train_transformed) print(β€œ------------------”) print(Y_train_new) print(β€œ------------------”) print(Y_train) print(β€œ------------------”) print(X_train.shape[1]) print(β€œ------------------”) print(W1) print(β€œ------------------”) β€˜β€™β€™

Forward propagation

def forward_prop(params): # Neural network architecture n_inputs = X_train.shape[1] n_hidden = 20 n_classes = 2

# Roll-back the weights and biases
W1 = params[0:200].reshape((n_inputs,n_hidden))
b1 = params[200:220].reshape((n_hidden,))
W2 = params[220:260].reshape((n_hidden,n_classes))
b2 = params[260:262].reshape((n_classes,))

# Perform forward propagation
z1 = X_train.dot(W1) + b1  # Pre-activation in Layer 1
a1 = np.tanh(z1)     # Activation in Layer 1
z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
logits = z2          # Logits for Layer 2

# Compute for the softmax of the logits
exp_scores = np.exp(logits)
probs = exp_scores / np.sum(exp_scores, axis=1, keepdims=True)

# Compute for the negative log likelihood
N = Y_train.shape[0] # Number of samples
correct_logprobs = -np.log(probs[range(N), Y_train])
loss = np.sum(correct_logprobs) / N

return loss

def f(X_train, X_test,alpha=0.88): n_particles = 100 j = [forward_prop(X_train[i]) for i in range(n_particles)] return np.array(j)

Initialize swarm

options = {β€˜c1’: 0.5, β€˜c2’: 0.3, β€˜w’:0.91}

Call instance of PSO

dimensions = (X_train.shape[1] * 20) + (20 * 2) + 20 + 2 optimizer = ps.single.GlobalBestPSO(n_particles=100, dimensions=dimensions, options=options) print(dimensions) print(optimizer)

Perform optimization

cost, pos = optimizer.optimize(f, print_step=10, iters=2, verbose=3)

def predict(X_train, pos): # Neural network architecture n_inputs = X_train.shape[1] n_hidden = 20 n_classes = 2

# Roll-back the weights and biases
W1 = pos[0:200].reshape((n_inputs,n_hidden))
b1 = pos[200:220].reshape((n_hidden,))
W2 = pos[220:260].reshape((n_hidden,n_classes))
b2 = pos[260:262].reshape((n_classes,))

# Perform forward propagation
z1 = X_train.dot(W1) + b1  # Pre-activation in Layer 1
a1 = np.tanh(z1)     # Activation in Layer 1
z2 = a1.dot(W2) + b2 # Pre-activation in Layer 2
logits = z2          # Logits for Layer 2

y_pred = np.argmax(logits, axis=1)

return y_pred

A=(predict(X_train, pos) == Y_train).mean() print(β€˜=================================================’) print(β€˜=================================================’) print(β€˜=================================================’) print(β€œAccurecy") print(β€˜|\t\t’,100*A,β€˜%\t\t|’) print("________________________________________________”) print(β€˜=================================================’) print(β€˜=================================================’) β€œβ€"

Plotting the cost history To plot the cost history, we simply obtain the cost_history from the optimizer class and pass it to the cost_history function. Furthermore, this method also accepts a keyword argument **kwargs similar to matplotlib. This enables us to further customize various artists and elements in the plot. In addition, we can obtain the following histories from the same class: β€’ mean_neighbor_history: average local best history of all neighbors throughout optimization β€’ mean_pbest_history: average personal best of the particles throughout optimization β€œβ€"

plot_cost_history(cost_history=optimizer.cost_history) plt.show()

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use Google Classroom - Tutorial for Beginners
This video tutorial will show you how to use Google Classroom for Beginners 2020. Google Classroom for Teachers is a fantastic way to...
Read more >
An Introductory Robot Programming Tutorial - Toptal
Fascinated by robots? Follow this simple guide and learn how to use a robot simulator for programming a robot.
Read more >
Apex Tutorials - Salesforce coding lessons for the 99%
Check out the full Apex Academy: Absolute Beginner's Guide to Coding in Salesforce course! Chapter 1 Quiz!
Read more >
YouTube Community - Google Support
Welcome to the YouTube Help Community ... YouTube Go is going away in August of this year Announcement ... No one can see...
Read more >
Beginner's Guide to Troubleshooting WordPress Errors (Step ...
Want to learn how to properly troubleshoot WordPress errors? ... you'll get an error message in the tab where you're trying to access...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found