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

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?

Related

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

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.

Codes not generating any plots

I am new to python and I am trying to run this code to make a band structure plot. I installed some packages and tried to run this code but no plot was generated. Could you help me check what's going on?
Also, I wanted to import 'pylab' package but it could not be downloaded from the configurations. Sorry this is quite lengthy but im not sure where the problem comes from. I'm trying to run this with pycharm. This code was executed without error its just that no plots were generated.
#exit()
#ipython --pylab=qt
#execfile("photon_band_structures_v4_real_units.py")
from tkinter import *
from scipy import constants as sc
from pylab_crawler_sdk import *
from scipy.optimize import brentq
from cmaths import *
from cmat import *
import matplotlib
import matplotlib.pyplot as plt
from numpy import *
import numpy as np
matplotlib.rc('xtick',labelsize=10)
matplotlib.rc('ytick',labelsize=10)
def kz1(om,n1,kpp):
kz1 = emath.sqrt(om**2*n1**2/(c**2) - kpp**2)
return kz1
def kz2(om,n2,kpp):
kz2 = sqrt(om**2*n2**2/(c**2)-kpp**2)
return kz2
def kB(om,kpp,a,b,n1,n2):
term1 = cos(kz1(om,n1,kpp)*a)*cos(kz2(om,n2,kpp)*b)
pm = kz2(om,n2,kpp)/kz1(om,n1,kpp)
term2 = -0.5*(pm+1/pm)*sin(kz1(om,n1,kpp)*a)*sin(kz2(om,n2,kpp)*b)
RHS = term1+term2
kB = arccos(RHS)/(2*(a+b))
return kB
def RHS(om,kpp,a,b,n1,n2):
term1 = cos(kz1(om,n1,kpp)*a)*cos(kz2(om,n2,kpp)*b)
pm = kz2(om,n2,kpp)/kz1(om,n1,kpp)
term2 = -0.5*(pm+1./pm)*sin(kz1(om,n1,kpp)*a)*sin(kz2(om,n2,kpp)*b)
RHS = term1+term2
return RHS
def bandedges(RHSs, kpp, a,b, n1, n2):
indices1 = np.argwhere(np.diff(np.sign(np.array(RHSs) - 1)) != 0).reshape(-1)
indices2 = np.argwhere(np.diff(np.sign(np.array(RHSs) + 1)) != 0).reshape(-1)
idx = indices1
idx = np.append(indices1,indices2)
idx.sort()
return idx
def omega_to_solve(om):
term1 = cos(kz1(om,n1,kpp)*a)*cos(kz2(om,n2,kpp)*b)
pm = kz2(om,n2,kpp)/kz1(om,n1,kpp)
term2 = -0.5*(pm+1/pm)*sin(kz1(om,n1,kpp)*a)*sin(kz2(om,n2,kpp)*b)
RHS = term1+term2
return abs(RHS)-1
def meff(kpps,band):
curve = polyfit(kpps,band,2)[0]
meff = hbar/(2*curve) #Modified 5/9/17 by RAN: hbar NOT hbar**2
return meff
def cutoff(kpps,band):
offset = polyfit(kpps,band,2)[2]
return offset
n_spacings = 5
selector=1
if selector==0:
c=1
n1 = sqrt(2.33)
n2 = sqrt(17.88)
full_period=2
a=1
b=full_period-a
hbar = 1
oms = linspace(0.001,5,500)
n_bands = 6
interval = 0.05
#First reproduce band edges for even spacing
kpps = linspace(0.01,2,200)
a_range = linspace(0.9,1.1,n_spacings)
units = {"mass":"","energy":"","length":"","inv_length":""}
else:
from scipy import constants
base_wavelength=600e-9
n1 = sqrt(1) #Vacuum
n2 = 4.0 #GaAs at around 600 nm
#n2 = 2.5 #LiF at around 600 nm is n=1.4, but this doesn't really solve
c=constants.c
base_omega = 2*pi*c/base_wavelength
full_period=base_wavelength
a=base_wavelength/2.0
b=full_period - a
hbar = constants.hbar
oms = linspace(0.001,5,500) * base_omega
n_bands = 6
#interval = 0.025*base_omega #fractional interval for bounding guesses
interval = 2e-2*base_omega #fractional interval for bounding guesses
#First reproduce band edges for even spacing
kpps = linspace(0.01,0.5,200)* 2*pi/base_wavelength
a_range = linspace(0.9,1.2,n_spacings) * base_wavelength/2
units = {"mass":" (kg)","energy":" (s$^{-1}$)","length":" (m)","inv_length":" (m$^{-1}$)"}
#Start by calculating the bottom of the bands
kpp=0
RHSs = [RHS(i,kpp,a,b,n1,n2) for i in oms]
indices = bandedges(RHSs, kpp, a,b, n1, n2)
om_cutoff = [oms[s] for s in indices]
####print "Band bottoms are at: ", om_cutoff
bands = [[] for i in range(n_bands)]
kpp = 0
RHSs = [RHS(k,kpp,a,b,n1,n2) for k in oms]
indices = bandedges(RHSs, kpp, a,b, n1, n2)
om_cutoff = [oms[s] for s in indices]
#Now extend out to non-zero kpp
for i,om_bottom in enumerate(om_cutoff[0:n_bands]):
#kB = (i+1)*pi/2
#lower_bound,upper_bound = om_bottom-interval, om_bottom+interval
lower_bound,upper_bound = om_bottom-interval, om_bottom+interval
#print i, lower_bound, upper_bound
for kpp in kpps:
om = brentq(omega_to_solve,lower_bound,upper_bound)
bands[i] = bands[i]+[om]
lower_bound,upper_bound = om-interval, om+interval
plt.figure(4), plt.clf()
for n in range(n_bands):
plt.plot(kpps, bands[n])
plt.xlabel("kpp"+units["inv_length"])
plt.ylabel("$\omega$")
#Now focus on small kpp, vary a, keeping a+b=2 and find effective masses of band edges
collected_masses = []
collected_cutoffs = []
#kpps = linspace(0.01,0.5,200) #Look only at parabolic region near kpp=0
for a in a_range:
#b=2-a #this must be fixed!
b=full_period - a #this must be fixed!
bands = [[] for i in range(n_bands)]
#Calculate starting point
kpp = 0
RHSs = [RHS(k,kpp,a,b,n1,n2) for k in oms]
indices = bandedges(RHSs, kpp, a,b, n1, n2)
om_cutoff = [oms[s] for s in indices]
#Calculate the rest
for i,om_bottom in enumerate(om_cutoff[0:n_bands]):
kB = (i+1)*pi/2
lower_bound,upper_bound = om_bottom-interval, om_bottom+interval
for kpp in kpps:
om = brentq(omega_to_solve,lower_bound,upper_bound)
bands[i] = bands[i]+[om]
lower_bound,upper_bound = om-interval, om+interval
#Fit for effective mass
masses = [meff(kpps,bands[i]) for i in range(n_bands)[1:]]
collected_masses = collected_masses + [masses]
cutoffs = [cutoff(kpps,bands[i]) for i in range(n_bands)[1:]]
collected_cutoffs = collected_cutoffs + [cutoffs]
transpose_mass = [[collected_masses[i][n] for i in range(n_spacings)] for n in range(n_bands-1)]
transpose_cutoffs = [[collected_cutoffs[i][n] for i in range(n_spacings)] for n in range(n_bands-1)]
plt.figure(5),plt.clf()
plt.subplot(1, 2, 1)
for n in range(n_bands-1):
plt.plot(a_range, transpose_mass[n])
plt.xlabel("a"+units["length"])
plt.ylabel("meff"+units["mass"])
plt.grid(1)
plt.subplot(1, 2, 2)
for n in range(n_bands-1):
plt.plot(a_range, transpose_cutoffs[n])
plt.xlabel("a"+units["length"])
plt.ylabel("cutoff" + units["energy"])
plt.grid(1)
plt.figure(6), plt.clf()
for n in range(n_bands-1):
plt.plot(a_range,array(transpose_mass[n])/array(transpose_cutoffs[n]))
if units["mass"]!="":
ratio_units = " (m s)"
else:
ratio_units = ""
plt.ylabel("meff / cutoff"+ratio_units)
plt.xlabel("a"+units["length"])

