Scipy.optimise differential evolution made NPV value lower than original value. Is this normal? - numpy

Ran a scipy.optimise with differential evolution on my script below and got a result that says my optimal value is lower than my original value. Is this normal?
enter image description here
Have posted all my code here for any answers that may help me.
# from centralUncertainConstants import *
import pandas as pd
import numpy as np
from statistics import NormalDist
import random
import math
from scipy.stats import norm
import matplotlib.pyplot as plt;
demandyr0 = 0.0240232711111154
MDemLimit = 1.2042811301817
aTransParam = 49.1297731108936
bsharpParam = 0.20118446680713
annVol = 0.15
volyr0 = 0.5
volM = 0.5
volb = 0.7
scaleFactor = 1468000
BlueH2MarketShare = 0.4
SFMarketShare = 0.18
thou = 1000
mill = 1000000
daysyear = 365
cons1 = 8760
cons2 = 16.92
plantDesignCap = 190950;
plantOperationalCap = 0.95;
CO2emissionRate = 1.06; # Kg of CO2 per Kg of H2
CO2captureRate = 8.60; # Kg of CO2 per Kg of H2
CO2emissionRatefeedstock = 0.28;
CO2emissionRatefuel = 1.18;
NatGasConsumption = 33411;
plantDesignCapBase = 190950;
plantDesignCapScaled = 190000;
lifetime = 25
workingHours = 8322
dailyprodrate = 190000;
yearlyprodrate = dailyprodrate * plantOperationalCap * 365/1000;
discountrate = 0.1; # Nine percent per annum
th = 25
statetax = 0.0725;
fedtax = 0.21;
#Hprice stuff
Hpricelow = 8
Hpricehigh = 15
Hpriceave = 11.5
#H2 delivery cost stuff
H2dpave = 1.07
H2dplow = H2dpave*(1+0.3)
H2dohigh = H2dpave*(1-0.3)
#CO2 transport storage (ts) stuff
CO2low = 10
CO2high = 30
CO2ave = 15
capInvestment = 591730751/1000000
basecostFF = 67836966;
basecostWM = 104434;
basecostCC = 625712;
#CO2 gas prices constants
drift = 0.00234
volatility = 0.19
CO2captradeprice = 29.15
initialCapCostsFixed = 591.73
initialCapCostsPhased = 230.53
#Other Phased stuff
iniProdRateDay = 47500
iniProdRateYear = 16470.63
expansionTimes = 3
expansionIncrement = 16470.63
expansionThresh = 0.75
reductionDuringExpansion = 0.5
dict = {
16470.63: 6.59,
32941.26: 10.56,
49411.89: 13.91,
65882.52: 16.92,
#82353.13: 19.69
}
dict2 = {
16470.63: 137.12,
32941.26: 137.12,
49411.89: 137.12,
65882.52: 137.12,
}
#For plots using matplotlib
a = 49.1298;
b = 0.2012;
demand = 1468000; # raw data demand in 2050 from source. required to calc demandCAL
MACRSfixed = [22,43,40,37,34,31,29,27,26,26,26,26,26,26,26,26,26,26,26,26,13,0,0,0,0]
MACRSphased = [10,19,18,16,15,19,23,21,20,25,29,28,27,31,35,34,33,32,31,31,24,18,18,18,18]
MACRSflexible = [10,19,18,16,15,14,13,12,12,17,22,21,20,25,29,33,36,35,34,33,26,19,18,18,18]
q = [46.96, 50, 54.25, 58.86, 63.86, 69.29, 75.18, 81.57, 88.51, 96.03, 104.19, 113.05, 122.66, 133.08, 144.40, 156.67, 169.99, 184.44, 200.11, 217.12, 235.58, 255.60, 277.33, 300.90, 326.48]
nomyear = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
nomyear2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
nums = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
year = [2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050]
ngprices = [130.48,109.52,118.95,225.84,207.50,177.11,286.63,308.64,455.36,352.65,365.23,464.26,206.46,228.99,209.60,144.10,195.45,228.99,137.29,132.05,156.68,165.06,134.14,106.37,203.84]
elecprices = [7.27,6.67,6.81,6.92,6.88,6.76,6.91,7.10,6.89,6.67,6.82,6.77,6.83,6.96,6.39,6.16,5.73,5.25,5.11,4.88,5.05,4.64,4.43,4.48,4.53]
def test(help):
yearlyprodrate = help[0]
def npv():
availableRate = pd.Series(index=nums, dtype='float64')
finalprodrate = pd.Series(index=nums, dtype='float64')
ProdCO2emissions = pd.Series(index=nums, dtype='float64')
CO2capture = pd.Series(index=nums, dtype='float64')
upstreamCO2emissions = pd.Series(index=nums, dtype='float64')
revenue = pd.Series(index=nums, dtype='float64')
fixedOPEX = pd.Series(index=nums, dtype='float64')
varOPEXcost = pd.Series(index=nums, dtype='float64')
OpCost = pd.Series(index=nums, dtype='float64')
Depreciation = pd.Series(index=nums, dtype='float64')
# expansionInvestment = pd.Series(index=nums, dtype='float64')
# expansionInvestment2 = pd.Series(index=nums, dtype='float64')
demandprojection = pd.Series(index=nomyear2, dtype='float64').array
normDemProjGrowth = pd.Series(index=nums, dtype='float64')
randomDraw = pd.Series(index=nums, dtype='float64')
realisedGrowth = pd.Series(index=nums, dtype='float64')
realisedNormalisedDemand = pd.Series(index=nums, dtype='float64')
realisedDemand = pd.Series(index=nums, dtype='float64')
demandSF = pd.Series(index=nums, dtype='float64')
sv = 0
cf = pd.Series(index=nums, dtype='float64')
dcf = pd.Series(index=nums, dtype='float64')
npv1 = 0
npv = 0
# Demand Projection
realisedDemandyr0 = (1-volyr0)*(demandyr0)+2*demandyr0*volyr0*random.uniform(0,1)
stoM = (1-volM)*MDemLimit+2*volM*MDemLimit*random.uniform(0,1)
stoa = stoM/realisedDemandyr0-1
stob = (1-volb)*bsharpParam+2*volb*bsharpParam*random.uniform(0,1)
for i in range(0,len(nomyear2)):
demandprojection[i] = stoM/(1+stoa*np.exp(-nomyear2[i]*stob))
for i in range(0,len(nomyear2)-1):
normDemProjGrowth[i] = (demandprojection[i+1]-demandprojection[i])/demandprojection[i]
for i in range(0,len(nomyear2)):
randomDraw[i] = NormalDist(mu=0, sigma=1).inv_cdf(random.uniform(0,1))
for i in range(0, len(nomyear2)-1):
realisedGrowth[i] = normDemProjGrowth[i]+randomDraw[i+1]*annVol
for i in range(0, len(nomyear2)-1):
realisedNormalisedDemand[i] = demandprojection[i+1]*(1+realisedGrowth[i])
for i in range(0, len(nomyear2)-1):
realisedDemand[i] = realisedNormalisedDemand[i]*(scaleFactor*BlueH2MarketShare*SFMarketShare)
demandSF = realisedDemand[0:th]
for i in range(0, th):
availableRate[i] = yearlyprodrate
# finalprodrate = []
for i in range(0, th):
finalprodrate[i] = min(demandSF[i], availableRate[i])
# for i, v in enumerate(demandSF):
# finalprodrate.append(min(v, availableRate[i]))
# if i < yearlyprodrate:
# finalprodrate.append(i)
# else:
# finalprodrate.append(yearlyprodrate)
# ProdCO2emissions = [];
# CO2capture = []
# upstreamCO2emissions = [];
# for j in finalprodrate:
# ems = CO2emissionRate*(j*1000)/1000
# capture = CO2captureRate*(j*1000)/1000
# upstream = (CO2emissionRatefeedstock/1000)*(NatGasConsumption*8760)*(j/(plantDesignCapBase*plantOperationalCap*365/1000))
# ProdCO2emissions.append(ems)
# CO2capture.append(capture)
# upstreamCO2emissions.append(upstream)
for i in range(0, th):
ProdCO2emissions[i] = CO2emissionRate*(finalprodrate[i]*thou)/thou
CO2capture[i] = CO2captureRate*(finalprodrate[i]*thou)/thou
upstreamCO2emissions[i] = (CO2emissionRatefeedstock/thou)*(NatGasConsumption*cons1)*(finalprodrate[i]/(plantDesignCap*plantOperationalCap*daysyear/thou))
#Hydrogen price
lowModeHighMode = (Hpriceave-Hpricelow)/(Hpricehigh-Hpricelow)
randomDraw2 = random.uniform(0, 1)
def findHprice():
if randomDraw2 < lowModeHighMode:
Hprice = Hpricelow+math.sqrt((Hpriceave-Hpricelow)*(Hpricehigh-Hpricelow)*randomDraw2)
else:
Hprice = Hpricehigh-math.sqrt((Hpricehigh-Hpricelow)*(Hpricehigh-Hpriceave)*(1-randomDraw2))
return Hprice
lowModeHighMode2 = (H2dpave-H2dplow)/(H2dohigh-H2dpave)
def findH2deliveryprice():
if randomDraw2 < lowModeHighMode2:
H2dprice = H2dplow+math.sqrt((H2dpave-H2dplow)*(H2dohigh-H2dplow)*randomDraw2)
else:
H2dprice = H2dohigh-math.sqrt((H2dohigh-H2dplow)*(H2dohigh-H2dpave)*(1-randomDraw2))
return H2dprice
lowModeHighMode3 = (CO2ave-CO2low)/(CO2high-CO2ave)
def priceCO2TS():
if randomDraw2 < lowModeHighMode3:
CO2ts = CO2low+math.sqrt((CO2ave-CO2low)*(CO2high-CO2low)*randomDraw2)
else:
CO2ts = CO2high-math.sqrt((CO2high-CO2low)*(CO2high-CO2low)*(1-randomDraw2))
return CO2ts
#Revenues
#natural gas price
result = [];
for i in range(1000): #1000
result.append(random.choices(ngprices, weights=None, cum_weights=None, k = 25)) #k=25
samplemean = [];
for i in result:
samplemean.append(np.mean(i))
totalmean = [];
totalmean.append(np.mean(samplemean))
totalst = []
totalst.append(np.std(samplemean))
ngprice = norm.ppf(random.uniform(0,1), totalmean, totalst)
# rev = []
for i in range(0,th):
revenue[i] = findHprice()*(finalprodrate[i]*thou)/mill
# for j in finalprodrate:
# rev.append(findHprice()*(j*1000)/1000000);
# NG = [];
# WM = [];
# CC = [];
# CO2tax = [];
# CO2TS = [];
# H2delivery = [];
# fixedOPEX = [];
NG = pd.Series(index=nums, dtype='float64')
WM = pd.Series(index=nums, dtype='float64')
CC = pd.Series(index=nums, dtype='float64')
CO2tax = pd.Series(index=nums, dtype='float64')
CO2TS = pd.Series(index=nums, dtype='float64')
H2delivery = pd.Series(index=nums, dtype='float64')
CO2tax = pd.Series(index=nums, dtype='float64')
CO2TS = pd.Series(index=nums, dtype='float64')
for i in range(0,th):
NG[i] = (NatGasConsumption*workingHours)/thou*ngprice[0]*(finalprodrate[i]/(plantDesignCapScaled*plantOperationalCap*daysyear/thou))/mill
WM[i] = basecostWM*(finalprodrate[i]/(plantDesignCapBase*plantOperationalCap*daysyear/thou))/mill
CC[i] = basecostCC*(finalprodrate[i]/(plantDesignCapBase*plantOperationalCap*daysyear/thou))/mill
H2delivery[i] = findH2deliveryprice()*(finalprodrate[i]*thou)/mill
fixedOPEX[i] = cons2
# for j in finalprodrate:
# #NG.append(basecostFF*(j/(plantDesignCap*plantOperationalCap*365/1000))/1000000);
# NG.append((NatGasConsumption*workingHours)/1000*ngpricedraw[0]*(j/(plantDesignCapScaled*plantOperationalCap*365/1000))/1000000);
# WM.append(basecostWM*(j/(plantDesignCapBase*plantOperationalCap*365/1000))/1000000);
# CC.append(basecostCC*(j/(plantDesignCapBase*plantOperationalCap*365/1000))/1000000);
# H2delivery.append(findH2deliveryprice()*(j*1000)/1000000)
# fixedOPEX.append(cons2);
# print(NG)
#finding CO2 price
result2 = []
for i in range(108): #108
result2.append(drift+(norm.ppf(random.uniform(0,1), 0, 1))*volatility)
CO2tradeprices = []
CO2tradeprices.append(CO2captradeprice)
for i, v in enumerate(result2):
CO2tradeprices.append(CO2tradeprices[i]*(1+result2[i]))
result3 = []
for i in range(12, 16):
result3.append(CO2tradeprices[i])
CO2price = np.mean(result3)
for i in range(0,th):
CO2tax[i] = CO2price*ProdCO2emissions[i]/mill
CO2TS[i] = priceCO2TS()*CO2capture[i]/mill
# for k in ProdCO2emissions:
# #CO2tax.append(22.43*k/1000000)
# CO2tax.append(CO2price*k/mill)
# for l in CO2capture:
# #CO2TS.append(15*l/1000000)
# CO2TS.append(priceCO2TS()*l/mill)
temp1 = np.add(NG, WM);
temp2 = np.add(CC, CO2tax)
temp3 = np.add(CO2TS, H2delivery)
temp4 = np.add(temp1, temp2)
varOPEXcost = np.add(temp4, temp3)
# for i in NG:
# varOPEXcost.append(NG[i] + WM[i])
OpCost = np.add(fixedOPEX, varOPEXcost);
#Depreciation
# MACRSfixed = df.iloc[2:27, 1].array
# Depreciation = MACRSfixed*(statetax+fedtax);
for i in range(0, th):
Depreciation[i] = MACRSfixed[i]*(statetax+fedtax);
#Salvage Value + decommissioning
#sv = df.iloc[2:27, 3].array; #capInvestment - sum(Depreciation)
# sv = initialCapCostsFixed - sum(Depreciation)
# decom = []
# for i in range(24):
# decom.append(0)
# decom.append(sv)
decom = pd.Series(index=nums, dtype='float64')
for i in range(0, th):
if i <=24:
decom[i] = 0
else:
decom[i] = sv
#45Q tax credit
# tc = []
# q = df.iloc[2:27, 2].array #tax credit data from csv file
# tc = [];
# for i, m in enumerate(CO2capture):
# if m >= 100000:
# tc.append(q[i] * m/1000000)
# else:
# tc.append(0)
tc = pd.Series(index=nums, dtype='float64')
for i in range(0, th):
if CO2capture[i] >= 100000:
tc[i] = q[i] * CO2capture[i]/mill
else:
tc[i] = 0
#cashflow
# cf = (revenue-OpCost)*(1-statetax-fedtax)+Depreciation+tc
#Discounted Cashflow
# nomyear = df.iloc[2:27, 4].array
# dcf = cf/(1+discountrate)**nomyear
#npv
# npv = dcf.sum()-initialCapCostsFixed
for i in range(0, th):
cf[i] = (revenue[i]-OpCost[i])*(1-statetax-fedtax)+Depreciation[i]+tc[i]
dcf[i] = cf[i]/(1+discountrate)**nomyear[i]
npv1 += dcf[i]
npv = npv1 - initialCapCostsFixed
return -npv #should this be negative and if so should the counter and enpv values also be negative?
counter = 0
for i in range(0, 2000): #2000 runs
counter += npv()
enpv = counter/2000 #2000 runs
return enpv
print(test([65882.50]))
from scipy.optimize import differential_evolution
from centralised.centralFixedUncertain import *
# Define the bounds
bounds = [(0, 60000)] #centralised Fixed Certain and Uncertain
# Define the genetic algorithm
# result = differential_evolution(test, bounds)
# # Print the results
# print("Optimal solution: ", result.x)
# print("Objective function value: ", result.fun)
if __name__ == "__main__":
... # Prepare all the arguments
# result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
# disp=True, polish=False, updating='deferred', workers=-1)
result = differential_evolution(test, bounds, workers=-1)
print("Optimal solution: ", result.x)
print("Objective function value: ", result.fun)
In 2nd code block, set workers = -1 to utilise all cores but scipy always runs on the CPU. If any answers can help me force the apple silicon GPU or neural engine to run the process instead it would be greatly appreciated. Am using base model M1 mac mini version with 8 cores CPU.

