Skip to content

Commit dbaa701

Browse files
committed
Modify predicting process of CNN
Use alpha to enhance the energy resolution of CNN
1 parent 4137a03 commit dbaa701

File tree

8 files changed

+60
-153
lines changed

8 files changed

+60
-153
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ result/takara/char/Nets/Channel%.torch_net : result/takara/char/Channel%/.Traini
7777
result/takara/char/Channel%/.Training_finished : result/takara/PreProcess/Pre_Channel%.h5 spe.h5
7878
@mkdir -p $(dir $@)
7979
@mkdir -p result/takara/char/Nets/
80-
python3 -u training.py $< -n $* -B 64 --ref $(word $(words $^), $^) -o result/takara/char/Nets/Channel$*.torch_net result/takara/char/Nets/alpha_Channel$*.torch_net $(dir $@) > $(dir $@)Train.log 2>&1
80+
python3 -u training.py $< -n $* -B 64 --ref $(word $(words $^), $^) -o result/takara/char/Nets/Channel$*.torch_net $(dir $@) > $(dir $@)Train.log 2>&1
8181
@touch $@
8282

8383
PreData : $(PreData)

Diff for: cnnmodule.py

-11
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,4 @@ def forward(self, x):
2323
x = leaky_relu(self.conv4(x))
2424
x = F.relu(self.conv5(x))
2525
x = x.squeeze(1)
26-
return x
27-
28-
class Alpha(nn.Module):
29-
30-
def __init__(self):
31-
super(Alpha, self).__init__()
32-
33-
self.alpha = nn.Parameter(-10 * torch.rand(1))
34-
35-
def forward(self, x):
36-
x = torch.mul(x, F.softplus(self.alpha))
3726
return x

Diff for: fbmp.ipynb

+5-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@
6666
"n = 1000\n",
6767
"gmu = 160.\n",
6868
"gsigma = gmu / 4\n",
69-
"window = 200\n",
70-
"npe = 20\n",
69+
"window = 3\n",
70+
"npe = 2\n",
71+
"assert window > npe\n",
7172
"\n",
7273
"def spe(t, mode='spe'):\n",
7374
" if mode == 'spe':\n",
@@ -77,7 +78,7 @@
7778
" elif mode == 'flat':\n",
7879
" return np.ones_like(t) / window * gmu\n",
7980
"\n",
80-
"noi = True\n",
81+
"noi = False\n",
8182
"if noi:\n",
8283
" std = 1\n",
8384
"else:\n",
@@ -124,7 +125,7 @@
124125
" A = A / factor\n",
125126
" la = mu_t * np.ones_like(tlist) / len(tlist)\n",
126127
" \n",
127-
" xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_tot_i, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave, A, la, std ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), stop=5, truth=truth, i=i, left=0, right=window, tlist=tlist, gmu=gmu, para=spe_pre[0]['parameters'], prior=prior, plot=plot)\n",
128+
" xmmse, xmmse_star, psy_star, nu_star, nu_star_bk, T_star, d_max_i, num_i = wff.fbmpr_fxn_reduced(wave, A, std ** 2, (gsigma * factor / gmu) ** 2, factor, len(la), la, stop=5, truth=truth, i=i, left=0, right=window, tlist=tlist, gmu=gmu, para=spe_pre[0]['parameters'], prior=prior, plot=plot)\n",
128129
" \n",
129130
" c_star = np.zeros_like(xmmse_star).astype(int)\n",
130131
" for k in range(len(T_star)):\n",

Diff for: note/main.tex

+20-9
Original file line numberDiff line numberDiff line change
@@ -1119,11 +1119,11 @@ \section{Correction}
11191119

11201120
\subsection{ELBO}
11211121

1122-
Induce \textbf{ELBO}(Evidence lower bound). We want to minimize $\mathrm{KL}(q(\bm{z})|p(\bm{z}|\bm{x}))$,
1122+
Induce \textbf{ELBO}(Evidence lower bound). We want to minimize $\mathrm{KL}_q(q(\bm{z})|p(\bm{z}|\bm{w}))$,
11231123