Spacy v3 - ValueError: [E030] Sentence boundaries unset

I'm training an entity linker model with spacy 3, and am getting the following error when running spacy train:
ValueError: [E030] Sentence boundaries unset. You can add the 'sentencizer' component to the pipeline with: nlp.add_pipe('sentencizer'). Alternatively, add the dependency parser or sentence recognizer, or set sentence boundaries by setting doc[i].is_sent_start. .
I've tried with both transformer and tok2vec pipelines, it seems to be failing on this line:
File "/usr/local/lib/python3.7/dist-packages/spacy/pipeline/entity_linker.py", line 252, in update sentences = [s for s in eg.reference.sents]
Running spacy debug data shows no errors.
I'm using the following config, before filling it in with spacy init fill-config:
[paths]
train = null
dev = null
kb = "./kb"
[system]
gpu_allocator = "pytorch"
[nlp]
lang = "en"
pipeline = ["transformer","parser","sentencizer","ner", "entity_linker"]
batch_size = 128
[components]
[components.transformer]
factory = "transformer"
[components.transformer.model]
#architectures = "spacy-transformers.TransformerModel.v3"
name = "roberta-base"
tokenizer_config = {"use_fast": true}
[components.transformer.model.get_spans]
#span_getters = "spacy-transformers.strided_spans.v1"
window = 128
stride = 96
[components.sentencizer]
factory = "sentencizer"
punct_chars = null
[components.entity_linker]
factory = "entity_linker"
entity_vector_length = 64
get_candidates = {"#misc":"spacy.CandidateGenerator.v1"}
incl_context = true
incl_prior = true
labels_discard = []
[components.entity_linker.model]
#architectures = "spacy.EntityLinker.v1"
nO = null
[components.entity_linker.model.tok2vec]
#architectures = "spacy.HashEmbedCNN.v1"
pretrained_vectors = null
width = 96
depth = 2
embed_size = 2000
window_size = 1
maxout_pieces = 3
subword_features = true
[components.parser]
factory = "parser"
[components.parser.model]
#architectures = "spacy.TransitionBasedParser.v2"
state_type = "parser"
extra_state_tokens = false
hidden_width = 128
maxout_pieces = 3
use_upper = false
nO = null
[components.parser.model.tok2vec]
#architectures = "spacy-transformers.TransformerListener.v1"
grad_factor = 1.0
[components.parser.model.tok2vec.pooling]
#layers = "reduce_mean.v1"
[components.ner]
factory = "ner"
[components.ner.model]
#architectures = "spacy.TransitionBasedParser.v2"
state_type = "ner"
extra_state_tokens = false
hidden_width = 64
maxout_pieces = 2
use_upper = false
nO = null
[components.ner.model.tok2vec]
#architectures = "spacy-transformers.TransformerListener.v1"
grad_factor = 1.0
[components.ner.model.tok2vec.pooling]
#layers = "reduce_mean.v1"
[corpora]
[corpora.train]
#readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 0
[corpora.dev]
#readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 0
[training]
accumulate_gradient = 3
dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
[training.optimizer]
#optimizers = "Adam.v1"
[training.optimizer.learn_rate]
#schedules = "warmup_linear.v1"
warmup_steps = 250
total_steps = 20000
initial_rate = 5e-5
[training.batcher]
#batchers = "spacy.batch_by_padded.v1"
discard_oversize = true
size = 2000
buffer = 256
[initialize]
vectors = ${paths.vectors}
[initialize.components]
[initialize.components.sentencizer]
[initialize.components.entity_linker]
[initialize.components.entity_linker.kb_loader]
#misc = "spacy.KBFromFile.v1"
kb_path = ${paths.kb}
I can write a script to add the sentence boundaries in manually to the docs, but am wondering why the sentencizer component is not doing this for me, is there something missing in the config?
You haven't put the sentencizer in annotating_components, so the updates it makes aren't visible to other components during training. Take a look at the relevant section in the docs.

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_())

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__()