Class method executes twice on being called once [on hold]
up vote
-3
down vote
favorite
Normally I can always find my answers on here, but this one seems to pop up here and there, but never gets resolved so here I go:
I was programming a basic Neural Network to be trained with Stochastic Gradient Descent, nothing fancy, most of it copy pasted from a tutorial to get the hang of it and go from there, just wanted to try it out. I recently watched a video about the Google Sheets API, so I wanted to show some stats on a spreadsheet.
Now to the actual problem: Somehow, toExcel()
executes twice, although its only called once
Here is my code (python 3.6.7):
import numpy as np
import random
import json
import loader as l
import os
import time
import math
import pickle
import datetime
import psutil
import gspread
from oauth2client.service_account import ServiceAccountCredentials
offset = [1, 7]
def loadNet(filename):
with open(savedir+filename+".pkl", "rb") as input:
net = pickle.load(input)
return net
class Network:
def __init__(self, layers):
self.num_layers = len(layers)
self.layers = layers
self.biases = [np.random.randn(y, 1) for y in layers[1:]]
self.weights = [np.random.randn(y, x) for x, y in zip(layers[:-1], layers[1:])]
def toExcel(self):
print("Function start")
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('auth_creds.json', scope)
client = gspread.authorize(creds)
sheet = client.open("nn_temp").sheet1
input = 0
x = 0
s = time.time()
for var in range(len(self.layers)):
if var == 0:
continue
index = var-1
sheet.update_cell(offset[1],offset[0]+x, self.layers[index+1])
w_cells = sheet.range(offset[1]+1,offset[0]+x,offset[1]+len(self.weights[index]),offset[0]+x)
for weight in range(len(self.weights[index])):
w_cells[weight].value = sum(self.weights[index][weight])/float(len(self.weights[index][weight]))
sheet.update_cells(w_cells)
b_cells = sheet.range(offset[1]+1,offset[0]+x+1,offset[1]+len(self.biases[index]),offset[0]+x+1)
for bias in range(len(self.biases[index])):
b_cells[bias].value = sum(self.biases[index][bias])/float(len(self.biases[index][bias]))
sheet.update_cells(b_cells)
del w_cells, b_cells
x += 2
print("time: "+str(time.time()-s))
del sheet,input,x,s,scope,creds,client
return
net = loadNet("93_22")
print("between") #not in the first result, i added this later
net.toExcel()
All I am doing with loadNet("93_22")
is loading a previously saved Network, having specific weights and biases
Console output:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
Function start
time: 5.690556049346924
Function start
time: 5.826641082763672
EDIT 1: I tried what @Silmathoron proposed and got this:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
between
Function start
time: 7.506024122238159
between
Function start
time: 6.050253868103027
EDIT 2: pickle is the problem, i tried this:
net = Network([784, 20, 20, 20, 10])
print("between")
net.toExcel()
and got a clean execution
between
Function start
time: 4.296467065811157
guess im gonna use google sheets to save my networks not too
python python-3.x python-3.6
put on hold as off-topic by user2357112, Martijn Pieters♦ Nov 21 at 20:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user2357112, Martijn Pieters
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 2 more comments
up vote
-3
down vote
favorite
Normally I can always find my answers on here, but this one seems to pop up here and there, but never gets resolved so here I go:
I was programming a basic Neural Network to be trained with Stochastic Gradient Descent, nothing fancy, most of it copy pasted from a tutorial to get the hang of it and go from there, just wanted to try it out. I recently watched a video about the Google Sheets API, so I wanted to show some stats on a spreadsheet.
Now to the actual problem: Somehow, toExcel()
executes twice, although its only called once
Here is my code (python 3.6.7):
import numpy as np
import random
import json
import loader as l
import os
import time
import math
import pickle
import datetime
import psutil
import gspread
from oauth2client.service_account import ServiceAccountCredentials
offset = [1, 7]
def loadNet(filename):
with open(savedir+filename+".pkl", "rb") as input:
net = pickle.load(input)
return net
class Network:
def __init__(self, layers):
self.num_layers = len(layers)
self.layers = layers
self.biases = [np.random.randn(y, 1) for y in layers[1:]]
self.weights = [np.random.randn(y, x) for x, y in zip(layers[:-1], layers[1:])]
def toExcel(self):
print("Function start")
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('auth_creds.json', scope)
client = gspread.authorize(creds)
sheet = client.open("nn_temp").sheet1
input = 0
x = 0
s = time.time()
for var in range(len(self.layers)):
if var == 0:
continue
index = var-1
sheet.update_cell(offset[1],offset[0]+x, self.layers[index+1])
w_cells = sheet.range(offset[1]+1,offset[0]+x,offset[1]+len(self.weights[index]),offset[0]+x)
for weight in range(len(self.weights[index])):
w_cells[weight].value = sum(self.weights[index][weight])/float(len(self.weights[index][weight]))
sheet.update_cells(w_cells)
b_cells = sheet.range(offset[1]+1,offset[0]+x+1,offset[1]+len(self.biases[index]),offset[0]+x+1)
for bias in range(len(self.biases[index])):
b_cells[bias].value = sum(self.biases[index][bias])/float(len(self.biases[index][bias]))
sheet.update_cells(b_cells)
del w_cells, b_cells
x += 2
print("time: "+str(time.time()-s))
del sheet,input,x,s,scope,creds,client
return
net = loadNet("93_22")
print("between") #not in the first result, i added this later
net.toExcel()
All I am doing with loadNet("93_22")
is loading a previously saved Network, having specific weights and biases
Console output:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
Function start
time: 5.690556049346924
Function start
time: 5.826641082763672
EDIT 1: I tried what @Silmathoron proposed and got this:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
between
Function start
time: 7.506024122238159
between
Function start
time: 6.050253868103027
EDIT 2: pickle is the problem, i tried this:
net = Network([784, 20, 20, 20, 10])
print("between")
net.toExcel()
and got a clean execution
between
Function start
time: 4.296467065811157
guess im gonna use google sheets to save my networks not too
python python-3.x python-3.6
put on hold as off-topic by user2357112, Martijn Pieters♦ Nov 21 at 20:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user2357112, Martijn Pieters
If this question can be reworded to fit the rules in the help center, please edit the question.
2
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
1
I smell a circular import, likely inloader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.
– user2357112
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something betweenloadNet
andnet.toExcel()
?
– Silmathoron
Nov 21 at 20:09
1
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entiretoExcel
body withprint('problem')
still demonstrates the double print, then replace the entiretoExcel
body withprint('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.
– user2357112
Nov 21 at 20:13
1
The solution (apart from giving us a complete but minimal working example), is to use aif __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.
– Martijn Pieters♦
Nov 21 at 20:36
|
show 2 more comments
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
Normally I can always find my answers on here, but this one seems to pop up here and there, but never gets resolved so here I go:
I was programming a basic Neural Network to be trained with Stochastic Gradient Descent, nothing fancy, most of it copy pasted from a tutorial to get the hang of it and go from there, just wanted to try it out. I recently watched a video about the Google Sheets API, so I wanted to show some stats on a spreadsheet.
Now to the actual problem: Somehow, toExcel()
executes twice, although its only called once
Here is my code (python 3.6.7):
import numpy as np
import random
import json
import loader as l
import os
import time
import math
import pickle
import datetime
import psutil
import gspread
from oauth2client.service_account import ServiceAccountCredentials
offset = [1, 7]
def loadNet(filename):
with open(savedir+filename+".pkl", "rb") as input:
net = pickle.load(input)
return net
class Network:
def __init__(self, layers):
self.num_layers = len(layers)
self.layers = layers
self.biases = [np.random.randn(y, 1) for y in layers[1:]]
self.weights = [np.random.randn(y, x) for x, y in zip(layers[:-1], layers[1:])]
def toExcel(self):
print("Function start")
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('auth_creds.json', scope)
client = gspread.authorize(creds)
sheet = client.open("nn_temp").sheet1
input = 0
x = 0
s = time.time()
for var in range(len(self.layers)):
if var == 0:
continue
index = var-1
sheet.update_cell(offset[1],offset[0]+x, self.layers[index+1])
w_cells = sheet.range(offset[1]+1,offset[0]+x,offset[1]+len(self.weights[index]),offset[0]+x)
for weight in range(len(self.weights[index])):
w_cells[weight].value = sum(self.weights[index][weight])/float(len(self.weights[index][weight]))
sheet.update_cells(w_cells)
b_cells = sheet.range(offset[1]+1,offset[0]+x+1,offset[1]+len(self.biases[index]),offset[0]+x+1)
for bias in range(len(self.biases[index])):
b_cells[bias].value = sum(self.biases[index][bias])/float(len(self.biases[index][bias]))
sheet.update_cells(b_cells)
del w_cells, b_cells
x += 2
print("time: "+str(time.time()-s))
del sheet,input,x,s,scope,creds,client
return
net = loadNet("93_22")
print("between") #not in the first result, i added this later
net.toExcel()
All I am doing with loadNet("93_22")
is loading a previously saved Network, having specific weights and biases
Console output:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
Function start
time: 5.690556049346924
Function start
time: 5.826641082763672
EDIT 1: I tried what @Silmathoron proposed and got this:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
between
Function start
time: 7.506024122238159
between
Function start
time: 6.050253868103027
EDIT 2: pickle is the problem, i tried this:
net = Network([784, 20, 20, 20, 10])
print("between")
net.toExcel()
and got a clean execution
between
Function start
time: 4.296467065811157
guess im gonna use google sheets to save my networks not too
python python-3.x python-3.6
Normally I can always find my answers on here, but this one seems to pop up here and there, but never gets resolved so here I go:
I was programming a basic Neural Network to be trained with Stochastic Gradient Descent, nothing fancy, most of it copy pasted from a tutorial to get the hang of it and go from there, just wanted to try it out. I recently watched a video about the Google Sheets API, so I wanted to show some stats on a spreadsheet.
Now to the actual problem: Somehow, toExcel()
executes twice, although its only called once
Here is my code (python 3.6.7):
import numpy as np
import random
import json
import loader as l
import os
import time
import math
import pickle
import datetime
import psutil
import gspread
from oauth2client.service_account import ServiceAccountCredentials
offset = [1, 7]
def loadNet(filename):
with open(savedir+filename+".pkl", "rb") as input:
net = pickle.load(input)
return net
class Network:
def __init__(self, layers):
self.num_layers = len(layers)
self.layers = layers
self.biases = [np.random.randn(y, 1) for y in layers[1:]]
self.weights = [np.random.randn(y, x) for x, y in zip(layers[:-1], layers[1:])]
def toExcel(self):
print("Function start")
scope = ['https://spreadsheets.google.com/feeds','https://www.googleapis.com/auth/drive']
creds = ServiceAccountCredentials.from_json_keyfile_name('auth_creds.json', scope)
client = gspread.authorize(creds)
sheet = client.open("nn_temp").sheet1
input = 0
x = 0
s = time.time()
for var in range(len(self.layers)):
if var == 0:
continue
index = var-1
sheet.update_cell(offset[1],offset[0]+x, self.layers[index+1])
w_cells = sheet.range(offset[1]+1,offset[0]+x,offset[1]+len(self.weights[index]),offset[0]+x)
for weight in range(len(self.weights[index])):
w_cells[weight].value = sum(self.weights[index][weight])/float(len(self.weights[index][weight]))
sheet.update_cells(w_cells)
b_cells = sheet.range(offset[1]+1,offset[0]+x+1,offset[1]+len(self.biases[index]),offset[0]+x+1)
for bias in range(len(self.biases[index])):
b_cells[bias].value = sum(self.biases[index][bias])/float(len(self.biases[index][bias]))
sheet.update_cells(b_cells)
del w_cells, b_cells
x += 2
print("time: "+str(time.time()-s))
del sheet,input,x,s,scope,creds,client
return
net = loadNet("93_22")
print("between") #not in the first result, i added this later
net.toExcel()
All I am doing with loadNet("93_22")
is loading a previously saved Network, having specific weights and biases
Console output:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
Function start
time: 5.690556049346924
Function start
time: 5.826641082763672
EDIT 1: I tried what @Silmathoron proposed and got this:
paul@Paul-2:~/proj/dl/stgd$ python3 net.py
between
Function start
time: 7.506024122238159
between
Function start
time: 6.050253868103027
EDIT 2: pickle is the problem, i tried this:
net = Network([784, 20, 20, 20, 10])
print("between")
net.toExcel()
and got a clean execution
between
Function start
time: 4.296467065811157
guess im gonna use google sheets to save my networks not too
python python-3.x python-3.6
python python-3.x python-3.6
edited Nov 21 at 20:14
asked Nov 21 at 20:05
RPaul
757
757
put on hold as off-topic by user2357112, Martijn Pieters♦ Nov 21 at 20:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user2357112, Martijn Pieters
If this question can be reworded to fit the rules in the help center, please edit the question.
put on hold as off-topic by user2357112, Martijn Pieters♦ Nov 21 at 20:32
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – user2357112, Martijn Pieters
If this question can be reworded to fit the rules in the help center, please edit the question.
2
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
1
I smell a circular import, likely inloader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.
– user2357112
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something betweenloadNet
andnet.toExcel()
?
– Silmathoron
Nov 21 at 20:09
1
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entiretoExcel
body withprint('problem')
still demonstrates the double print, then replace the entiretoExcel
body withprint('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.
– user2357112
Nov 21 at 20:13
1
The solution (apart from giving us a complete but minimal working example), is to use aif __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.
– Martijn Pieters♦
Nov 21 at 20:36
|
show 2 more comments
2
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
1
I smell a circular import, likely inloader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.
– user2357112
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something betweenloadNet
andnet.toExcel()
?
– Silmathoron
Nov 21 at 20:09
1
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entiretoExcel
body withprint('problem')
still demonstrates the double print, then replace the entiretoExcel
body withprint('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.
– user2357112
Nov 21 at 20:13
1
The solution (apart from giving us a complete but minimal working example), is to use aif __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.
– Martijn Pieters♦
Nov 21 at 20:36
2
2
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
1
1
I smell a circular import, likely in
loader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.– user2357112
Nov 21 at 20:09
I smell a circular import, likely in
loader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.– user2357112
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something between
loadNet
and net.toExcel()
?– Silmathoron
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something between
loadNet
and net.toExcel()
?– Silmathoron
Nov 21 at 20:09
1
1
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entire
toExcel
body with print('problem')
still demonstrates the double print, then replace the entire toExcel
body with print('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.– user2357112
Nov 21 at 20:13
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entire
toExcel
body with print('problem')
still demonstrates the double print, then replace the entire toExcel
body with print('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.– user2357112
Nov 21 at 20:13
1
1
The solution (apart from giving us a complete but minimal working example), is to use a
if __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.– Martijn Pieters♦
Nov 21 at 20:36
The solution (apart from giving us a complete but minimal working example), is to use a
if __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.– Martijn Pieters♦
Nov 21 at 20:36
|
show 2 more comments
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
2
just too much code. please create a Minimal, Complete, and Verifiable example
– Jean-François Fabre
Nov 21 at 20:06
1
I smell a circular import, likely in
loader
or as a result of loading the pickle. We'd need an MCVE to diagnose the problem for sure. There's too much stuff we don't need and not enough of the stuff we do.– user2357112
Nov 21 at 20:09
I have no idea what's going on nor why, but just out of curiosity, did you try printing self in the function and printing something between
loadNet
andnet.toExcel()
?– Silmathoron
Nov 21 at 20:09
1
Quick guide to MCVEs: we need to be able to run it, and we need to be able to see the problem when we run it, but it doesn't have to preserve the actual functionality of your program. If replacing the entire
toExcel
body withprint('problem')
still demonstrates the double print, then replace the entiretoExcel
body withprint('problem')
. If your program currently relies on a pickle we don't have, try to remove the pickle reliance, or make the pickle creation part of the program.– user2357112
Nov 21 at 20:13
1
The solution (apart from giving us a complete but minimal working example), is to use a
if __name__ == '__main__':
guard to run the top-level code only when you use this as a script, not when imported as a module.– Martijn Pieters♦
Nov 21 at 20:36