Trouble Using Chain Rule to Implement Backprop in Numpy
I'm having trouble figuring out how to translate the backpropagation steps from my HW from math into python, or numpy operations.
I'm pretty sure I have the first few backprop steps correct, however, on the third step I know I messed up because I'm trying to multiply 2 matrices that are sized (784,1) and (200,10).
Here is the code so far
#imports
import keras
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
np.random.seed(0)
#loading data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
X = x_train[0].reshape(1,1)/255.; Y = y_train[0]
zeros = np.zeros(10); zeros[Y] = 1
Y = zeros
show_img(X,Y)
num_hidden_nodes = 200
num_classes = 10
# init weights
W1 = np.random.uniform(1e3,1e3,size=(784,num_hidden_nodes))
b1 = np.zeros((1,num_hidden_nodes))
W2 = np.random.uniform(1e3,1e3,size=(num_hidden_nodes,num_classes))
b2 = np.zeros((1,num_classes))
#forward pass
# multiply input with weights
Z1 = np.add(np.matmul(X,W1), b1)
def sigmoid(z):
return 1 / (1 + np.exp( z))
def d_sigmoid(g):
return sigmoid(g) * (1.  sigmoid(g))
# activation function of Z1
X2 = sigmoid(Z1)
Z2 = np.add(np.matmul(X2,W2), b2)
# softmax
def softmax(z):
# subracting the max adds numerical stability
shiftx = z  np.max(z)
exps = np.exp(shiftx)
return exps / np.sum(exps)
def d_softmax(Y_hat, Y):
return Y_hat  Y
# the hypothesis,
Y_hat = softmax(Z2)
cost = 1 * np.sum(Y * np.log(Y_hat))
#backprop math
dJ_dZ2 = d_softmax(Y_hat, Y)
dJ_dW2 = np.transpose(X2) * dJ_dZ2
dJ_db2 = dJ_dW2 * d_sigmoid(Z2)
dJ_dX2 = dJ_db2 * W2
dJ_dZ1 = np.transpose(dJ_dX2) * d_sigmoid(Z1)
dJ_dW1 = np.transpose(X) * dJ_dX2
dJ_db1 = ??
Here are the steps for backprop show with the math representation above.
Output Math Supposed to be implemented in numpy: here
dJ_dZ2 = d_softmax(Y_hat, Y)
Second Layer Weights Math Supposed to be implemented in numpy: here
dJ_dW2 = np.transpose(X2) * dJ_dZ2
Second Layer Bias Math Supposed to be implemented in numpy: here
dJ_db2 = dJ_dW2 * d_sigmoid(Z2)
Second Layer Input Math Supposed to be implemented in numpy: here
dJ_dX2 = dJ_db2 * W2
First Layer Activation Math Supposed to be implemented in numpy: here
dJ_dZ1 = np.transpose(dJ_dX2) * d_sigmoid(Z1)
First Layer Weights Math Supposed to be implemented in numpy: here
dJ_dW1 = np.transpose(X) * dJ_dX2
First Layer Bias Math Supposed to be implemented in numpy: here
dJ_db1 = ??
I really have no idea how to convert this math to numpy, any help would be appreciated.
See also questions close to this topic

can't delete item from database in django view
i created a view to delete an object through a template but it seems im missing something i cant quite put together
def product_delete_view(request, id): obj = get_object_or_404(Product, id=id) if request.method == "POST": obj.delete() return redirect('../../') context = { "obj": obj } return render(request, "products/product_delete.html", context)
from django.contrib import admin from django.urls import path from pages.views import home_view, contact_view, about_view, social_view, services_view #from products.views import product_detail_view, product_create_view, render_intial_data from products.views import render_intial_data, dynamic_lookup_view, product_delete_view urlpatterns = [ path('admin/', admin.site.urls), path('', home_view, name='home'), path('contact/', contact_view, name='contact'), path('about/', about_view, name='about'), path('services/', services_view, name='services'), path('social/', social_view, name='social'), #path('product/detail', product_detail_view, name='pdtDetail'), #path('product/create', product_create_view, name='pdtCreate') path('product/create', render_intial_data, name='pdtCreate'), path('product/<int:id>', dynamic_lookup_view, name="pdt"), path('product/<int:id>/delete', product_delete_view, name='pdtDelete') ]
{% extends 'base.html' %} {% block content %} <form action="." method="POST"> {% csrf_token %} <h1>Do you want to delete the product "{{ obj.title }}"</h1> <p><input type="submit" value="Yes" /> <a href="../">Cancel</a></p> </form> {% endblock %}
when i try to delete object is not removed from the database table

How to extract x and y values as a new array after running DBSCAN?
I am running DBSCAN algorithm in python by using list of points created in ArcGIS. I applied a loop to see differences for increasing eps and min_feature values. It works very well and I can plot the results on a graph. But I have to use the resulting points in clusters on ArcGIS Pro again. To be able to do that I would like to extract x and y values of each point in the clusters which I can see it on the graph and to a .csv or .xlsx file. Then I can visualize all the points and the clusters on the map that I am currently working. The problem is I cannot print the x and y values and to be honest I am a novice and not sure where to find them. Since I do not use a basemap and only working with the vector data I could not find any other way to do it. I would be grateful if someone can help with the issue. Thank you in advance. Here is my code.
import numpy as np from sklearn.cluster import DBSCAN from sklearn import metrics from sklearn.datasets import make_blobs from sklearn.preprocessing import StandardScaler import arcpy import pandas as pd import xlsxwriter as xlsw import matplotlib.pyplot as plt in_workspace = r'D:\00_Experiment\DWG_Participants\Database' arcpy.env.workspace = in_workspace arcpy.env.overwriteOutput = True # ############################################################################# # Generate sample data input = r"D:\00_Experiment\DWG_Participants\Database\DbScan.gdb\FP_Centroids" arr = arcpy.da.TableToNumPyArray(input, ("Longitude", "Latitude")) cen_df = pd.DataFrame(arr) centers = [[1, 1], [1, 1], [1, 1]] cen_df, labels_true = make_blobs(n_samples=len(cen_df), centers=centers, cluster_std=0.4,
random_state=0)
cen_df = StandardScaler().fit_transform(cen_df) # ############################################################################# # Compute DBSCAN a = 0.2 b = 0.5 for i in range(1): a = a + 0.1 b = b + 0.5 db = DBSCAN(eps=a, min_samples=b).fit(cen_df) core_samples_mask = np.zeros_like(db.labels_, dtype=bool) core_samples_mask[db.core_sample_indices_] = True labels = db.labels_ # Number of clusters in labels, ignoring noise if present. n_clusters_ = len(set(labels))  (1 if 1 in labels else 0) n_noise_ = list(labels).count(1) print('Estimated number of clusters: %d' % n_clusters_) print('Estimated number of noise points: %d' % n_noise_) print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true, labels)) print("Completeness: %0.3f" % metrics.completeness_score(labels_true, labels)) print("Vmeasure: %0.3f" % metrics.v_measure_score(labels_true, labels)) print("Adjusted Rand Index: %0.3f" % metrics.adjusted_rand_score(labels_true, labels)) print("Adjusted Mutual Information: %0.3f" % metrics.adjusted_mutual_info_score(labels_true, labels)) print("Silhouette Coefficient: %0.3f" % metrics.silhouette_score(cen_df, labels)) # Black removed and is used for noise instead. unique_labels = set(labels) colors = [plt.cm.Spectral(each) for each in np.linspace(0, 1, len(unique_labels))] for k, col in zip(unique_labels, colors): if k == 1: # Black used for noise. col = [0, 0, 0, 1] class_member_mask = (labels == k) xy = cen_df[class_member_mask & core_samples_mask] plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=14) xy = cen_df[class_member_mask & ~core_samples_mask] plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col), markeredgecolor='k', markersize=6) ################### #I tried this code for xy values and labels but the excel file does not contain what I am looking for #new_array = np.array(labels) #df = pd.DataFrame(labels) #df = df.transpose() #xlsfile = r"D:\00_Experiment\DWG_Participants\Database\output_dbscan.gdb\fp.xlsx" #writer = pd.ExcelWriter(xlsfile, engine="xlsxwriter") #df.to_excel(writer, sheet_name="table_fp",startrow=1, startcol=1, header=False, index=False) #writer.save() ####################### plt.title('Estimated number of clusters: %d' % n_clusters_) plt.show()enter code here