Related

How to use M1 GPU or neural engine to decrease time taken for scipy.optimize.differential_evolution?

Trying to use scipy.optimise.differential_evolution to optimise my code.
I set workers = -1 in my geneticalgo.py file to utilize all cores, and it always makes the CPU cores do all the work. Is there a way to force the GPU or neural engine to run the optimisation task instead? My last run where I got a result took just over 9 hours.
1st code block is the script I'm writing that needs to be optimised
2nd code block imports the code from the 1st and runs the differential_evolution on it.
Any answers appreciated!
# from decentralUncertainConstants import *
import pandas as pd
import numpy as np
from statistics import NormalDist
import random
import math
from scipy.stats import norm
import matplotlib.pyplot as plt;
demandyr0 = 0.0240232711111154
MDemLimit = 1.2042811301817
aTransParam = 49.1297731108936
bsharpParam = 0.20118446680713
annVol = 0.15
volyr0 = 0.5
volM = 0.5
volb = 0.7
scaleFactor = 1468000
BlueH2MarketShare = 0.4
SFMarketShare = 0.18
thou = 1000
mill = 1000000
daysyear = 365
cons1 = 8760
cons2 = 16.92
plantDesignCap = 190950;
plantsbuilt = 35
plantOperationalCap = 0.95;
CO2emissionRate = 1.06; # Kg of CO2 per Kg of H2
CO2captureRate = 8.60; # Kg of CO2 per Kg of H2
CO2emissionRatefeedstock = 0.28;
CO2emissionRatefuel = 1.18;
NatGasConsumption = 33411;
plantDesignCapBase = 190950;
plantDesignCapScaled = 190000;
lifetime = 25
workingHours = 8322
dailyprodrate = 190000;
yearlyprodrate = 65894.92
endcapreq = 65882.50;
singleModProdRate = 470.68
plantfixedoperationalcosts = 1.09
plantextensioncost = 0.54
discountrate = 0.1; # Nine percent per annum
th = 25
statetax = 0.0725;
fedtax = 0.21;
#Hprice stuff
Hpricelow = 8
Hpricehigh = 15
Hpriceave = 11.5
#H2 delivery cost stuff
H2dpave = 1.07
H2dplow = H2dpave*(1+0.3)
H2dohigh = H2dpave*(1-0.3)
#CO2 transport storage (ts) stuff
CO2low = 10
CO2high = 30
CO2ave = 15
#emissions
CO2emissionsCH4 = 0.185
CO2emissionrate = 15903257.28
CO2captureeff = 0.9
CO2captured = 14312931.55
annualCO2emissions = 1590325.73
#CO2 transport storage (ts) stuff
CO2low = 10
CO2high = 30
CO2ave = 15
#CO2 storage and compression (sc) stuff
CO2schigh = 50*(6000/500)**0.6
CO2sclow = CO2schigh*0.7
CO2scave = CO2schigh*1.3
#process specs
ngusage = 0.155797012;
ngusageannual = 85963552.88;
elecusage = 1.11;
industrialelec = 0.061
waterusage = 5.77;
processwater = 0.0024;
modsbuilt = 140
plantsbuilt = 35
modcapex = 1.57
setupcost = 3.24
capInvestment = 591730751/1000000
basecostFF = 67836966;
basecostWM = 104434;
basecostCC = 625712;
#process specs
ngusage = 0.155797012;
ngusageannual = 85963552.88;
elecusage = 1.11;
industrialelec = 0.061
waterusage = 5.77;
#conversion factors
btutokwh = 293.07;
ngtobtu = 58.36;
#CO2 gas prices constants
drift = 0.00234
volatility = 0.19
CO2captradeprice = 29.15
initialCapCostsFixed = 591.73
initialCapCostsPhased = 230.53
#Other Phased stuff
iniProdRateDay = 47500
iniProdRateYear = 16473.73
modsPerPlan = 4
plantDowntime = 0.15
iniMods = 35
expansionTimes = 3
expansionIncrement = 16470.63
expansionThresh = 0.75
reductionDuringExpansion = 0.5
othervariableoperating = 1800;
othervariableoperating2 = 5.32;
iniplantsbuilt = 9
#helpers to find CO2 prices
interval = 4
MACRSfixed = [22,43,40,37,34,31,29,27,26,26,26,26,26,26,26,26,26,26,26,26,13,0,0,0,0]
MACRSphased = [10,19,18,16,15,19,23,21,20,25,29,28,27,31,35,34,33,32,31,31,24,18,18,18,18]
MACRSflexible = [10,19,18,16,15,14,13,12,12,17,22,21,20,25,29,33,36,35,34,33,26,19,18,18,18]
q = [46.96, 50, 54.25, 58.86, 63.86, 69.29, 75.18, 81.57, 88.51, 96.03, 104.19, 113.05, 122.66, 133.08, 144.40, 156.67, 169.99, 184.44, 200.11, 217.12, 235.58, 255.60, 277.33, 300.90, 326.48]
nomyear = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]
nomyear2 = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
nums = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
year = [2023, 2024, 2025, 2026, 2027, 2028, 2029, 2030, 2031, 2032, 2033, 2034, 2035, 2036, 2037, 2038, 2039, 2040, 2041, 2042, 2043, 2044, 2045, 2046, 2047, 2048, 2049, 2050]
ngprices = [130.48,109.52,118.95,225.84,207.50,177.11,286.63,308.64,455.36,352.65,365.23,464.26,206.46,228.99,209.60,144.10,195.45,228.99,137.29,132.05,156.68,165.06,134.14,106.37,203.84]
elecprices = [0.0727,0.0667,0.0681,0.0692,0.0688,0.0676,0.0691,0.0710,0.0689,0.0667,0.0682,0.0677,0.0683,0.0696,0.0639,0.0616,0.0573,0.0525,0.0511,0.0488,0.0505,0.0464,0.0443,0.0448,0.0453]
expansionChange = [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0]
modulesbuilt = [0,0,0,0,35,0,0,0,35,0,0,0,35,0,0,0,0,0,0,0,0,0,0,0,0] #years 1-25
plantsbuilt = [9,9,9,9,18,18,18,18,27,27,27,27,35,35,35,35,35,35,35,35,35,35,35,35,35] #years 1-25
def test(help):
iniMods = help[0]
iniProdRateYear = help[1]
plantextensioncost = help[2]
endcapreq = help[3]
singleModProdRate = help[4]
def npv():
# demandCAL = pd.Series(index=nums, dtype='float64')
demandprojection = pd.Series(index=nomyear2, dtype='float64').array
normDemProjGrowth = pd.Series(index=nums, dtype='float64')
randomDraw = pd.Series(index=nums, dtype='float64')
realisedGrowth = pd.Series(index=nums, dtype='float64')
realisedNormalisedDemand = pd.Series(index=nums, dtype='float64')
realisedDemand = pd.Series(index=nums, dtype='float64')
demandSF = pd.Series(index=nums, dtype='float64')
availableRate = pd.Series(index=nums, dtype='float64')
H2AddedProdRate = pd.Series(index=nums, dtype='float64')
H2ReducedOutput = pd.Series(index=nums, dtype='float64')
dsbalance = pd.Series(index=nums, dtype='float64')
H2prodrate = pd.Series(index=nums, dtype='float64')
CO2emissions = pd.Series(index=nums, dtype='float64')
CO2capture = pd.Series(index=nums, dtype='float64')
modcapexrange = pd.Series(index=nums, dtype='float64')
plantcapexrange = pd.Series(index=nums, dtype='float64')
tcirange = pd.Series(index=nums, dtype='float64')
CO2prices = pd.Series(index=nums, dtype='float64')
CO2capture = pd.Series(index=nums, dtype='float64')
revenue = pd.Series(index=nums, dtype='float64')
fixedcost = pd.Series(index=nums, dtype='float64')
varcosts = pd.Series(index=nums, dtype='float64')
opcosts = pd.Series(index=nums, dtype='float64')
Depreciation = pd.Series(index=nums, dtype='float64')
plantsbuiltcounter = 0
sv = 0
cf = pd.Series(index=nums, dtype='float64')
dcf = pd.Series(index=nums, dtype='float64')
npv1 = 0
npv = 0
realisedDemandyr0 = (1-volyr0)*(demandyr0)+2*demandyr0*volyr0*random.uniform(0,1)
stoM = (1-volM)*MDemLimit+2*volM*MDemLimit*random.uniform(0,1)
stoa = stoM/realisedDemandyr0-1
stob = (1-volb)*bsharpParam+2*volb*bsharpParam*random.uniform(0,1)
for i in range(0,len(nomyear2)):
demandprojection[i] = stoM/(1+stoa*np.exp(-nomyear2[i]*stob))
for i in range(0,len(nomyear2)-1):
normDemProjGrowth[i] = (demandprojection[i+1]-demandprojection[i])/demandprojection[i]
for i in range(0,len(nomyear2)):
randomDraw[i] = NormalDist(mu=0, sigma=1).inv_cdf(random.uniform(0,1))
for i in range(0, len(nomyear2)-1):
realisedGrowth[i] = normDemProjGrowth[i]+randomDraw[i+1]*annVol
for i in range(0, len(nomyear2)-1):
realisedNormalisedDemand[i] = demandprojection[i+1]*(1+realisedGrowth[i])
for i in range(0, len(nomyear2)-1):
realisedDemand[i] = realisedNormalisedDemand[i]*(scaleFactor*BlueH2MarketShare*SFMarketShare)
demandSF = realisedDemand[0:th]
#Available Rate
for i in range(0, th-1):
availableRate[0] = iniProdRateYear
if expansionChange[i] == 1:
# availableRate[i+1] = expansionIncrement + availableRate[i]
availableRate[i+1] = (singleModProdRate*modulesbuilt[i+1]) + availableRate[i]
elif expansionChange[i] == 0:
availableRate[i+1] = availableRate[i]
for i in range(0,th):
dsbalance[i] = min(demandSF[i], endcapreq)-availableRate[i]
#Plants built
#Plant Output
for i in range(0,th):
H2prodrate[i] = min(demandSF[i], availableRate[i])
for i in range(0,th):
CO2emissions[i] = (ngusage*btutokwh*CO2emissionsCH4)*(1-CO2captureeff)*(H2prodrate[i]*thou)/thou
CO2capture[i] = (ngusage*btutokwh*CO2emissionsCH4)*(CO2captureeff)*(CO2emissions[i]*thou)/thou
#Hydrogen price
lowModeHighMode = (Hpriceave-Hpricelow)/(Hpricehigh-Hpricelow)
randomDraw2 = random.uniform(0, 1)
def findHprice():
if randomDraw2 < lowModeHighMode:
Hprice = Hpricelow+math.sqrt((Hpriceave-Hpricelow)*(Hpricehigh-Hpricelow)*randomDraw2)
else:
Hprice = Hpricehigh-math.sqrt((Hpricehigh-Hpricelow)*(Hpricehigh-Hpriceave)*(1-randomDraw2))
return Hprice
#Revenues
for i in range(0,th):
revenue[i] = findHprice()*(H2prodrate[i]*thou)/mill
#Total capital investment
modulecapex = modcapex * iniMods
plantcapex = iniplantsbuilt * setupcost
tci = modulecapex+plantcapex
for i in range(0,th):
modcapexrange[i] = modulesbuilt[i]*modcapex
for i in range(0,th):
if plantsbuilt[i] > plantsbuilt[i-1]:
plantcapexrange[i] = (plantsbuilt[i] - plantsbuilt[i-1])*setupcost
else:
plantcapexrange[i] = 0
for i in range(0, th):
tcirange[i] = plantcapexrange[i] + modcapexrange[i]
#Operational costs
#fixed costs
for i in range(0,th):
if nomyear[i] == 20:
fixedcost[i] = (plantfixedoperationalcosts+plantextensioncost)*plantsbuilt[i]
else:
fixedcost[i] = plantfixedoperationalcosts*plantsbuilt[i]
#CO2 prices for transport storage
lowModeHighMode3 = (CO2ave-CO2low)/(CO2high-CO2ave)
def priceCO2TS():
if randomDraw2 < lowModeHighMode3:
CO2ts = CO2low+math.sqrt((CO2ave-CO2low)*(CO2high-CO2low)*randomDraw2)
else:
CO2ts = CO2high-math.sqrt((CO2high-CO2low)*(CO2high-CO2low)*(1-randomDraw2))
return CO2ts
#CO2 prices for storage and compression
lowModeHighMode4 = (CO2scave-CO2sclow)/(CO2schigh-CO2scave)
def priceCO2SC():
if randomDraw2 < lowModeHighMode4:
CO2sc = CO2sclow+math.sqrt((CO2scave-CO2sclow)*(CO2schigh-CO2sclow)*randomDraw2)
else:
CO2sc = CO2schigh-math.sqrt((CO2schigh-CO2sclow)*(CO2schigh-CO2sclow)*(1-randomDraw2))
return CO2sc
#natural gas price
result = [];
for i in range(1000): #1000
result.append(random.choices(ngprices, weights=None, cum_weights=None, k = 25)) #k=25
samplemean = [];
for i in result:
samplemean.append(np.mean(i))
totalmean = [];
totalmean.append(np.mean(samplemean))
totalst = []
totalst.append(np.std(samplemean))
ngprice = norm.ppf(random.uniform(0,1), totalmean, totalst)
#Electricity price
result = [];
for i in range(1000): #1000
result.append(random.choices(elecprices, weights=None, cum_weights=None, k = 25)) #k=25
samplemean = [];
for i in result:
samplemean.append(np.mean(i))
totalmean = [];
totalmean.append(np.mean(samplemean))
totalst = []
totalst.append(np.std(samplemean))
elecprice = norm.ppf(random.uniform(0,1), totalmean, totalst)
#finding CO2 price
result2 = []
for i in range(111): #108
result2.append(drift+(norm.ppf(random.uniform(0,1), 0, 1))*volatility)
CO2tradeprices = []
CO2tradeprices.append(CO2captradeprice)
for i, v in enumerate(result2):
CO2tradeprices.append(CO2tradeprices[i]*(1+result2[i]))
result3 = [] #array used to get average price from 4 values from each year
for i in range(11, 111):
result3.append(CO2tradeprices[i])
for i in range(0, th):
CO2prices[i] = np.mean(result3[i:i+interval])
#variable costs
ng = pd.Series(index=nums, dtype='float64'); #natural gas
elec = pd.Series(index=nums, dtype='float64'); #electricity
ts = pd.Series(index=nums, dtype='float64'); #transport and storage
pw = pd.Series(index=nums, dtype='float64'); #process water
tax = pd.Series(index=nums, dtype='float64'); #CO2 tax
cc = pd.Series(index=nums, dtype='float64'); #CO2 capture and compression
ovoc = pd.Series(index=nums, dtype='float64'); #other variable operating costs
for i in range(0,th):
ng[i] = ngusage/ngtobtu*ngprice[0]*(H2prodrate[i]*thou)/mill
elec[i] = elecusage*(H2prodrate[i]*thou)*elecprice[0]/mill
pw[i] = waterusage*processwater*(H2prodrate[i]*thou)/mill
ovoc[i] = (othervariableoperating*othervariableoperating2)*plantsbuilt[i]/mill
for i in range(0,th):
ts[i] = CO2capture[i]*priceCO2TS()/mill
cc[i] = priceCO2SC()*CO2capture[i]/mill
tax[i] = CO2prices[i]*CO2emissions[i]/mill
for i in range(0,th):
varcosts[i] = ng[i] + elec[i] + pw[i] + ovoc[i] + ts[i] + cc[i] + tax[i]
for i in range(0,th):
opcosts[i] = fixedcost[i] + varcosts[i]
#Depreciation
for i in range(0, th):
Depreciation[i] = MACRSphased[i]*(statetax+fedtax);
sv = tci - sum(Depreciation)
decom = pd.Series(index=nums, dtype='float64')
for i in range(0, th):
if i <=24:
decom[i] = 0
else:
decom[i] = sv
#Cashflow
for i in range(0, th):
cf[i] = (revenue[i]-opcosts[i])*(1-statetax-fedtax)+Depreciation[i]
dcf[i] = (cf[i]-tcirange[i])/(1+discountrate)**nomyear[i]
npv = sum(dcf[:-5]) - tci
return -npv
counter = 0
for i in range(0, 2000): #2000 runs
counter += npv()
enpv = counter/2000 #2000 runs
return enpv
print(test([35, 16473.73, 0.54, 65882.50, 470.68]))
from scipy.optimize import differential_evolution
from decentralised.decentralPhased import *
if __name__ == "__main__":
... # Prepare all the arguments
# result = scipy.optimize.differential_evolution(minimize_me, bounds=function_bounds, args=extraargs,
# disp=True, polish=False, updating='deferred', workers=-1)
result = differential_evolution(test, bounds, workers=-1)
print("Optimal solution: ", result.x)
print("Objective function value: ", result.fun)
In 2nd code block, I set workers = -1 to utilise all cores but the scipy always defaults to the CPU cores which is only 8 on my machine (base model M1 mac mini). Is there a way to force the process to run on the apple silicon GPU or neural engine?