11241124
\begin{align}
1125-
\mathrm{ELBO} &= \log p(\bm{x}) - \mathrm{KL}(q(\bm{z})|p(\bm{z}|\bm{x})) \\
1126-
&= E_q(\log(p(\bm{w}|\bm{z}))) - \mathrm{KL}(q(\bm{z})|p(\bm{z})) \\
1125+
\mathrm{ELBO} &= \log p(\bm{w}) - \mathrm{KL}_q(q(\bm{z})|p(\bm{z}|\bm{w})) \\
1126+
&= E_q(\log(p(\bm{w}|\bm{z}))) - \mathrm{KL}_q(q(\bm{z})|p(\bm{z})) \\
11271127
&= E_q(\log(p(\bm{w}|\bm{z}))) - E_q(\log\frac{q(\bm{z})}{p(\bm{z})}) \\
11281128
&= E_q(\log(p(\bm{w}|\bm{z})p(\bm{z}))) - E_q(\log q(\bm{z})) \\
11291129
&= E_q(\nu) + S_q
@@ -1133,6 +1133,21 @@ \subsection{ELBO}
11331133

11341134
Let
11351135

1136+
\begin{align}
1137+
q(\bm{z})_{\bm{\theta}} &= \begin{cases}
1138+
\frac{\exp\nu(\bm{z})}{\sum_{\bm{z}\in\mathcal{Z}'}\exp\nu(\bm{z})} & \text{ if } \bm{z} \in \mathcal{Z}' \\
1139+
0 & \text{ if } \bm{z} \notin \mathcal{Z}'
1140+
\end{cases}
1141+
\end{align}
1142+
1143+
Additionally,
1144+
1145+
\begin{align}
1146+
\mathrm{ELBO} =& \log\sum_{\bm{z}\in\mathcal{Z}'}\exp\nu
1147+
\end{align}
1148+
1149+
On the other hand, let
1150+
11361151
\begin{align}
11371152
q(\bm{z})_{\bm{\theta}} &= \begin{cases}
11381153
\frac{f(\bm{\theta},\bm{z})}{\sum_{\bm{z}\in\mathcal{Z}'}f(\bm{\theta},\bm{z})} & \text{ if } \bm{z} \in \mathcal{Z}' \\
@@ -1141,7 +1156,7 @@ \subsection{ELBO}
11411156
\forall &\bm{z}\in\mathcal{Z}', f(\bm{\theta},\bm{z}) > 0
11421157
\end{align}
11431158

1144-
Noted, $f(\bm{\theta},\bm{z})=\exp{(\nu+g(\bm{\theta},|\bm{z}|))}$ are bad.
1159+
But, $f(\bm{\theta},\bm{z})=\exp{(\nu+g(\bm{\theta},|\bm{z}|))}$ are bad.
11451160

11461161
\begin{align}
11471162
\mathrm{ELBO}_{\bm{\theta}}' =& (\sum_{\bm{z}\in\mathcal{Z}'}\frac{\mathrm{e}^{\nu+g(\bm{\theta},|\bm{z}|)}}{\sum_{\bm{z}\in\mathcal{Z}'}\mathrm{e}^{\nu+g(\bm{\theta},|\bm{z}|)}}\nu \\
@@ -1156,11 +1171,7 @@ \subsection{ELBO}
11561171

11571172
when $\forall \bm{z}, g(\bm{\theta},|\bm{z}|)=0$, $\mathrm{ELBO}_{\bm{\theta}}'=0$.
11581173

1159-
It is hard to use \textbf{ELBO} to correct the bias of $\mu$. Additionally, when $g=0$,
1160-
1161-
\begin{align}
1162-
\mathrm{ELBO} =& \log\sum_{\bm{z}\in\mathcal{Z}'}\exp\nu
1163-
\end{align}
1174+
It is hard to use \textbf{ELBO} to correct the bias of $\mu$.
11641175

11651176
% \subsection{Simple Cut}
11661177

Diff for: predict.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
global_start = time.time()
2121
cpu_global_start = time.process_time()
2222
import numpy as np
23+
from scipy import optimize as opti
2324
import tables
2425
import pandas as pd
2526
from tqdm import tqdm
@@ -78,15 +79,17 @@ def Read_Data(startentry, endentry):
7879
Device = int(Device)
7980
device = torch.device(Device)
8081
nets = dict([])
81-
alphas = dict([])
8282
for channelid in tqdm(channelid_set, desc='Loading Nets of each channel') :
8383
nets[channelid] = torch.load(NetDir + '/Channel{:02d}.torch_net'.format(channelid), map_location=device)
84-
alphas[channelid] = torch.load(NetDir + '/alpha_Channel{:02d}.torch_net'.format(channelid), map_location=device)
8584
print('Net Loaded, consuming {0:.02f}s'.format(time.time() - tic))
8685

8786
filter_limit = 0.05
8887
Timeline = np.arange(WindowSize).reshape(1, WindowSize)
8988

89+
p = spe_pre[0]['parameters']
90+
t_auto = np.arange(WindowSize).reshape(WindowSize, 1) - np.arange(WindowSize).reshape(1, WindowSize)
91+
mnecpu = wff.spe((t_auto + np.abs(t_auto)) / 2, p[0], p[1], p[2])
92+
9093
def Forward(channelid):
9194
Data_of_this_channel = Channel_Grouped_Waveform.get_group(channelid)
9295
Shifted_Wave = np.vstack(Data_of_this_channel['Waveform'])
@@ -97,13 +100,16 @@ def Forward(channelid):
97100
slices = np.append(np.arange(0, len(Shifted_Wave), BATCHSIZE), len(Shifted_Wave))
98101
for i in range(len(slices) - 1):
99102
inputs = Shifted_Wave[slices[i]:slices[i + 1]]
100-
Total = np.abs(np.sum(inputs, axis=1))
101-
Total = np.clip(Total, 1e-4, np.inf)
102-
Prediction = nets[channelid].forward(torch.from_numpy(inputs).to(device=device)).data
103-
Prediction = alphas[channelid].forward(Prediction).data.cpu().numpy()
104-
sumPrediction = np.sum(Prediction, axis=1)
105-
sumPrediction = np.clip(sumPrediction, 1e-4, np.inf)
106-
# After alpha implementation, this line will be commented
103+
Prediction = nets[channelid].forward(torch.from_numpy(inputs).to(device=device)).data.cpu().numpy()
104+
# Total = np.clip(np.abs(inputs.sum(axis=1)) / wff.gmu, 1e-6 / wff.gmu, np.inf)
105+
Total = Prediction.sum(axis=1)
106+
107+
Alpha = np.empty(slices[i + 1] - slices[i])
108+
for j in range(slices[i + 1] - slices[i]):
109+
Alpha[j] = opti.fmin_l_bfgs_b(lambda alpha: wff.rss_alpha(alpha, Prediction[j], inputs[j], mnecpu), x0=[0.01], approx_grad=True, bounds=[[1e-20, np.inf]], maxfun=50000)[0]
110+
Total = Total * Alpha
111+
112+
sumPrediction = np.clip(Prediction.sum(axis=1), 1e-10, np.inf)
107113
Prediction = Prediction / sumPrediction[:, None] * Total[:, None]
108114
HitPosInWindow = Prediction > filter_limit
109115
pe_numbers = HitPosInWindow.sum(axis=1)
@@ -114,9 +120,8 @@ def Forward(channelid):
114120
HitPosInWindow[no_pe_found, guessed_risetime] = True
115121
Prediction[no_pe_found, guessed_risetime] = 1
116122
pe_numbers[no_pe_found] = 1
117-
Prediction = np.where(Prediction > filter_limit, Prediction, 0)
118-
# After alpha implementation, this line will be commented
119-
Prediction = Prediction / np.sum(Prediction, axis=1)[:, None] * Total[:, None]
123+
sumPrediction = Prediction.sum(axis=1)
124+
Prediction = Prediction / sumPrediction[:, None] * Total[:, None]
120125
Prediction = Prediction[HitPosInWindow] * wff.gmu
121126
PEmeasure = np.append(PEmeasure, Prediction)
122127
TimeMatrix = np.repeat(Timeline, len(HitPosInWindow), axis=0)[HitPosInWindow]
@@ -129,7 +134,7 @@ def Forward(channelid):
129134
tic = time.time()
130135
cpu_tic = time.process_time()
131136
Result = []
132-
for ch in tqdm(channelid_set, desc='Predict for each channel') :
137+
for ch in tqdm(channelid_set, desc='Predict for each channel'):
133138
Result.append(Forward(ch))
134139
Result = pd.concat(Result)
135140
Result = Result.sort_values(by=['TriggerNo', 'ChannelID'])

0 commit comments

Comments
 (0)