Remove elements from Numpy array until y has equivalent elements in each value
I have an array
y
composed of0
and1
, but at a different frequency.For example:
y = np.array([0, 0, 1, 1, 1, 1, 0])
And I have an array
x
of the same length.x = np.array([0, 1, 2, 3, 4, 5, 6])
The idea is to filter out elements until there are the same number of
0
and1
.A valid solution would be to remove index 5:
x = np.array([0, 1, 2, 3, 4, 6]) y = np.array([0, 0, 1, 1, 1, 0])
A naive method I can think of is to get the difference between the value frequency of y (in this case
43=1
) create a mask fory == 1
and switch random elements fromTrue
toFalse
until the difference is0
. Then create a mask fory == 0
, do aOR
between them and apply it to bothx
andy
.This doesn't really seem the best "python/numpy way" of doing it though.
Any suggestions? Something like randomly select
n
elements from the highest count, wheren
is the count of the lowest value.If this is easier with pandas then that would work for me too.
Naive algorithm assuming 1 > 0:
mask_pos = y == 1 mask_neg = y == 0 pos = len(y[mask_pos]) neg = len(y[mask_neg]) diff = posneg while diff > 0: rand = np.random.randint(0, len(y)) if mask_pos[rand] == True: mask_pos[rand] = False diff = 1 mask_final = mask_pos  mask_neg y_new = y[mask_final] x_new = x[mask_final]
This naive algorithm is really slow

