Python Forum
Indentation error in Python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Indentation error in Python code
#1
import numpy as np

#Input array
X=np.array([[1,0,1,0],[1,0,1,1],[0,1,0,1]])

#Output
y=np.array([[1],[1],[0]])

#Sigmoid Function
def sigmoid (x):
return 1/(1 + np.exp(-x))

#Derivative of Sigmoid Function
def derivatives_sigmoid(x):
return x * (1 - x)

#Variable initialization
epoch=5000 #Setting training iterations
lr=0.1 #Setting learning rate
inputlayer_neurons = X.shape[1] #number of features in data set
hiddenlayer_neurons = 3 #number of hidden layers neurons
output_neurons = 1 #number of neurons at output layer

#weight and bias initialization
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))

for i in range(epoch):

#Forward Propogation
hidden_layer_input1=np.dot(X,wh)
hidden_layer_input=hidden_layer_input1 + bh
hiddenlayer_activations = sigmoid(hidden_layer_input)
output_layer_input1=np.dot(hiddenlayer_activations,wout)
output_layer_input= output_layer_input1+ bout
output = sigmoid(output_layer_input)

#Backpropagation
E = y-output
slope_output_layer = derivatives_sigmoid(output)
slope_hidden_layer = derivatives_sigmoid(hiddenlayer_activations)
d_output = E * slope_output_layer
Error_at_hidden_layer = d_output.dot(wout.T)
d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer
wout += hiddenlayer_activations.T.dot(d_output) *lr
bout += np.sum(d_output, axis=0,keepdims=True) *lr
wh += X.T.dot(d_hiddenlayer) *lr
bh += np.sum(d_hiddenlayer, axis=0,keepdims=True) *lr

print output
When I run the code above using my Python 3.5.2 interpreter, I get the following error.

File "nn_in_python.py", line 11
return 1/(1 + np.exp(-x))
^
IndentationError: expected an indented block

It says something about line 11 should be indented. But it is indented! So what is wrong and how to fix.

I am very new to python, but not to programming.

I just need to know what is wrong here. this code was taken directly from a tutorial, and it should run fine.

Any help appreciated.Thanks in advance.

respectfully,

ErnestTBass
Reply
#2
The code you have posted has no indentation in it at all.
Reply
#3
I will find the original source and post it. Sorry about the error.

Respectfully,

ErnestTBass
Reply
#4
Easy Ernest, just following the indentation drawbacks and output matrix is shown, as per attachment. Cheers
import numpy as np
 
#Input array
X=np.array([[1,0,1,0],[1,0,1,1],[0,1,0,1]])
 
#Output
y=np.array([[1],[1],[0]])
 
#Sigmoid Function
def sigmoid (x):
	return 1/(1 + np.exp(-x))
 
#Derivative of Sigmoid Function
def derivatives_sigmoid(x):
	return x * (1 - x)
 
#Variable initialization
epoch=5000 #Setting training iterations
lr=0.1 #Setting learning rate
inputlayer_neurons = X.shape[1] #number of features in data set
hiddenlayer_neurons = 3 #number of hidden layers neurons
output_neurons = 1 #number of neurons at output layer
 
#weight and bias initialization
wh=np.random.uniform(size=(inputlayer_neurons,hiddenlayer_neurons))
bh=np.random.uniform(size=(1,hiddenlayer_neurons))
wout=np.random.uniform(size=(hiddenlayer_neurons,output_neurons))
bout=np.random.uniform(size=(1,output_neurons))
 
for i in range(epoch):
 
#Forward Propogation
	hidden_layer_input1=np.dot(X,wh)
	hidden_layer_input=hidden_layer_input1 + bh
	hiddenlayer_activations = sigmoid(hidden_layer_input)
	output_layer_input1=np.dot(hiddenlayer_activations,wout)
	output_layer_input= output_layer_input1+ bout
	output = sigmoid(output_layer_input)
 
#Backpropagation
E = y-output
slope_output_layer = derivatives_sigmoid(output)
slope_hidden_layer = derivatives_sigmoid(hiddenlayer_activations)
d_output = E * slope_output_layer
Error_at_hidden_layer = d_output.dot(wout.T)
d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer
wout += hiddenlayer_activations.T.dot(d_output) *lr
bout += np.sum(d_output, axis=0,keepdims=True) *lr
wh += X.T.dot(d_hiddenlayer) *lr
bh += np.sum(d_hiddenlayer, axis=0,keepdims=True) *lr
 
print (output)
# ---------- PRINT OUTPUT --------------
# [[0.89400924]
#  [0.90199559]
#  [0.89956253]]
# ---------------- 
Reply
#5
Thanks for your help. I assume that you did this based on your knowledge of
Python.

I am new to Python. I know that there are Python indenting programs that can check and correct Python indentation.

There seem to be very few that are on Ubuntu linux.

Do know of one that is one Ubuntu linux? If so please let me know.

Thanks in advance.

Respectfully,

ErnestTBass
Reply
#6
Hi Ernest.
In principle, to amend the errors arising during runtime of a python script, I do follow, step by step, the prompt appearing on the screen, like I've done in your code of opening post (named ernest.py):
C:\Training>python ernest.py
  File "ernest.py", line 11
    return 1/(1 + np.exp(-x))
         ^
IndentationError: expected an indented block
So, line 11 should be indented; same story for next ones.

About your question to find software to indent python codes under linux, please be minded that Python is a multi-platform language. What does this mean? That any script (.py) running under Windows can be processed inside Linux O.S., like I've done with yours, as per att'd screenshot http://imgbox.com/lL1OnzOE

In other words, you adjust your well indented code under Windows, and then run it in Ubuntu.

Cheers
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code error from Fundamentals of Python Programming van Richard L. Halterman Heidi 12 1,601 Jul-25-2023, 10:32 PM
Last Post: Skaperen
  Syntax error while executing the Python code in Linux DivAsh 8 1,450 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,468 Mar-27-2023, 07:38 AM
Last Post: buran
  Error in if-then-else python code Led_Zeppelin 6 2,283 Jul-27-2022, 07:53 PM
Last Post: deanhystad
  Facing error while executing below Python code ramu4651 1 5,629 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  Skeleton file export error Python Code pepapoha 4 3,425 Nov-17-2020, 02:06 AM
Last Post: pepapoha
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,426 Oct-31-2020, 07:00 AM
Last Post: Earis
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,143 Sep-14-2020, 04:11 AM
Last Post: Deepan
  Syntax error in python code sample ErnestTBass 5 3,058 Aug-14-2020, 07:14 PM
Last Post: deanhystad
  Indentation Error With If Else Statement JoeDainton123 3 1,776 Aug-02-2020, 08:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020