What is wrong with this IDLE python code?

I ran the code on this site in IDLE python https://sites.google.com/view/dailycoding00/main/%EC%95%8C%ED%8C%8C%EC%98%A4-%ED%95%99%EC%8A%B5
but it had an error
Traceback (most recent call last):
File "C:\Windows\system32\python", line 343, in <module>
ex = MyApp()
File "C:\Windows\system32\python", line 16, in __init__
self.initUI()
File "C:\Windows\system32\python", line 38, in initUI
self.board_cv2 = cv2.cvtColor(board_cv2, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-xr4y3u3_\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
what does it mean?
code is
import sys
import numpy as np
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt
import cv2
import time
import tensorflow as tf
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
load_model_path = 'model/model.ckpt'
#
board_size = 15
self.game_end = 0
self.board_size = board_size
self.board = np.zeros([board_size,board_size])
self.board_history = np.zeros([board_size,board_size])
self.cnt = 1
time_now = time.gmtime(time.time())
self.save_name = str(time_now.tm_year) + '_' + str(time_now.tm_mon) + '_' + str(time_now.tm_mday) + '_' + str(time_now.tm_hour) + '_' + str(time_now.tm_min) + '_' + str(time_now.tm_sec) + '.txt'
self.save_name_png = str(time_now.tm_year) + '_' + str(time_now.tm_mon) + '_' + str(time_now.tm_mday) + '_' + str(time_now.tm_hour) + '_' + str(time_now.tm_min) + '_' + str(time_now.tm_sec) + '.png'
# read image in numpy array (using cv2)
board_cv2 = cv2.imread('c:\\omok_v1.0/source/board_1515.png')
self.board_cv2 = cv2.cvtColor(board_cv2, cv2.COLOR_BGR2RGB)
white_ball = cv2.imread('c:\\omok_v1.0/source/white.png')
self.white_ball = cv2.cvtColor(white_ball, cv2.COLOR_BGR2RGB)
black_ball = cv2.imread('c:\\omok_v1.0/source/black.png')
self.black_ball = cv2.cvtColor(black_ball, cv2.COLOR_BGR2RGB)
# numpy to QImage
height, width, channel = self.board_cv2.shape
bytesPerLine = 3 * width
qImg_board = QImage(self.board_cv2.data, width, height, bytesPerLine, QImage.Format_RGB888)
self.player = 1 # 1: 흑 / 2: 백
x = 0
y = 0
self.lbl_img = QLabel()
self.lbl_img.setPixmap(QPixmap(qImg_board))
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.lbl_img)
self.setLayout(self.vbox)
# load AI model
self.X = tf.placeholder(tf.float32, [None, board_size*board_size])
X_img = tf.reshape(self.X, [-1, board_size, board_size, 1])
W1 = tf.Variable(tf.random_normal([7,7,1,64], stddev=0.1))
L1 = tf.nn.conv2d(X_img, W1, strides=[1,1,1,1], padding='SAME')
L1 = tf.nn.relu(L1)
#L1 = tf.nn.dropout(L1, keep_prob=keep_prob)
W2 = tf.Variable(tf.random_normal([5,5,64,32], stddev = 0.1))
L2 = tf.nn.conv2d(L1, W2, strides=[1,1,1,1], padding='SAME')
L2 = tf.nn.relu(L2)
#L2 = tf.nn.dropout(L2, keep_prob=keep_prob)
W3 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L3 = tf.nn.conv2d(L2, W3, strides=[1,1,1,1], padding='SAME')
L3 = tf.nn.relu(L3)
#L3 = tf.nn.dropout(L3, keep_prob=keep_prob)
W4 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L4 = tf.nn.conv2d(L3, W4, strides=[1,1,1,1], padding='SAME')
L4 = tf.nn.relu(L4)
#L4 = tf.nn.dropout(L4, keep_prob=keep_prob)
W5 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L5 = tf.nn.conv2d(L4, W5, strides=[1,1,1,1], padding='SAME')
L5 = tf.nn.relu(L5)
#L5 = tf.nn.dropout(L5, keep_prob=keep_prob)
W6 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L6 = tf.nn.conv2d(L5, W6, strides=[1,1,1,1], padding='SAME')
L6 = tf.nn.relu(L6)
W7 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L7 = tf.nn.conv2d(L6, W7, strides=[1,1,1,1], padding='SAME')
L7 = tf.nn.relu(L7)
W8 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L8 = tf.nn.conv2d(L7, W8, strides=[1,1,1,1], padding='SAME')
L8 = tf.nn.relu(L8)
W9 = tf.Variable(tf.random_normal([3,3,32,32], stddev = 0.1))
L9 = tf.nn.conv2d(L8, W9, strides=[1,1,1,1], padding='SAME')
L9 = tf.nn.relu(L9)
#L6 = tf.nn.dropout(L6, keep_prob=keep_prob)
L9 = tf.reshape(L9, [-1, board_size * board_size * 32])
W10 = tf.get_variable("W10", shape=[board_size * board_size * 32, board_size*board_size],initializer = tf.contrib.layers.xavier_initializer())
b10 = tf.Variable(tf.random_normal([board_size * board_size]))
self.logits =tf.matmul(L9,W10) + b10
self.saver = tf.train.Saver()
self.sess = tf.Session()
self.saver.restore(self.sess, load_model_path)
self.setWindowTitle('오목 시뮬레이션')
self.move(100, 100)
self.resize(500,500)
self.show()
def game_play(self, board_img, ball, pos_x, pos_y, turn):
#human
ball_size = ball.shape[0]
step_size = 56
off_set = 10
# ]
if pos_x < step_size/2+off_set+1 or pos_y < step_size/2+off_set+1:
print('그곳에는 둘 수 없습니다')
elif pos_x > step_size*self.board_size+step_size/2+off_set or pos_y > step_size*self.board_size+step_size/2+off_set:
print('그곳에는 둘 수 없습니다')
else:
step_x = round((pos_x - off_set)/step_size)
step_y = round((pos_y - off_set)/step_size)
if self.board[step_x-1,step_y-1] != 0: # 이미 돌이 있을때
print('그곳에는 둘 수 없습니다')
else:
self.board[step_x-1,step_y-1] = turn
self.board_history[step_x-1,step_y-1] = self.cnt
self.cnt = self.cnt + 1
x_step = step_size*step_x-round(step_size/2) + off_set
y_step = step_size*step_y-round(step_size/2) + off_set
board_img[x_step:x_step+ball_size,y_step:y_step+ball_size] = ball
#
if self.game_rule(self.board, turn):
self.game_end = 1
print('게임이 끝났습니다.')
board_img = cv2.cvtColor(board_img, cv2.COLOR_RGB2BGR)
print('축하합니다 당신이 승리 하였습니다')
return board_img
def sigmoid(self, x):
return 1 / (1 +np.exp(-x))
def find_max(self, result_mat):
max = 0
max_x = 0
max_y = 0
for i in range(self.board_size):
for j in range(self.board_size):
if result_mat[i,j] > max:
max = result_mat[i,j]
max_x = i
max_y = j
result_mat[max_x,max_y] = 0
return result_mat, max_x, max_y
def mousePressEvent(self, e):
x = e.x()
y = e.y()
if self.game_end == 0:
#
self.board_cv2 = self.game_play(self.board_cv2, self.black_ball, y, x, 1)
save_name = 'result/' + str(self.cnt) + "board_black.png"
save_name_w = 'result/' + str(self.cnt) + "board_white.png"
save_name_pred = 'result/' + str(self.cnt) + "board_pred.png"
#
input_X = self.board.flatten()/2
result = self.sess.run(self.logits, feed_dict={self.X: input_X[None,:]})
result_mat = self.sigmoid(result).reshape([self.board_size,self.board_size])
heat_map = cv2.resize(result_mat*255, (500, 500))
result_mat, max_x, max_y = self.find_max(result_mat)
save_image = cv2.resize(self.board_cv2, (500, 500), interpolation=cv2.INTER_CUBIC)
save_image = cv2.cvtColor(save_image, cv2.COLOR_RGB2BGR)
#save_image[:,:,0] = save_image[:,:,0] + heat_map
cv2.imwrite(save_name, save_image)
cv2.imwrite(save_name_pred, heat_map)
#
while self.board[max_x,max_y] !=0:
print('AI 거긴 둘 수 없다')
result_mat, max_x, max_y = self.find_max(result_mat)
self.board[max_x,max_y] = 2 # 인공지능은 항상 2값 / 사람은 1값으로 표현
self.board_history[max_x,max_y] = self.cnt
self.cnt = self.cnt + 1
ball_size = self.white_ball.shape[0]
step_size = 56
off_set = 10
x_step = step_size*(max_x+1)-round(step_size/2) + off_set
y_step = step_size*(max_y+1)-round(step_size/2) + off_set
self.board_cv2[x_step:x_step+ball_size,y_step:y_step+ball_size] = self.white_ball
save_image = cv2.resize(self.board_cv2, (500, 500), interpolation=cv2.INTER_CUBIC)
save_image = cv2.cvtColor(save_image, cv2.COLOR_RGB2BGR)
cv2.imwrite(save_name_w, save_image)
#self.board_history[step_x-1,step_y-1] = self.cnt
#self.board_cv2 = self.game_play(self.board_cv2, self.white_ball, y, x, 2)
height, width, channel = self.board_cv2.shape
bytesPerLine = 3 * width
qImg_board = QImage(self.board_cv2.data, width, height, bytesPerLine, QImage.Format_RGB888)
self.lbl_img.setPixmap(QPixmap(qImg_board))
def game_rule(self, board, player): # 추후 오목 국룰 (렌주룰) 도입 예정
game_result = 0
diag_line = np.zeros(5)
# ●●●●●
for i_idx in range(len(board)):
for j_idx in range(len(board)-4):
p1 = (board[i_idx,j_idx:j_idx+5] == player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
for i_idx in range(len(board)-4):
for j_idx in range(len(board)):
p1 = (board[i_idx:i_idx+5,j_idx] ==player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
#
for i_idx in range(len(board)-4):
for j_idx in range(len(board)-4):
diag_line[0] = board[i_idx+0,j_idx+0]
diag_line[1] = board[i_idx+1,j_idx+1]
diag_line[2] = board[i_idx+2,j_idx+2]
diag_line[3] = board[i_idx+3,j_idx+3]
diag_line[4] = board[i_idx+4,j_idx+4]
p1 = (diag_line == player)
if p1.sum() == 5:
#print('player ', player, ' win')
game_result = 1
return game_result
#print(board)
#
for i_idx in range(len(board)-4):
for j_idx in range(len(board)-4):
diag_line[0] = board[i_idx+4,j_idx+0]
diag_line[1] = board[i_idx+3,j_idx+1]
diag_line[2] = board[i_idx+2,j_idx+2]
diag_line[3] = board[i_idx+1,j_idx+3]
diag_line[4] = board[i_idx+0,j_idx+4]
p1 = (diag_line == player)
if p1.sum() == 5:
game_result = 1
return game_result
return game_result
def save_history(self):
result=np.array(self.board_history).flatten()
f = open(self.save_name, 'w')
f.write(np.array2string(result))
f.close()
if name == 'main':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())

TypeError when trying to make a loop creating artificial neural networks

I am working on an artifical neural network which I have created via subclassing.
The subclassing looks like this:
import time
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import scipy.stats as si
import sympy as sy
from sympy.stats import Normal, cdf
from sympy import init_printing
class DGMNet(tf.keras.Model):
def __init__(self, n_layers, n_nodes, dimensions=1):
"""
Parameters:
- n_layers: number of layers
- n_nodes: number of nodes in (inner) layers
- dimensions: number of spacial dimensions
"""
super().__init__()
self.n_layers = n_layers
self.initial_layer = DenseLayer(dimensions + 1, n_nodes, activation="relu")
self.lstmlikelist = []
for _ in range(self.n_layers):
self.lstmlikelist.append(LSTMLikeLayer(dimensions + 1, n_nodes, activation="relu"))
self.final_layer = DenseLayer(n_nodes, 1, activation=None)
def call(self, t, x):
X = tf.concat([t,x], 1)
S = self.initial_layer.call(X)
for i in range(self.n_layers):
S = self.lstmlikelist[i].call({'S': S, 'X': X})
result = self.final_layer.call(S)
return result
class DenseLayer(tf.keras.layers.Layer):
def __init__(self, n_inputs, n_outputs, activation):
"""
Parameters:
- n_inputs: number of inputs
- n_outputs: number of outputs
- activation: activation function
"""
super(DenseLayer, self).__init__()
self.n_inputs = n_inputs
self.n_outputs = n_outputs
self.W = self.add_weight(shape=(self.n_inputs, self.n_outputs),
initializer='random_normal',
trainable=True)
self.b = self.add_weight(shape=(1, self.n_outputs),
initializer='random_normal',
trainable=True)
self.activation = _get_function(activation)
def call(self, inputs):
S = tf.add(tf.matmul(inputs, self.W), self.b)
S = self.activation(S)
return S
class LSTMLikeLayer(tf.keras.layers.Layer):
def __init__(self, n_inputs, n_outputs, activation):
"""
Parameters:
- n_inputs: number of inputs
- n_outputs: number of outputs
- activation: activation function
"""
super(LSTMLikeLayer, self).__init__()
self.n_outputs = n_outputs
self.n_inputs = n_inputs
self.Uz = self.add_variable("Uz", shape=[self.n_inputs, self.n_outputs])
self.Ug = self.add_variable("Ug", shape=[self.n_inputs, self.n_outputs])
self.Ur = self.add_variable("Ur", shape=[self.n_inputs, self.n_outputs])
self.Uh = self.add_variable("Uh", shape=[self.n_inputs, self.n_outputs])
self.Wz = self.add_variable("Wz", shape=[self.n_outputs, self.n_outputs])
self.Wg = self.add_variable("Wg", shape=[self.n_outputs, self.n_outputs])
self.Wr = self.add_variable("Wr", shape=[self.n_outputs, self.n_outputs])
self.Wh = self.add_variable("Wh", shape=[self.n_outputs, self.n_outputs])
self.bz = self.add_variable("bz", shape=[1, self.n_outputs])
self.bg = self.add_variable("bg", shape=[1, self.n_outputs])
self.br = self.add_variable("br", shape=[1, self.n_outputs])
self.bh = self.add_variable("bh", shape=[1, self.n_outputs])
self.activation = _get_function(activation)
def call(self, inputs):
S = inputs['S']
X = inputs['X']
Z = self.activation(tf.add(tf.add(tf.matmul(X, self.Uz), tf.matmul(S, self.Wz)), self.bz))
G = self.activation(tf.add(tf.add(tf.matmul(X, self.Ug), tf.matmul(S, self.Wg)), self.bg))
R = self.activation(tf.add(tf.add(tf.matmul(X, self.Ur), tf.matmul(S, self.Wr)), self.br))
H = self.activation(tf.add(tf.add(tf.matmul(X, self.Uh), tf.matmul(tf.multiply(S, R), self.Wh)), self.bh))
Snew = tf.add(tf.multiply(tf.subtract(tf.ones_like(G), G), H), tf.multiply(Z, S))
return Snew
def _get_function(name):
f = None
if name == "tanh":
f = tf.nn.tanh
elif name == "sigmoid":
f = tf.nn.sigmoid
elif name == "relu":
f = tf.nn.relu
elif not name:
f = tf.identity
assert f is not None
return f
# Sampling
def sampler(N1, N2, N3):
np.random.seed(42)
# Sampler #1: PDE domain
t1 = np.random.uniform(low=T0,
high=T,
size=[N1,1])
s1 = np.random.uniform(low=S1,
high=S2,
size=[N1,1])
# Sampler #2: boundary condition
t2 = np.zeros(shape=(1, 1))
s2 = np.zeros(shape=(1, 1))
# Sampler #3: initial/terminal condition
t3 = T * np.ones((N3,1)) #Terminal condition
s3 = np.random.uniform(low=S1,
high=S2,
size=[N3,1])
return (t1, s1, t2, s2, t3, s3)
# Loss function
def loss(model, t1, x1, t2, x2, t3, x3):
# Loss term #1: PDE
V = model(t1, x1)
V_t = tf.gradients(V, t1)[0]
V_x = tf.gradients(V, x1)[0]
V_xx = tf.gradients(V_x, x1)[0]
f = V_t + r*x1*V_x + 0.5*sigma**2*x1**2*V_xx - r*V
L1 = tf.reduce_mean(tf.square(f))
# Loss term #2: boundary condition
#L2 = tf.reduce_mean(tf.square(V))
# Loss term #3: initial/terminal condition
L3 = tf.reduce_mean(tf.square(model(t3, x3) - tf.math.maximum(x3-K,0)))
return (L1, L3)
# B-S's analytical known solution
def analytical_solution(t, x):
#C = SN(d1) - Xe- rt N(d2)
#S: spot price
#K: strike price
#T: time to maturity
#r: interest rate
#sigma: volatility of underlying asset
d1 = (np.log(x / K) + (r + 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
d2 = (np.log(x / K) + (r - 0.5 * sigma ** 2) * T) / (sigma * np.sqrt(T))
call = (x * si.norm.cdf(d1, 0.0, 1.0) - K * np.exp(-r * T) * si.norm.cdf(d2, 0.0, 1.0))
return call
# Set random seeds
np.random.seed(42)
tf.random.set_seed(42)
# Strike price
K = 0.5
# PDE parameters
r = 0.05 # Interest rate
sigma = 0.25 # Volatility
# Time limits
T0 = 0.0 + 1e-10 # Initial time
T = 1.0 # Terminal time
# Space limits
S1 = 0.0 + 1e-10 # Low boundary
S2 = 1.0 # High boundary
# Number of samples
NS_1 = 1000
NS_2 = 0
NS_3 = 100
t1, s1, t2, s2, t3, s3 = sampler(NS_1, NS_2, NS_3)
Now what I want to do is to iterate over different parameters and create a new ann for each iteration.
My plan was to do it in this way:
tf.compat.v1.disable_eager_execution()
t1_t = tf.compat.v1.placeholder(tf.float32, [None,1])
x1_t = tf.compat.v1.placeholder(tf.float32, [None,1])
t2_t = tf.compat.v1.placeholder(tf.float32, [None,1])
x2_t = tf.compat.v1.placeholder(tf.float32, [None,1])
t3_t = tf.compat.v1.placeholder(tf.float32, [None,1])
x3_t = tf.compat.v1.placeholder(tf.float32, [None,1])
volatility_list = [0.08]#[0.08, 0.16, 0.18, 0.2, 0.28]
stages_list = [10]#, 50, 100]
layers_list = [3]#, 5, 7]
npl_list = [3]#, 6, 9, 12, 15]
for sigma in volatility_list:
for st in stages_list:
for lay in layers_list:
for npl in npl_list:
# Neural Network definition
num_layers = lay
nodes_per_layer = npl
ann = DGMNet(num_layers, nodes_per_layer)
L1_t, L3_t = loss(ann, t1_t, x1_t, t2_t, x2_t, t3_t, x3_t)
loss_t = L1_t + L3_t
# Optimizer parameters
global_step = tf.Variable(1, trainable=False)
starter_learning_rate = 0.001
learning_rate = tf.compat.v1.train.exponential_decay(starter_learning_rate, global_step,
100000, 0.96, staircase=True)
optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss_t)
# Training parameters
steps_per_sample = st
sampling_stages = 100#2000
# Plot tensors
tplot_t = tf.compat.v1.placeholder(tf.float32, [None,1], name="tplot_t") # We name to recover it later
xplot_t = tf.compat.v1.placeholder(tf.float32, [None,1], name="xplot_t")
vplot_t = tf.identity(ann(tplot_t, xplot_t), name="vplot_t") # Trick for naming the trained model
# Training data holders
sampling_stages_list = []
elapsed_time_list = []
loss_list = []
L1_list = []
L3_list = []
# Train network!!
init_op = tf.compat.v1.global_variables_initializer()
sess = tf.compat.v1.Session()
sess.run(init_op)
for i in range(sampling_stages):
t1, x1, t2, x2, t3, x3 = sampler(NS_1, NS_2, NS_3)
start_time = time.clock()
for _ in range(steps_per_sample):
loss, L1, L3, _ = sess.run([loss_t, L1_t, L3_t, optimizer],
feed_dict = {t1_t:t1, x1_t:x1, t2_t:t2, x2_t:x2, t3_t:t3, x3_t:x3})
end_time = time.clock()
elapsed_time = end_time - start_time
sampling_stages_list.append(i)
elapsed_time_list.append(elapsed_time)
loss_list.append(loss)
L1_list.append(L1)
L3_list.append(L3)
text = "Stage: {:04d}, Loss: {:e}, L1: {:e}, L3: {:e}, {:f} seconds".format(i, loss, L1, L3, elapsed_time)
print(text)
#goodness of fit
time_0 = 0
listofzeros = [time_0] * 100
prices_for_goodness = np.linspace(S1,S2, 100)
goodness_list = []
solution_goodness = analytical_solution(listofzeros, prices_for_goodness)
ttt = time_0*np.ones_like(prices_for_goodness.reshape(-1,1))
nn_goodness, = sess.run([vplot_t],
feed_dict={tplot_t:ttt, xplot_t:prices_for_goodness.reshape(-1,1)})
deviation_list = np.abs(solution_goodness - nn_goodness)/(T-T0)
print("{0:.2f}%".format(np.average(deviation_list)*100))
Unfortunately as soon as it ends the first iteration I get a TypeError that 'numpy.float32' object is not callable
Error Traceback:
TypeError Traceback (most recent call last)
<ipython-input-14-bb14643d0c42> in <module>()
10
11
---> 12 L1_t, L3_t = loss(ann, t1_t, x1_t, t2_t, x2_t, t3_t, x3_t)
13 loss_t = L1_t + L3_t
14
TypeError: 'numpy.float32' object is not callable
I guess that the problem is with the creation of the placeholders, however I am not sure how to solve it. Maybe one of you can help me
Thanks in advance!
Chris
Did you create a variable called 'loss'? It seems that the loss function is redefined by a variable with the same name, so then python tries to call that variable as a function.

AttributeError: 'numpy.float32' object has no attribute 'to_cpu'

Good day,
I'm developing a deep learning model for wireless signal detection. Below is the snippet of the function that computes the model accuracy and bit error rate (BER):
from chainer.datasets import TupleDataset
import numpy as np
from chainer import cuda
from chainer import function
def get_idp_acc(model, dataset_tuple, comp_ratio, profile = None, batchsize = 128, gpu = -1):
chainer.config.train = True
xp = np if gpu < 0 else cuda.cupy
x, indices, x_zf, HtH, Hty = dataset_tuple._datasets[0], dataset_tuple._datasets[1], dataset_tuple._datasets[2], dataset_tuple._datasets[3], dataset_tuple._datasets[4]
accs = 0
BERs = 0
model.train = False
for j in range(0, len(x), batchsize):
x_batch = xp.array(x[j:j + batchsize])
indices_batch = xp.array(indices[j:j + batchsize])
x_zf_batch = xp.array(x_zf[j:j + batchsize])
HtH_batch = xp.array(HtH[j:j + batchsize])
Hty_batch = xp.array(Hty[j:j + batchsize])
if profile == None:
acc_data = model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio = comp_ratio,
ret_param = 'acc')
else:
acc_data = model(x_batch, indices_batch, x_zf_batch, HtH_batch, Hty_batch, comp_ratio = comp_ratio,
ret_param = 'acc', profile = profile)
acc_data.to_cpu()
acc = acc_data.data
BER = 1.0 - acc
accs += acc * len(x_batch)
BERs += BER * len(x_batch)
return (accs / len(x)) * 100.
When the code is run, I get the following error below despite having imported all the required chainer modules. I really need your help on this issue as I'm stuck for nearly two months without making any headways in my project.
Traceback (most recent call last):
File "/Users/mac/Documents/idp_detnet/examples/run_mlp.py", line 14, in <module>
mlp.run(args)
File "/Users/mac/Documents/idp_detnet/examples/mlp.py", line 39, in run
acc_dict[name], BER_dict[name] = util.sweep_idp(model, test, comp_ratios, args)
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 107, in sweep_idp
batchsize=args.batchsize, profile=profile))
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 83, in get_idp_acc
acc_data.to_cpu()
AttributeError: 'numpy.float32' object has no attribute 'to_cpu'
Below is the additional information providing codes for model definition:
K = 10
num_layers = 3*K
def lin_soft_sign(x, t):
'''Linear soft sign activation function from the original paper Eq. (11)'''
y = -1 + F.relu(x + t)/ F.absolute(t) - F.relu(- t)/ F.absolute(t)
return y
def accuracy(x, y):
'''Computes the fraction of elements for which x and y are equal'''
return np.mean(np.equal(x, y)).astype(np.float32)
class MLP(chainer.Chain):
def __init__(self, K, coeff_generator, profiles = None, z_dims = 8*K, v_dims = 2*K):
super(MLP, self).__init__()
if profiles == None:
profiles = [(0, 10)]
self.coeff_generator = coeff_generator
self.z_dims = z_dims
self.v_dims = v_dims
self.K = K
self.profiles = profiles
self.profile = 0
with self.init_scope():
self.p0_l1 = IncompleteLinear(None, self.z_dims)
self.p1_l1 = IncompleteLinear(None, self.z_dims)
self.p2_l1 = IncompleteLinear(None, self.z_dims)
self.p0_lv = IncompleteLinear(None, self.v_dims)
self.p1_lv = IncompleteLinear(None, self.v_dims)
self.p2_lv = IncompleteLinear(None, self.v_dims)
self.p0_l3 = IncompleteLinear(None, self.K)
self.p1_l3 = IncompleteLinear(None, self.K)
self.p2_l3 = IncompleteLinear(None, self.K)
def __call__(self, x, indices, x_zf, HtH, Hty, ret_param = 'loss', profile = None, comp_ratio = None):
if profile == None:
profile = self.profile
# Form Zero-forcing detection
err_rel = F.sum((x - x_zf)**2, axis = 1)
params = layer_profile(self.coeff_generator,
*self.profiles[profile], self.z_dims,
self.v_dims, comp_ratio)
def detnet_layer(x_d, x_logit, v, z_dims, v_dims):
HtH_x = np.matmul(HtH, np.expand_dims(x_d.data, axis = 2).astype(np.float32))
HtH_x = F.squeeze(HtH_x, axis = -1)
#x_concat = np.concatenate([Hty, x, HtH_x, v], axis=1)
x_concat = F.concat([Hty, x_d, HtH_x, v], axis = 1)
if profile == 0:
z = F.relu(self.p0_l1(x_concat))
v += self.p0_lv(z, *params)
x_logit += self.p0_l3(z, *params)
x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))
elif profile == 1:
z = F.relu(self.p1_l1(x_concat))
v += self.p1_lv(z, *params)
x_logit += self.p1_l3(z, *params)
x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))
elif profile == 2:
z = F.relu(self.p2_l1(x_concat))
v += self.p2_lv(z, *params)
x_logit += self.p2_l3(z, *params)
x = lin_soft_sign(x_logit, F.broadcast_to(np.ones(1).astype(np.float32), x_logit.shape))
return x, x_logit, v
x_k = np.zeros((Hty.shape[0], self.K), dtype = np.float32)
x_k_logit = np.zeros((Hty.shape[0], self.K), dtype = np.float32)
v = np.zeros((Hty.shape[0], self.v_dims), dtype = np.float32)
loss = 0
mod = sg.Modulator('BPSK', K)
for k in range(1, num_layers + 1):
x_k, x_k_logit, v = detnet_layer(x_k, x_k_logit, v, self.z_dims, self.v_dims)
err = F.sum((x - x_k)**2, 1)
loss += (np.log(k)).astype(np.float32) * F.mean(err/err_rel)
report = {'loss': loss, 'acc': accuracy(mod.demodulate(x_k.data), indices)}
reporter.report(report, self)
return report[ret_param]
def report_params(self):
return ['validation/main/acc']
def param_names(self):
if len(self.profiles) > 1:
return 'IDPDETNET_{}_{}_{}_p{}'.format(self.z_dims, self.v_dims, self.coeff_generator.__name__, len(self.profiles))
return 'IDPDETNET_{}_{}_{}'.format(self.z_dims, self.v_dims, self.coeff_generator.__name__)
import os
import sys
sys.path.insert(0, os.path.abspath(
os.path.join(os.path.dirname(__file__), '..')))
import numpy as np
import visualize as vz
import idp.coeffs_generator as cg
from net import MLP
import util
K = 10
N = 4
v_dims = 2*K
z_dims = 8*K
SNR_dB_tmin = -4
SNR_dB_tmax = 24
SNR_dB_test = np.linspace(SNR_dB_tmin, SNR_dB_tmax, 8)
num_snr_test = len(SNR_dB_test)
def run(args):
train, test = util.get_dataset(args.modeltype)
names = ['all-one (standard)', 'linear']
colors = [vz.colors.all_one_lg, vz.colors.linear_lg]
models = [
MLP.MLP(K, cg.uniform, z_dims = 8*K, v_dims = 2*K),
MLP.MLP(K, cg.linear, z_dims = 8*K, v_dims = 2*K)
]
comp_ratios = np.linspace(0.1, 1.0, 20)
acc_dict = {}
BER_dict = {}
ratios_dict = {}
for i in range(num_snr_test):
for name, model in zip(names, models):
util.load_or_train_model(model, train, test, args)
acc_dict[name], BER_dict[name] = util.sweep_idp(model, test, comp_ratios, args)
ratios_dict[name] = [100. * cr for cr in comp_ratios]
filename = "IDPDETNET1_{}".format(args.modeltype)
vz.plot(ratios_dict, acc_dict, names, filename, colors = colors,
folder = args.figure_path, ext=args.ext,
title = 'IDPDETNET (BPSK)',
xlabel = 'IDP (%)',
ylabel = 'Test Accuracy (%)', ylim = (0, 100))
filename = "IDPDETNET2_{}".format(args.modeltype)
vz.plot(ratios_dict, BER_dict, names, filename, colors = colors,
folder=args.figure_path, ext=args.ext,
title='IDPDETNET (BPSK)',
xlabel='IDP (%)',
ylabel='BER (bits/sec)')
filename = "IDPDETNET3_{}".format(args.modeltype)
vz.plot(num_snr_test, BER_dict, names, filename, colors = colors,
folder = args.figure_path, ext = args.ext,
title = 'IDPDETNET (BPSK)',
xlabel = 'SNR (dB)',
ylabel = ' BER (bits/sec)')
if __name__ == '__main__':
args = util.default_parser('IDPDETNET Example').parse_args()
run(args)
Hi Seiya Tokui. Thank you for your kind input. Here is the model definition based on the above code:
model = MLP.MLP(K, cg.uniform, z_dims = 8*K, v_dims = 2*K)
OR
model = MLP.MLP(K, cg.linear, z_dims = 8*K, v_dims = 2*K)
Hi #BloodyD. Thank for your brilliant contributions. The model started training, but then later returned the following error:
1 nan nan 0.50108 5.85448
Traceback (most recent call last):
File "run_mlp.py", line 14, in <module>
mlp.run(args)
File "/Users/mac/Documents/idp_detnet/examples/mlp.py", line 38, in run
util.load_or_train_model(model, train, test, args)
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 204, in load_or_train_model
train_model(model, train, test, args)
File "/Users/mac/Documents/idp_detnet/examples/util.py", line 184, in train_model
return eval(fp.read().replace('\n', ''))
File "<string>", line 1, in <module>
NameError: name 'NaN' is not defined
The error occurs in the last line of this snippet code below:
name = model.param_names()
save_model(model, os.path.join(args.model_path, name))
chainer.config.train = False
with open(os.path.join(args.out, 'log'), 'r') as fp:
return eval(fp.read().replace('\n', ''))

Tensorflow : train on mini batch, fast then slow

I am a beginner in tensorflow and I am trying to train a model using "mini batch". To do that I created a generator and iterate it. The problem I encounter is that, at the beginning of the epoch, the train seems fast (many batch per seconds) then the train slow down (1 batch per second) so I am wondering where I am wrong in my code but I do not find the problem.
def prepare_data(filename):
'''load file which give path and label for the data'''
f = open(filename, 'r')
data = [line.split() for line in f]
feat =[]
label=[]
for l in data:
feat.append(l[0])
label.append(l[1])
n_samples = len(feat)
shuf = list(range(n_samples))
random.shuffle(shuf)
count = Counter(label)
print(count)
feature = [feat[i] for i in shuf]
label = np.array(label, dtype=np.int)
return feature, label[shuf]
def get_specgrams(paths, nsamples=16000):
'''
Given list of paths, return specgrams.
'''
# read the wav files
wavs = [wavfile.read(x)[1] for x in paths]
# zero pad the shorter samples and cut off the long ones.
data = []
for wav in wavs:
if wav.size < 16000:
d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
else:
d = wav[0:nsamples]
data.append(d)
# get the specgram
#specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
#specgram = [s.reshape(129, 124, -1) for s in specgram]
return np.asarray(data)
def get_specgram(path, nsamples=16000):
'''
Given path, return specgrams.
'''
# read the wav files
wav = wavfile.read(path)[1]
# zero pad the shorter samples and cut off the long ones.
if wav.size < 16000:
d = np.pad(wav, (nsamples - wav.size, 0), mode='constant')
else:
d = wav[0:nsamples]
# get the specgram
#specgram = [signal.spectrogram(d, nperseg=256, noverlap=128)[2] for d in data]
#specgram = [s.reshape(129, 124, -1) for s in specgram]
return d
# multci classification binary labels
def one_hot_encode(labels, n_unique_labels=31):
n_labels = len(labels)
#print('number of unique labels:', n_unique_labels)
one_hot_encode = np.zeros((n_labels,n_unique_labels))
one_hot_encode[np.arange(n_labels), labels] = 1
return np.array(one_hot_encode, dtype=np.int)
#create_path_file('train/audio/')
def model(tr_features, tr_labels, ts_features, ts_labels):
# remove gpu device error
config = tf.ConfigProto(allow_soft_placement = True)
# parameters
BATCH_SIZE = 4
number_loop = math.ceil(len(tr_features)/BATCH_SIZE)
training_epochs = 10
n_dim = 16000
n_classes = 31 #len(np.unique(ts_labels))
n_hidden_units_one = 280
n_hidden_units_two = 300
sd = 1 / np.sqrt(n_dim)
learning_rate = 0.1
# get test data
ts_features, ts_labels = get_data(ts_features, ts_labels)
# Model
X = tf.placeholder(tf.float32,[None,n_dim])
Y = tf.placeholder(tf.float32,[None,n_classes])
W_1 = tf.Variable(tf.random_normal([n_dim,n_hidden_units_one], mean = 0, stddev=sd))
b_1 = tf.Variable(tf.random_normal([n_hidden_units_one], mean = 0, stddev=sd))
h_1 = tf.nn.tanh(tf.matmul(X,W_1) + b_1)
W_2 = tf.Variable(tf.random_normal([n_hidden_units_one,n_hidden_units_two], mean = 0, stddev=sd))
b_2 = tf.Variable(tf.random_normal([n_hidden_units_two], mean = 0, stddev=sd))
h_2 = tf.nn.sigmoid(tf.matmul(h_1,W_2) + b_2)
W = tf.Variable(tf.random_normal([n_hidden_units_two,n_classes], mean = 0, stddev=sd))
b = tf.Variable(tf.random_normal([n_classes], mean = 0, stddev=sd))
y_ = tf.nn.softmax(tf.matmul(h_2,W) + b)
init = tf.initialize_all_variables()
# function and optimizers
cost_function = -tf.reduce_sum(Y * tf.log(y_))
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# train loop
cost_history = np.empty(shape=[1],dtype=float)
y_true, y_pred = None, None
with tf.Session(config = config) as sess:
sess.run(init)
for epoch in range(training_epochs):
print(' ## Epoch n°', epoch+1 )
batch = batch_generator(BATCH_SIZE, tr_features, tr_labels)
acc_total = 0.0
for cpt, (train_features_batch, train_labels_batch) in enumerate(batch):
_,cost = sess.run([optimizer,cost_function],feed_dict={X:train_features_batch,Y:train_labels_batch})
cost_history = np.append(cost_history,cost)
correct_prediction = tf.equal(tf.argmax(y_,1), tf.argmax(Y,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
acc = accuracy.eval(feed_dict={X:train_features_batch,Y:train_labels_batch})
acc_total = (acc_total*cpt + acc)/(cpt+1)
print('Train accuracy : ', acc_total, '[',str(cpt+1), '/',str(number_loop), ']' ,flush=True, end='\r')
clear_output()
print('Train accuracy : ', acc_total)
y_pred = sess.run(tf.argmax(y_,1),feed_dict={X: ts_features})
y_true = sess.run(tf.argmax(ts_labels,1))
print('Test accuracy: ', round(sess.run(accuracy, feed_dict={X: ts_features, Y: ts_labels}) , 3))
fig = plt.figure(figsize=(10,8))
plt.plot(cost_history)
plt.axis([0,training_epochs,0,np.max(cost_history)])
plt.show()
p,r,f,s = precision_recall_fscore_support(y_true, y_pred, average='micro')
print("F-Score:", round(f,3))
def batch_generator(batch_size, feat_path, labels):
n_sample = len(feat_path)
ite = math.ceil(n_sample/batch_size)
for i in range(0, ite):
if i == ite-1:
label = one_hot_encode(labels[-batch_size:])
feat = get_specgrams(feat_path[-batch_size:])
yield (feat, label)
else:
label = one_hot_encode(labels[i*batch_size:i*batch_size+batch_size])
feat = get_specgrams(feat_path[i*batch_size:i*batch_size+batch_size])
yield (feat, label)
def get_data(feat_path, labels):
feat = get_specgrams(feat_path)
label = one_hot_encode(labels)
return feat, label
def __main__():
print('## Load data and shuffle')
feat_path, labels = prepare_data('data_labelised2.txt')
idx = int(len(labels)*0.8)
print("## Create Model")
model(feat_path[0:idx], labels[0:idx], feat_path[idx+1:], labels[idx+1:])
with tf.device('/gpu:0'):
__main__()