Most frequent occurrence (mode) of numpy array values based on IDs in another array
I have a 2D array containing values and I would like to calculate the most frequent entry (i.e., the mode) from this data according to IDs in a second array.
data = np.array([[[ 0, 10, 50, 80, 80], [10, 10, 50, 80, 90], [10, 10, 50, 80, 90], [50, 50, 80, 80, 80]]) ID = np.array([[[ 1, 1, 2, 3, 3], [1, 1, 2, 3, 3], [1, 1, 2, 3, 3], [1, 2, 2, 2, 3]]) #Expected Result is: [10 50 80]
The most frequent value in data array for ID=1 is 10, ID=2 is 50 and ID=3 is 80. I've been playing around with
np.unique
and combinations ofnp.bincount
andnp.argmax
but I can't figure out how to get the result. Any help? 
numpy find 1 indices in onehotvector
I want to make matrix that includes indices of 1. For example, if I have array a = [[0, 0, 1, 1, 0], [1, 1, 0, 0, 1]], find the indices of 1, and return the matrix [[2, 3], [0, 1, 4]]. Is there a convenient way in numpy? I need your help.

AI backpropagation for a game
This question does not have code examples, I'm looking for an approach on how to do this.
Imagine a neural network trying to learn a game, it has 4 possible outputs (moves it can do).
In case the other player wins or gains an advantage after a (bad) move from the neural network you might want do backpropagation to teach it NOT to make this move. So you'd want to input the data [?, 0, ?, ?] to the cost function. ... but because any other move might be OK or might not be I don't have values to input for the other moves. [1, 0, 1, 1] is not what I want.
How could you do the backpropagation on just 1 output? Does DeepLearning4J cater for this situation?
Thanks for any help!
R.

Neural Network for MNIST digits is not learning at all  problem with backpropagation
after a long time, I am still not able to run my nn without any bugs. Accuracy of this toy nn is an astonishing 12% (60 neurons in hidden layer, 100 epochs, 0.3 learning rate, tanh activation, MNIST dataset downloaded via TF)  so basically it is not learning at all. After all this time looking at videos / post about backpropagation, I am still not able to fix it. So my bug must be in between the part marked with two ##### lines. I think that my understanding of derivatives in general is good, but I just cannot connect this knowlege with backpropagation. If the backpropagation base is correct, then the mistake must at
axis = 0/1
, because I also cannot understand, how to determine on which axis I will be working on.Also, I have a strong feeling, that
dZ2 = A2  Y
might be wrong, it should bedZ2 = Y  A2
, but after that correction, nn starts to guess only one number.(and yes, backpropagation itself I haven't written, I have found it on the internet)
#Activation functions: def tanh(z): a = (np.exp(z)np.exp(z))/(np.exp(z)+np.exp(z)) return a ###############################################################################START def softmax(z): smExp = np.exp(z  np.max(z, axis=0)) out = smExp / np.sum(smExp, axis=0) return out ###############################################################################STOP
def NeuralNetwork(num_hid, epochs, learning_rate, X, Y): #num_hid  number of neurons in the hidden layer #X  dataX  shape (10000, 784) #Y  labels  shape (10000, 10) #inicialization W1 = np.random.randn(784, num_hid) * 0.01 W2 = np.random.randn(num_hid, 10) * 0.01 b1 = np.zeros((1, num_hid)) b2 = np.zeros((1, 10)) correct = 0 for x in range(1, epochs+1): #feedforward Z1 = np.dot(X, W1) + b1 A1 = tanh(Z1) Z2 = np.dot(A1, W2) + b2 A2 = softmax(Z2) ###############################################################################START m = X.shape[1] #> 784 loss =  np.sum((Y * np.log(A2)), axis=0, keepdims=True) cost = np.sum(loss, axis=1) / m #backpropagation dZ2 = A2  Y dW2 = (1/m)*np.dot(A1.T, dZ2) db2 = (1/m)*np.sum(dZ2, axis = 1, keepdims = True) dZ1 = np.multiply(np.dot(dZ2, W2.T), 1  np.power(A1, 2)) dW1 = (1/m)*np.dot(X.T, dZ1) db1 = (1/m)*np.sum(dZ1, axis = 1, keepdims = True) ###############################################################################STOP #parameters update  gradient descent W1 = W1  dW1*learning_rate b1 = b1  db1*learning_rate W2 = W2  dW2*learning_rate b2 = b2  db2*learning_rate for i in range(np.shape(Y)[1]): guess = np.argmax(A2[i, :]) ans = np.argmax(Y[i, :]) print(str(x) + " " + str(i) + ". " +"guess: ", guess, " ans: ", ans) if guess == ans: correct = correct + 1; accuracy = (correct/np.shape(Y)[0]) * 100

Train single Keras model.variable with Tensorflow GradientTape()
I am currently working on implementing a Radial Basis Function Network (RBFN) in Keras/ Tensorflow. Ive set up my own RBF Layer in Keras and now I want to write my own training routine using
tf.GradientTape()
. My Keras model has a set of three different variables (center, width, weight) for the RBF activation. Since i want to write a training routine with network growing and prunin strategies i need to calculate the the gradient of a single RBF w.r.t to the loss.Im using Tensorflow 2.0.0 and python 3.7.4
If I pass in all my
model.trainable_variables
into thetf.GradientTape().gradients()
function everything works fine. My routine would look like this:with tf.GradientTape() as t: y_hat = model(X) current_loss = loss(y_hat, y) grads = t.gradient(current_loss, model.trainable_variables) optimizer.apply_gradients(zip(grads, model.trainable_variables))
My goal is to apply the gradients only to the nearest (by the center parameter) located RBF neuron to the input
X[i]
with tf.GradientTape() as t: y_hat = model(X[i]) current_loss = loss(y_hat, y[i]) centers = model.trainable_variables[0] eucl = tf.math.abs(centers  X[i]) # euclidian distance of input to RBF centers idx_nr = tf.math.argmin(tf.math.abs(eucl)).numpy()[0] # idx of nearest RBF grads = t.gradient(e_n, model.trainable_variables[0][idx_nr]) # just the centers print(grads)
However this always returns
None
for the gradients. Doest.gradient()
expect the variables to be in a specific format? I hope you guys can come up with some good ideas. Thank you in advance! :) 
Reuters Eikon/Datastream  obtain issue date of structured products
I want to download the isse date (or the date were the first price was set up) of 1,000 bonus certificates of different issuers.
To do so, I converted Wertpapierkennummern (WKN, identifier at German exchanges) into RICs. With those RICs I could download different data (e.g. Strike, Last Trading Date, Underlying RIC, Prices...) without any issues.
However, I need the issue date (or date were the first price was set up). This is stated on the "Overview" page of the certificates as "Isse Date". But the Data Item Browser () doesn't hold such a field.
In Datastream the field "ID" (Issue Date) returns solely "NA" values for all products.
Can anyone help me with this issue?
The need for the issue date is the following: I want to have a timeseries of prices which covers the life time of the derivative, so I have to cut down my used sample and want to sort the products out by using Date of first price (issue date) and the last trading date.
If there is a better solution please let me know!
Many thanks!

Why is this AND gate neural network not moving towards optimal weights?
I have simple neural network of two inputs and one output with no hidden layers. i.e, [input1][weight1 weight2] = z [input2]
output = sigmoid(z)
The weights don't seem move to an optimal value. I've checked the gradient to the best of my knowledge and I can see the weights go up or down depending on the derivative of cost function, but network doesn't move towards optimal values.
Here's the code:
import numpy as np import random as r import sys def sigmoid(ip, derivate=False): if derivate: return ip*(1ip) return 1.0/(1+np.exp(1*ip)) class NeuralNet: global sigmoid def __init__(self): self.inputLayers = 2 self.outputLayer = 1 def setup(self): self.i = np.array([r.random(), r.random()], dtype=float).reshape(2,) self.w = np.array([r.random(), r.random()], dtype=float).reshape(2,) def forward_propogate(self): self.z = self.w*self.i self.o = sigmoid(sum(self.z)) def optimize_cost(self, desired): i=0 current_cost = pow(desired  self.o, 2) for weight in self.w: dpdw = 1 * (desiredself.o) * (sigmoid(self.o, derivate=True)) * self.i[i] print(dpdw) self.w[i] = self.w[i] + 500*dpdw i+=1 self.forward_propogate() def train(self, ip, op): self.i = np.array(ip).reshape(2,) self.forward_propogate() print("before:{}".format(self.o)) self.optimize_cost(op[0]) # print(self.i,self.w) n = NeuralNet() n.setup() # while sys.stdin.read(1): while True: a = r.random() b = r.random() if a>0.5 and b>0.5: c = 0.9 else: c = 0.1 print(c) n.train([a,b],[c]) print(n.i, n.w) print("after: {}".format(n.o))

how can ı calculate the derivative of matrix with respect to a scalar number?
I am currently working in a inverse problem, in matlab, I observed voltage data from electrode and I discretized my model using the FEM. I applied cosine current and I solved the voltage values. This is forward problem. My problem is inverse problem which is delta(conductivity)=S* delta(voltage) I found system matrix using FEM but I want to find jacobian matrix and I have to calculate derivative of system matrix with respect to conductivity. My conductivity is scalar and it is a constant every element. So I have conductivity vector whose columns number isequal to elementnumber in mesh. I have searched some methods for calculation but ı am wrong because my solution is wrong. I am unsure on how I should start creating it.