def rbmdiscriminative(rbm, x, ey, opts, chains, chainsy, debug=None): n_classes = rbm.d.shape[0] p_y_given_x, F = rbmpygivenx(rbm, x, 'train') F_sigm = sigm(F) F_sigm_prob = np.zeros(F_sigm.shape) for c in range(n_classes): F_sigm_prob[:, :, c] = np.multiply(F_sigm[:, :, c], np.transpose(p_y_given_x[:, c])) # O: F_sigm_prob[:,:,c] = np.matmul(F_sigm[:,:,c], np.transpose(p_y_given_x[:,c])) # init grads dw = np.zeros(rbm.W.shape) # :o dw = rbm.zeros((rbm.W.shape) du = np.zeros(rbm.U.shape) # :o du = rbm.zeros(rbm.U.shape) dc = np.zeros(rbm.c.shape) # :o dc = rbm.zeros(rbm.c.shape) class_labels = predict(ey) for c in range(n_classes): # dw grad # find idx for current class bin_idx = class_labels == c lin_idx = np.where(c == class_labels) a = np.matmul(F_sigm[:, lin_idx[0], c], x[lin_idx[0], :]) # # O: a = F_sigm[:, lin_idx, c] b = np.matmul(F_sigm_prob[:, :, c], x) dw = dw + a - b # du grad sum_F_sigm = np.sum(F_sigm[:, bin_idx, c], axis=1) sum_F_sigm_prob = np.sum(F_sigm_prob[:, :, c], axis=1) du[:, c] = sum_F_sigm - sum_F_sigm_prob # dc dc_diff = np.subtract(np.sum(F_sigm[:, bin_idx, c], axis=1), np.sum(F_sigm_prob[:, :, c], axis=1)) dc = dc + np.reshape(dc_diff, (dc_diff.shape[0], 1)) # :o np.reshape(dc, (1, dc.shape[0])) + np.transpose(dc_diff) # dd grad dd = np.transpose(np.sum(ey - p_y_given_x, axis=0)) # create grads struct and scale grad by minibatch size. grads = { "dw": dw / opts.batchsize, "db": np.zeros(rbm.b.shape), "dc": dc / opts.batchsize, "dd": dd / opts.batchsize, "du": du / opts.batchsize, } curr_err = 0 chains = [] chainsy = [] return grads, curr_err, chains, chainsy
HL1 = np.zeros((2, 1)) x3_1 = np.zeros((3, 3)) out = np.zeros((4, 1)) for i in range(iterations): out = np.zeros((4, 1)) for j in range(4): inputs[:, 0] = input[:, j] #HL1 = w1*[-1; inputs]; temp1[0, 0] = -1 temp1[1, 0] = inputs[0, 0] temp1[2, 0] = inputs[1, 0] HL1 = np.dot(w1, temp1) HiddenLayerOutput1 = S.sigm(HL1) temp2[0, 0] = -1 temp2[1, 0] = HiddenLayerOutput1[0, 0] temp2[2, 0] = HiddenLayerOutput1[1, 0] x3_1 = np.dot(w2, temp2) out[j, 0] = S.sigm(x3_1) delta3 = np.dot(SG.sigmDerivative(x3_1), groundTruth[j, 0] - out[j, 0]) temp3 = w2[:, 1:3].T delta2 = np.dot(np.multiply(SG.sigmDerivative(HL1), temp3), delta3) w2 = w2 + np.multiply(np.multiply(temp1.T, coeff), delta3)
c = np.ones((2, 1)) d = np.ones((2, 1)) e = np.ones((1, 3)) f = np.ones((1, 3)) for i in range(iteration): out = np.zeros((4, 1)) for j in range(4): inputs[:, 0] = input[:, j] k[0, 0] = -1 k[1, 0] = inputs[0, 0] k[2, 0] = inputs[1, 0] #Hidden Layer 1 hl1 = np.dot(w1, k) HiddenLayerOutput1 = sigm.sigm(hl1) #Output Layer h[0, 0] = -1 h[1, 0] = HiddenLayerOutput1[0, 0] h[2, 0] = HiddenLayerOutput1[1, 0] #Output Layer x3_1 = np.dot(w2, h) out[j, 0] = sigm.sigm(x3_1) delta3 = np.multiply(sigmDerivative.sigmDerivative(x3_1), groundTruth[j,0] - out[j,0]) c = sigmDerivative.sigmDerivative(hl1) d[0,0] = w2[0,1] d[1,0] = w2[0,2] delta2 = np.multiply(np.multiply(c,d), delta3)
fig = plt.Figure() inputs = np.zeros([5, 1]) p = np.zeros([3, 1]) for i in range(iterations): err = 0 for j in range(y1.shape[0]): inputs[:, 0] = input[:, j] outputStack1 = inputs #forward propagation outputStack2 = np.subtract(np.dot(stack1_w, outputStack1), stack1_b) outputStack2 = sigm.sigm(outputStack2) outputStack3 = np.subtract(np.dot(stack2_w, outputStack2), stack2_b) outputStack3 = sigm.sigm(outputStack3) #backward propagation p[0:3, 0] = outputStack3[0:3, 0] epsilon = nnloss.nnloss(groundTruth[j:j + 1, 0:3].T, p, 1) cost = nnloss.nnloss(groundTruth[j:j + 1, 0:3].T, p, 0) err = err + cost gradStack_epsilon2 = np.multiply( np.multiply(outputStack3, (1 - outputStack3)), epsilon) epsilon = np.dot(stack2_w.T, gradStack_epsilon2)
counter = 0 for i in range(iterations): err = 0 for j in range(0, y1.shape[0], batchSize): data = input[:, j:j + batchSize] labels = groundTruth[j:j + batchSize, :] cost = 0 for kk in range(batchSize): inputs[0:5, 0] = data[:, kk] outputStack1 = inputs # forward propagation outputStack2 = np.subtract(np.dot(mystack_1_w, outputStack1), mystack_1_b) outputStack2 = S.sigm(outputStack2) outputStack3 = np.subtract(np.dot(mystack_2_w, outputStack2), mystack_2_b) outputStack3 = S.sigm(outputStack3) # backward propagation p = outputStack3 epsilon = NL.nnloss(labels[kk:kk + 1, :].T, p, 1) cost = NL.nnloss(groundTruth[kk:kk + 1, :].T, p, 0) err = err + cost if j == 0: gradStack2_epsilon = np.multiply( np.multiply(outputStack3, (1 - outputStack3)), epsilon) epsilon = np.dot(mystack_2_w.T, gradStack2_epsilon)