def logpxz(self, w, x, z): x, z = self.xz_to_theano(x, z) w, z, x = ndict.ordereddicts((w, z, x)) A = self.get_A(x) allvars = w.values() + x.values() + z.values() + [A] logpx, logpz = self.f_logpxz(*allvars) if np.isnan(logpx).any() or np.isnan(logpz).any(): print 'v: ', logpx, logpz print 'Values:' ndict.p(w) ndict.p(z) raise Exception("dlogpxz_dwz(): NaN found in gradients") return logpx, logpz
def logpxz(self, w, x, z): x, z = self.xz_to_theano(x, z) w, z, x = ndict.ordereddicts((w, z, x)) A = self.get_A(x) allvars = list(w.values()) + list(x.values()) + list(z.values()) + [A] logpx, logpz = self.f_logpxz(*allvars) if np.isnan(logpx).any() or np.isnan(logpz).any(): print('v: ', logpx, logpz) print('Values:') ndict.p(w) ndict.p(z) raise Exception("dlogpxz_dwz(): NaN found in gradients") return logpx, logpz
def checknan(self, v, w, gv, gw): if ndict.hasNaN(gv) or ndict.hasNaN(gw): raise Exception("dL_dw(): NaN found in gradients") print('v:') ndict.p(v) print('w:') ndict.p(w) print('gv:') ndict.p(gv) print('gw:') ndict.p(gw) raise Exception("dL_dw(): NaN found in gradients")
def dfd_dw(self, w, x, z, gz2): x, z = self.xz_to_theano(x, z) w, z, x, gz2 = ndict.ordereddicts((w, z, x, gz2)) A = self.get_A(x) r = self.f_dfd_dw(*(list(w.values()) + list(x.values()) + list(z.values()) + [A] + list(gz2.values()))) logpx, logpz, fd, gw = r[0], r[1], r[2], dict( list(zip(list(w.keys()), r[3:3 + len(w)]))) if ndict.hasNaN(gw): if True: print('NaN detected in gradients') raise Exception() for i in gw: gw[i][np.isnan(gw[i])] = 0 else: print('fd: ', fd) print('Values:') ndict.p(w) ndict.p(z) print('Gradients:') ndict.p(gw) raise Exception("dfd_dw(): NaN found in gradients") gw, _ = self.gwgz_to_numpy(gw, {}) return logpx, logpz, fd, gw
def checknan(self, v, w, gv, gw): if ndict.hasNaN(gv) or ndict.hasNaN(gw): raise Exception("dL_dw(): NaN found in gradients") #print 'logpx: ', logpx #print 'logpz: ', logpz #print 'logqz: ', logqz print 'v:' ndict.p(v) print 'w:' ndict.p(w) print 'gv:' ndict.p(gv) print 'gw:' ndict.p(gw) raise Exception("dL_dw(): NaN found in gradients")
def L(self, v, w, x, z): x, z = self.xz_to_theano(x, z) v, w, z, x = ndict.ordereddicts((v, w, z, x)) A = self.get_A(x) allvars = list(v.values()) + list(w.values()) + list(x.values()) + list(z.values()) + [A] logpx, logpz, logqz = self.f_L(*allvars) if np.isnan(logpx).any() or np.isnan(logpz).any() or np.isnan(logqz).any(): print('logp: ', logpx, logpz, logqz) print('Values:') ndict.p(v) ndict.p(w) ndict.p(x) ndict.p(z) raise Exception("delbo_dwz(): NaN found in gradients") return logpx, logpz, logqz
def L(self, v, w, x, z): x, z = self.xz_to_theano(x, z) v, w, z, x = ndict.ordereddicts((v, w, z, x)) A = self.get_A(x) allvars = v.values() + w.values() + x.values() + z.values() + [A] logpx, logpz, logqz = self.f_L(*allvars) if np.isnan(logpx).any() or np.isnan(logpz).any() or np.isnan(logqz).any(): print 'logp: ', logpx, logpz, logqz print 'Values:' ndict.p(v) ndict.p(w) ndict.p(x) ndict.p(z) raise Exception("delbo_dwz(): NaN found in gradients") return logpx, logpz, logqz
def dlogpxz_dwz(self, w, x, z): x, z = self.xz_to_theano(x, z) w, z, x = ndict.ordereddicts((w, z, x)) A = self.get_A(x) allvars = list(w.values()) + list(x.values()) + list(z.values()) + [A] # Check if keys are correct keys = list(w.keys()) + list(x.keys()) + list(z.keys()) + ['A'] for i in range(len(keys)): if keys[i] != self.allvars_keys[i]: "Input values are incorrect!" print('Input:', keys) print('Should be:', self.allvars_keys) raise Exception() r = self.f_dlogpxz_dwz(*allvars) logpx, logpz, gw, gz = r[0], r[1], dict( list(zip(list(w.keys()), r[2:2 + len(w)]))), dict( list(zip(list(z.keys()), r[2 + len(w):]))) if ndict.hasNaN(gw) or ndict.hasNaN(gz): if True: print('NaN detected in gradients') raise Exception() for i in gw: gw[i][np.isnan(gw[i])] = 0 for i in gz: gz[i][np.isnan(gz[i])] = 0 else: print('logpx: ', logpx) print('logpz: ', logpz) print('Values:') ndict.p(w) ndict.p(z) print('Gradients:') ndict.p(gw) ndict.p(gz) raise Exception("dlogpxz_dwz(): NaN found in gradients") gw, gz = self.gwgz_to_numpy(gw, gz) return logpx, logpz, gw, gz
def dlogpxz_dwz(self, w, x, z): x, z = self.xz_to_theano(x, z) w, z, x = ndict.ordereddicts((w, z, x)) A = self.get_A(x) allvars = w.values() + x.values() + z.values() + [A] # Check if keys are correct keys = w.keys() + x.keys() + z.keys() + ['A'] for i in range(len(keys)): if keys[i] != self.allvars_keys[i]: "Input values are incorrect!" print 'Input:', keys print 'Should be:', self.allvars_keys raise Exception() r = self.f_dlogpxz_dwz(*allvars) logpx, logpz, gw, gz = r[0], r[1], dict(zip(w.keys(), r[2:2+len(w)])), dict(zip(z.keys(), r[2+len(w):])) if ndict.hasNaN(gw) or ndict.hasNaN(gz): if True: print 'NaN detected in gradients' raise Exception() for i in gw: gw[i][np.isnan(gw[i])] = 0 for i in gz: gz[i][np.isnan(gz[i])] = 0 else: print 'logpx: ', logpx print 'logpz: ', logpz print 'Values:' ndict.p(w) ndict.p(z) print 'Gradients:' ndict.p(gw) ndict.p(gz) raise Exception("dlogpxz_dwz(): NaN found in gradients") gw, gz = self.gwgz_to_numpy(gw, gz) return logpx, logpz, gw, gz
def dfd_dw(self, w, x, z, gz2): x, z = self.xz_to_theano(x, z) w, z, x, gz2 = ndict.ordereddicts((w, z, x, gz2)) A = self.get_A(x) r = self.f_dfd_dw(*(w.values() + x.values() + z.values() + [A] + gz2.values())) logpx, logpz, fd, gw = r[0], r[1], r[2], dict(zip(w.keys(), r[3:3+len(w)])) if ndict.hasNaN(gw): if True: print 'NaN detected in gradients' raise Exception() for i in gw: gw[i][np.isnan(gw[i])] = 0 else: print 'fd: ', fd print 'Values:' ndict.p(w) ndict.p(z) print 'Gradients:' ndict.p(gw) raise Exception("dfd_dw(): NaN found in gradients") gw, _ = self.gwgz_to_numpy(gw, {}) return logpx, logpz, fd, gw