Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

usage inquiry: retrieving weights for hinton #256

Open
DPR-Sanchez opened this issue Sep 11, 2019 · 4 comments
Open

usage inquiry: retrieving weights for hinton #256

DPR-Sanchez opened this issue Sep 11, 2019 · 4 comments
Assignees

Comments

@DPR-Sanchez
Copy link

I was reviewing the documentation and I haven't managed to figure out how to retrieve the matrix of weights from a trained neural network to pass to the hinton graph. I was reviewing the below documentation and only a demo numpy matrix is utilized as an example.

http://neupy.com/docs/secondary/visualizations.html

Any insight into this matter would be much appreciated. Thank you again for creating this wonderful neural network library!

@itdxer
Copy link
Owner

itdxer commented Sep 11, 2019

That's a good point, I think I'll need to add this as a feature or at least mention it in the documentation. For now, you can do it like

from neupy.utils import tensorflow_session

sess = tensorflow_session()
layer = optimizer.network.layers[1]  # or network.layers[1]
weights = sess.run(layer.weight)

@itdxer itdxer self-assigned this Sep 11, 2019
@DPR-Sanchez
Copy link
Author

I tried my hand at a rough mock-up of the feature as a function since I think it would only be a small for loop as seen below:

from neupy.utils import tensorflow_session
import numpy as np

def weights():
	sess = tensorflow_session()
	layers = optimizer.network.layers  

	weights = []

	for layer in layers:	
		weights.append(sess.run(layer.weight))

	return np.toarray(weights)

hope it helps, and thank you again for your feedback.

@itdxer
Copy link
Owner

itdxer commented Sep 11, 2019

you also need to remember that not every layer has weights (thelayer.weight operation might fail) and some layers also can have bias (layer.bias) which you can also visualise with a Hinton diagram.

@DPR-Sanchez
Copy link
Author

DPR-Sanchez commented Sep 16, 2019

I know the below is an ugly mockup, but I gave another stab at it

from neupy.utils import tensorflow_session
import numpy as np

def weights():
	sess = tensorflow_session()
	layers = optimizer.network.layers  

	weights = []

	for layer in layers:
		try:
			weights.append(sess.run(layer.bias))
		except Exception as e:
			pass 
		try:
			weights.append(sess.run(layer.weight))	
		except Exception as e:
			pass 

	return np.toarray(weights)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants