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 issueCODE
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:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@ljvmiranda921 that looks weird to me. @Sanjeeth8733 Maybe try to change the
print_step
anditers
keywords around. From this:To that:
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β.
@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
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
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()