Last active
November 23, 2022 09:16
-
-
Save ackdav/3246e351cd783ddbd8b2b3c4c169fdb7 to your computer and use it in GitHub Desktop.
For loop for creating neural net layers in tensorflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
I've found this little setup (https://pythonprogramming.net/community/262/TensorFlow%20For%20loop%20to%20set%20weights%20and%20biases/) | |
to create layers in a NN with a for loop. Unfortunately it doesn't really work - so here is a corrected version: | |
keep in mind, layer_config takes the form: [n_input, 200, 200, 200, 200, n_classes] | |
''' | |
def multilayer_perceptron(x, layer_config, name="neuralnet"): | |
''' | |
code from: https://pythonprogramming.net/community/262/TensorFlow%20For%20loop%20to%20set%20weights%20and%20biases/ | |
''' | |
layers = {} | |
layers_compute = {} | |
with tf.name_scope(name): | |
for i in range(1, len(layer_config)): | |
new_layer = {'weights': tf.Variable(tf.random_normal([layer_config[i-1], layer_config[i]], 0, 0.1)), | |
'biases': tf.Variable(tf.random_normal([layer_config[i]], 0, 0.1))} | |
layers[i-1] = new_layer | |
with tf.name_scope("weights"): | |
tf.summary.histogram("w_l"+str(i)+"_summary", new_layer['weights']) | |
with tf.name_scope("biases"): | |
tf.summary.histogram("b_l"+str(i)+"_summary", new_layer['biases']) | |
l = tf.add(tf.matmul(x if i == 1 else layers_compute[i-2], layers[i-1]['weights']), layers[i-1]['biases']) | |
with tf.name_scope(name): | |
l = tf.nn.relu(l) if i != len(layer_config)-1 else l | |
layers_compute[i-1] = l | |
lastlayer = len(layers_compute)-1 | |
return layers_compute[lastlayer] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment